This repository has been archived by the owner on Jun 15, 2020. It is now read-only.
forked from jahmed76/multer-gcs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
109 lines (89 loc) · 2.62 KB
/
index.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
const storage = require('@google-cloud/storage');
const uuid = require('uuid/v4');
function getFilename(req, file, cb) {
cb(null, uuid());
}
function getDestination(req, file, cb) {
cb(null, '');
}
function GCStorage(opts = {}) {
this.getFilename = opts.filename || getFilename;
if ('string' === typeof opts.destination) {
this.getDestination = function($0, $1, cb) {
cb(null, opts.destination);
};
} else {
this.getDestination = opts.destination || getDestination;
}
opts.bucket = opts.bucket || process.env.GCS_BUCKET;
if (!opts.bucket) {
throw new Error(
'You have to specify bucket for Google Cloud Storage to work.'
);
}
const options = ['projectId', 'keyFilename', 'credentials'];
const config = options.reduce((accumulator, option) => {
if (opts[option]) accumulator[option] = opts[option];
return accumulator;
}, {});
this.gcobj = Object.keys(config).length > 0 ? storage(config) : storage();
this.gcsBucket = this.gcobj.bucket(opts.bucket);
opts.transformers = opts.transformers || [];
this.options = opts;
}
GCStorage.prototype._handleFile = function(req, file, cb) {
const self = this;
self.getDestination(req, file, (err, destination) => {
if (err) {
return cb(err);
}
self.getFilename(req, file, (err, filename) => {
if (err) {
return cb(err);
}
// set options for upload
const uploadOptions = {
metadata: self.options.metadata || {contentType: file.mimetype},
predefinedAcl: self.options.acl || 'projectPrivate',
};
const uploadPath = destination ? `${destination}/${filename}` : filename;
const gcFile = self.gcsBucket.file(uploadPath);
let stream = file.stream;
for (const transformer of self.options.transformers) {
stream = stream.pipe(transformer()).on('error', (err) => {
return cb(err);
});
}
stream
.pipe(gcFile.createWriteStream(uploadOptions))
.on('error', (err) => {
return cb(err);
})
.on('finish', () => {
const urlConfig = self.options.urlConfig || {
actions: 'read',
expires: new Date(new Date().getTime() + 7 * 24 * 60 * 60 * 1000),
};
gcFile.getSignedUrl(urlConfig, (err, url) => {
if (err) {
return cb(err);
}
return cb(null, {
destination: destination,
path: uploadPath,
url: url,
filename: filename,
mimetype: uploadOptions.metadata.contentType || file.mimetype,
});
});
});
});
});
};
GCStorage.prototype._removeFile = function _removeFile(req, file, cb) {
const gcFile = this.gcsBucket.file(file.path);
gcFile.delete(cb);
};
module.exports = function(opts) {
return new GCStorage(opts);
};