-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
98 lines (96 loc) · 2.89 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
/**
* Module dependencies
*/
var debug = require('debug')('skipper-gcs');
var qs = require('querystring');
var Writable = require('stream').Writable;
var _ = require('lodash');
var gcloud = require('gcloud');
var concat = require('concat-stream');
var 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']
});
var gcs = gcloud.storage({
projectId: globalOpts.projectId,
keyFilename: globalOpts.keyFilename
});
var bucket = gcs.bucket(globalOpts.bucket);
var adapter = {
ls: function(dirname, cb) {
bucket.getFiles({ prefix: dirname }, function(err, files) {
if (err) {
cb(err)
} else {
files = _.pluck(files, 'name');
cb(null, files);
}
});
},
read: function(fd, cb) {
var remoteReadStream = bucket.file(fd).createReadStream();
remoteReadStream
.on('error', function(err) {
cb(err);
})
.on('response', function(response) {
// 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) { return cb(new Error('TODO')); },
/**
* 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);
var 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) {
var metadata = {};
_.defaults(metadata, options.metadata);
metadata.contentType = mime.lookup(__newFile.fd);
var file = bucket.file(__newFile.fd);
var stream = file.createWriteStream({
metadata: metadata
});
stream.on('error', function(err) {
receiver__.emit('error', err);
return;
})
stream.on('finish', function() {
__newFile.extra = file.metadata;
__newFile.extra.Location = 'https://storage.googleapis.com/' + globalOpts.bucket + '/' + __newFile.fd;
if(globalOpts.public) file.makePublic();
done();
});
__newFile.pipe(stream);
}
return receiver__;
}
};
return adapter
};