Support for resources

pull/41/head
Laurent Cozic 2017-06-24 19:51:43 +01:00
parent 03c9d55397
commit 85b3d40aad
6 changed files with 102 additions and 25 deletions

View File

@ -6,11 +6,12 @@ import { promiseChain } from 'lib/promise-utils.js';
import { folderItemFilename } from 'lib/string-utils.js'
import { BaseModel } from 'lib/base-model.js';
import { Note } from 'lib/models/note.js';
import { Resource } from 'lib/models/resource.js';
import { Folder } from 'lib/models/folder.js';
import jsSHA from "jssha";
const Promise = require('promise');
const fs = require('fs');
const fs = require('fs-extra');
const stringToStream = require('string-to-stream')
const BLOCK_OPEN = "<div>";
@ -557,7 +558,7 @@ async function fuzzyMatch(note) {
return null;
}
async function saveNoteToDb(note) {
async function saveNoteToStorage(note) {
note = Note.filter(note);
let existingNote = await fuzzyMatch(note);
@ -580,15 +581,36 @@ async function saveNoteToDb(note) {
return Note.save(diff, { autoTimestamp: false });
} else {
console.info('NNNNNNNNNNNNNNNNN4');
// return Note.save(note, {
// isNew: true,
// autoTimestamp: false,
// });
// id: noteResource.id,
// data: decodedData,
// mime: noteResource.mime,
// title: noteResource.filename,
// filename: noteResource.filename,
// CREATE TABLE resources (
// id TEXT PRIMARY KEY,
// title TEXT,
// mime TEXT,
// filename TEXT,
// created_time INT,
// updated_time INT
for (let i = 0; i < note.resources.length; i++) {
let resource = note.resources[i];
let toSave = Object.assign({}, resource);
delete toSave.data;
await Resource.save(toSave, { isNew: true });
await filePutContents(Resource.fullPath(toSave), resource.data);
}
return Note.save(note, {
isNew: true,
autoTimestamp: false,
});
}
}
function importEnex(db, parentFolderId, resourceDir, filePath) {
function importEnex(parentFolderId, filePath) {
let stream = fs.createReadStream(filePath);
return new Promise((resolve, reject) => {
@ -641,7 +663,7 @@ function importEnex(db, parentFolderId, resourceDir, filePath) {
note.body = processMdArrayNewLines(result.lines);
note.id = uuid.create();
return saveNoteToDb(note);
return saveNoteToStorage(note);
// SAVE NOTE HERE
// saveNoteToDisk(parentFolder, note);
@ -726,7 +748,7 @@ function importEnex(db, parentFolderId, resourceDir, filePath) {
processNotes().then(() => {
stream.resume();
}).catch((error) => {
console.info('Error processing note', error);
console.error('Error processing note', error);
});
}
note = null;
@ -754,7 +776,7 @@ function importEnex(db, parentFolderId, resourceDir, filePath) {
let r = {
id: noteResource.id,
data: decodedData,
mime_type: noteResource.mime,
mime: noteResource.mime,
title: noteResource.filename,
filename: noteResource.filename,
};

View File

@ -18,10 +18,12 @@ import { _ } from 'lib/locale.js';
import os from 'os';
import fs from 'fs-extra';
const APPNAME = 'joplin';
const dataDir = os.homedir() + '/.local/share/' + APPNAME;
const dataDir = os.homedir() + '/.local/share/' + Setting.value('appName');
const resourceDir = dataDir + '/resources';
Setting.setConstant('dataDir', dataDir);
Setting.setConstant('resourceDir', resourceDir);
process.on('unhandledRejection', (reason, p) => {
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
});
@ -56,19 +58,21 @@ async function main() {
// console.info('DELETING ALL DATA');
// await db.exec('DELETE FROM notes');
// await db.exec('DELETE FROM changes');
// await db.exec('DELETE FROM folders');
// await db.exec('DELETE FROM resources');
// await db.exec('DELETE FROM deleted_items');
// await db.exec('DELETE FROM tags');
// await db.exec('DELETE FROM note_tags');
console.info('DELETING ALL DATA');
await db.exec('DELETE FROM notes');
await db.exec('DELETE FROM changes');
await db.exec('DELETE FROM folders');
await db.exec('DELETE FROM resources');
await db.exec('DELETE FROM deleted_items');
await db.exec('DELETE FROM tags');
await db.exec('DELETE FROM note_tags');
let folder = await Folder.save({ title: 'test' });
// // let folder = await Folder.save({ title: 'test' });
// let folder = await Folder.loadByField('title', 'test');
// await importEnex(db, folder.id, resourceDir, '/mnt/c/Users/Laurent/Desktop/Laurent.enex'); //'/mnt/c/Users/Laurent/Desktop/Laurent.enex');
// return;
//let folder = await Folder.loadByField('title', 'test');
await importEnex(folder.id, '/mnt/c/Users/Laurent/Desktop/Laurent.enex'); //'/mnt/c/Users/Laurent/Desktop/Laurent.enex');
return;

View File

@ -313,6 +313,7 @@ class BaseModel {
BaseModel.MODEL_TYPE_NOTE = 1;
BaseModel.MODEL_TYPE_FOLDER = 2;
BaseModel.MODEL_TYPE_SETTING = 3;
BaseModel.MODEL_TYPE_RESOURCE = 4;
BaseModel.tableInfo_ = null;
BaseModel.tableKeys_ = null;
BaseModel.db_ = null;

15
lib/mime-utils.js Normal file

File diff suppressed because one or more lines are too long

23
lib/models/resource.js Normal file
View File

@ -0,0 +1,23 @@
import { BaseModel } from 'lib/base-model.js';
import { Setting } from 'lib/models/setting.js';
import { mime } from 'lib/mime-utils.js';
class Resource extends BaseModel {
static tableName() {
return 'resources';
}
static itemType() {
return BaseModel.MODEL_TYPE_RESOURCE;
}
static fullPath(resource) {
let extension = mime.toFileExtension(resource.mime);
extension = extension ? '.' + extension : '';
return Setting.value('resourceDir') + '/' + resource.id + extension;
}
}
export { Resource };

View File

@ -37,6 +37,10 @@ class Setting extends BaseModel {
});
}
static setConstant(key, value) {
this.constants_[key] = value;
}
static setValue(key, value) {
if (!this.cache_) throw new Error('Settings have not been initialized!');
@ -56,6 +60,8 @@ class Setting extends BaseModel {
}
static value(key) {
if (key in this.constants_) return this.constants_[key];
if (!this.cache_) throw new Error('Settings have not been initialized!');
for (let i = 0; i < this.cache_.length; i++) {
@ -136,4 +142,10 @@ Setting.defaults_ = {
'sync.onedrive.auth': { value: '', type: 'string' },
};
// Contains constants that are set by the application and
// cannot be modified by the user:
Setting.constants_ = {
'appName': 'joplin',
}
export { Setting };