From 2369bcf9fcf53289fa5053171f1353c5ecb87064 Mon Sep 17 00:00:00 2001 From: zadam Date: Wed, 6 May 2020 23:11:34 +0200 Subject: [PATCH] fixes for image download --- .idea/dataSources.xml | 18 ++-------------- src/services/attributes.js | 1 + src/services/notes.js | 43 ++++++++++++++++++++------------------ 3 files changed, 26 insertions(+), 36 deletions(-) diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml index a9dfc5250..70caa7f2a 100644 --- a/.idea/dataSources.xml +++ b/.idea/dataSources.xml @@ -1,25 +1,11 @@ - + sqlite.xerial true org.sqlite.JDBC - jdbc:sqlite:$USER_HOME$/trilium-data/document.db - - - sqlite.xerial - true - org.sqlite.JDBC - jdbc:sqlite:$PROJECT_DIR$/dist/trilium linux x64/trilium-data/document.db - - - file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.16.1/xerial-sqlite-license.txt - - - file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.16.1/sqlite-jdbc-3.16.1.jar - - + jdbc:sqlite:$PROJECT_DIR$/../trilium-data/document.db \ No newline at end of file diff --git a/src/services/attributes.js b/src/services/attributes.js index d1abd7cec..bab322cbd 100644 --- a/src/services/attributes.js +++ b/src/services/attributes.js @@ -19,6 +19,7 @@ const BUILTIN_ATTRIBUTES = [ { type: 'label', name: 'appTheme' }, { type: 'label', name: 'hidePromotedAttributes' }, { type: 'label', name: 'readOnly' }, + { type: 'label', name: 'autoReadOnlyDisabled' }, { type: 'label', name: 'cssClass' }, { type: 'label', name: 'iconClass' }, { type: 'label', name: 'keyboardShortcut' }, diff --git a/src/services/notes.js b/src/services/notes.js index 4df9846e8..152c72ec4 100644 --- a/src/services/notes.js +++ b/src/services/notes.js @@ -293,15 +293,11 @@ async function downloadImages(noteId, content) { if (!url.includes('api/images/') // this is and exception for the web clipper's "imageId" && (url.length !== 20 || url.toLowerCase().startsWith('http'))) { - if (url in downloadImagePromises) { - // download is already in progress - continue; - } if (url in imageUrlToNoteIdMapping) { const imageNote = await repository.getNote(imageUrlToNoteIdMapping[url]); - if (imageNote || imageNote.isDeleted) { + if (!imageNote || imageNote.isDeleted) { delete imageUrlToNoteIdMapping[url]; } else { @@ -322,6 +318,11 @@ async function downloadImages(noteId, content) { continue; } + if (url in downloadImagePromises) { + // download is already in progress + continue; + } + // this is done asynchronously, it would be too slow to wait for the download // given that save can be triggered very often downloadImagePromises[url] = downloadImage(noteId, url); @@ -338,28 +339,30 @@ async function downloadImages(noteId, content) { // are downloaded and the IMG references are not updated. For this occassion we have this code // which upon the download of all the images will update the note if the links have not been fixed before - const imageNotes = await repository.getNotes(Object.values(imageUrlToNoteIdMapping)); + await sql.transactional(async () => { + const imageNotes = await repository.getNotes(Object.values(imageUrlToNoteIdMapping)); - const origNote = await repository.getNote(noteId); - const origContent = await origNote.getContent(); - let updatedContent = origContent; + const origNote = await repository.getNote(noteId); + const origContent = await origNote.getContent(); + let updatedContent = origContent; - for (const url in imageUrlToNoteIdMapping) { - const imageNote = imageNotes.find(note => note.noteId === imageUrlToNoteIdMapping[url]); + for (const url in imageUrlToNoteIdMapping) { + const imageNote = imageNotes.find(note => note.noteId === imageUrlToNoteIdMapping[url]); - if (imageNote && !imageNote.isDeleted) { - updatedContent = replaceUrl(updatedContent, url, imageNote); + if (imageNote && !imageNote.isDeleted) { + updatedContent = replaceUrl(updatedContent, url, imageNote); + } } - } - // update only if the links have not been already fixed. - if (updatedContent !== origContent) { - await origNote.setContent(updatedContent); + // update only if the links have not been already fixed. + if (updatedContent !== origContent) { + await origNote.setContent(updatedContent); - await scanForLinks(origNote); + await scanForLinks(origNote); - console.log(`Fixed the image links for note ${noteId} to the offline saved.`); - } + console.log(`Fixed the image links for note ${noteId} to the offline saved.`); + } + }); }, 5000); });