mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-13 01:36:22 +00:00
Cloning of a note should also clone its attachments -> works if the notes are in different storages now
This commit is contained in:
@@ -664,7 +664,7 @@ class NoteList extends React.Component {
|
||||
content: firstNote.content
|
||||
})
|
||||
.then((note) => {
|
||||
attachmentManagement.cloneAttachments(storage.key, firstNote, note)
|
||||
attachmentManagement.cloneAttachments(firstNote, note)
|
||||
return note
|
||||
})
|
||||
.then((note) => {
|
||||
|
||||
@@ -284,21 +284,21 @@ function deleteAttachmentsNotPresentInNote (markdownContent, storageKey, noteKey
|
||||
/**
|
||||
* Clones the attachments of a given note.
|
||||
* 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.
|
||||
* @param storageKey Key of the current storage
|
||||
* @param oldNote Note that is being cloned
|
||||
* @param newNote Clone of the note
|
||||
*/
|
||||
function cloneAttachments (storageKey, oldNote, newNote) {
|
||||
const storage = findStorage.findStorage(storageKey)
|
||||
const attachmentsPaths = getAbsolutePathsOfAttachmentsInContent(oldNote.content, storage.path) || []
|
||||
function cloneAttachments (oldNote, newNote) {
|
||||
const oldStorage = findStorage.findStorage(oldNote.storage)
|
||||
const newStorage = findStorage.findStorage(newNote.storage)
|
||||
const attachmentsPaths = getAbsolutePathsOfAttachmentsInContent(oldNote.content, oldStorage.path) || []
|
||||
|
||||
const destinationFolder = path.join(storage.path, DESTINATION_FOLDER, newNote.key)
|
||||
const destinationFolder = path.join(newStorage.path, DESTINATION_FOLDER, newNote.key)
|
||||
if (!sander.existsSync(destinationFolder)) {
|
||||
sander.mkdirSync(destinationFolder)
|
||||
}
|
||||
|
||||
for (const attachment of attachmentsPaths) {
|
||||
const destination = path.join(storage.path, DESTINATION_FOLDER, newNote.key, path.basename(attachment))
|
||||
const destination = path.join(newStorage.path, DESTINATION_FOLDER, newNote.key, path.basename(attachment))
|
||||
sander.copyFileSync(attachment).to(destination)
|
||||
}
|
||||
newNote.content = replaceNoteKeyWithNewNoteKey(newNote.content, oldNote.key, newNote.key)
|
||||
|
||||
@@ -396,9 +396,8 @@ it('should test that moveAttachments returns a correct modified content version'
|
||||
})
|
||||
|
||||
it('should test that cloneAttachments modifies the content of the new note correctly', function () {
|
||||
const storageKey = 'storageKey'
|
||||
const oldNote = {key: 'oldNoteKey', content: 'oldNoteContent'}
|
||||
const newNote = {key: 'newNoteKey', content: 'oldNoteContent'}
|
||||
const oldNote = {key: 'oldNoteKey', content: 'oldNoteContent', storage: 'storageKey'}
|
||||
const newNote = {key: 'newNoteKey', content: 'oldNoteContent', storage: 'storageKey'}
|
||||
const testInput =
|
||||
'Test input' +
|
||||
' \n' +
|
||||
@@ -409,17 +408,18 @@ it('should test that cloneAttachments modifies the content of the new note corre
|
||||
'Test input' +
|
||||
' \n' +
|
||||
'[' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + newNote.key + path.sep + 'pdf.pdf](pdf})'
|
||||
systemUnderTest.cloneAttachments(storageKey, oldNote, newNote)
|
||||
systemUnderTest.cloneAttachments(oldNote, newNote)
|
||||
|
||||
expect(newNote.content).toBe(expectedOutput)
|
||||
})
|
||||
|
||||
it('should test that cloneAttachments finds all attachments and copies them to the new location', function () {
|
||||
const storageKey = 'storageKey'
|
||||
const storagePath = 'storagePath'
|
||||
const dummyStorage = {path: storagePath}
|
||||
const oldNote = {key: 'oldNoteKey', content: 'oldNoteContent'}
|
||||
const newNote = {key: 'newNoteKey', content: 'oldNoteContent'}
|
||||
const storagePathOld = 'storagePathOld'
|
||||
const storagePathNew = 'storagePathNew'
|
||||
const dummyStorageOld = {path: storagePathOld}
|
||||
const dummyStorageNew = {path: storagePathNew}
|
||||
const oldNote = {key: 'oldNoteKey', content: 'oldNoteContent', storage: 'storageKeyOldNote'}
|
||||
const newNote = {key: 'newNoteKey', content: 'oldNoteContent', storage: 'storageKeyNewNote'}
|
||||
const testInput =
|
||||
'Test input' +
|
||||
' \n' +
|
||||
@@ -430,17 +430,20 @@ it('should test that cloneAttachments finds all attachments and copies them to t
|
||||
const copyFileSyncResp = {to: jest.fn()}
|
||||
sander.copyFileSync = jest.fn()
|
||||
sander.copyFileSync.mockReturnValue(copyFileSyncResp)
|
||||
findStorage.findStorage = jest.fn(() => dummyStorage)
|
||||
findStorage.findStorage = jest.fn()
|
||||
findStorage.findStorage.mockReturnValueOnce(dummyStorageOld)
|
||||
findStorage.findStorage.mockReturnValue(dummyStorageNew)
|
||||
|
||||
const pathAttachmentOneFrom = path.join(storagePath, systemUnderTest.DESTINATION_FOLDER, oldNote.key, 'image.jpg')
|
||||
const pathAttachmentOneTo = path.join(storagePath, systemUnderTest.DESTINATION_FOLDER, newNote.key, 'image.jpg')
|
||||
const pathAttachmentOneFrom = path.join(storagePathOld, systemUnderTest.DESTINATION_FOLDER, oldNote.key, 'image.jpg')
|
||||
const pathAttachmentOneTo = path.join(storagePathNew, systemUnderTest.DESTINATION_FOLDER, newNote.key, 'image.jpg')
|
||||
|
||||
const pathAttachmentTwoFrom = path.join(storagePath, systemUnderTest.DESTINATION_FOLDER, oldNote.key, 'pdf.pdf')
|
||||
const pathAttachmentTwoTo = path.join(storagePath, systemUnderTest.DESTINATION_FOLDER, newNote.key, 'pdf.pdf')
|
||||
const pathAttachmentTwoFrom = path.join(storagePathOld, systemUnderTest.DESTINATION_FOLDER, oldNote.key, 'pdf.pdf')
|
||||
const pathAttachmentTwoTo = path.join(storagePathNew, systemUnderTest.DESTINATION_FOLDER, newNote.key, 'pdf.pdf')
|
||||
|
||||
systemUnderTest.cloneAttachments(storageKey, oldNote, newNote)
|
||||
systemUnderTest.cloneAttachments(oldNote, newNote)
|
||||
|
||||
expect(findStorage.findStorage).toHaveBeenCalledWith(storageKey)
|
||||
expect(findStorage.findStorage).toHaveBeenCalledWith(oldNote.storage)
|
||||
expect(findStorage.findStorage).toHaveBeenCalledWith(newNote.storage)
|
||||
expect(sander.copyFileSync).toHaveBeenCalledTimes(2)
|
||||
expect(copyFileSyncResp.to).toHaveBeenCalledTimes(2)
|
||||
expect(sander.copyFileSync.mock.calls[0][0]).toBe(pathAttachmentOneFrom)
|
||||
|
||||
Reference in New Issue
Block a user