joplin/ReactNativeClient/lib/models/resource.js

52 lines
1.4 KiB
JavaScript
Raw Normal View History

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-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-05 21:29:00 +00:00
// RNFIX: Temporary disabled for React Native
2017-07-02 12:02:07 +00:00
static content(resource) {
2017-07-05 21:29:00 +00:00
// // TODO: node-only, and should probably be done with streams
// const fs = require('fs-extra');
// return fs.readFile(this.fullPath(resource));
2017-07-02 12:02:07 +00:00
}
static setContent(resource, content) {
2017-07-05 21:29:00 +00:00
// // TODO: node-only, and should probably be done with streams
// const fs = require('fs-extra');
// let buffer = new Buffer(content);
// return fs.writeFile(this.fullPath(resource), buffer);
2017-07-02 12:02:07 +00:00
}
2017-06-24 18:51:43 +00:00
}
export { Resource };