-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorecloud.js
More file actions
230 lines (211 loc) · 12.7 KB
/
Copy pathstorecloud.js
File metadata and controls
230 lines (211 loc) · 12.7 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/**
* @class storecloud
* @module Node
*/
var fs = require('fs');
var crypto = require('crypto');
var pathLib = require('path');
var rest = require('restler');
var moment = require('moment');
var mime = require('mime');
var _ = require('lodash');
module.exports = function (privateKey, googleServicesEmail, storageBucket) {
// Lets pull from environment variables if information is not given
googleServicesEmail = googleServicesEmail || process.env.GOOGLE_SERVICES_EMAIL;
storageBucket = storageBucket || process.env.GCS_STORAGE_BUCKET;
privateKey = privateKey || process.env.GCS_PRIVATE_KEY;
if(!googleServicesEmail || !storageBucket || !privateKey) {
// console.log(googleServicesEmail, '|', storageBucket, '|', privateKey);
throw 'Google Cloud Storage not configured';
}
// Accepts paths too for private key
if (!privateKey.match(/BEGIN (RSA )?PRIVATE KEY/)) {
privateKey = fs.readFileSync(privateKey,'utf8').toString();
}
var storecloud = {
/**
* @param acl string See below
*
* https://developers.google.com/storage/docs/accesscontrol#extension
* project-private Gives permission to the project team based on their roles. Anyone who is part of the team has READ permission and project owners and project editors have FULL_CONTROL permission. This is the default ACL for newly created buckets. This is also the default ACL for newly created objects unless the default object ACL for that bucket has been changed.
* private Gives the bucket or object owner FULL_CONTROL permission for a bucket or object.
* public-read Gives the bucket or object owner FULL_CONTROL permission and gives all anonymous users READ permission. When you apply this to an object, anyone on the Internet can read the object without authenticating. When you apply this to a bucket, anyone on the Internet can list objects without authenticating. Important: By default, publicly readable objects are served with a Cache-Control header allowing such objects to be cached for 3600 seconds. If you need to ensure that updates become visible immediately, you should set a Cache-Control header of 'Cache-Control:private, max-age=0, no-transform' on such objects. For help doing this, see the gsutil setmeta command.
* public-read-write Gives the bucket owner FULL_CONTROL permission and gives all anonymous users READ and WRITE permission. This ACL applies only to buckets. When you apply this to a bucket, anyone on the Internet can list, create, overwrite and delete objects without authenticating.
* authenticated-read Gives the bucket or object owner FULL_CONTROL permission and gives all authenticated Google account holders READ permission.
* bucket-owner-read Gives the object owner FULL_CONTROL permission and gives the bucket owner READ permission. This is used only with objects.
* bucket-owner-full-control Gives the bucket owner FULL_CONTROL permission. This is used only with objects.
*/
defaultAcl: function (acl, callback) {
var expiry = new Date(moment().add(1, 'hour').format()).getTime(); // This url should expire in one hour
var stringPolicy = 'PUT\n' + '\n' + '\n' + expiry + '\n' + 'x-goog-acl:' + acl + '\n' + '/' + storageBucket + '/?defaultObjectAcl'; // Lets put together our policy
var signature = encodeURIComponent(crypto.createSign('sha256').update(stringPolicy).sign(privateKey,'base64')); // create signature and make it url safe
var url = 'https://' + storageBucket + '.storage.googleapis.com/?defaultObjectAcl&GoogleAccessId=' + googleServicesEmail + '&Expires=' + expiry + '&Signature=' + signature; // signed url
rest.put(url, {
headers: {
'x-goog-acl': acl
}
}).on('complete', function (err, res) {
if (callback) { callback(); }
});
},
/** Setup Cors
*/
cors: function (xml, callback) {
var expiry = new Date(moment().add(1, 'hour').format()).getTime(); // This url should expire in one hour
var stringPolicy = 'PUT\n' + '\n' + '\n' + expiry + '\n' + '/' + storageBucket + '/?cors'; // Lets put together our policy
var signature = encodeURIComponent(crypto.createSign('sha256').update(stringPolicy).sign(privateKey,'base64')); // create signature and make it url safe
var url = 'https://' + storageBucket + '.storage.googleapis.com/?cors&GoogleAccessId=' + googleServicesEmail + '&Expires=' + expiry + '&Signature=' + signature; // signed url
rest.put(url, {
data: xml
}).on('complete', function (err, res) {
if (callback) { callback(); }
});
},
getCors: function () {
var expiry = new Date(moment().add(1, 'hour').format()).getTime(); // This url should expire in one hour
var stringPolicy = 'GET\n' + '\n' + '\n' + expiry + '\n' + '/' + storageBucket + '/?cors'; // Lets put together our policy
var signature = encodeURIComponent(crypto.createSign('sha256').update(stringPolicy).sign(privateKey,'base64')); // create signature and make it url safe
var url = 'https://' + storageBucket + '.storage.googleapis.com/?cors&GoogleAccessId=' + googleServicesEmail + '&Expires=' + expiry + '&Signature=' + signature; // signed url
rest.get(url).on('complete', function (err, res) {
// console.log(res.rawEncoded);
});
},
/** Check if file exists
* @param {string} key
* @param {function} callback
*/
exists: function (key, callback) {
rest.get('https://' + storageBucket + '.storage.googleapis.com/' + key + '?v=' + Date.now()).on('complete', function (data, res) {
callback(res.statusCode != 404);
});
},
/** Get meta data for the given key
* @param {string} key
* @param {function} callback
*/
metaData: function (key, callback) {
var expiry = new Date(moment().add(1, 'hour').format()).getTime(); // This url should expire in one hour
var stringPolicy = 'HEAD\n' + '\n' + '\n' + expiry + '\n' + '/' + storageBucket + '/' + key; // Lets put together our policy
var signature = encodeURIComponent(crypto.createSign('sha256').update(stringPolicy).sign(privateKey,'base64')); // create signature and make it url safe
var url = 'https://' + storageBucket + '.storage.googleapis.com/' + key + '?GoogleAccessId=' + googleServicesEmail + '&Expires=' + expiry + '&Signature=' + signature; // signed url
rest.head(url).on('complete', function (err, res) {
callback(res.headers);
});
},
makePrivate: function (key, callback) {
this.metaData(key, function (metaData) {
var expiry = new Date(moment().add(1, 'hour').format()).getTime(); // This url should expire in one hour
var stringPolicy = 'PUT\n' + '\n' + '\n' + expiry + '\n' +
'x-goog-acl:bucket-owner-full-control\n' +
// 'x-goog-if-generation-match:'+metaData['x-goog-generation']+'\n' +
// 'x-goog-if-metageneration-match:'+metaData['x-goog-metageneration']+'\n' +
'/' + storageBucket + '/' + key + '?acl'; // Lets put together our policy
//base64Policy = Buffer(stringPolicy, 'utf-8').toString('base64'), // convert it to Base64
var signature = encodeURIComponent(crypto.createSign('sha256').update(stringPolicy).sign(privateKey,'base64')); // create signature and make it url safe
var url = 'https://' + storageBucket + '.storage.googleapis.com/' + key + '?acl&generation=' + metaData['x-goog-generation'] + '&GoogleAccessId=' + googleServicesEmail + '&Expires=' + expiry + '&Signature=' + signature; // signed url
rest.put(url, {
headers: {
// 'x-goog-if-generation-match': metaData['x-goog-generation'],
// 'x-goog-if-metageneration-match': metaData['x-goog-metageneration'],
'x-goog-acl': 'bucket-owner-full-control'
}
}).on('complete', callback);
});
},
makePublic: function (key, callback) {
// TODO Maintain Content-Disposition and Content-Type
this.metaData(key, function (metaData) {
var expiry = new Date(moment().add(1, 'hour').format()).getTime(); // This url should expire in one hour
var stringPolicy = 'PUT\n' + '\n' + '\n' + expiry + '\n' +
'x-goog-acl:public-read\n' +
'/' + storageBucket + '/' + key + '?acl'; // Lets put together our policy
var signature = encodeURIComponent(crypto.createSign('sha256').update(stringPolicy).sign(privateKey,'base64')); // create signature and make it url safe
var url = 'https://' + storageBucket + '.storage.googleapis.com/' + key + '?acl&generation=' + metaData['x-goog-generation'] + '&GoogleAccessId=' + googleServicesEmail + '&Expires=' + expiry + '&Signature=' + signature; // signed url
rest.put(url, {
headers: {
'x-goog-acl': 'public-read'
}
}).on('complete', callback);
});
},
getPublicUrl: function (key) {
return 'https://' + storageBucket + '.storage.googleapis.com/' + key;
},
getPrivateUrl: function (key) {
// As described here: https://developers.google.com/storage/docs/accesscontrol#Signed-URLs
var expiry = new Date(moment().add(1, 'hour').format()).getTime(); // This url should expire in one hour
var stringPolicy = 'GET\n' + '\n' + '\n' + expiry + '\n' + '/' + storageBucket + '/' + key; // Lets put together our policy
var signature = encodeURIComponent(crypto.createSign('sha256').update(stringPolicy).sign(privateKey,'base64')); // create signature and make it url safe
return 'https://' + storageBucket + '.storage.googleapis.com/' + key +'?GoogleAccessId=' + googleServicesEmail + '&Expires=' + expiry + '&Signature=' + signature;
},
remove: function (key, callback) {
var expiry = new Date(moment().add(1, 'hour').format()).getTime(); // This url should expire in one hour
var stringPolicy = 'DELETE\n' + '\n' + '\n' + expiry + '\n' + '/' + storageBucket + '/' + key; // Lets put together our policy
var signature = encodeURIComponent(crypto.createSign('sha256').update(stringPolicy).sign(privateKey,'base64')); // create signature and make it url safe
var url = 'https://' + storageBucket + '.storage.googleapis.com/' + key + '?GoogleAccessId=' + googleServicesEmail + '&Expires=' + expiry + '&Signature=' + signature; // signed url
rest.del(url).on('complete', callback);
},
/**
* @param isAttachment When isAttachment is set, accessing the file should force a download prompt.
* @param customFields {Object} dictionary for custom fields
*/
uploadRequest: function (filename, key, isAttachment, customFields) {
var mimeType = mime.lookup(filename);
var uploadPolicy = {
'expiration': moment().add(1, 'hour').toISOString(),
'conditions': [
{'bucket': storageBucket},
{'key': key},
{'Content-Type': mimeType}
]
};
if(isAttachment) {
uploadPolicy.conditions.push({
'Content-Disposition': 'attachment; filename=' + pathLib.basename(filename)
});
}
if(customFields && customFields['Cache-Control']) {
uploadPolicy.conditions.push({
'Cache-Control': customFields['Cache-Control']
});
}
_.each(customFields, function (value, field) {
var customField = {};
customField['x-goog-meta-' + field] = value;
uploadPolicy.conditions.push(customField);
});
var uploadSignature = crypto.createSign('sha256').update(new Buffer(JSON.stringify(uploadPolicy)).toString('base64')).sign(privateKey,'base64');
var request = {
GoogleAccessId: googleServicesEmail,
key: key,
'Content-Type': mimeType,
bucket: storageBucket,
policy: new Buffer(JSON.stringify(uploadPolicy)).toString('base64'),
signature: uploadSignature,
};
if(isAttachment) {
request['Content-Disposition'] = 'attachment; filename=' + pathLib.basename(filename);
}
if(customFields && customFields['Cache-Control']) {
request['Cache-Control'] = customFields['Cache-Control'];
}
_.each(customFields, function (value, field) {
request['x-goog-meta-' + field] = value;
});
return request;
},
upload: function (filename, key, isAttachment, customFields, callback) {
if (!callback) { callback = function () {}; }
var uploadRequest = this.uploadRequest(filename, key, isAttachment, customFields);
uploadRequest.file = rest.file(filename); // Add the file to the upload request
// multipart request sending a 321567 byte long file using https
rest.post('https://' + storageBucket + '.storage.googleapis.com/', {
multipart: true,
data: uploadRequest
}).on('complete', function (err, res) {
callback(res.statusCode == 204 || res.statusCode == 200 ? true : false);
});
}
};
return storecloud;
};