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');
|
2017-11-03 02:09:34 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-11-20 20:25:23 +02:00
|
|
|
static filename(resource) {
|
2017-12-02 01:15:49 +02:00
|
|
|
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 : '';
|
2017-11-20 20:25:23 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-10-24 21:40:15 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-07-26 20:36:16 +02:00
|
|
|
static isResourceUrl(url) {
|
2017-11-07 20:39:11 +02:00
|
|
|
return url && url.length === 34 && url[0] === ':' && url[1] === '/';
|
2017-07-26 20:36:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-10-20 00:02:13 +02:00
|
|
|
Resource.IMAGE_MAX_DIMENSION = 1920;
|
|
|
|
|
2017-12-14 20:12:14 +02:00
|
|
|
module.exports = Resource;
|