Skip to content

Commit 88856b7

Browse files
authored
Merge pull request #1963 from ehhc/OnBlur_Throws_Exceptions_On_Snippet_Notes
OnBlur throws exceptions if the notetype is snippet -> Fixes #1962
2 parents eda4e46 + b526d48 commit 88856b7

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

browser/main/lib/dataApi/attachmentManagement.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,9 @@ function deleteAttachmentFolder (storageKey, noteKey) {
271271
* @param noteKey NoteKey of the current note. Is used to determine the belonging attachment folder.
272272
*/
273273
function deleteAttachmentsNotPresentInNote (markdownContent, storageKey, noteKey) {
274+
if (storageKey == null || noteKey == null || markdownContent == null) {
275+
return
276+
}
274277
const targetStorage = findStorage.findStorage(storageKey)
275278
const attachmentFolder = path.join(targetStorage.path, DESTINATION_FOLDER, noteKey)
276279
const attachmentsInNote = getAttachmentsInContent(markdownContent)
@@ -280,11 +283,10 @@ function deleteAttachmentsNotPresentInNote (markdownContent, storageKey, noteKey
280283
attachmentsInNoteOnlyFileNames.push(attachmentsInNote[i].replace(new RegExp(STORAGE_FOLDER_PLACEHOLDER + escapeStringRegexp(path.sep) + noteKey + escapeStringRegexp(path.sep), 'g'), ''))
281284
}
282285
}
283-
284286
if (fs.existsSync(attachmentFolder)) {
285287
fs.readdir(attachmentFolder, (err, files) => {
286288
if (err) {
287-
console.error("Error reading directory '" + attachmentFolder + "'. Error:")
289+
console.error('Error reading directory \'' + attachmentFolder + '\'. Error:')
288290
console.error(err)
289291
return
290292
}
@@ -293,17 +295,17 @@ function deleteAttachmentsNotPresentInNote (markdownContent, storageKey, noteKey
293295
const absolutePathOfFile = path.join(targetStorage.path, DESTINATION_FOLDER, noteKey, file)
294296
fs.unlink(absolutePathOfFile, (err) => {
295297
if (err) {
296-
console.error("Could not delete '%s'", absolutePathOfFile)
298+
console.error('Could not delete \'%s\'', absolutePathOfFile)
297299
console.error(err)
298300
return
299301
}
300-
console.info("File '" + absolutePathOfFile + "' deleted because it was not included in the content of the note")
302+
console.info('File \'' + absolutePathOfFile + '\' deleted because it was not included in the content of the note')
301303
})
302304
}
303305
})
304306
})
305307
} else {
306-
console.info("Attachment folder ('" + attachmentFolder + "') did not exist..")
308+
console.debug('Attachment folder (\'' + attachmentFolder + '\') did not exist..')
307309
}
308310
}
309311

tests/dataApi/attachmentManagement.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,38 @@ it('should test that deleteAttachmentsNotPresentInNote does not delete reference
339339
expect(fsUnlinkCallArguments.includes(path.join(attachmentFolderPath, dummyFilesInFolder[0]))).toBe(false)
340340
})
341341

342+
it('should test that deleteAttachmentsNotPresentInNote does nothing if noteKey, storageKey or noteContent was null', function () {
343+
const noteKey = null
344+
const storageKey = null
345+
const markdownContent = ''
346+
347+
findStorage.findStorage = jest.fn()
348+
fs.existsSync = jest.fn()
349+
fs.readdir = jest.fn()
350+
fs.unlink = jest.fn()
351+
352+
systemUnderTest.deleteAttachmentsNotPresentInNote(markdownContent, storageKey, noteKey)
353+
expect(fs.existsSync).not.toHaveBeenCalled()
354+
expect(fs.readdir).not.toHaveBeenCalled()
355+
expect(fs.unlink).not.toHaveBeenCalled()
356+
})
357+
358+
it('should test that deleteAttachmentsNotPresentInNote does nothing if noteKey, storageKey or noteContent was undefined', function () {
359+
const noteKey = undefined
360+
const storageKey = undefined
361+
const markdownContent = ''
362+
363+
findStorage.findStorage = jest.fn()
364+
fs.existsSync = jest.fn()
365+
fs.readdir = jest.fn()
366+
fs.unlink = jest.fn()
367+
368+
systemUnderTest.deleteAttachmentsNotPresentInNote(markdownContent, storageKey, noteKey)
369+
expect(fs.existsSync).not.toHaveBeenCalled()
370+
expect(fs.readdir).not.toHaveBeenCalled()
371+
expect(fs.unlink).not.toHaveBeenCalled()
372+
})
373+
342374
it('should test that moveAttachments moves attachments only if the source folder existed', function () {
343375
fse.existsSync = jest.fn(() => false)
344376
fse.moveSync = jest.fn()
@@ -403,6 +435,8 @@ it('should test that cloneAttachments modifies the content of the new note corre
403435
'![' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + oldNote.key + path.sep + 'image.jpg](imageName}) \n' +
404436
'[' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + oldNote.key + path.sep + 'pdf.pdf](pdf})'
405437
newNote.content = testInput
438+
findStorage.findStorage = jest.fn()
439+
findStorage.findStorage.mockReturnValue({path: 'dummyStoragePath'})
406440

407441
const expectedOutput =
408442
'Test input' +

0 commit comments

Comments
 (0)