|
8 | 8 | // * deleteFile(filename) |
9 | 9 | // * getFileData(filename) |
10 | 10 | // * getFileLocation(config, filename) |
| 11 | +// Adapter classes should implement the following functions: |
| 12 | +// * validateFilename(filename) |
| 13 | +// * handleFileStream(filename, req, res, contentType) |
11 | 14 | // |
12 | 15 | // Default is GridFSBucketAdapter, which requires mongo |
13 | 16 | // and for the API server to be using the DatabaseController with Mongo |
14 | 17 | // database adapter. |
15 | 18 |
|
16 | 19 | import type { Config } from '../../Config'; |
| 20 | +import Parse from 'parse/node'; |
17 | 21 | /** |
18 | 22 | * @module Adapters |
19 | 23 | */ |
@@ -56,6 +60,46 @@ export class FilesAdapter { |
56 | 60 | * @return {string} Absolute URL |
57 | 61 | */ |
58 | 62 | getFileLocation(config: Config, filename: string): string {} |
| 63 | + |
| 64 | + /** Validate a filename for this adapter type |
| 65 | + * |
| 66 | + * @param {string} filename |
| 67 | + * |
| 68 | + * @returns {null|Parse.Error} null if there are no errors |
| 69 | + */ |
| 70 | + // validateFilename(filename: string): ?Parse.Error {} |
| 71 | + |
| 72 | + /** Handles Byte-Range Requests for Streaming |
| 73 | + * |
| 74 | + * @param {string} filename |
| 75 | + * @param {object} req |
| 76 | + * @param {object} res |
| 77 | + * @param {string} contentType |
| 78 | + * |
| 79 | + * @returns {Promise} Data for byte range |
| 80 | + */ |
| 81 | + // handleFileStream(filename: string, res: any, req: any, contentType: string): Promise |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * Simple filename validation |
| 86 | + * |
| 87 | + * @param filename |
| 88 | + * @returns {null|Parse.Error} |
| 89 | + */ |
| 90 | +export function validateFilename(filename): ?Parse.Error { |
| 91 | + if (filename.length > 128) { |
| 92 | + return new Parse.Error(Parse.Error.INVALID_FILE_NAME, 'Filename too long.'); |
| 93 | + } |
| 94 | + |
| 95 | + const regx = /^[_a-zA-Z0-9][a-zA-Z0-9@. ~_-]*$/; |
| 96 | + if (!filename.match(regx)) { |
| 97 | + return new Parse.Error( |
| 98 | + Parse.Error.INVALID_FILE_NAME, |
| 99 | + 'Filename contains invalid characters.' |
| 100 | + ); |
| 101 | + } |
| 102 | + return null; |
59 | 103 | } |
60 | 104 |
|
61 | 105 | export default FilesAdapter; |
0 commit comments