Skip to content

Commit cd6233a

Browse files
committed
Cloning of a note should also clone its attachments -> works if the notes are in different storages now
1 parent f76224b commit cd6233a

File tree

3 files changed

+26
-23
lines changed

3 files changed

+26
-23
lines changed

browser/main/NoteList/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ class NoteList extends React.Component {
664664
content: firstNote.content
665665
})
666666
.then((note) => {
667-
attachmentManagement.cloneAttachments(storage.key, firstNote, note)
667+
attachmentManagement.cloneAttachments(firstNote, note)
668668
return note
669669
})
670670
.then((note) => {

browser/main/lib/dataApi/attachmentManagement.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -284,21 +284,21 @@ function deleteAttachmentsNotPresentInNote (markdownContent, storageKey, noteKey
284284
/**
285285
* Clones the attachments of a given note.
286286
* Copies the attachments to their new destination and updates the content of the new note so that the attachment-links again point to the correct destination.
287-
* @param storageKey Key of the current storage
288287
* @param oldNote Note that is being cloned
289288
* @param newNote Clone of the note
290289
*/
291-
function cloneAttachments (storageKey, oldNote, newNote) {
292-
const storage = findStorage.findStorage(storageKey)
293-
const attachmentsPaths = getAbsolutePathsOfAttachmentsInContent(oldNote.content, storage.path) || []
290+
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) || []
294294

295-
const destinationFolder = path.join(storage.path, DESTINATION_FOLDER, newNote.key)
295+
const destinationFolder = path.join(newStorage.path, DESTINATION_FOLDER, newNote.key)
296296
if (!sander.existsSync(destinationFolder)) {
297297
sander.mkdirSync(destinationFolder)
298298
}
299299

300300
for (const attachment of attachmentsPaths) {
301-
const destination = path.join(storage.path, DESTINATION_FOLDER, newNote.key, path.basename(attachment))
301+
const destination = path.join(newStorage.path, DESTINATION_FOLDER, newNote.key, path.basename(attachment))
302302
sander.copyFileSync(attachment).to(destination)
303303
}
304304
newNote.content = replaceNoteKeyWithNewNoteKey(newNote.content, oldNote.key, newNote.key)

tests/dataApi/attachmentManagement.test.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -396,9 +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 storageKey = 'storageKey'
400-
const oldNote = {key: 'oldNoteKey', content: 'oldNoteContent'}
401-
const newNote = {key: 'newNoteKey', content: 'oldNoteContent'}
399+
const oldNote = {key: 'oldNoteKey', content: 'oldNoteContent', storage: 'storageKey'}
400+
const newNote = {key: 'newNoteKey', content: 'oldNoteContent', storage: 'storageKey'}
402401
const testInput =
403402
'Test input' +
404403
'![' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + oldNote.key + path.sep + 'image.jpg](imageName}) \n' +
@@ -409,17 +408,18 @@ it('should test that cloneAttachments modifies the content of the new note corre
409408
'Test input' +
410409
'![' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + newNote.key + path.sep + 'image.jpg](imageName}) \n' +
411410
'[' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + newNote.key + path.sep + 'pdf.pdf](pdf})'
412-
systemUnderTest.cloneAttachments(storageKey, oldNote, newNote)
411+
systemUnderTest.cloneAttachments(oldNote, newNote)
413412

414413
expect(newNote.content).toBe(expectedOutput)
415414
})
416415

417416
it('should test that cloneAttachments finds all attachments and copies them to the new location', function () {
418-
const storageKey = 'storageKey'
419-
const storagePath = 'storagePath'
420-
const dummyStorage = {path: storagePath}
421-
const oldNote = {key: 'oldNoteKey', content: 'oldNoteContent'}
422-
const newNote = {key: 'newNoteKey', content: 'oldNoteContent'}
417+
const storagePathOld = 'storagePathOld'
418+
const storagePathNew = 'storagePathNew'
419+
const dummyStorageOld = {path: storagePathOld}
420+
const dummyStorageNew = {path: storagePathNew}
421+
const oldNote = {key: 'oldNoteKey', content: 'oldNoteContent', storage: 'storageKeyOldNote'}
422+
const newNote = {key: 'newNoteKey', content: 'oldNoteContent', storage: 'storageKeyNewNote'}
423423
const testInput =
424424
'Test input' +
425425
'![' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + oldNote.key + path.sep + 'image.jpg](imageName}) \n' +
@@ -430,17 +430,20 @@ it('should test that cloneAttachments finds all attachments and copies them to t
430430
const copyFileSyncResp = {to: jest.fn()}
431431
sander.copyFileSync = jest.fn()
432432
sander.copyFileSync.mockReturnValue(copyFileSyncResp)
433-
findStorage.findStorage = jest.fn(() => dummyStorage)
433+
findStorage.findStorage = jest.fn()
434+
findStorage.findStorage.mockReturnValueOnce(dummyStorageOld)
435+
findStorage.findStorage.mockReturnValue(dummyStorageNew)
434436

435-
const pathAttachmentOneFrom = path.join(storagePath, systemUnderTest.DESTINATION_FOLDER, oldNote.key, 'image.jpg')
436-
const pathAttachmentOneTo = path.join(storagePath, systemUnderTest.DESTINATION_FOLDER, newNote.key, 'image.jpg')
437+
const pathAttachmentOneFrom = path.join(storagePathOld, systemUnderTest.DESTINATION_FOLDER, oldNote.key, 'image.jpg')
438+
const pathAttachmentOneTo = path.join(storagePathNew, systemUnderTest.DESTINATION_FOLDER, newNote.key, 'image.jpg')
437439

438-
const pathAttachmentTwoFrom = path.join(storagePath, systemUnderTest.DESTINATION_FOLDER, oldNote.key, 'pdf.pdf')
439-
const pathAttachmentTwoTo = path.join(storagePath, systemUnderTest.DESTINATION_FOLDER, newNote.key, 'pdf.pdf')
440+
const pathAttachmentTwoFrom = path.join(storagePathOld, systemUnderTest.DESTINATION_FOLDER, oldNote.key, 'pdf.pdf')
441+
const pathAttachmentTwoTo = path.join(storagePathNew, systemUnderTest.DESTINATION_FOLDER, newNote.key, 'pdf.pdf')
440442

441-
systemUnderTest.cloneAttachments(storageKey, oldNote, newNote)
443+
systemUnderTest.cloneAttachments(oldNote, newNote)
442444

443-
expect(findStorage.findStorage).toHaveBeenCalledWith(storageKey)
445+
expect(findStorage.findStorage).toHaveBeenCalledWith(oldNote.storage)
446+
expect(findStorage.findStorage).toHaveBeenCalledWith(newNote.storage)
444447
expect(sander.copyFileSync).toHaveBeenCalledTimes(2)
445448
expect(copyFileSyncResp.to).toHaveBeenCalledTimes(2)
446449
expect(sander.copyFileSync.mock.calls[0][0]).toBe(pathAttachmentOneFrom)

0 commit comments

Comments
 (0)