|
| 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 | + this._filesAdapter.getFileDataAsync(config, req.params.filename).then((data) => { |
| 22 | + res.status(200); |
| 23 | + var contentType = mime.lookup(req.params.filename); |
| 24 | + res.set('Content-type', contentType); |
| 25 | + res.end(data); |
| 26 | + }).catch((error) => { |
| 27 | + res.status(404); |
| 28 | + res.set('Content-type', 'text/plain'); |
| 29 | + res.end('File not found.'); |
| 30 | + }); |
| 31 | + }; |
| 32 | + } |
| 33 | + |
| 34 | + createHandler() { |
| 35 | + return (req, res, next) => { |
| 36 | + if (!req.body || !req.body.length) { |
| 37 | + next(new Parse.Error(Parse.Error.FILE_SAVE_ERROR, |
| 38 | + 'Invalid file upload.')); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + if (req.params.filename.length > 128) { |
| 43 | + next(new Parse.Error(Parse.Error.INVALID_FILE_NAME, |
| 44 | + 'Filename too long.')); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + if (!req.params.filename.match(/^[_a-zA-Z0-9][a-zA-Z0-9@\.\ ~_-]*$/)) { |
| 49 | + next(new Parse.Error(Parse.Error.INVALID_FILE_NAME, |
| 50 | + 'Filename contains invalid characters.')); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + // If a content-type is included, we'll add an extension so we can |
| 55 | + // return the same content-type. |
| 56 | + let extension = ''; |
| 57 | + let hasExtension = req.params.filename.indexOf('.') > 0; |
| 58 | + let contentType = req.get('Content-type'); |
| 59 | + if (!hasExtension && contentType && mime.extension(contentType)) { |
| 60 | + extension = '.' + mime.extension(contentType); |
| 61 | + } |
| 62 | + |
| 63 | + let filename = rack() + '_' + req.params.filename + extension; |
| 64 | + this._filesAdapter.createFileAsync(req.config, filename, req.body).then(() => { |
| 65 | + res.status(201); |
| 66 | + var location = this._filesAdapter.getFileLocation(req.config, req, filename); |
| 67 | + res.set('Location', location); |
| 68 | + res.json({ url: location, name: filename }); |
| 69 | + }).catch((error) => { |
| 70 | + console.log(error); |
| 71 | + next(new Parse.Error(Parse.Error.FILE_SAVE_ERROR, |
| 72 | + 'Could not store file.')); |
| 73 | + }); |
| 74 | + }; |
| 75 | + } |
| 76 | + |
| 77 | + getExpressRouter() { |
| 78 | + let router = express.Router(); |
| 79 | + router.get('/files/:appId/:filename', this.getHandler()); |
| 80 | + |
| 81 | + router.post('/files', function(req, res, next) { |
| 82 | + next(new Parse.Error(Parse.Error.INVALID_FILE_NAME, |
| 83 | + 'Filename not provided.')); |
| 84 | + }); |
| 85 | + |
| 86 | + router.post('/files/:filename', |
| 87 | + Middlewares.allowCrossDomain, |
| 88 | + BodyParser.raw({type: '*/*', limit: '20mb'}), |
| 89 | + Middlewares.handleParseHeaders, |
| 90 | + this.createHandler() |
| 91 | + ); |
| 92 | + |
| 93 | + return router; |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +export default FilesController; |
0 commit comments