Skip to content

Commit bfcf349

Browse files
committed
Attachment management should only become active on cloning notes if the note was of type MARKDOWN_NOTE -> No Stacktraces otherwise!
1 parent cd6233a commit bfcf349

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

browser/main/lib/dataApi/attachmentManagement.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -288,20 +288,24 @@ function deleteAttachmentsNotPresentInNote (markdownContent, storageKey, noteKey
288288
* @param newNote Clone of the note
289289
*/
290290
function cloneAttachments (oldNote, newNote) {
291-
const oldStorage = findStorage.findStorage(oldNote.storage)
292-
const newStorage = findStorage.findStorage(newNote.storage)
293-
const attachmentsPaths = getAbsolutePathsOfAttachmentsInContent(oldNote.content, oldStorage.path) || []
291+
if (newNote.type === 'MARKDOWN_NOTE') {
292+
const oldStorage = findStorage.findStorage(oldNote.storage)
293+
const newStorage = findStorage.findStorage(newNote.storage)
294+
const attachmentsPaths = getAbsolutePathsOfAttachmentsInContent(oldNote.content, oldStorage.path) || []
294295

295-
const destinationFolder = path.join(newStorage.path, DESTINATION_FOLDER, newNote.key)
296-
if (!sander.existsSync(destinationFolder)) {
297-
sander.mkdirSync(destinationFolder)
298-
}
296+
const destinationFolder = path.join(newStorage.path, DESTINATION_FOLDER, newNote.key)
297+
if (!sander.existsSync(destinationFolder)) {
298+
sander.mkdirSync(destinationFolder)
299+
}
299300

300-
for (const attachment of attachmentsPaths) {
301-
const destination = path.join(newStorage.path, DESTINATION_FOLDER, newNote.key, path.basename(attachment))
302-
sander.copyFileSync(attachment).to(destination)
301+
for (const attachment of attachmentsPaths) {
302+
const destination = path.join(newStorage.path, DESTINATION_FOLDER, newNote.key, path.basename(attachment))
303+
sander.copyFileSync(attachment).to(destination)
304+
}
305+
newNote.content = replaceNoteKeyWithNewNoteKey(newNote.content, oldNote.key, newNote.key)
306+
} else {
307+
console.debug('Cloning of the attachment was skipped since it only works for MARKDOWN_NOTEs')
303308
}
304-
newNote.content = replaceNoteKeyWithNewNoteKey(newNote.content, oldNote.key, newNote.key)
305309
}
306310

307311
module.exports = {

tests/dataApi/attachmentManagement.test.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,8 @@ it('should test that moveAttachments returns a correct modified content version'
396396
})
397397

398398
it('should test that cloneAttachments modifies the content of the new note correctly', function () {
399-
const oldNote = {key: 'oldNoteKey', content: 'oldNoteContent', storage: 'storageKey'}
400-
const newNote = {key: 'newNoteKey', content: 'oldNoteContent', storage: 'storageKey'}
399+
const oldNote = {key: 'oldNoteKey', content: 'oldNoteContent', storage: 'storageKey', type: 'MARKDOWN_NOTE'}
400+
const newNote = {key: 'newNoteKey', content: 'oldNoteContent', storage: 'storageKey', type: 'MARKDOWN_NOTE'}
401401
const testInput =
402402
'Test input' +
403403
'![' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + oldNote.key + path.sep + 'image.jpg](imageName}) \n' +
@@ -418,8 +418,8 @@ it('should test that cloneAttachments finds all attachments and copies them to t
418418
const storagePathNew = 'storagePathNew'
419419
const dummyStorageOld = {path: storagePathOld}
420420
const dummyStorageNew = {path: storagePathNew}
421-
const oldNote = {key: 'oldNoteKey', content: 'oldNoteContent', storage: 'storageKeyOldNote'}
422-
const newNote = {key: 'newNoteKey', content: 'oldNoteContent', storage: 'storageKeyNewNote'}
421+
const oldNote = {key: 'oldNoteKey', content: 'oldNoteContent', storage: 'storageKeyOldNote', type: 'MARKDOWN_NOTE'}
422+
const newNote = {key: 'newNoteKey', content: 'oldNoteContent', storage: 'storageKeyNewNote', type: 'MARKDOWN_NOTE'}
423423
const testInput =
424424
'Test input' +
425425
'![' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + oldNote.key + path.sep + 'image.jpg](imageName}) \n' +
@@ -451,3 +451,19 @@ it('should test that cloneAttachments finds all attachments and copies them to t
451451
expect(sander.copyFileSync.mock.calls[1][0]).toBe(pathAttachmentTwoFrom)
452452
expect(copyFileSyncResp.to.mock.calls[1][0]).toBe(pathAttachmentTwoTo)
453453
})
454+
455+
it('should test that cloneAttachments finds all attachments and copies them to the new location', function () {
456+
const oldNote = {key: 'oldNoteKey', content: 'oldNoteContent', storage: 'storageKeyOldNote', type: 'SOMETHING_ELSE'}
457+
const newNote = {key: 'newNoteKey', content: 'oldNoteContent', storage: 'storageKeyNewNote', type: 'SOMETHING_ELSE'}
458+
const testInput = 'Test input'
459+
oldNote.content = testInput
460+
newNote.content = testInput
461+
462+
sander.copyFileSync = jest.fn()
463+
findStorage.findStorage = jest.fn()
464+
465+
systemUnderTest.cloneAttachments(oldNote, newNote)
466+
467+
expect(findStorage.findStorage).not.toHaveBeenCalled()
468+
expect(sander.copyFileSync).not.toHaveBeenCalled()
469+
})

0 commit comments

Comments
 (0)