1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-02-07 19:30:04 +02:00

83 lines
2.2 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-08-01 23:40:14 +02:00
import { FsDriverDummy } from 'lib/fs-driver-dummy.js';
2017-08-02 17:47:25 +00:00
import { markdownUtils } from 'lib/markdown-utils.js';
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-08-01 23:40:14 +02:00
static isSupportedImageMimeType(type) {
const imageMimeTypes = ["image/jpg", "image/jpeg", "image/png", "image/gif"];
return imageMimeTypes.indexOf(type.toLowerCase()) >= 0;
}
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-08-01 23:40:14 +02:00
static markdownTag(resource) {
let tagAlt = resource.alt ? resource.alt : resource.title;
if (!tagAlt) tagAlt = '';
let lines = [];
if (Resource.isSupportedImageMimeType(resource.mime)) {
lines.push("![");
2017-08-02 17:47:25 +00:00
lines.push(markdownUtils.escapeLinkText(tagAlt));
2017-08-01 23:40:14 +02:00
lines.push("](:/" + resource.id + ")");
} else {
lines.push("[");
2017-08-02 17:47:25 +00:00
lines.push(markdownUtils.escapeLinkText(tagAlt));
2017-08-01 23:40:14 +02:00
lines.push("](:/" + resource.id + ")");
}
return lines.join('');
}
2017-06-25 00:19:11 +01:00
static pathToId(path) {
return filename(path);
}
static async 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
}
Resource.IMAGE_MAX_DIMENSION = 1920;
2017-06-24 19:51:43 +01:00
export { Resource };