-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
51 lines (42 loc) · 1.47 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
const crypto = require('crypto');
const fs = require('fs');
const path = require('path');
const algorithm = 'md5';
const fileBagPath = path.resolve(__dirname, 'files');
const fileBagDataPath = path.resolve(fileBagPath, 'data');
const fileBagInfoPath = path.resolve(fileBagPath, 'info');
module.exports = {
getFileBagInfo: function() {
return {
rootPath: fileBagPath,
dataPath: fileBagDataPath,
infoPath: fileBagInfoPath
}
},
createFileInfo: function(fileUploaded){
let hashName = crypto.createHash(algorithm);
let hashData = crypto.createHash(algorithm);
let idHash = crypto.createHash(algorithm);
hashName.update(fileUploaded.name);
hashData.update(fileUploaded.data);
let info = {
name: fileUploaded.name,
mimetype: fileUploaded.mimetype,
dataHash: hashData.digest('hex'),
nameHash: hashName.digest('hex'),
}
info.id = idHash.update(info.dataHash + info.nameHash).digest('hex');
return info;
},
getAllFilesInfo: function() {
return fs.readdirSync(fileBagInfoPath).map(fileName => {
let filePath = path.resolve(fileBagInfoPath, fileName);
let content = fs.readFileSync(filePath);
return JSON.parse(content);
});
},
getFileInfo: function(id) {
return this.getAllFilesInfo()
.filter(fileInfo => fileInfo.id === id)[0];
}
}