-
Notifications
You must be signed in to change notification settings - Fork 644
Expand file tree
/
Copy pathgcs.js
More file actions
134 lines (122 loc) · 4.24 KB
/
Copy pathgcs.js
File metadata and controls
134 lines (122 loc) · 4.24 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
/* jshint node:true */
// Google Cloud Storage backend for uploadfs. See also
// local.js.
const storage = require('@google-cloud/storage');
const extname = require('path').extname;
const _ = require('lodash');
const utils = require('../utils');
const path = require('path');
module.exports = function() {
let contentTypes;
let client;
let cachingTime;
let https;
let bucketName;
let endpoint = 'storage.googleapis.com';
let defaultTypes;
let noProtoEndpoint;
let validation = false;
const self = {
init: function (options, callback) {
if (options.validation) {
validation = options.validation;
}
// Ultimately the result will look like https://storage.googleapis.com/[BUCKET_NAME]/[OBJECT_NAME]
// The rest is based mostly on s3 knox surmises.
if (options.endpoint) {
endpoint = options.endpoint;
if (!endpoint.match(/^https?:/)) {
const defaultSecure = ((!options.port) || (options.port === 443));
const secure = options.secure || defaultSecure;
let port = options.port || 443;
const protocol = secure ? 'https://' : 'http://';
if (secure && (port === 443)) {
port = '';
} else if ((!secure) && (port === 80)) {
port = '';
} else {
port = ':' + port;
}
endpoint = protocol + endpoint + port;
}
}
// The storage client auth relies on the presence of Application Default
// Credentials.
// i.e., one of GOOGLE_APPLICATION_CREDENTIALS environment variable; a
// credential file created by using the gcloud auth application-default
// login command`; or the attached service account, returned by the
// metadata server.
//
// See https://docs.cloud.google.com/docs/authentication/application-default-credentials
client = new storage.Storage();
bucketName = options.bucket;
defaultTypes = require(path.join(__dirname, '/contentTypes.js'));
if (options.contentTypes) {
_.extend(contentTypes, defaultTypes, options.contentTypes);
} else {
contentTypes = defaultTypes;
}
https = options.https;
cachingTime = options.cachingTime;
self.options = options;
return callback(null);
},
copyIn: function(localPath, path, options, callback) {
path = utils.removeLeadingSlash(self.options, path);
let ext = extname(path);
if (ext.length) {
ext = ext.substr(1);
}
let contentType = contentTypes[ext];
if (!contentType) {
contentType = 'application/octet-stream';
}
let cacheControl = 'no-cache';
if (cachingTime) {
cacheControl = 'public, max-age=' + cachingTime;
}
const uploadOptions = {
destination: path,
gzip: true,
public: true,
validation,
metadata: {
cacheControl,
ContentType: contentType
}
};
const bucket = client.bucket(bucketName);
return bucket.upload(localPath, uploadOptions, callback);
},
copyOut: function(path, localPath, options, callback) {
path = utils.removeLeadingSlash(self.options, path);
const mergedOptions = _.assign({
destination: localPath,
validation
}, options);
client.bucket(bucketName).file(path).download(mergedOptions, callback);
},
remove: function(path, callback) {
path = utils.removeLeadingSlash(self.options, path);
client.bucket(bucketName).file(path).delete({}, callback);
},
enable: function(path, callback) {
path = utils.removeLeadingSlash(self.options, path);
client.bucket(bucketName).file(path).makePublic(callback);
},
disable: function(path, callback) {
path = utils.removeLeadingSlash(self.options, path);
client.bucket(bucketName).file(path).makePrivate({}, callback);
},
getUrl: function (path) {
noProtoEndpoint = endpoint.replace(/^https?:\/\//i, '');
const url = (https ? 'https://' : 'http://') + bucketName + '.' + noProtoEndpoint;
return utils.addPathToUrl(self.options, url, path);
},
destroy: function(callback) {
// No file descriptors or timeouts held
return callback(null);
}
};
return self;
};