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-02-09 12:53:02 -08:00
|
|
|
|
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-02-21 12:02:18 -05:00
|
|
|
return this.adapter.getFileData(config, filename);
|
2016-02-09 12:53:02 -08:00
|
|
|
}
|
|
|
|
|
|
2016-03-01 09:02:33 -05:00
|
|
|
createFile(config, filename, data, contentType) {
|
2016-02-20 12:25:43 -05:00
|
|
|
filename = randomHexString(32) + '_' + filename;
|
2016-02-21 12:02:18 -05:00
|
|
|
var location = this.adapter.getFileLocation(config, filename);
|
2016-03-01 09:02:33 -05:00
|
|
|
return this.adapter.createFile(config, 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-02-21 12:02:18 -05:00
|
|
|
return this.adapter.deleteFile(config, 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'];
|
|
|
|
|
if (filename.indexOf('tfss-') === 0) {
|
|
|
|
|
fileObject['url'] = 'http://files.parsetfss.com/' + config.fileKey + '/' + encodeURIComponent(filename);
|
|
|
|
|
} else {
|
2016-02-21 12:02:18 -05:00
|
|
|
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;
|