Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Delete functionality #11

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ const hostnameToIP = {
};

const generateRandomIP = () => {
let randomByte = function() {
let randomByte = function () {
return Math.round(Math.random() * 256);
};

let isPrivate = function(ip) {
let isPrivate = function (ip) {
return /^10\.|^192\.168\.|^172\.16\.|^172\.17\.|^172\.18\.|^172\.19\.|^172\.20\.|^172\.21\.|^172\.22\.|^172\.23\.|^172\.24\.|^172\.25\.|^172\.26\.|^172\.27\.|^172\.28\.|^172\.29\.|^172\.30\.|^172\.31\./.test(
ip
);
Expand All @@ -47,9 +47,9 @@ const getServerByDistance = async inputCity => {
cities.map(city => {
return request(
"https://www.distance24.org/route.json?stops=" +
inputCity +
"|" +
city.split(".storage.com")[0]
inputCity +
"|" +
city.split(".storage.com")[0]
);
})
);
Expand All @@ -74,7 +74,7 @@ const getCityFromIP = ip => {
};

/* GET home page. */
router.get("/", function(req, res, next) {
router.get("/", function (req, res, next) {
res.render("index", {
title: "Storage Virtualization",
success: true,
Expand Down Expand Up @@ -245,6 +245,28 @@ router.get("/download", (req, res, next) => {
return res.json({ message: "file no exist", success: false });
});

router.get("/delete", (req, res, next) => {
const fileHash = req.query.hash;
if (!fs.existsSync(mapPath)) {
return res.json({ message: "no files exist" });
}
let map = JSON.parse(fs.readFileSync(mapPath));
const fileDetails = map[fileHash].details;
let fileBackups = map[fileHash].backups;
let possiblePresentFile = fileBackups.filter(
file => file.hostname === process.env.SELF_HOSTNAME
);
if (possiblePresentFile.length !== 0) {
try {
fs.unlinkSync(possiblePresentFile[0].path + fileDetails.name);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What we'd like to do, is delete the file clusterwide. So it would make sense if you can make async delete API calls to the rest of the servers and remove it from the map, and then store it.

return res.json({ message: "Deleted", success: true });
}
catch (err) {
return res.json({ message: "file no exist", success: false });
}
}
});

const generateValidIP = async () => {
let ip = generateRandomIP();
console.log("TCL: ip", ip);
Expand Down
177 changes: 102 additions & 75 deletions src/views/list.ejs
Original file line number Diff line number Diff line change
@@ -1,80 +1,107 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>StorageVirtualization</title>
<link rel="stylesheet" href="/stylesheets/style.css" />
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<%- include partials/nav %>
<style>
table {
border-collapse: collapse;
}

table,
th,
td {
border: 1px solid black;
}
</style>
<% if(message) {%>
<p style="color: red"><%= message %></p>
<% } %>
<table id="table1">
<tr>
<td>
Filename
</td>
<td>
Size(in bytes)
</td>
<td>Date Uploaded</td>
<td>
Hash
</td>
<td>
Action
</td>
</tr>
<% map.forEach(function(file, i){ %>
<tr>
<td><%= file.name %></td>
<td><%= file.size %></td>
<td><%= file.date %></td>
<td><%= file.hash %></td>
<td>
<button onclick="getDownloadLink('<%= file.hash %>')">
Generate Download Link
</button>
</td>
</tr>
<% }); %>
</table>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>StorageVirtualization</title>
<link rel="stylesheet" href="/stylesheets/style.css" />
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>

<p id="dlink"></p>
<body>
<%- include partials/nav %>
<style>
table {
border-collapse: collapse;
}

<script>
function getDownloadLink(hash) {
console.log("hash is", hash);
document.getElementById("dlink").innerHTML =
"Please wait while the link loads..";
$.ajax({
type: "GET",
url: "/getServerToDownloadFrom?hash=" + hash,
success: function(response) {
console.log("sresponse", response);
document.getElementById("dlink").innerHTML =
"<a href=" + response.url + ">Download Link</a>";
},
error: function(response) {
console.log("fresponse", response);
}
});
}
</script>
</body>
</html>
table,
th,
td {
border: 1px solid black;
}
</style>
<% if(message) {%>
<p style="color: red"><%= message %></p>
<% } %>
<table id="table1">
<tr>
<td>
Filename
</td>
<td>
Size(in bytes)
</td>
<td>Date Uploaded</td>
<td>
Hash
</td>
<td>
Action
</td>
</tr>
<% map.forEach(function(file, i){ %>
<tr>
<td><%= file.name %></td>
<td><%= file.size %></td>
<td><%= file.date %></td>
<td><%= file.hash %></td>
<td>
<button onclick="getDownloadLink('<%= file.hash %>')">
Generate Download Link
</button>
<button onclick="Delete('<%= file.hash %>')">
Delete File
</button>

</td>
</tr>
<% }); %>
</table>

<p id="dlink"></p>
<p id="delete"></p>

<script>
function getDownloadLink(hash) {
console.log("hash is", hash);
document.getElementById("dlink").innerHTML =
"Please wait while the link loads..";
$.ajax({
type: "GET",
url: "/getServerToDownloadFrom?hash=" + hash,
success: function (response) {
console.log("sresponse", response);
document.getElementById("dlink").innerHTML =
"<a href=" + response.url + ">Download Link</a>";
},
error: function (response) {
console.log("fresponse", response);
}
});
}
function Delete(hash) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be renamed to lowercase delete

console.log("hash is", hash);
document.getElementById("delete").innerHTML =
"Deleting..";
$.ajax({
type: "GET",
url: "/delete?hash=" + hash,
success: function (response) {
console.log("sresponse", response);
document.getElementById("delete").innerHTML =
"<p>" + response.url + "</p>";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

response.url will be empty, so you can update this to rather display the message

},
error: function (response) {
console.log("fresponse", response);
document.getElementById("delete").innerHTML =
"<p>" + response.url + "</p>";
}
});
}
</script>
</body>

</html>