Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions browser/main/lib/dataApi/attachmentManagement.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ 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) {
return
}
const targetStorage = findStorage.findStorage(storageKey)
const attachmentFolder = path.join(targetStorage.path, DESTINATION_FOLDER, noteKey)
const attachmentsInNote = getAttachmentsInContent(markdownContent)
Expand All @@ -254,11 +257,10 @@ function deleteAttachmentsNotPresentInNote (markdownContent, storageKey, noteKey
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('Error reading directory \'' + attachmentFolder + '\'. Error:')
console.error(err)
return
}
Expand All @@ -267,17 +269,17 @@ function deleteAttachmentsNotPresentInNote (markdownContent, storageKey, noteKey
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('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")
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..")
console.debug('Attachment folder (\'' + attachmentFolder + '\') did not exist..')
}
}

Expand Down
34 changes: 34 additions & 0 deletions tests/dataApi/attachmentManagement.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,38 @@ it('should test that deleteAttachmentsNotPresentInNote does not delete reference
expect(fsUnlinkCallArguments.includes(path.join(attachmentFolderPath, dummyFilesInFolder[0]))).toBe(false)
})

it('should test that deleteAttachmentsNotPresentInNote does nothing if noteKey, storageKey or noteContent was null', function () {
const noteKey = null
const storageKey = null
const markdownContent = ''

findStorage.findStorage = jest.fn()
fs.existsSync = jest.fn()
fs.readdir = jest.fn()
fs.unlink = jest.fn()

systemUnderTest.deleteAttachmentsNotPresentInNote(markdownContent, storageKey, noteKey)
expect(fs.existsSync).not.toHaveBeenCalled()
expect(fs.readdir).not.toHaveBeenCalled()
expect(fs.unlink).not.toHaveBeenCalled()
})

it('should test that deleteAttachmentsNotPresentInNote does nothing if noteKey, storageKey or noteContent was undefined', function () {
const noteKey = undefined
const storageKey = undefined
const markdownContent = ''

findStorage.findStorage = jest.fn()
fs.existsSync = jest.fn()
fs.readdir = jest.fn()
fs.unlink = jest.fn()

systemUnderTest.deleteAttachmentsNotPresentInNote(markdownContent, storageKey, noteKey)
expect(fs.existsSync).not.toHaveBeenCalled()
expect(fs.readdir).not.toHaveBeenCalled()
expect(fs.unlink).not.toHaveBeenCalled()
})

it('should test that moveAttachments moves attachments only if the source folder existed', function () {
fse.existsSync = jest.fn(() => false)
fse.moveSync = jest.fn()
Expand Down Expand Up @@ -403,6 +435,8 @@ it('should test that cloneAttachments modifies the content of the new note corre
'![' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + oldNote.key + path.sep + 'image.jpg](imageName}) \n' +
'[' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + oldNote.key + path.sep + 'pdf.pdf](pdf})'
newNote.content = testInput
findStorage.findStorage = jest.fn()
findStorage.findStorage.mockReturnValue({path: 'dummyStoragePath'})

const expectedOutput =
'Test input' +
Expand Down