From 2f4eb595f61284aaa8c7c420359dd880df094e8a Mon Sep 17 00:00:00 2001 From: ehhc Date: Wed, 23 May 2018 19:47:41 +0200 Subject: [PATCH] OnBlur throws exceptions if the notetype is snippet -> Fixes #1962 --- .../main/lib/dataApi/attachmentManagement.js | 60 ++++++++++--------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/browser/main/lib/dataApi/attachmentManagement.js b/browser/main/lib/dataApi/attachmentManagement.js index 1373cf94d..c83fc79da 100644 --- a/browser/main/lib/dataApi/attachmentManagement.js +++ b/browser/main/lib/dataApi/attachmentManagement.js @@ -232,39 +232,41 @@ function deleteAttachmentFolder (storageKey, noteKey) { * @param noteKey NoteKey of the current note. Is used to determine the belonging attachment folder. */ function deleteAttachmentsNotPresentInNote (markdownContent, storageKey, noteKey) { - const targetStorage = findStorage.findStorage(storageKey) - const attachmentFolder = path.join(targetStorage.path, DESTINATION_FOLDER, noteKey) - const attachmentsInNote = getAttachmentsInContent(markdownContent) - const attachmentsInNoteOnlyFileNames = [] - if (attachmentsInNote) { - for (let i = 0; i < attachmentsInNote.length; i++) { - attachmentsInNoteOnlyFileNames.push(attachmentsInNote[i].replace(new RegExp(STORAGE_FOLDER_PLACEHOLDER + escapeStringRegexp(path.sep) + noteKey + escapeStringRegexp(path.sep), 'g'), '')) + if (storageKey && noteKey && markdownContent !== null && typeof markdownContent !== 'undefined') { + const targetStorage = findStorage.findStorage(storageKey) + const attachmentFolder = path.join(targetStorage.path, DESTINATION_FOLDER, noteKey) + const attachmentsInNote = getAttachmentsInContent(markdownContent) + const attachmentsInNoteOnlyFileNames = [] + if (attachmentsInNote) { + for (let i = 0; i < attachmentsInNote.length; i++) { + attachmentsInNoteOnlyFileNames.push(attachmentsInNote[i].replace(new RegExp(STORAGE_FOLDER_PLACEHOLDER + escapeStringRegexp(path.sep) + noteKey + escapeStringRegexp(path.sep), 'g'), '')) + } } - } - if (fs.existsSync(attachmentFolder)) { - fs.readdir(attachmentFolder, (err, files) => { - if (err) { - console.error("Error reading directory '" + attachmentFolder + "'. Error:") - console.error(err) - return - } - files.forEach(file => { - if (!attachmentsInNoteOnlyFileNames.includes(file)) { - const absolutePathOfFile = path.join(targetStorage.path, DESTINATION_FOLDER, noteKey, file) - fs.unlink(absolutePathOfFile, (err) => { - if (err) { - console.error("Could not delete '%s'", absolutePathOfFile) - console.error(err) - return - } - console.info("File '" + absolutePathOfFile + "' deleted because it was not included in the content of the note") - }) + if (fs.existsSync(attachmentFolder)) { + fs.readdir(attachmentFolder, (err, files) => { + if (err) { + console.error("Error reading directory '" + attachmentFolder + "'. Error:") + console.error(err) + return } + files.forEach(file => { + if (!attachmentsInNoteOnlyFileNames.includes(file)) { + const absolutePathOfFile = path.join(targetStorage.path, DESTINATION_FOLDER, noteKey, file) + fs.unlink(absolutePathOfFile, (err) => { + if (err) { + console.error("Could not delete '%s'", absolutePathOfFile) + console.error(err) + return + } + console.info("File '" + absolutePathOfFile + "' deleted because it was not included in the content of the note") + }) + } + }) }) - }) - } else { - console.info("Attachment folder ('" + attachmentFolder + "') did not exist..") + } else { + console.info("Attachment folder ('" + attachmentFolder + "') did not exist..") + } } }