-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseStorage.js
84 lines (72 loc) · 2.2 KB
/
BaseStorage.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
const moment = require('moment'),
path = require('path');
class StorageBase {
constructor() {
Object.defineProperty(this, 'requiredFns', {
value: ['exists', 'save', 'serve', 'delete', 'read'],
writable: false
});
}
getTargetDir(baseDir) {
const date = moment(),
month = date.format('MM'),
year = date.format('YYYY');
if (baseDir) {
return path.join(baseDir, year, month);
}
return path.join(year, month);
}
/**
*
* @param {String} dir
* @param {String} name
* @param {String} ext
* @param {Number} i index
* @returns {Promise<String>}
*/
generateUnique(dir, name, ext, i) {
let filename,
append = '';
if (i) {
append = '-' + i;
}
if (ext) {
filename = name + append + ext;
} else {
filename = name + append;
}
return this.exists(filename, dir).then((exists) => {
if (exists) {
i = i + 1;
return this.generateUnique(dir, name, ext, i);
} else {
return path.join(dir, filename);
}
});
}
/**
* @param {Object} file
* @param {String} file.name
* @param {String} targetDir
*
* @returns {Promise<String>} unique file path
*/
getUniqueFileName(file, targetDir) {
var ext = path.extname(file.name), name;
// poor extension validation
// .1 or .342 is not a valid extension, .mp4 is though!
if (!ext.match(/\.\d+$/)) {
name = this.getSanitizedFileName(path.basename(file.name, ext));
return this.generateUnique(targetDir, name, ext, 0);
} else {
name = this.getSanitizedFileName(path.basename(file.name));
return this.generateUnique(targetDir, name, null, 0);
}
}
getSanitizedFileName(fileName) {
// below only matches ascii characters, @, and .
// unicode filenames like город.zip would therefore resolve to ----.zip
return fileName.replace(/[^\w@.]/gi, '-');
}
}
module.exports = StorageBase;