-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.js
202 lines (169 loc) · 6.49 KB
/
router.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
var express = require("express");
var multer = require("multer");
var crypto = require("crypto");
// Initializing constants
const hostUrl = "http://localhost:3000/";
// Database conector
var File = require("./model/fileSchema");
const { render } = require("express/lib/response");
var router = express.Router();
// Initialize file storage
var file_storage = multer.diskStorage({
destination:"file_storage",
// Name under which file is stored
filename: function (req, file, cb) {
cb(null, req.uniqName + "--" + file.originalname);
req.storedFileName = req.uniqName + "--" + file.originalname;
}
});
// The stored file name is randomly generated
function insertFile(req, res, next){
uniquestr = crypto.pseudoRandomBytes(16).toString("hex");
req.uniqName = uniquestr;
next();
}
// This uploads file to the storage
var upload = multer({storage:file_storage});
// *********************************** GET METHODS ***********************************
// Render main upload screen
router.get("/",function(req,res){
res.render("main_screen");
});
// Render download screen if valid file has was appended
router.get("/download/:fileKey", async (req, res) => {
var fileKey = req.params.fileKey;
// Find the file in database
var data = await File.findOne({downloadURL: hostUrl + "download/" + fileKey})
if (data == null)
res.redirect("/")
else
res.render("download_screen",{fileLink: fileKey, fileName: data.originalName, fileSize: data.fileSize})
});
// Render manage screen if url links to valid file
router.get("/manage/:fileKey", async (req, res) => {
var fileKey = req.params.fileKey;
// Find the file in database
var data = await File.findOne({manageURL: hostUrl + "manage/" + fileKey})
if (data == null)
res.redirect("/")
else {
// Convert The date into readable string
var formatedDate = data.uploadDate.toUTCString();
res.render("manage_screen",{fileLink: fileKey, fileSize: data.fileSize, fileName: data.originalName, upload_date: formatedDate, times_downloaded: data.downloadCount, download_url: data.downloadURL})
}
});
// Download file from the download screen
router.get("/getFile/:fileKey", async (req, res) => {
var fileKey = req.params.fileKey;
// Find the file in DB
var data = await File.findOne({downloadURL: hostUrl + "download/" + fileKey})
// Check if file was found
if (data == null){
return res.status(400).send();}
else{
var absPath = __dirname+'\\file_storage\\' + data.fileName;
res.download(absPath,data.originalName);}
});
// Download file from the manage screen
router.get("/getFileInManage/:fileKey", async (req, res) => {
var fileKey = req.params.fileKey;
// Find the file in DB
var data = await File.findOne({manageURL: hostUrl + "manage/" + fileKey})
// Check if file was found
if (data == null){
return res.status(400).send();}
else{
var absPath = __dirname+'\\file_storage\\' + data.fileName;
res.download(absPath,data.originalName);}
});
// Prints About screen
router.get("/about", (req, res) => {
res.render("about_screen");
});
// *********************************** POST METHODS ***********************************
// Upload file from the main screen
router.post("/upload", insertFile, upload.single("inputFile"), (req, res) => {
try {
var uniqueName = req.storedFileName;
// Get the correct size format for output
var fileSizeString = ""
if (req.file.size < 1024)
fileSizeString = req.file.size + "B"
else if (req.file.size < 1024 * 1024)
fileSizeString = Math.round(req.file.size / 1024) + "KB"
else if (req.file.size < 1024 * 1024 * 1024)
fileSizeString = Math.round(req.file.size / 1024 / 1024) + "MB"
else
fileSizeString = Math.round(req.file.size / 1024 / 1024 / 1024) + "GB"
console.log("Stored file: " + uniqueName + " with size: " + fileSizeString);
// Generate pseudo random strings for download and manage link
var downloadURL = hostUrl + "download/" + crypto.pseudoRandomBytes(16).toString("hex");
var manageURL = hostUrl + "manage/" + crypto.pseudoRandomBytes(16).toString("hex");
// Save the original name
var originalName = req.file.originalname;
// Create database object for storing metadata
var newFile = new File({
fileName: uniqueName,
downloadURL: downloadURL,
manageURL: manageURL,
originalName: originalName,
fileSize: fileSizeString
});
// Save file to database
newFile.save();
urls = {download: downloadURL,
manage: manageURL }
// Render site
res.send(urls);
} catch (err) {
console.log(err);
}
})
// Update downloads counter when downloading file - in download screen
router.post("/updateDownloads", async (req, res) => {
var URL = hostUrl + "download/" + req.body.fileURL
// Find file and update
try{
await File.findOneAndUpdate({downloadURL: URL}, {$inc : {downloadCount : 1}});
} catch (err) {
console.log(err);
}
res.sendStatus(201);
})
// Update downloads counter when downloading file - in manage screen
router.post("/updateDownloadsInManage", async (req, res) => {
var URL = hostUrl + "manage/" + req.body.fileURL
// Find file and update
try{
await File.findOneAndUpdate({manageURL: URL}, {$inc : {downloadCount : 1}});
} catch (err) {
console.log(err);
}
res.sendStatus(201);
})
// Generate new download url for the selected file - in manage screen
router.post("/updateURL", async(req,res) => {
// Manage url for searching in DB
var manageURL = hostUrl + "manage/" + req.body.fileURL
// New download url
uniquestr = crypto.pseudoRandomBytes(16).toString("hex");
var newURL = hostUrl + "download/" + uniquestr
// Update the new download url based on manage url
try{
await File.findOneAndUpdate({manageURL: manageURL}, {downloadURL : newURL});
} catch (err) {
console.log(err);
}
res.send({URL: newURL})
})
// Remove the selected file from database - in manage screen
router.post("/removeFile", async (req, res) => {
var URL = hostUrl + "manage/" + req.body.fileURL
try{
await File.findOneAndRemove({manageURL: URL});
} catch (err) {
console.log(err);
}
res.redirect("/");
})
module.exports = router;