|
| 1 | +// FilesController.js |
| 2 | + |
| 3 | +import express from 'express'; |
| 4 | +import mime from 'mime'; |
| 5 | +import { Parse } from 'parse/node'; |
| 6 | +import BodyParser from 'body-parser'; |
| 7 | +import hat from 'hat'; |
| 8 | +import * as Middlewares from '../middlewares'; |
| 9 | +import Config from '../Config'; |
| 10 | + |
| 11 | +const rack = hat.rack(); |
| 12 | + |
| 13 | +export class FilesController { |
| 14 | + constructor(filesAdapter) { |
| 15 | + this._filesAdapter = filesAdapter; |
| 16 | + } |
| 17 | + |
| 18 | + getHandler() { |
| 19 | + return (req, res) => { |
| 20 | + let config = new Config(req.params.appId); |
| 21 | + let filename = req.params.filename; |
| 22 | + this._filesAdapter.getFileData(config, filename).then((data) => { |
| 23 | + res.status(200); |
| 24 | + var contentType = mime.lookup(filename); |
| 25 | + res.set('Content-type', contentType); |
| 26 | + res.end(data); |
| 27 | + }).catch((error) => { |
| 28 | + res.status(404); |
| 29 | + res.set('Content-type', 'text/plain'); |
| 30 | + res.end('File not found.'); |
| 31 | + }); |
| 32 | + }; |
| 33 | + } |
| 34 | + |
| 35 | + createHandler() { |
| 36 | + return (req, res, next) => { |
| 37 | + if (!req.body || !req.body.length) { |
| 38 | + next(new Parse.Error(Parse.Error.FILE_SAVE_ERROR, |
| 39 | + 'Invalid file upload.')); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + if (req.params.filename.length > 128) { |
| 44 | + next(new Parse.Error(Parse.Error.INVALID_FILE_NAME, |
| 45 | + 'Filename too long.')); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + if (!req.params.filename.match(/^[_a-zA-Z0-9][a-zA-Z0-9@\.\ ~_-]*$/)) { |
| 50 | + next(new Parse.Error(Parse.Error.INVALID_FILE_NAME, |
| 51 | + 'Filename contains invalid characters.')); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + // If a content-type is included, we'll add an extension so we can |
| 56 | + // return the same content-type. |
| 57 | + let extension = ''; |
| 58 | + let hasExtension = req.params.filename.indexOf('.') > 0; |
| 59 | + let contentType = req.get('Content-type'); |
| 60 | + if (!hasExtension && contentType && mime.extension(contentType)) { |
| 61 | + extension = '.' + mime.extension(contentType); |
| 62 | + } |
| 63 | + |
| 64 | + let filename = rack() + '_' + req.params.filename + extension; |
| 65 | + this._filesAdapter.createFile(req.config, filename, req.body).then(() => { |
| 66 | + res.status(201); |
| 67 | + var location = this._filesAdapter.getFileLocation(req.config, filename); |
| 68 | + res.set('Location', location); |
| 69 | + res.json({ url: location, name: filename }); |
| 70 | + }).catch((error) => { |
| 71 | + next(new Parse.Error(Parse.Error.FILE_SAVE_ERROR, |
| 72 | + 'Could not store file.')); |
| 73 | + }); |
| 74 | + }; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Find file references in REST-format object and adds the url key |
| 79 | + * with the current mount point and app id. |
| 80 | + * Object may be a single object or list of REST-format objects. |
| 81 | + */ |
| 82 | + expandFilesInObject(config, object) { |
| 83 | + if (object instanceof Array) { |
| 84 | + object.map((obj) => this.expandFilesInObject(config, obj)); |
| 85 | + return; |
| 86 | + } |
| 87 | + if (typeof object !== 'object') { |
| 88 | + return; |
| 89 | + } |
| 90 | + for (let key in object) { |
| 91 | + let fileObject = object[key]; |
| 92 | + if (fileObject && fileObject['__type'] === 'File') { |
| 93 | + if (fileObject['url']) { |
| 94 | + continue; |
| 95 | + } |
| 96 | + let filename = fileObject['name']; |
| 97 | + if (filename.indexOf('tfss-') === 0) { |
| 98 | + fileObject['url'] = 'http://files.parsetfss.com/' + config.fileKey + '/' + encodeURIComponent(filename); |
| 99 | + } else { |
| 100 | + fileObject['url'] = this._filesAdapter.getFileLocation(config, filename); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + getExpressRouter() { |
| 107 | + let router = express.Router(); |
| 108 | + router.get('/files/:appId/:filename', this.getHandler()); |
| 109 | + |
| 110 | + router.post('/files', function(req, res, next) { |
| 111 | + next(new Parse.Error(Parse.Error.INVALID_FILE_NAME, |
| 112 | + 'Filename not provided.')); |
| 113 | + }); |
| 114 | + |
| 115 | + router.post('/files/:filename', |
| 116 | + Middlewares.allowCrossDomain, |
| 117 | + BodyParser.raw({type: '*/*', limit: '20mb'}), |
| 118 | + Middlewares.handleParseHeaders, |
| 119 | + this.createHandler() |
| 120 | + ); |
| 121 | + |
| 122 | + return router; |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +export default FilesController; |
0 commit comments