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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
/*******************************************************************************
* Copyright (c) 2012, 2016 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License v1.0
* (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution
* License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html).
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
/*eslint-env node*/
/*eslint-disable consistent-return*/
var ETag = require('./util/etag');
var path = require('path');
var Promise = require('bluebird');
var rimraf = require('rimraf');
var fse = require('fs-extra');
var api = require('./api');
var fs = Promise.promisifyAll(require('fs'));
/*
* Utils for representing files as objects in the Orion File API
* http://wiki.eclipse.org/Orion/Server_API/File_API
*/
/**
* Builds up an array of a directory's children, as File objects.
* @param {String} parentLocation Parent location in the file api for child items (ugh)
* @param {Array} [exclude] Filenames of children to hide. If `null`, everything is included.
* @param {Function} callback Invoked as func(error?, children)
* @returns A promise
*/
var getChildren = exports.getChildren = function(fileRoot, workspaceDir, directory, depth, excludes) {
return fs.readdirAsync(directory)
.then(function(files) {
return Promise.map(files, function(file) {
if (Array.isArray(excludes) && excludes.indexOf(file) !== -1) {
return null; // omit
}
var filepath = path.join(directory, file);
return fs.statAsync(filepath)
.then(function(stats) {
return fileJSON(fileRoot, workspaceDir, filepath, stats, depth ? depth - 1 : 0);
})
.catch(function() {
return null; // suppress rejection
});
});
})
.then(function(results) {
return results.filter(function(r) { return r; });
});
};
/**
* @parma {String} p A location in the local filesystem (eg C:\\Users\\whatever\\foo)
* @throws {Error} If p is outside the workspaceDir (and thus is unsafe)
*/
var safePath = exports.safePath = function(workspaceDir, p) {
workspaceDir = path.normalize(workspaceDir);
p = path.normalize(p);
var relativePath = path.relative(workspaceDir, p);
if (relativePath.indexOf('..' + path.sep) === 0) {
throw new Error('Path ' + p + ' is outside workspace');
}
return p;
};
/**
* @param {String} filepath The URL-encoded path, for example 'foo/My%20Work/baz.txt'
* @returns {String} The filesystem path represented by interpreting 'path' relative to the workspace dir.
* The returned value is URL-decoded.
* @throws {Error} If rest is outside of the workspaceDir (and thus is unsafe)
*/
var safeFilePath = exports.safeFilePath = function(workspaceDir, filepath) {
return safePath(workspaceDir, path.join(workspaceDir, filepath));
};
var getParents = exports.getParents = function(fileRoot, relativePath) {
var segs = relativePath.split('/');
if(segs && segs.length > 0 && segs[segs.length-1] === ""){// pop the last segment if it is empty. In this case wwwpath ends with "/".
segs.pop();
}
segs.pop();//The last segment now is the directory itself. We do not need it in the parents array.
var loc = fileRoot;
var parents = [];
for (var i=0; i < segs.length; i++) {
var seg = segs[i];
loc = api.join(loc, seg);
var location = loc + "/";
parents.push({
Name: seg,
ChildrenLocation: {pathname: location, query: {depth:1}},
Location: location
});
}
return parents.reverse();
};
/**
* Performs the equivalent of rm -rf on a directory.
* @param {Function} callback Invoked as callback(error)
*/
exports.rumRuff = function(dirpath, callback) {
rimraf(dirpath, callback);
};
/**
* Copy srcPath to destPath
* @param {String} srcPath
* @param {String} destPath
* @param {Function} callback Invoked as callback(error?, destPath)
* @returns promise
*/
var copy = exports.copy = function(srcPath, destPath, callback) {
return new Promise(function(fulfill, reject) {
return fse.copy(srcPath, destPath, {clobber: true, limit: 32}, function(err) {
if (err) {
if (callback) callback(err);
return reject(err);
}
if (callback) callback(null, destPath);
fulfill(destPath);
});
});
};
/**
* @param {Function} callback Invoked as callback(error, stats)
* @deprecated just use Promise.promisify(fs).statAsync() instead
*/
exports.withStats = function(filepath, callback) {
fs.stat(filepath, function(error, stats) {
if (error) { callback(error); }
else {
callback(null, stats);
}
});
};
exports.decodeSlug = function(slug) {
if (typeof slug === "string") return decodeURIComponent(slug);
return slug;
};
/**
* Gets the stats for filepath and calculates the ETag based on the bytes in the file.
* @param {Function} callback Invoked as callback(error, stats, etag) -- the etag can be null if filepath represents a directory.
*/
exports.withStatsAndETag = function(filepath, callback) {
fs.stat(filepath, function(error, stats) {
if (error) {
callback(error);
return;
}
if (!stats.isFile()) {
// no etag
callback(null, stats, null);
return;
}
var etag = ETag();
var stream = fs.createReadStream(filepath);
stream.pipe(etag);
stream.on('error', callback);
stream.on('end', function() {
callback(null, stats, etag.read());
});
});
};
exports.withETag = function(filepath, callback) {
var etag = ETag();
var stream = fs.createReadStream(filepath);
stream.pipe(etag);
stream.on('error', callback);
stream.on('end', function() {
callback(null, etag.read());
});
};
/**
* @returns {String} The Location of for a file resource.
*/
function getFileLocation(fileRoot, wwwpath, isDir) {
return api.join(fileRoot, wwwpath) + (isDir ? '/' : '');
}
/**
* Gets a boolean associated with a key. Copes with Orion server REST API's use of "true" and "false" strings.
* @param {Object} obj
* @param {String} key
* @returns {Boolean} Returns <code>false</code> if there is no such key, or if the value is not the boolean <code>true</code>
* or the string <code>"true"</code>.
*/
function getBoolean(obj, key) {
var val = obj[key];
return Object.prototype.hasOwnProperty.call(obj, key) && (val === true || val === 'true');
}
var decorators = [];
/**
* Shared decorator, used by workspace as well.
*/
exports.getDecorators = function(){
return decorators;
};
/**
* Used to add different decorators to generate respond json.
* @param {func} decorator functions to be added;
*/
exports.addDecorator = function(func) {
decorators.push(func);
};
/**
* Helper for fulfilling a file metadata GET request.
* @param {String} fileRoot The "/file" prefix or equivalent.
* @param {Object} req HTTP request object
* @param {Object} res HTTP response object
* @param {String} filepath The physical path to the file on the server.
* @param {Object} stats
* @param {String} etag
* @param {Boolean} [includeChildren=false]
* @param {Object} [metadataMixins] Additional metadata to mix in to the response object.
*/
var writeFileMetadata = exports.writeFileMetadata = function(fileRoot, req, res, filepath, stats, etag, depth, metadataMixins) {
var result;
return fileJSON(fileRoot, req.user.workspaceDir, filepath, stats, depth, metadataMixins)
.then(function(originalJson) {
result = originalJson;
return Promise.map(decorators, function(decorator){
return decorator(fileRoot, req, filepath, result);
});
})
.then(function(){
if (etag) {
result.ETag = etag;
res.setHeader('ETag', etag);
}
res.setHeader("Cache-Control", "no-cache");
api.write(null, res, null, result);
})
.catch(api.writeError.bind(null, 500, res));
};
function fileJSON(fileRoot, workspaceDir, filepath, stats, depth, metadataMixins) {
depth = depth || 0;
var isDir = stats.isDirectory();
var wwwpath = api.toURLPath(filepath.substring(workspaceDir.length + 1));
var result = {
Name: path.basename(filepath),
Location: getFileLocation(fileRoot, wwwpath, isDir),
Directory: isDir,
LocalTimeStamp: stats.mtime.getTime(),
Parents: getParents(fileRoot, wwwpath),
Attributes: {
// TODO fix this
ReadOnly: false, //!(stats.mode & USER_WRITE_FLAG === USER_WRITE_FLAG),
Executable: false //!(stats.mode & USER_EXECUTE_FLAG === USER_EXECUTE_FLAG)
}
};
if (metadataMixins) {
Object.keys(metadataMixins).forEach(function(property) {
result[property] = metadataMixins[property];
});
}
if (!isDir) {
return Promise.resolve(result);
}
// Orion's File Client expects ChildrenLocation to always be present
result.ChildrenLocation = {pathname: result.Location, query: {depth:1}};
result.ImportLocation = result.Location.replace(/\/file/, "/xfer/import").replace(/\/$/, "");
result.ExportLocation = result.Location.replace(/\/file/, "/xfer/export").replace(/\/$/, "") + ".zip";
if (depth <= 0) {
return Promise.resolve(result);
}
return getChildren(fileRoot, workspaceDir, filepath, depth)
.then(function(children) {
result.Children = children;
return result;
});
}
/**
* Helper for fulfilling a file POST request (for example, copy, move, or create).
* @param {String} fileRoot The route of the /file handler (not including context path)
* @param {Object} req
* @parma {Object} res
* @param {String} wwwpath
* @param {String} destFilepath
* @param {Object} [metadata] Additional metadata to be mixed in to the File response.
* @param {Number} [statusCode] Status code to send on a successful response. By default, `201 Created` is sent if
* a new resource was created, and and `200 OK` if an existing resource was overwritten.
*/
exports.handleFilePOST = function(fileRoot, req, res, destFilepath, metadataMixins, statusCode) {
var isDirectory = req.body && getBoolean(req.body, 'Directory');
var writeResponse = function(isOverwrite) {
// TODO: maybe set ReadOnly and Executable based on fileAtts
if (typeof statusCode === 'number') {
res.status(statusCode);
} else {
// Status code 200 indicates that an existing resource was replaced, or we're POSTing to a URL
res.status(isOverwrite ? 200 : 201);
}
return fs.statAsync(destFilepath)
.then(function(stats) {
return writeFileMetadata(fileRoot, req, res, destFilepath, stats, /*etag*/null, /*depth*/0, metadataMixins);
})
.catch(function(err) {
api.writeError(500, res, err.message);
});
};
fs.statAsync(destFilepath)
.catchReturn({ code: 'ENOENT' }, null) // suppress reject when file does not exist
.then(function(stats) {
return !!stats; // just return whether the file existed
})
.then(function(destExists) {
var xCreateOptions = (req.headers['x-create-options'] || "").split(",");
var isCopy = xCreateOptions.indexOf('copy') !== -1, isMove = xCreateOptions.indexOf('move') !== -1;
if (isCopy && isMove) {
return api.write(400, res, null, 'Illegal combination of X-Create-Options.');
}
if (xCreateOptions.indexOf('no-overwrite') !== -1 && destExists) {
return api.writeError(412, res, new Error('A file or folder with the same name already exists at this location.'));
}
if (isCopy || isMove) {
var sourceUrl = req.body.Location;
if (!sourceUrl) {
return api.writeError(400, res, 'Missing Location property in request body');
}
var sourceFilepath = safeFilePath(req.user.workspaceDir, api.rest(fileRoot, api.matchHost(req, sourceUrl)));
return fs.statAsync(sourceFilepath)
.then(function(/*stats*/) {
return isCopy ? copy(sourceFilepath, destFilepath) : fs.renameAsync(sourceFilepath, destFilepath);
})
.then(writeResponse.bind(null, destExists))
.catch(function(err) {
if (err.code === 'ENOENT') {
return api.writeError(404, res, 'File not found:' + sourceUrl);
}
return api.writeError(500, res, err);
});
}
// Just a regular file write
return Promise.resolve()
.then(function(){
return destExists ? fs.unlinkAsync(destFilepath) : null;
})
.then(function() {
return isDirectory ? fs.mkdirAsync(destFilepath) : fs.writeFileAsync(destFilepath, '');
})
.then(writeResponse.bind(null, destExists))
.catch(api.writeError.bind(null, 500, res));
});
};
|