diff --git a/db/demo.tar b/db/demo.tar index c193055e3..7298d8808 100644 Binary files a/db/demo.tar and b/db/demo.tar differ diff --git a/db/schema.sql b/db/schema.sql index a8a1a37c4..d6963d921 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -96,19 +96,6 @@ CREATE TABLE attributes hash TEXT default "" not null, isInheritable int DEFAULT 0 NULL); CREATE INDEX IDX_attributes_name_value on attributes (name, value); -CREATE TABLE IF NOT EXISTS "notes" ( - `noteId` TEXT NOT NULL, - `title` TEXT NOT NULL DEFAULT "note", - `content` TEXT NULL DEFAULT NULL, - `isProtected` INT NOT NULL DEFAULT 0, - `type` TEXT NOT NULL DEFAULT 'text', - `mime` TEXT NOT NULL DEFAULT 'text/html', - `hash` TEXT DEFAULT "" NOT NULL, - `isDeleted` INT NOT NULL DEFAULT 0, - `dateCreated` TEXT NOT NULL, - `dateModified` TEXT NOT NULL, - PRIMARY KEY(`noteId`) -); CREATE TABLE IF NOT EXISTS "links" ( `linkId` TEXT NOT NULL, `noteId` TEXT NOT NULL, @@ -130,3 +117,26 @@ CREATE INDEX IDX_attributes_noteId_index on attributes (noteId); CREATE INDEX IDX_attributes_value_index on attributes (value); +CREATE TABLE IF NOT EXISTS "note_contents" ( + `noteContentId` TEXT NOT NULL, + `noteId` TEXT NOT NULL, + `isProtected` INT NOT NULL DEFAULT 0, + `content` TEXT NULL DEFAULT NULL, + `hash` TEXT DEFAULT "" NOT NULL, + `dateCreated` TEXT NOT NULL, + `dateModified` TEXT NOT NULL, + PRIMARY KEY(`noteContentId`) +); +CREATE UNIQUE INDEX `IDX_note_contents_noteId` ON `note_contents` (`noteId`); +CREATE TABLE IF NOT EXISTS "notes" ( + `noteId` TEXT NOT NULL, + `title` TEXT NOT NULL DEFAULT "note", + `isProtected` INT NOT NULL DEFAULT 0, + `type` TEXT NOT NULL DEFAULT 'text', + `mime` TEXT NOT NULL DEFAULT 'text/html', + `hash` TEXT DEFAULT "" NOT NULL, + `isDeleted` INT NOT NULL DEFAULT 0, + `dateCreated` TEXT NOT NULL, + `dateModified` TEXT NOT NULL, + PRIMARY KEY(`noteId`) +); diff --git a/src/routes/api/import.js b/src/routes/api/import.js index ee048759a..dbebf6aab 100644 --- a/src/routes/api/import.js +++ b/src/routes/api/import.js @@ -5,55 +5,11 @@ const enexImportService = require('../../services/import/enex'); const opmlImportService = require('../../services/import/opml'); const tarImportService = require('../../services/import/tar'); const singleImportService = require('../../services/import/single'); -const messagingService = require('../../services/messaging'); const cls = require('../../services/cls'); const path = require('path'); const noteCacheService = require('../../services/note_cache'); const log = require('../../services/log'); - -class ImportContext { - constructor(importId, safeImport) { - // importId is to distinguish between different import events - it is possible (though not recommended) - // to have multiple imports going at the same time - this.importId = importId; - - this.safeImport = safeImport; - - // // count is mean to represent count of exported notes where practical, otherwise it's just some measure of progress - this.progressCount = 0; - this.lastSentCountTs = Date.now(); - } - - async increaseProgressCount() { - this.progressCount++; - - if (Date.now() - this.lastSentCountTs >= 500) { - this.lastSentCountTs = Date.now(); - - await messagingService.sendMessageToAllClients({ - importId: this.importId, - type: 'import-progress-count', - progressCount: this.progressCount - }); - } - } - - async importFinished(noteId) { - await messagingService.sendMessageToAllClients({ - importId: this.importId, - type: 'import-finished', - noteId: noteId - }); - } - - // must remaing non-static - async reportError(message) { - await messagingService.sendMessageToAllClients({ - type: 'import-error', - message: message - }); - } -} +const ImportContext = require('../../services/import_context'); async function importToBranch(req) { let {parentNoteId, importId, safeImport} = req.params; diff --git a/src/services/import/tar.js b/src/services/import/tar.js index 888f3c2f4..2deff2974 100644 --- a/src/services/import/tar.js +++ b/src/services/import/tar.js @@ -13,9 +13,7 @@ const stream = require('stream'); const path = require('path'); const commonmark = require('commonmark'); const mimeTypes = require('mime-types'); - -let importNoteCount; -let lastSentCountTs = Date.now(); +const ImportContext = require('../import_context'); /** * @param {ImportContext} importContext @@ -24,7 +22,8 @@ let lastSentCountTs = Date.now(); * @return {Promise<*>} */ async function importTar(importContext, fileBuffer, importRootNote) { - importNoteCount = 0; + importContext = importContext || new ImportContext("1", false); + // maps from original noteId (in tar file) to newly generated noteId const noteIdMap = {}; const attributes = []; diff --git a/src/services/import_context.js b/src/services/import_context.js new file mode 100644 index 000000000..bca12895d --- /dev/null +++ b/src/services/import_context.js @@ -0,0 +1,49 @@ +"use strict"; + +const messagingService = require('./messaging'); + +class ImportContext { + constructor(importId, safeImport) { + // importId is to distinguish between different import events - it is possible (though not recommended) + // to have multiple imports going at the same time + this.importId = importId; + + this.safeImport = safeImport; + + // // count is mean to represent count of exported notes where practical, otherwise it's just some measure of progress + this.progressCount = 0; + this.lastSentCountTs = Date.now(); + } + + async increaseProgressCount() { + this.progressCount++; + + if (Date.now() - this.lastSentCountTs >= 500) { + this.lastSentCountTs = Date.now(); + + await messagingService.sendMessageToAllClients({ + importId: this.importId, + type: 'import-progress-count', + progressCount: this.progressCount + }); + } + } + + async importFinished(noteId) { + await messagingService.sendMessageToAllClients({ + importId: this.importId, + type: 'import-finished', + noteId: noteId + }); + } + + // must remaing non-static + async reportError(message) { + await messagingService.sendMessageToAllClients({ + type: 'import-error', + message: message + }); + } +} + +module.exports = ImportContext; \ No newline at end of file diff --git a/src/services/sql_init.js b/src/services/sql_init.js index 4e7a1c17f..c2a23fdf3 100644 --- a/src/services/sql_init.js +++ b/src/services/sql_init.js @@ -77,16 +77,21 @@ async function createInitialDatabase(username, password) { await sql.executeScript(schema); const Note = require("../entities/note"); + const NoteContent = require("../entities/note_content"); const Branch = require("../entities/branch"); const rootNote = await new Note({ noteId: 'root', title: 'root', - content: '', type: 'text', mime: 'text/html' }).save(); + const rootNoteContent = await new NoteContent({ + noteId: rootNote.noteId, + content: '' + }).save(); + await new Branch({ branchId: 'root', noteId: 'root', @@ -96,7 +101,7 @@ async function createInitialDatabase(username, password) { }).save(); const tarImportService = require("./import/tar"); - await tarImportService.importTar(demoFile, rootNote); + await tarImportService.importTar(null, demoFile, rootNote); const startNoteId = await sql.getValue("SELECT noteId FROM branches WHERE parentNoteId = 'root' AND isDeleted = 0 ORDER BY notePosition");