From e698aa582277265b5b946f1a335e48ccf6a36857 Mon Sep 17 00:00:00 2001 From: zadam Date: Thu, 16 Mar 2023 20:13:34 +0100 Subject: [PATCH] uploading image to attachment --- src/services/image.js | 2 +- src/services/notes.js | 43 ++++++++++++++++--------------------------- 2 files changed, 17 insertions(+), 28 deletions(-) diff --git a/src/services/image.js b/src/services/image.js index 0edc0b548..280a4cdbf 100644 --- a/src/services/image.js +++ b/src/services/image.js @@ -129,7 +129,7 @@ function saveImage(parentNoteId, uploadBuffer, originalName, shrinkImageSwitch, fileName, note, noteId: note.noteId, - url: `api/images/${note.noteId}/${fileName}` + url: `api/images/${note.noteId}/${encodeURIComponent(fileName)}` }; } diff --git a/src/services/notes.js b/src/services/notes.js index 9d6260cfd..aeb7ad6eb 100644 --- a/src/services/notes.js +++ b/src/services/notes.js @@ -21,6 +21,7 @@ const dayjs = require("dayjs"); const htmlSanitizer = require("./html_sanitizer"); const ValidationError = require("../errors/validation_error"); const noteTypesService = require("./note_types"); +const {attach} = require("jsdom/lib/jsdom/living/helpers/svg/basic-types.js"); function getNewNotePosition(parentNoteId) { const note = becca.notes[parentNoteId]; @@ -370,7 +371,7 @@ function findRelationMapLinks(content, foundLinks) { } } -const imageUrlToNoteIdMapping = {}; +const imageUrlToAttachmentIdMapping = {}; async function downloadImage(noteId, imageUrl) { try { @@ -379,13 +380,11 @@ async function downloadImage(noteId, imageUrl) { const title = path.basename(parsedUrl.pathname); const imageService = require('../services/image'); - const {note} = imageService.saveImage(noteId, imageBuffer, title, true, true); + const {attachment} = imageService.saveImageToAttachment(noteId, imageBuffer, title, true, true); - note.addLabel('imageUrl', imageUrl); + imageUrlToAttachmentIdMapping[imageUrl] = attachment.attachmentId; - imageUrlToNoteIdMapping[imageUrl] = note.noteId; - - log.info(`Download of '${imageUrl}' succeeded and was saved as image note '${note.noteId}'`); + log.info(`Download of '${imageUrl}' succeeded and was saved as image attachment '${attachment.attachmentId}' of note '${noteId}'`); } catch (e) { log.error(`Download of '${imageUrl}' for note '${noteId}' failed with error: ${e.message} ${e.stack}`); @@ -395,10 +394,10 @@ async function downloadImage(noteId, imageUrl) { /** url => download promise */ const downloadImagePromises = {}; -function replaceUrl(content, url, imageNote) { +function replaceUrl(content, url, attachment) { const quotedUrl = utils.quoteRegex(url); - return content.replace(new RegExp(`\\s+src=[\"']${quotedUrl}[\"']`, "ig"), ` src="api/images/${imageNote.noteId}/${imageNote.title}"`); + return content.replace(new RegExp(`\\s+src=[\"']${quotedUrl}[\"']`, "ig"), ` src="api/notes/${attachment.parentId}/images/${encodeURIComponent(attachment.title)}"`); } function downloadImages(noteId, content) { @@ -424,32 +423,22 @@ function downloadImages(noteId, content) { content = `${content.substr(0, imageMatch.index)} note.type === 'image'); - - if (existingImage) { - imageUrlToNoteIdMapping[url] = existingImage.noteId; - - content = replaceUrl(content, url, existingImage); - continue; - } - if (url in downloadImagePromises) { // download is already in progress continue; @@ -472,7 +461,7 @@ function downloadImages(noteId, content) { // which upon the download of all the images will update the note if the links have not been fixed before sql.transactional(() => { - const imageNotes = becca.getNotes(Object.values(imageUrlToNoteIdMapping), true); + const imageNotes = becca.getNotes(Object.values(imageUrlToAttachmentIdMapping), true); const origNote = becca.getNote(noteId); @@ -484,8 +473,8 @@ function downloadImages(noteId, content) { const origContent = origNote.getContent(); let updatedContent = origContent; - for (const url in imageUrlToNoteIdMapping) { - const imageNote = imageNotes.find(note => note.noteId === imageUrlToNoteIdMapping[url]); + for (const url in imageUrlToAttachmentIdMapping) { + const imageNote = imageNotes.find(note => note.noteId === imageUrlToAttachmentIdMapping[url]); if (imageNote && !imageNote.isDeleted) { updatedContent = replaceUrl(updatedContent, url, imageNote);