2017-06-24 19:51:43 +01:00
|
|
|
import { BaseModel } from 'lib/base-model.js';
|
2017-07-02 13:02:07 +01:00
|
|
|
import { BaseItem } from 'lib/models/base-item.js';
|
2017-06-24 19:51:43 +01:00
|
|
|
import { Setting } from 'lib/models/setting.js';
|
|
|
|
import { mime } from 'lib/mime-utils.js';
|
2017-06-25 00:19:11 +01:00
|
|
|
import { filename } from 'lib/path-utils.js';
|
2017-07-02 13:02:07 +01:00
|
|
|
import lodash from 'lodash';
|
2017-06-24 19:51:43 +01:00
|
|
|
|
2017-07-02 13:02:07 +01:00
|
|
|
class Resource extends BaseItem {
|
2017-06-24 19:51:43 +01:00
|
|
|
|
|
|
|
static tableName() {
|
|
|
|
return 'resources';
|
|
|
|
}
|
|
|
|
|
2017-07-03 20:50:45 +01:00
|
|
|
static modelType() {
|
|
|
|
return BaseModel.TYPE_RESOURCE;
|
2017-06-24 19:51:43 +01:00
|
|
|
}
|
|
|
|
|
2017-07-02 16:46:03 +01:00
|
|
|
static async serialize(item, type = null, shownKeys = null) {
|
2017-07-02 13:02:07 +01:00
|
|
|
let fieldNames = this.fieldNames();
|
|
|
|
fieldNames.push('type_');
|
|
|
|
lodash.pull(fieldNames, 'sync_time');
|
|
|
|
return super.serialize(item, 'resource', fieldNames);
|
|
|
|
}
|
|
|
|
|
2017-06-24 19:51:43 +01:00
|
|
|
static fullPath(resource) {
|
|
|
|
let extension = mime.toFileExtension(resource.mime);
|
|
|
|
extension = extension ? '.' + extension : '';
|
|
|
|
return Setting.value('resourceDir') + '/' + resource.id + extension;
|
|
|
|
}
|
|
|
|
|
2017-06-25 00:19:11 +01:00
|
|
|
static pathToId(path) {
|
|
|
|
return filename(path);
|
|
|
|
}
|
|
|
|
|
2017-07-05 22:29:00 +01:00
|
|
|
// RNFIX: Temporary disabled for React Native
|
|
|
|
|
2017-07-02 13:02:07 +01:00
|
|
|
static content(resource) {
|
2017-07-05 22:29:00 +01:00
|
|
|
// // TODO: node-only, and should probably be done with streams
|
|
|
|
// const fs = require('fs-extra');
|
|
|
|
// return fs.readFile(this.fullPath(resource));
|
2017-07-02 13:02:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static setContent(resource, content) {
|
2017-07-05 22:29:00 +01:00
|
|
|
// // TODO: node-only, and should probably be done with streams
|
|
|
|
// const fs = require('fs-extra');
|
|
|
|
// let buffer = new Buffer(content);
|
|
|
|
// return fs.writeFile(this.fullPath(resource), buffer);
|
2017-07-02 13:02:07 +01:00
|
|
|
}
|
|
|
|
|
2017-06-24 19:51:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export { Resource };
|