-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.js
97 lines (88 loc) · 3.5 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
const _ = require("lodash");
const {Storage} = require("@google-cloud/storage");
module.exports = {
REGEX_EXTENSION: /(?:\.([^.]+))?$/,
REGEX_SUFFIX: /(\d+)?(?!.*\d)/,
/**
* Get filename based on settings
* @param {string} originalName original filename of incoming file stream
* @param {string} uuidName a generated UUID followed by the original extension
* @param {boolean|string} keepName whether to use original filename, defaults to false
* [keepName=false, default]:
* The `upload` implements a unique filename by combining:
* • a generated UUID (like "4d5f444-38b4-4dc3-b9c3-74cb7fbbc932")
* • the uploaded file's original extension (like ".jpg")
*
* [keepName=true]:
* The `upload` will keep the original filename and extension.
*
* [keepName=specified string]:
* The `upload` will rename the file as the value of `keepName`.
* @returns {string} expected filename
*/
getFilename(originalName, uuidName, keepName = false) {
let saveAs = uuidName;
if (_.isBoolean(keepName) && keepName) {
saveAs = originalName;
} else if (_.isString(keepName)) {
saveAs = keepName
}
let extName = this.REGEX_EXTENSION.exec(saveAs)[0];
if (!extName) {
extName = this.REGEX_EXTENSION.exec(uuidName)[0];
saveAs += extName;
}
return saveAs;
},
/**
* Rename the file with incremental suffix
* @param filename
* @returns {string} new filename
*/
incrementalRename: function (filename) {
const suffixRe = this.REGEX_SUFFIX;
const currSuffix = suffixRe.exec(filename)[0];
if (currSuffix) {
return filename.replace(suffixRe, Number(currSuffix) + 1);
} else {
return filename.replace(this.REGEX_EXTENSION, '_1$&');
}
},
/**
* Get a bucket from gcs.
* @param {object} options Options to access buckets
*/
getBucket: function (options) {
const authOpts = {
projectId: options.projectId || process.env.GOOGLE_CLOUD_PROJECT,
keyFilename: options.keyFilename || (this.projectId ? process.env.GOOGLE_APPLICATION_CREDENTIALS : undefined),
}
const storage = new Storage(this.stripKeysWithNilValues(authOpts));
return storage.bucket(options.bucket);
},
/**
* Get a bucket from gcs. Create a new one if not exists.
* @param {object} options Options to access the bucket.
* @param {function} cb Callback function executed after creation
*/
getOrCreateBucket: function (options, cb) {
const bucket = this.getBucket(options);
bucket.exists().then(exists => {
if (!exists[0]) {
const metadata = this.stripKeysWithNilValues(options.bucketMetadata);
bucket.create(metadata).then(data => {
const newBucket = data[0];
cb(newBucket);
})
} else {
cb(bucket);
}
});
},
/**
* destructive -- mutates, returns reference only for convenience
*/
stripKeysWithNilValues: function (dictionary) {
return _.omitBy(dictionary, _.isNil);
},
}