-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
Copy pathFilesAdapter.js
114 lines (104 loc) · 4.26 KB
/
FilesAdapter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*eslint no-unused-vars: "off"*/
// Files Adapter
//
// Allows you to change the file storage mechanism.
//
// Adapter classes must implement the following functions:
// * createFile(filename, data, contentType)
// * deleteFile(filename)
// * getFileData(filename)
// * getFileLocation(config, filename)
// Adapter classes should implement the following functions:
// * validateFilename(filename)
// * handleFileStream(filename, req, res, contentType)
//
// Default is GridFSBucketAdapter, which requires mongo
// and for the API server to be using the DatabaseController with Mongo
// database adapter.
import type { Config } from '../../Config';
import Parse from 'parse/node';
/**
* @interface
* @memberof module:Adapters
*/
export class FilesAdapter {
/** Responsible for storing the file in order to be retrieved later by its filename
*
* @param {string} filename - the filename to save
* @param {*} data - the buffer of data from the file
* @param {string} contentType - the supposed contentType
* @discussion the contentType can be undefined if the controller was not able to determine it
* @param {object} options - (Optional) options to be passed to file adapter (S3 File Adapter Only)
* - tags: object containing key value pairs that will be stored with file
* - metadata: object containing key value pairs that will be stored with file (https://docs.aws.amazon.com/AmazonS3/latest/user-guide/add-object-metadata.html)
* @discussion options are not supported by all file adapters. Check the your adapter's documentation for compatibility
* @param {Config} config - (Optional) server configuration
* @discussion config may be passed to adapter to allow for more complex configuration and internal call of getFileLocation (if needed). This argument is not supported by all file adapters. Check the your adapter's documentation for compatibility
*
* @return {Promise<{url?: string, name?: string, location?: string}>|Promise<undefined>} Either a plain promise that should fail if storage didn't succeed, or a promise resolving to an object containing url and/or an updated filename and/or location (if relevant)
*/
createFile(filename: string, data, contentType: string, options: Object, config: Config): Promise {}
/** Responsible for deleting the specified file
*
* @param {string} filename - the filename to delete
*
* @return {Promise} a promise that should fail if the deletion didn't succeed
*/
deleteFile(filename: string): Promise {}
/** Responsible for retrieving the data of the specified file
*
* @param {string} filename - the name of file to retrieve
*
* @return {Promise} a promise that should pass with the file data or fail on error
*/
getFileData(filename: string): Promise<any> {}
/** Returns an absolute URL where the file can be accessed
*
* @param {Config} config - server configuration
* @param {string} filename
*
* @return {string | Promise<string>} Absolute URL
*/
getFileLocation(config: Config, filename: string): string | Promise<string> {}
/** Validate a filename for this adapter type
*
* @param {string} filename
*
* @returns {null|Parse.Error} null if there are no errors
*/
// validateFilename(filename: string): ?Parse.Error {}
/** Handles Byte-Range Requests for Streaming
*
* @param {string} filename
* @param {object} req
* @param {object} res
* @param {string} contentType
*
* @returns {Promise} Data for byte range
*/
// handleFileStream(filename: string, res: any, req: any, contentType: string): Promise
/** Responsible for retrieving metadata and tags
*
* @param {string} filename - the filename to retrieve metadata
*
* @return {Promise} a promise that should pass with metadata
*/
// getMetadata(filename: string): Promise<any> {}
}
/**
* Simple filename validation
*
* @param filename
* @returns {null|Parse.Error}
*/
export function validateFilename(filename): ?Parse.Error {
if (filename.length > 128) {
return new Parse.Error(Parse.Error.INVALID_FILE_NAME, 'Filename too long.');
}
const regx = /^[_a-zA-Z0-9][a-zA-Z0-9@. ~_-]*$/;
if (!filename.match(regx)) {
return new Parse.Error(Parse.Error.INVALID_FILE_NAME, 'Filename contains invalid characters.');
}
return null;
}
export default FilesAdapter;