-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
302 lines (249 loc) · 7.63 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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
const fs = require("fs");
const path = require("path");
const yauzl = require('yauzl');
const { mkdirp } = require('mkdirp');
const { DirItem } = require("./dir_item");
const getAllFiles = async function (dirPath, arrayOfFiles) {
let files = await fs.promises.readdir(dirPath)
arrayOfFiles = arrayOfFiles || []
files.forEach(async function (file) {
if (fs.statSync(dirPath + "/" + file).isDirectory()) {
arrayOfFiles = await getAllFiles(dirPath + "/" + file, arrayOfFiles)
} else {
arrayOfFiles.push(path.join(__dirname, dirPath, file))
}
})
return arrayOfFiles
}
const convertBytes = function (bytes) {
const sizes = ["Bytes", "KB", "MB", "GB", "TB"]
if (bytes == 0) {
return "N/A"
}
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)))
if (i == 0) {
return bytes + " " + sizes[i]
}
return (bytes / Math.pow(1024, i)).toFixed(1) + " " + sizes[i]
}
const convertStringToBytes = function (sizeString) {
const sizes = ["Bytes", "KB", "MB", "GB", "TB"];
const [value, unit] = sizeString.split(" ");
const index = sizes.indexOf(unit);
if (index === -1 || isNaN(value)) {
return 0;
}
return parseFloat(value) * Math.pow(1024, index);
}
function getStringSize(str) {
return new Blob([str]).size;
}
async function removeDir(directoryPath) {
if (fs.existsSync(directoryPath)) {
const files = await fs.promises.readdir(directoryPath);
for (const file of files) {
const curPath = path.join(directoryPath, file);
const stats = await fs.promises.lstat(curPath);
if (stats.isDirectory()) {
await removeDir(curPath);
} else {
await fs.promises.unlink(curPath);
}
}
await fs.promises.rmdir(directoryPath);
}
}
function replacePathPrefix(fullPath, realPrefix) {
if (fullPath.startsWith(realPrefix)) {
return "./" + fullPath.slice(realPrefix.length);
}
return fullPath;
}
function getParent(filePath) {
let lastIndex = filePath.lastIndexOf('/');
if (lastIndex === -1) return filePath;
let item = filePath.slice(0, lastIndex);
if (item.length > 1) {
return item;
} else {
return filePath.slice(0, lastIndex + 1);
}
}
function extractFiles(zipFilePath, outputDir, totalUncompressedSize, socket, io) {
let extractedBytes = 0;
let lastPercentage = 0;
yauzl.open(zipFilePath, { lazyEntries: true }, (err, zipfile) => {
if (err) {
console.log("Unknown error.");
throw err
};
zipfile.readEntry();
zipfile.on('entry', (entry) => {
const fullPath = path.join(outputDir, entry.fileName);
if (/\/$/.test(entry.fileName)) {
mkdirp(fullPath).then(() => {
zipfile.readEntry();
}).catch((err) => {
if (socket) {
io.to(socket.id).emit({
err: "Unknown error while unzipping!"
})
}
console.error('An error occurred while ensuring directory exists:', err);
return
});
} else {
mkdirp(path.dirname(fullPath)).then(() => {
zipfile.openReadStream(entry, (err, readStream) => {
if (err) throw err;
readStream.on('data', (chunk) => {
extractedBytes += chunk.length;
if (socket) {
const percentage = Math.floor((extractedBytes / totalUncompressedSize) * 100);
if (percentage > lastPercentage) {
let = lastPercentage = percentage;
console.log(`Progress: ${percentage}%`);
io.to(socket.id).emit("unzip-progress", {
progress: percentage
})
}
}
});
readStream.on('end', () => {
zipfile.readEntry();
});
readStream.pipe(fs.createWriteStream(fullPath));
});
}).catch((err) => {
console.log("We can't extract zip files with passwords.");
console.error('An error occurred while ensuring directory exists:', err);
zipfile.readEntry();
});
}
});
zipfile.on('end', () => {
console.log('Extraction completed.');
io.to(socket.id).emit("unzip-completed", {
message: "Successfully completed!"
})
});
zipfile.on('error', (err) => {
io.to(socket.id).emit("error", {
err: "Unknown error!"
})
console.error('An error occurred:', err);
});
});
}
const getDirItems = async function (dirPath, mode, config) {
try {
const data = await fs.promises.readdir(dirPath, { withFileTypes: true });
let arrayOfItems = [];
for (const [index, file] of data.entries()) {
let fileStats = fs.statSync(path.join(dirPath, file.name));
let isDirectory = fileStats.isDirectory();
let size = fileStats.size;
let parentPath;
if (mode === "Quality mode" && isDirectory) {
size = await getTotalSize(path.join(dirPath, file.name));
}
if (file.parentPath === config.folder + "/") {
parentPath = "./";
} else {
parentPath = replacePathPrefix(file.parentPath, `${config.folder}/`);
}
let item;
mode === "Quality mode" && isDirectory ?
item = new DirItem(file.name, parentPath, `${parentPath}${file.name}`, isDirectory, fileStats.birthtime, fileStats.mtime, size, index)
: item = new DirItem(file.name, parentPath, `${parentPath}${file.name}`, isDirectory, fileStats.birthtime, fileStats.mtime, convertBytes(size), index);
arrayOfItems.push(item);
}
return arrayOfItems;
} catch (err) {
console.error(err);
return [];
}
};
const getTotalSize = async function (directoryPath, stringOutput = true) {
const getSize = async (filePath) => {
const stats = await fs.promises.stat(filePath);
if (stats.isDirectory()) {
const files = await fs.promises.readdir(filePath);
const sizes = await Promise.all(files.map(file => getSize(path.join(filePath, file))));
return sizes.reduce((acc, size) => acc + size, 0);
} else {
return stats.size;
}
};
try {
const totalSize = await getSize(directoryPath);
if (stringOutput) {
return convertBytes(totalSize);
} else {
return totalSize;
}
} catch (err) {
console.error('Error calculating size:', err);
throw err;
}
};
const outputFolderSize = async (config) => {
try {
let folderSize = await getTotalSize(config.folder);
if (config.storage_limit) {
console.log("Total size: ".green, `${folderSize} / ${config.storage_limit}`);
} else {
console.log("Total size: ".green, folderSize);
}
} catch (err) {
console.error(err);
}
}
const getRemainingFolderSpace = async (config) => {
let maxSize = convertStringToBytes(config.storage_limit);
let folderSize = await getTotalSize(config.folder, false);
let remainingSpace = maxSize - folderSize;
return remainingSpace;
}
const checkSecurityIssue = async (req) => {
if (req.query.folder) {
if (req.query.folder.match(/\/\.\./)) {
return true
}
}
if (req.query.filepath) {
if (req.query.filepath.match(/\/\.\./)) {
return true
}
}
if (req.query.path) {
if (req.query.path.match(/\/\.\./)) {
return true
}
}
if (req.query.oldFilepath) {
if (req.query.oldFilepath.match(/\/\.\./)) {
return true
}
}
if (req.query.newFilepath) {
if (req.query.newFilepath.match(/\/\.\./)) {
return true
}
}
return false
}
module.exports = {
getTotalSize,
getDirItems,
getParent,
replacePathPrefix,
removeDir,
convertStringToBytes,
getStringSize,
getRemainingFolderSpace,
convertBytes,
extractFiles,
outputFolderSize,
checkSecurityIssue
};