-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFile.js
35 lines (31 loc) · 866 Bytes
/
File.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
const multer = require("multer");
const path = require("path");
const fs = require("fs").promises;
class File {
static uploadFile() {
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "public/");
},
filename: function (req, file, cb) {
const uniqueSuffix = Date.now() + "-" + Math.round(Math.random() * 1e9);
const extension = path.extname(file.originalname);
cb(null, file.fieldname + "-" + uniqueSuffix + extension);
},
});
return multer({ storage: storage });
}
static async deleteFile(filePath) {
try {
await fs.unlink(filePath);
return true;
} catch (err) {
if (err.code === "ENOENT") {
// File does not exist, no need to delete
return true;
}
return false;
}
}
}
module.exports = File;