2017-06-24 18:51:43 +00:00
|
|
|
import { BaseModel } from 'lib/base-model.js';
|
2017-07-02 12:02:07 +00:00
|
|
|
import { BaseItem } from 'lib/models/base-item.js';
|
2017-06-24 18:51:43 +00:00
|
|
|
import { Setting } from 'lib/models/setting.js';
|
|
|
|
import { mime } from 'lib/mime-utils.js';
|
2017-06-24 23:19:11 +00:00
|
|
|
import { filename } from 'lib/path-utils.js';
|
2017-07-02 12:02:07 +00:00
|
|
|
import lodash from 'lodash';
|
2017-06-24 18:51:43 +00:00
|
|
|
|
2017-07-02 12:02:07 +00:00
|
|
|
class Resource extends BaseItem {
|
2017-06-24 18:51:43 +00:00
|
|
|
|
|
|
|
static tableName() {
|
|
|
|
return 'resources';
|
|
|
|
}
|
|
|
|
|
2017-07-03 19:50:45 +00:00
|
|
|
static modelType() {
|
|
|
|
return BaseModel.TYPE_RESOURCE;
|
2017-06-24 18:51:43 +00:00
|
|
|
}
|
|
|
|
|
2017-07-05 21:52:31 +00:00
|
|
|
static fsDriver() {
|
|
|
|
if (!Resource.fsDriver_) Resource.fsDriver_ = new FsDriverDummy();
|
|
|
|
return Resource.fsDriver_;
|
|
|
|
}
|
|
|
|
|
2017-07-02 15:46:03 +00:00
|
|
|
static async serialize(item, type = null, shownKeys = null) {
|
2017-07-02 12:02:07 +00:00
|
|
|
let fieldNames = this.fieldNames();
|
|
|
|
fieldNames.push('type_');
|
|
|
|
lodash.pull(fieldNames, 'sync_time');
|
|
|
|
return super.serialize(item, 'resource', fieldNames);
|
|
|
|
}
|
|
|
|
|
2017-06-24 18:51:43 +00:00
|
|
|
static fullPath(resource) {
|
|
|
|
let extension = mime.toFileExtension(resource.mime);
|
|
|
|
extension = extension ? '.' + extension : '';
|
|
|
|
return Setting.value('resourceDir') + '/' + resource.id + extension;
|
|
|
|
}
|
|
|
|
|
2017-06-24 23:19:11 +00:00
|
|
|
static pathToId(path) {
|
|
|
|
return filename(path);
|
|
|
|
}
|
|
|
|
|
2017-07-02 12:02:07 +00:00
|
|
|
static content(resource) {
|
2017-07-05 21:52:31 +00:00
|
|
|
return this.fsDriver().readFile(this.fullPath(resource));
|
2017-07-02 12:02:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static setContent(resource, content) {
|
2017-07-05 21:52:31 +00:00
|
|
|
return this.fsDriver().writeBinaryFile(this.fullPath(resource), content);
|
2017-07-02 12:02:07 +00:00
|
|
|
}
|
|
|
|
|
2017-06-24 18:51:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export { Resource };
|