1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-03-03 15:32:30 +02:00

58 lines
1.4 KiB
JavaScript
Raw Normal View History

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-05 22:52:31 +01:00
static fsDriver() {
if (!Resource.fsDriver_) Resource.fsDriver_ = new FsDriverDummy();
return Resource.fsDriver_;
}
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_');
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-02 13:02:07 +01:00
static content(resource) {
2017-07-05 22:52:31 +01:00
return this.fsDriver().readFile(this.fullPath(resource));
2017-07-02 13:02:07 +01:00
}
static setContent(resource, content) {
2017-07-05 22:52:31 +01:00
return this.fsDriver().writeBinaryFile(this.fullPath(resource), content);
2017-07-02 13:02:07 +01:00
}
static isResourceUrl(url) {
return url.length === 34 && url[0] === ':' && url[1] === '/';
}
static urlToId(url) {
if (!this.isResourceUrl(url)) throw new Error('Not a valid resource URL: ' + url);
return url.substr(2);
}
2017-06-24 19:51:43 +01:00
}
export { Resource };