-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuploads.js
More file actions
264 lines (227 loc) · 11.1 KB
/
Copy pathuploads.js
File metadata and controls
264 lines (227 loc) · 11.1 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
const { Router } = require('express');
const multer = require("multer");
const multerS3 = require("multer-s3");
const { MongoConnector } = require('../../server/MongoConnector');
const sanitizeHtml = require('sanitize-html');
const router = Router();
const sharp = require('sharp');
const fs = require('fs');
const path = require('path');
const { S3Client, PutObjectCommand, DeleteObjectCommand } = require("@aws-sdk/client-s3");
/**
* @param {MongoConnector} db - The MongoDB connector instance.
* @returns {Router} The router instance.
*/
sanitizeHtmlAllowedTags = sanitizeHtml.defaults.allowedTags.concat(['img', 'embed', 'iframe']);
function generateRandomFilename() {
const length = 20;
const characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
let result = "";
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * characters.length));
}
return result;
}
module.exports = (db, s3Client) => {
const config = require('../../config.json');
/**
* Uploads a file to an S3 bucket using multer and multerS3.
*
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @param {string} [directory=""] - The directory path within the S3 bucket where the file will be stored. e.g "images" or "posts".
* @param {string[]} [forceFormats=[]] - An array of allowed file formats. If provided, only files with these formats will be accepted.
* @returns {Promise<string>} - A promise that resolves with the S3 key of the uploaded file, or rejects with an error.
*/
function uploadFile(req, res, directory = "", forceFormats = [], registerFile = true) {
return new Promise((resolve, reject) => {
const filename = generateRandomFilename();
let s3Path;
const s3upload = multer({
storage: multerS3({
s3: s3Client,
bucket: "transmitterstorage",
acl: "public-read",
contentType: multerS3.AUTO_CONTENT_TYPE,
contentDisposition: "inline",
cacheControl: "max-age=31536000",
key: function (request, file, cb) {
const fileExtension = file.originalname.split('.').pop().toLowerCase();
if (forceFormats.length > 0 && !forceFormats.includes(fileExtension.toLowerCase())) {
return cb(new Error(`Invalid file format. Expected format(s): ${forceFormats.join(', ')}`), null);
}
// Generate the filename for S3 storage
if (directory !== "") {
s3Path = `${directory}/${filename}.${fileExtension}`;
} else {
s3Path = `${filename}.${fileExtension}`;
}
cb(null, s3Path);
},
contentDisposition: "inline",
}),
}).array("upload", 1);
// Execute the upload
s3upload(req, res, function (error) {
if (error) {
console.error("File upload error:", error.message);
reject(error);
} else {
if (req.files && req.files.length > 0) {
if (registerFile) registerFileInDB(req.session.userID, req.files[0].key, s3Path, directory);
resolve(req.files[0].key);
} else {
reject(new Error("No files uploaded"));
}
}
});
});
}
async function registerFileInDB(userID, filename, path, type) {
try {
const entry = await db.createFileEntry(userID, filename, path, type)
console.log("File registered in database:", filename);
return entry;
}
catch (error) {
console.error("Error registering file in database:", error);
return null;
}
}
function deleteFile(path) {
return new Promise((resolve, reject) => {
s3Client.send(new DeleteObjectCommand({
Bucket: "transmitterstorage",
Key: path
})).then(() => {
console.log("File deleted from S3");
db.deleteFileEntry(path);
resolve();
}).catch((error) => {
console.error("Error deleting file from S3:", error);
reject(error);
});
});
}
router.post("/uploadFile", async function (req, res) {
if (!req.session.userID) return res.status(401).send("Not logged in");
const permissions = await db.getUserPermissions(req.session.userID);
if (!permissions.includes("admin") && !permissions.includes("writer") && !permissions.includes("canPost")) return res.status(403).send("You cannot upload an image");
console.log("Uploading file");
let filename;
try {
filename = await uploadFile(req, res, "files");
} catch (error) {
return res.status(500).send(error.message);
}
return res.status(200).send(filename);
});
router.post('/uploadImage', async (req, res) => {
if (!req.session.userID) return res.status(401).send("Not logged in");
const permissions = await db.getUserPermissions(req.session.userID);
if (!permissions.includes("admin") && !permissions.includes("writer") && !permissions.includes("canPost")) return res.status(403).send("You cannot upload a File");
console.log("Uploading image");
let filename;
try {
filename = await uploadFile(req, res, "images", ["jpg", "jpeg", "png", "webp", "heic"]);
} catch (error) {
return res.status(500).send(error.message);
}
return res.status(200).send(filename);
});
router.post('/uploadHomework', async (req, res) => {
if (!req.session.userID) return res.status(401).send("Not logged in");
const permissions = await db.getUserPermissions(req.session.userID);
if (!permissions.includes("admin") && !permissions.includes("classmate")) return res.status(403).send("You cannot upload a Homework");
let newFilename;
const oldFilename = req.query.filename;
if (!oldFilename) return res.status(400).send("Missing filename");
try {
newFilename = await uploadFile(req, res, "homework", [], false);
const entry = await registerFileInDB(req.session.userID, oldFilename, newFilename, "homework");
return res.status(200).send(entry.path);
} catch (error) {
return res.status(500).send(error.message);
}
});
router.post('/uploadProfilePicture', multer().single('file'), async (req, res) => {
if (!req.session.userID) return res.status(401).send("Not logged in");
console.log("Uploading profile picture");
let { x, y, scale } = req.body;
x = parseFloat(x);
y = parseFloat(y);
scale = parseFloat(scale);
const file = req.file;
console.log("Uploading profile picture");
// Validate the file
if (!file) return res.status(400).send("Missing file");
// Validate the x, y, and scale
if (typeof x === "undefined" || typeof y === "undefined" || typeof scale === "undefined") return res.status(400).send("Missing x, y, or scale");
if (isNaN(x) || isNaN(y) || isNaN(scale)) return res.status(400).send("x, y, and scale must be numbers");
if (typeof x !== "number" || typeof y !== "number" || typeof scale !== "number") return res.status(400).send("x, y, and scale must be numbers");
try {
const image = sharp(file.buffer)
const metadata = await image.metadata();
const width = metadata.width;
const height = metadata.height;
const extractWidth = Math.floor(width / scale);
const extractHeight = Math.floor(height / scale);
let extractLeft = Math.floor(x * width);
let extractTop = Math.floor(y * height);
if (extractWidth + extractLeft > width) extractLeft = 0;
if (extractHeight + extractTop > height) extractTop = 0;
if (extractLeft + extractWidth > width || extractTop + extractHeight > height) {
console.warn("Invalid crop size");
return res.status(400).send("Invalid crop");
}
image
.rotate()
.extract({ width: extractWidth, height: extractHeight, left: extractLeft, top: extractTop })
.resize(config.profilePictureResolution, config.profilePictureResolution)
.toBuffer()
.then(async (data) => {
//write the file to local disk on fs
console.log("Writing file to disk");
const filename = generateRandomFilename();
const fileExtension = file.originalname.split('.').pop().toLowerCase();
const newFilename = `${filename}.${fileExtension}`;
const s3Path = `profile-pictures/${newFilename}`;
await s3Client.send(new PutObjectCommand({
Bucket: "transmitterstorage",
Key: s3Path,
Body: data,
ContentType: file.mimetype,
ACL: "public-read",
CacheControl: "max-age=31536000",
ContentDisposition: "inline"
}));
registerFileInDB(req.session.userID, newFilename, s3Path, "profile-pictures");
const currentProfilePic = await db.getPreference(req.session.userID, 'profilePic');
if (currentProfilePic.type === "custom") {
console.log("Deleting old profile picture");
await deleteFile(currentProfilePic.content);
};
db.setPreference(req.session.userID, 'profilePic', { "type": "custom", "content": s3Path });
res.status(200).send(s3Path);
});
} catch (error) {
console.error("Error processing image:", error);
return res.status(500).send("Error processing image");
}
});
router.post('/resetProfilePicture', async (req, res) => {
if (!req.session.userID) return res.status(401).send("Not logged in");
const currentProfilePic = await db.getPreference(req.session.userID, 'profilePic');
if (currentProfilePic.type === "custom") {
console.log("Deleting old profile picture");
await deleteFile(currentProfilePic.content);
};
function generateRandomProfilePic() {
return { "type": "default", "content": "#" + Math.floor(Math.random() * 16777215).toString(16) };
}
const newProfilePic = generateRandomProfilePic();
db.setPreference(req.session.userID, 'profilePic', newProfilePic);
res.status(200).send(newProfilePic);
});
return router;
};