diff --git a/browser/main/lib/dataApi/attachmentManagement.js b/browser/main/lib/dataApi/attachmentManagement.js index a93fc8a5..077f02d8 100644 --- a/browser/main/lib/dataApi/attachmentManagement.js +++ b/browser/main/lib/dataApi/attachmentManagement.js @@ -245,41 +245,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) { - if (storageKey != null && noteKey != null && markdownContent != null) { - 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 == null || noteKey == null || markdownContent == null) { + return + } + 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 } - } - - 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') + }) } - 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.debug('Attachment folder (\'' + attachmentFolder + '\') did not exist..') } }