2016-02-09 12:53:02 -08:00
|
|
|
// FilesController.js
|
|
|
|
|
import { Parse } from 'parse/node';
|
2016-02-12 02:02:55 +01:00
|
|
|
import { randomHexString } from '../cryptoUtils';
|
2016-02-21 12:02:18 -05:00
|
|
|
import AdaptableController from './AdaptableController';
|
2016-02-21 23:47:07 -05:00
|
|
|
import { FilesAdapter } from '../Adapters/Files/FilesAdapter';
|
2016-03-01 10:14:03 -05:00
|
|
|
import path from 'path';
|
|
|
|
|
import mime from 'mime';
|
2016-02-09 12:53:02 -08:00
|
|
|
|
2016-06-07 15:59:45 -07:00
|
|
|
const legacyFilesRegex = new RegExp("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}-.*");
|
|
|
|
|
|
2016-02-21 12:02:18 -05:00
|
|
|
export class FilesController extends AdaptableController {
|
2016-02-09 12:53:02 -08:00
|
|
|
|
2016-02-20 12:25:43 -05:00
|
|
|
getFileData(config, filename) {
|
2016-03-23 23:29:16 -04:00
|
|
|
return this.adapter.getFileData(filename);
|
2016-02-09 12:53:02 -08:00
|
|
|
}
|
|
|
|
|
|
2016-03-01 09:02:33 -05:00
|
|
|
createFile(config, filename, data, contentType) {
|
2016-03-07 00:30:21 -08:00
|
|
|
|
2016-03-01 10:14:03 -05:00
|
|
|
let extname = path.extname(filename);
|
2016-03-07 00:30:21 -08:00
|
|
|
|
2016-03-01 10:14:03 -05:00
|
|
|
const hasExtension = extname.length > 0;
|
2016-03-07 00:30:21 -08:00
|
|
|
|
2016-03-01 10:14:03 -05:00
|
|
|
if (!hasExtension && contentType && mime.extension(contentType)) {
|
|
|
|
|
filename = filename + '.' + mime.extension(contentType);
|
|
|
|
|
} else if (hasExtension && !contentType) {
|
|
|
|
|
contentType = mime.lookup(filename);
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-20 12:25:43 -05:00
|
|
|
filename = randomHexString(32) + '_' + filename;
|
2016-03-01 10:14:03 -05:00
|
|
|
|
2016-02-21 12:02:18 -05:00
|
|
|
var location = this.adapter.getFileLocation(config, filename);
|
2016-03-23 23:29:16 -04:00
|
|
|
return this.adapter.createFile(filename, data, contentType).then(() => {
|
2016-02-20 12:25:43 -05:00
|
|
|
return Promise.resolve({
|
|
|
|
|
url: location,
|
|
|
|
|
name: filename
|
2016-02-09 12:53:02 -08:00
|
|
|
});
|
2016-02-20 12:25:43 -05:00
|
|
|
});
|
2016-02-22 13:59:24 -08:00
|
|
|
}
|
2016-02-18 23:19:21 -05:00
|
|
|
|
2016-02-20 12:25:43 -05:00
|
|
|
deleteFile(config, filename) {
|
2016-03-23 23:29:16 -04:00
|
|
|
return this.adapter.deleteFile(filename);
|
2016-02-10 18:42:21 -05:00
|
|
|
}
|
|
|
|
|
|
2016-02-09 19:31:23 -08:00
|
|
|
/**
|
|
|
|
|
* Find file references in REST-format object and adds the url key
|
|
|
|
|
* with the current mount point and app id.
|
|
|
|
|
* Object may be a single object or list of REST-format objects.
|
|
|
|
|
*/
|
2016-02-22 13:59:24 -08:00
|
|
|
expandFilesInObject(config, object) {
|
2016-02-09 19:31:23 -08:00
|
|
|
if (object instanceof Array) {
|
|
|
|
|
object.map((obj) => this.expandFilesInObject(config, obj));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (typeof object !== 'object') {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
for (let key in object) {
|
|
|
|
|
let fileObject = object[key];
|
|
|
|
|
if (fileObject && fileObject['__type'] === 'File') {
|
|
|
|
|
if (fileObject['url']) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
let filename = fileObject['name'];
|
2016-06-07 15:59:45 -07:00
|
|
|
// all filenames starting with "tfss-" should be from files.parsetfss.com
|
|
|
|
|
// all filenames starting with a "-" seperated UUID should be from files.parse.com
|
|
|
|
|
// all other filenames have been migrated or created from Parse Server
|
2016-07-10 19:15:30 +02:00
|
|
|
if (config.fileKey === undefined) {
|
2016-02-21 12:02:18 -05:00
|
|
|
fileObject['url'] = this.adapter.getFileLocation(config, filename);
|
2016-07-10 19:15:30 +02:00
|
|
|
} else {
|
|
|
|
|
if (filename.indexOf('tfss-') === 0) {
|
|
|
|
|
fileObject['url'] = 'http://files.parsetfss.com/' + config.fileKey + '/' + encodeURIComponent(filename);
|
|
|
|
|
} else if (legacyFilesRegex.test(filename)) {
|
|
|
|
|
fileObject['url'] = 'http://files.parse.com/' + config.fileKey + '/' + encodeURIComponent(filename);
|
|
|
|
|
} else {
|
|
|
|
|
fileObject['url'] = this.adapter.getFileLocation(config, filename);
|
|
|
|
|
}
|
2016-02-09 19:31:23 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-22 13:59:24 -08:00
|
|
|
|
2016-02-21 23:47:07 -05:00
|
|
|
expectedAdapterType() {
|
|
|
|
|
return FilesAdapter;
|
|
|
|
|
}
|
2016-02-09 12:53:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default FilesController;
|