1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-12 08:54:00 +02:00
joplin/ReactNativeClient/lib/models/Resource.js

87 lines
2.4 KiB
JavaScript
Raw Normal View History

2017-12-14 20:12:14 +02:00
const BaseModel = require('lib/BaseModel.js');
const BaseItem = require('lib/models/BaseItem.js');
const Setting = require('lib/models/Setting.js');
const { mime } = require('lib/mime-utils.js');
const { filename } = require('lib/path-utils.js');
const { FsDriverDummy } = require('lib/fs-driver-dummy.js');
const { markdownUtils } = require('lib/markdown-utils.js');
2017-06-24 20:51:43 +02:00
2017-07-02 14:02:07 +02:00
class Resource extends BaseItem {
2017-06-24 20:51:43 +02:00
static tableName() {
return 'resources';
}
2017-07-03 21:50:45 +02:00
static modelType() {
return BaseModel.TYPE_RESOURCE;
2017-06-24 20:51:43 +02: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 23:52:31 +02:00
static fsDriver() {
if (!Resource.fsDriver_) Resource.fsDriver_ = new FsDriverDummy();
return Resource.fsDriver_;
}
2017-07-02 17:46:03 +02:00
static async serialize(item, type = null, shownKeys = null) {
2017-07-02 14:02:07 +02:00
let fieldNames = this.fieldNames();
fieldNames.push('type_');
return super.serialize(item, 'resource', fieldNames);
}
static filename(resource) {
let extension = resource.file_extension;
if (!extension) extension = resource.mime ? mime.toFileExtension(resource.mime) : '';
2017-06-24 20:51:43 +02:00
extension = extension ? '.' + extension : '';
return resource.id + extension;
}
static fullPath(resource) {
return Setting.value('resourceDir') + '/' + this.filename(resource);
2017-06-24 20:51:43 +02:00
}
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 19:47:25 +02:00
lines.push(markdownUtils.escapeLinkText(tagAlt));
2017-08-01 23:40:14 +02:00
lines.push("](:/" + resource.id + ")");
} else {
lines.push("[");
2017-08-02 19:47:25 +02:00
lines.push(markdownUtils.escapeLinkText(tagAlt));
2017-08-01 23:40:14 +02:00
lines.push("](:/" + resource.id + ")");
}
return lines.join('');
}
2017-06-25 01:19:11 +02:00
static pathToId(path) {
return filename(path);
}
static async content(resource) {
2017-07-05 23:52:31 +02:00
return this.fsDriver().readFile(this.fullPath(resource));
2017-07-02 14:02:07 +02:00
}
static setContent(resource, content) {
2017-07-05 23:52:31 +02:00
return this.fsDriver().writeBinaryFile(this.fullPath(resource), content);
2017-07-02 14:02:07 +02:00
}
static isResourceUrl(url) {
return url && 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 20:51:43 +02:00
}
Resource.IMAGE_MAX_DIMENSION = 1920;
2017-12-14 20:12:14 +02:00
module.exports = Resource;