Moved getting the url for every file from RestQuery into FilesController.

This commit is contained in:
Nikita Lutsenko
2016-02-09 19:31:23 -08:00
parent 3072782574
commit 8ca25cbabe
6 changed files with 69 additions and 66 deletions

View File

@@ -18,9 +18,10 @@ export class FilesController {
getHandler() {
return (req, res) => {
let config = new Config(req.params.appId);
this._filesAdapter.getFileDataAsync(config, req.params.filename).then((data) => {
let filename = req.params.filename;
this._filesAdapter.getFileDataAsync(config, filename).then((data) => {
res.status(200);
var contentType = mime.lookup(req.params.filename);
var contentType = mime.lookup(filename);
res.set('Content-type', contentType);
res.end(data);
}).catch((error) => {
@@ -63,17 +64,45 @@ export class FilesController {
let filename = rack() + '_' + req.params.filename + extension;
this._filesAdapter.createFileAsync(req.config, filename, req.body).then(() => {
res.status(201);
var location = this._filesAdapter.getFileLocation(req.config, req, filename);
var location = this._filesAdapter.getFileLocation(req.config, filename);
res.set('Location', location);
res.json({ url: location, name: filename });
}).catch((error) => {
console.log(error);
next(new Parse.Error(Parse.Error.FILE_SAVE_ERROR,
'Could not store file.'));
});
};
}
/**
* Find file references in REST-format object and adds the url key
* with the current mount point and app id.
* Object may be a single object or list of REST-format objects.
*/
expandFilesInObject(config, object) {
if (object instanceof Array) {
object.map((obj) => this.expandFilesInObject(config, obj));
return;
}
if (typeof object !== 'object') {
return;
}
for (let key in object) {
let fileObject = object[key];
if (fileObject && fileObject['__type'] === 'File') {
if (fileObject['url']) {
continue;
}
let filename = fileObject['name'];
if (filename.indexOf('tfss-') === 0) {
fileObject['url'] = 'http://files.parsetfss.com/' + config.fileKey + '/' + encodeURIComponent(filename);
} else {
fileObject['url'] = this._filesAdapter.getFileLocation(config, filename);
}
}
}
}
getExpressRouter() {
let router = express.Router();
router.get('/files/:appId/:filename', this.getHandler());