forked from onlinemad/skipper-gcs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
109 lines (103 loc) · 3.02 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
/**
* Module dependencies
*/
const Writable = require("stream").Writable;
const _ = require("lodash");
const storage = require("@google-cloud/storage");
const concat = require("concat-stream");
const mime = require("mime");
/**
* skipper-gcs
*
* @param {Object} globalOpts
* @return {Object}
*/
module.exports = function GCSStore(globalOpts) {
globalOpts = globalOpts || {};
_.defaults(globalOpts, {
email: "",
bucket: "",
scopes: ["https://www.googleapis.com/auth/devstorage.full_control",],
});
const gcs = storage({
projectId: globalOpts.projectId,
keyFilename: globalOpts.keyFilename,
});
const bucket = gcs.bucket(globalOpts.bucket);
const adapter = {
ls: function(dirname, cb) {
bucket.getFiles({ prefix: dirname, }, function(err, files) {
if (err) {
cb(err);
} else {
files = _.map(files, "name");
cb(null, files);
}
});
},
read: function(fd, cb) {
const remoteReadStream = bucket.file(fd).createReadStream();
remoteReadStream
.on("error", function(err) {
cb(err);
})
.on("response", function() {
// Server connected and responded with the specified status and headers.
})
.on("end", function() {
// The file is fully downloaded.
})
.pipe(concat(function(data) {
cb(null, data);
}));
},
rm: function(fd, cb) {
bucket.file(fd).delete(cb);
},
/**
* A simple receiver for Skipper that writes Upstreams to Google Cloud Storage
*
* @param {Object} options
* @return {Stream.Writable}
*/
receive: function GCSReceiver(options) {
options = options || {};
options = _.defaults(options, globalOpts);
const receiver__ = Writable({
objectMode: true,
});
// This `_write` method is invoked each time a new file is received
// from the Readable stream (Upstream) which is pumping filestreams
// into this receiver. (filename === `__newFile.filename`).
receiver__._write = function onFile(__newFile, encoding, done) {
const metadata = {};
_.defaults(metadata, options.metadata);
metadata.contentType = mime.lookup(__newFile.fd);
const file = bucket.file(__newFile.fd);
const stream = file.createWriteStream({
metadata: metadata,
});
stream.on("error", function(err) {
receiver__.emit("error", err);
return;
});
stream.on("finish", function() {
__newFile.extra = file.metadata;
if(globalOpts.public) {
file.makePublic().then(() => {
__newFile.extra.Location = "https://storage.googleapis.com/" + globalOpts.bucket + "/" + __newFile.fd;
done();
});
} else {
file.makePrivate().then(() => {
done();
});
}
});
__newFile.pipe(stream);
};
return receiver__;
},
};
return adapter;
};