-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
154 lines (139 loc) · 4.69 KB
/
utils.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
const path = require('path');
const decompress = require('decompress');
const fs = require('fs');
const config = JSON.parse(fs.readFileSync('config/config.json', 'utf8'));
/**
* Adds a new entry to the server.log file in that form:
* *Wed Jan 1 2020 00:00:00 GMT+0800 (Malaysia Time): [POST] /url*
* @param {any} req Request object
* @param {any} res Response object
* @param {any} next A reference to the next middleware
*/
const addLogEntry = (req, res, next) => {
var now = new Date().toString();
var log = `\r\n${now}: [${req.method}] ${req.url}`;
fs.appendFile(`server.log`, log, { encoding: `utf-8` }, (err) => {
if(err != null || err != undefined)
console.log(err);
});
next();
}
/**
* Checks if the `file` object exists in the request body or not
* @param {any} req Request object
* @param {any} res Response object
*/
const checkIfFileExists = (req, res) => {
// Check if req.files object exists
if(req.files == null || req.files == undefined)
{
res.statusCode = 400;
res.send('No files found');
return;
}
// Check if req.files.file object exists
if(req.files.file == null || req.files.file == undefined){
res.statusCode = 404;
res.send('File object not found, make sure that request parameter name is named "file"');
return
}
}
/**
* Extracts the `file` object from the request body
* @param {any} req Request object
*/
const getFileObject = (req) => {
// Check if there are more than one file object and pick only one file
return req.files.file.length > 1 ? req.files.file[0] : req.files.file;
}
/**
* Checks the `mimetype` object of the uploaded file, if matches, it will
* continue, else it will return an `Invalid format found` message
* @param {any} req Request object
* @param {any} res Response object
* @param {any} uploadedFile File object to be checked
* @param {any} mimeType A string (or substring) of the mimetype to be checked
*/
const checkMimeType = (req, res, uploadedFile, mimeType) => {
let imageMimeType = uploadedFile.mimetype;
// Check for image mime type
if(! imageMimeType.includes(mimeType))
{
console.log(req.files.file);
res.statusCode = 400;
res.send('Invalid format found');
return;
}
}
/**
* Generates a unique file path for a specific file
* @param {any} uploadedFile File object
*/
const generateUniquePath = (uploadedFile) => {
let extension = path.extname(uploadedFile.name);
// Create storage directory if not existed
createDirIfNotExist(config.uploadLocation);
// Generate unique path and store file
return config.uploadLocation + guid() + extension;
}
/**
* Stores a file into a unique location and can do a decompression for a file if it is a compressed file
* @param {any} res Response object
* @param {any} uploadedFile File object
* @param {any} uniquePath A unique path for the file to be stored in
* @param {any} doDecompress A flag that represents weather to do a decompression or not
*/
const storeFile = (res, uploadedFile, uniquePath, doDecompress) => {
uploadedFile.mv(uniquePath).then((file) => {
if(doDecompress) {
// Decompress file
decompressFile(uniquePath, (uniqueImagePaths) => {
// Return unique image locations
res.send({imagePaths: uniqueImagePaths});
});
}
}).catch((err) => {
if (err) {
console.log(err);
res.statusCode = 500;
return res.send(err);
}
});
}
exports.addLogEntry = addLogEntry;
exports.checkIfFileExists = checkIfFileExists;
exports.getFileObject = getFileObject;
exports.checkMimeType = checkMimeType;
exports.generateUniquePath = generateUniquePath;
exports.storeFile = storeFile;
//* Private Methods *//
const guid = () => {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
}
const decompressFile = (uniquePath, callback) => {
let uniqueImageLocations = [];
decompress(uniquePath, config.zippedImagesLocation, {
map: file => {
let filePath = guid() + path.extname(file.path);
file.path = filePath;
uniqueImageLocations.push(path.join(config.zippedImagesLocation,filePath));
return file;
}
})
.then(files => {
callback(uniqueImageLocations);
}).catch((err) => {
console.log(err);
callback(uniqueImageLocations);
});
}
const createDirIfNotExist = (path) => {
if (!fs.existsSync(path)){
fs.mkdirSync(path);
}
}