Repository navigation
Expand file tree
/
Copy pathapi.js
More file actions
327 lines (265 loc) · 10.2 KB
/
Copy pathapi.js
File metadata and controls
327 lines (265 loc) · 10.2 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// S3 Explorer API
const assert = require('assert');
const fs = require('fs');
const os = require('os');
const Path = require('path');
const async = require('async');
const mime = require('mime');
const Tools = require('pixl-tools');
const S3 = require('s3-api');
const VALID_S3_BUCKET = /^[a-z0-9\-\.]+$/;
const VALID_S3_PATH = /^[\w\s\/\-\.\=\&]+$/;
module.exports = {
startup: function(callback) {
// A worker child is starting up
// Keep the AWS SDK's default credential chain for env vars, profiles and IAM roles.
// s3-api supplies its own region, so explicitly honor AWS_REGION when provided.
const s3_options = {};
if (process.env.AWS_REGION) s3_options.region = process.env.AWS_REGION;
this.s3 = new S3(s3_options);
this.s3.attachLogAgent( this.logger );
callback();
},
handler: function(args, callback) {
// call appropriate method based on URI
// all requests will be authenticated at this point (args.params.session)
var uri = args.request.url;
var func = '';
// all these API calls need to be no-cached
args.response.headers['Cache-Control'] = "private, no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0";
// route api to specific method
if (uri.match(/\/api\/(\w+)/)) func = 'api_' + RegExp.$1;
if (func && this[func]) {
this.logDebug(9, "Delegating request to API handler: " + func, uri);
this[func]( args, callback );
}
else {
this.doError( 'api', "Unknown API: " + uri, callback );
}
},
api_echo: function(args, callback) {
// self test
callback({ code: 0 });
},
api_run: function(args, callback) {
// get initial app configuration for client-side web app
var self = this;
var env_id = 'local';
var client_config = {
repos: [],
version: this.config.version,
env_id: 'local',
env_short_title: 'Local',
hostnames: {}
};
// fetch list of buckets in s3
this.s3.listBuckets( function(err, buckets) {
if (err) return self.doError('init', "Failed to fetch S3 bucket list: " + err, callback);
// trim bucket list suitable for current environment and region
client_config.repos = buckets.sort();
callback({
code: 0,
config: client_config
});
} );
},
api_getFileList: function(args, callback) {
// get list of files (and possibly subfolders)
var self = this;
var params = Tools.mergeHashes( args.params, args.query );
if (!this.requireParams(params, {
repo: VALID_S3_BUCKET,
path: VALID_S3_PATH
}, callback)) return;
var path = params.path.replace(/^\//, '');
if (path.length && !path.match(/\/$/)) path += '/';
this.logDebug(5, "Getting file list: " + path, params);
this.s3.listFolders( { bucket: params.repo, remotePath: path }, function(err, folders, files) {
var items = [];
(folders || []).forEach( function(folder_path) {
items.push({ filename: Path.basename(folder_path), type: 'directory' });
} );
(files || []).forEach( function(file) {
items.push({
filename: Path.basename(file.key),
type: 'file',
size: file.size,
modified: file.mtime
});
} );
self.logDebug(5, "File listing complete");
callback({ code: 0, files: items });
} ); // s3.listFolders
},
api_renameFile: function(args, callback) {
// rename single file in same directory
var self = this;
var params = Tools.mergeHashes( args.params, args.query );
if (!this.requireParams(params, {
repo: VALID_S3_BUCKET,
path: VALID_S3_PATH,
new_filename: /^[\w\-\.]+$/
}, callback)) return;
var old_path = params.path.replace(/^\//, '');
var parent_path = old_path.match(/\//) ? Path.dirname(old_path) : '';
var new_path = (parent_path + '/' + params.new_filename).replace(/^\//, '');
this.logDebug(5, "Renaming file: " + old_path + " to: " + new_path, params);
this.s3.moveFile( { sourceBucket: params.repo, bucket: params.repo, sourceKey: old_path, key: new_path }, function(err) {
if (err) return self.doError( 'rename', err, callback );
self.logDebug(5, "Rename complete");
callback({ code: 0 });
} );
},
api_deleteFile: function(args, callback) {
// delete single file in existing directory
var self = this;
var params = Tools.mergeHashes( args.params, args.query );
if (!this.requireParams(params, {
repo: VALID_S3_BUCKET,
path: VALID_S3_PATH
}, callback)) return;
this.logDebug(5, "Deleting file: " + params.path, params);
this.s3.deleteFile( { bucket: params.repo, key: params.path.replace(/^\//, '') }, function(err) {
if (err) return self.doError( 'delete', err, callback );
self.logDebug(5, "Delete complete");
callback({ code: 0 });
} );
},
api_deleteFolder: function(args, callback) {
// delete entire folder and all files and sub-folders within
var self = this;
var params = Tools.mergeHashes( args.params, args.query );
if (!this.requireParams(params, {
repo: VALID_S3_BUCKET,
path: VALID_S3_PATH
}, callback)) return;
// sanity check
if (params.path.match(/^\/$/)) return this.doError('delete', "Cannot delete bucket root.", callback);
this.logDebug(5, "Deleting folder: " + params.path, params);
this.s3.deleteFiles( { bucket: params.repo, remotePath: params.path.replace(/^\//, '') + '/' }, function(err) {
if (err) return self.doError( 'delete', err, callback );
self.logDebug(5, "Delete complete");
callback({ code: 0 });
} );
},
api_upload: function(args, callback) {
// upload one or more files
var self = this;
var params = Tools.mergeHashes( args.params, args.query );
if (!this.requireParams(params, {
repo: VALID_S3_BUCKET,
path: VALID_S3_PATH
}, callback)) return;
var path = params.path.replace(/^\//, '');
if (!args.files || !Tools.numKeys(args.files)) {
return this.doError('upload', "No files found in upload data.", callback);
}
this.logDebug(5, "Uploading files to: " + path, args.files);
async.eachSeries( Object.keys(args.files),
function(key, callback) {
var file = args.files[key];
var s3_key = path + '/' + Path.basename(file.name).replace(/[^\w\-\.]+/g, '_');
s3_key = s3_key.replace(/^\//, '');
self.s3.uploadFile( { bucket: params.repo, localFile: file.path, key: s3_key }, callback );
},
function(err) {
if (err) return self.doError('upload', err, callback);
self.logDebug(5, "Upload complete");
callback({ code: 0 });
}
); // async.eachSeries
},
api_getFileContent: function(args, callback) {
// get raw text file content for editing
var self = this;
var params = Tools.mergeHashes( args.params, args.query );
if (!this.requireParams(params, {
repo: VALID_S3_BUCKET,
path: VALID_S3_PATH
}, callback)) return;
var path = params.path.replace(/^\//, '');
this.logDebug(5, "Fetching text file for editing: " + path, params);
this.s3.getBuffer( { bucket: params.repo, key: path }, function(err, data) {
if (err) return self.doError('edit', err, callback);
self.logDebug(5, "Fetch complete");
callback({ code: 0, text: data.toString() });
} );
},
api_saveFileContent: function(args, callback) {
// save raw text file content, from editor
var self = this;
var params = args.params;
if (!this.requireParams(params, {
repo: VALID_S3_BUCKET,
path: VALID_S3_PATH,
data: /[\s\S]/
}, callback)) return;
var path = params.path.replace(/^\//, '');
var buf = Buffer.from( params.data );
this.logDebug(5, "Writing text to file: " + path, { repo: params.repo, size: buf.length });
this.s3.putBuffer( { bucket: params.repo, key: path, value: buf }, function(err) {
if (err) return self.doError('edit', err, callback);
self.logDebug(5, "Write complete");
callback({ code: 0 });
} );
},
api_view: function(args, callback) {
// view file in browser (or download)
var self = this;
var query = args.query;
var uri = args.request.url.replace(/\?.*$/, '');
var temp_file = os.tmpdir() + '/s3explorer-temp-' + process.pid + '-' + Tools.generateShortID() + '.bin';
if (!uri.match(/\/api\/view\/([\w\-\.]+)\/(.+)$/)) return this.doError('view', "Malformed URI: " + uri, callback);
var bucket = RegExp.$1;
var path = RegExp.$2;
// path may have spaces or ampersands, and these are NOT decoded automatically, as they are not part of the query string
path = decodeURIComponent(path);
this.logDebug(5, "Downloading file for streaming to client: " + path, { bucket, temp_file });
this.s3.downloadFile( { bucket: bucket, key: path, localFile: temp_file }, function(err) {
if (err) return self.doError('view', err, callback);
// if request timed out during s3 download, delete temp file and abort
if (args.aborted) {
self.logDebug(5, "Request was aborted, deleting temp file: " + temp_file );
fs.unlink( temp_file, function() {} );
return callback({ code: 'aborted' });
}
// since we are running in a worker process, and the file could be huge, send it back using file mode:
// https://github.com/jhuckaby/pixl-server-pool#file-responses (becomes a stream in the parent)
args.response.type = "file";
args.response.status = "200 OK";
args.response.headers['Accept-Ranges'] = "none";
args.response.headers['Content-Type'] = mime.getType( Path.basename(path) ) || 'application/octet-stream';
if (query.download) args.response.headers['Content-Disposition'] = 'attachment; filename="' + Path.basename(path) + '"';
args.response.body = temp_file;
args.response.delete = true;
callback();
} );
},
requireParams: function(params, rules, callback) {
// require certain params to exist and pass regexp rules
assert( arguments.length == 3, "Wrong number of arguments to requireParams" );
for (var key in rules) {
var regexp = rules[key];
if (typeof(params[key]) == 'undefined') {
this.doError('api', "Missing parameter: " + key, callback);
return false;
}
if (!params[key].toString().match(regexp)) {
this.doError('api', "Malformed parameter: " + key, callback);
return false;
}
}
return true;
},
doError: function(code, msg, callback) {
// log error and send api response
assert( arguments.length == 3, "Wrong number of arguments to doError" );
this.logError( code, ''+msg );
callback({ code: code, description: ''+msg });
return false;
},
shutdown: function(callback) {
// Worker child is shutting down
callback();
}
};