|
1 | 1 | // FilesController.js
|
2 |
| - |
3 |
| -import express from 'express'; |
4 |
| -import mime from 'mime'; |
5 | 2 | import { Parse } from 'parse/node';
|
6 |
| -import BodyParser from 'body-parser'; |
7 |
| -import * as Middlewares from '../middlewares'; |
8 |
| -import Config from '../Config'; |
9 | 3 | import { randomHexString } from '../cryptoUtils';
|
10 | 4 |
|
11 | 5 | export class FilesController {
|
12 | 6 | constructor(filesAdapter) {
|
13 | 7 | this._filesAdapter = filesAdapter;
|
14 | 8 | }
|
15 | 9 |
|
16 |
| - static getHandler() { |
17 |
| - return (req, res) => { |
18 |
| - let config = new Config(req.params.appId); |
19 |
| - return config.filesController.getHandler()(req, res); |
20 |
| - } |
21 |
| - } |
22 |
| - |
23 |
| - getHandler() { |
24 |
| - return (req, res) => { |
25 |
| - let config = new Config(req.params.appId); |
26 |
| - let filename = req.params.filename; |
27 |
| - this._filesAdapter.getFileData(config, filename).then((data) => { |
28 |
| - res.status(200); |
29 |
| - var contentType = mime.lookup(filename); |
30 |
| - res.set('Content-type', contentType); |
31 |
| - res.end(data); |
32 |
| - }).catch((error) => { |
33 |
| - res.status(404); |
34 |
| - res.set('Content-type', 'text/plain'); |
35 |
| - res.end('File not found.'); |
36 |
| - }); |
37 |
| - }; |
| 10 | + getFileData(config, filename) { |
| 11 | + return this._filesAdapter.getFileData(config, filename); |
38 | 12 | }
|
39 | 13 |
|
40 |
| - static createHandler() { |
41 |
| - return (req, res, next) => { |
42 |
| - let config = req.config; |
43 |
| - return config.filesController.createHandler()(req, res, next); |
44 |
| - } |
45 |
| - } |
46 |
| - |
47 |
| - createHandler() { |
48 |
| - return (req, res, next) => { |
49 |
| - if (!req.body || !req.body.length) { |
50 |
| - next(new Parse.Error(Parse.Error.FILE_SAVE_ERROR, |
51 |
| - 'Invalid file upload.')); |
52 |
| - return; |
53 |
| - } |
54 |
| - |
55 |
| - if (req.params.filename.length > 128) { |
56 |
| - next(new Parse.Error(Parse.Error.INVALID_FILE_NAME, |
57 |
| - 'Filename too long.')); |
58 |
| - return; |
59 |
| - } |
60 |
| - |
61 |
| - if (!req.params.filename.match(/^[_a-zA-Z0-9][a-zA-Z0-9@\.\ ~_-]*$/)) { |
62 |
| - next(new Parse.Error(Parse.Error.INVALID_FILE_NAME, |
63 |
| - 'Filename contains invalid characters.')); |
64 |
| - return; |
65 |
| - } |
66 |
| - |
67 |
| - const filesController = req.config.filesController; |
68 |
| - // If a content-type is included, we'll add an extension so we can |
69 |
| - // return the same content-type. |
70 |
| - let extension = ''; |
71 |
| - let hasExtension = req.params.filename.indexOf('.') > 0; |
72 |
| - let contentType = req.get('Content-type'); |
73 |
| - if (!hasExtension && contentType && mime.extension(contentType)) { |
74 |
| - extension = '.' + mime.extension(contentType); |
75 |
| - } |
76 |
| - |
77 |
| - let filename = randomHexString(32) + '_' + req.params.filename + extension; |
78 |
| - filesController._filesAdapter.createFile(req.config, filename, req.body).then(() => { |
79 |
| - res.status(201); |
80 |
| - var location = filesController._filesAdapter.getFileLocation(req.config, filename); |
81 |
| - res.set('Location', location); |
82 |
| - res.json({ url: location, name: filename }); |
83 |
| - }).catch((error) => { |
84 |
| - next(new Parse.Error(Parse.Error.FILE_SAVE_ERROR, |
85 |
| - 'Could not store file.')); |
| 14 | + createFile(config, filename, data) { |
| 15 | + filename = randomHexString(32) + '_' + filename; |
| 16 | + var location = this._filesAdapter.getFileLocation(config, filename); |
| 17 | + return this._filesAdapter.createFile(config, filename, data).then(() => { |
| 18 | + return Promise.resolve({ |
| 19 | + url: location, |
| 20 | + name: filename |
86 | 21 | });
|
87 |
| - }; |
88 |
| - } |
89 |
| - |
90 |
| - static deleteHandler() { |
91 |
| - return (req, res, next) => { |
92 |
| - let config = req.config; |
93 |
| - return config.filesController.deleteHandler()(req, res, next); |
94 |
| - } |
95 |
| - } |
| 22 | + }); |
| 23 | + } |
96 | 24 |
|
97 |
| - deleteHandler() { |
98 |
| - return (req, res, next) => { |
99 |
| - this._filesAdapter.deleteFile(req.config, req.params.filename).then(() => { |
100 |
| - res.status(200); |
101 |
| - // TODO: return useful JSON here? |
102 |
| - res.end(); |
103 |
| - }).catch((error) => { |
104 |
| - next(new Parse.Error(Parse.Error.FILE_DELETE_ERROR, |
105 |
| - 'Could not delete file.')); |
106 |
| - }); |
107 |
| - }; |
| 25 | + deleteFile(config, filename) { |
| 26 | + return this._filesAdapter.deleteFile(config, filename); |
108 | 27 | }
|
109 | 28 |
|
110 | 29 | /**
|
@@ -135,32 +54,6 @@ export class FilesController {
|
135 | 54 | }
|
136 | 55 | }
|
137 | 56 | }
|
138 |
| - |
139 |
| - static getExpressRouter() { |
140 |
| - let router = express.Router(); |
141 |
| - router.get('/files/:appId/:filename', FilesController.getHandler()); |
142 |
| - |
143 |
| - router.post('/files', function(req, res, next) { |
144 |
| - next(new Parse.Error(Parse.Error.INVALID_FILE_NAME, |
145 |
| - 'Filename not provided.')); |
146 |
| - }); |
147 |
| - |
148 |
| - router.post('/files/:filename', |
149 |
| - Middlewares.allowCrossDomain, |
150 |
| - BodyParser.raw({type: '*/*', limit: '20mb'}), |
151 |
| - Middlewares.handleParseHeaders, |
152 |
| - FilesController.createHandler() |
153 |
| - ); |
154 |
| - |
155 |
| - router.delete('/files/:filename', |
156 |
| - Middlewares.allowCrossDomain, |
157 |
| - Middlewares.handleParseHeaders, |
158 |
| - Middlewares.enforceMasterKeyAccess, |
159 |
| - FilesController.deleteHandler() |
160 |
| - ); |
161 |
| - |
162 |
| - return router; |
163 |
| - } |
164 | 57 | }
|
165 | 58 |
|
166 | 59 | export default FilesController;
|
0 commit comments