1
0
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:
ehhc
2018-05-20 15:49:15 +02:00
parent f76224bd17
commit cd6233a3d7
3 changed files with 26 additions and 23 deletions

View File

@@ -664,7 +664,7 @@ class NoteList extends React.Component {
content: firstNote.content content: firstNote.content
}) })
.then((note) => { .then((note) => {
attachmentManagement.cloneAttachments(storage.key, firstNote, note) attachmentManagement.cloneAttachments(firstNote, note)
return note return note
}) })
.then((note) => { .then((note) => {

View File

@@ -284,21 +284,21 @@ function deleteAttachmentsNotPresentInNote (markdownContent, storageKey, noteKey
/** /**
* Clones the attachments of a given note. * 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. * 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 oldNote Note that is being cloned
* @param newNote Clone of the note * @param newNote Clone of the note
*/ */
function cloneAttachments (storageKey, oldNote, newNote) { function cloneAttachments (oldNote, newNote) {
const storage = findStorage.findStorage(storageKey) const oldStorage = findStorage.findStorage(oldNote.storage)
const attachmentsPaths = getAbsolutePathsOfAttachmentsInContent(oldNote.content, storage.path) || [] 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)) { if (!sander.existsSync(destinationFolder)) {
sander.mkdirSync(destinationFolder) sander.mkdirSync(destinationFolder)
} }
for (const attachment of attachmentsPaths) { 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) sander.copyFileSync(attachment).to(destination)
} }
newNote.content = replaceNoteKeyWithNewNoteKey(newNote.content, oldNote.key, newNote.key) newNote.content = replaceNoteKeyWithNewNoteKey(newNote.content, oldNote.key, newNote.key)

View File

@@ -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 () { it('should test that cloneAttachments modifies the content of the new note correctly', function () {
const storageKey = 'storageKey' const oldNote = {key: 'oldNoteKey', content: 'oldNoteContent', storage: 'storageKey'}
const oldNote = {key: 'oldNoteKey', content: 'oldNoteContent'} const newNote = {key: 'newNoteKey', content: 'oldNoteContent', storage: 'storageKey'}
const newNote = {key: 'newNoteKey', content: 'oldNoteContent'}
const testInput = const testInput =
'Test input' + 'Test input' +
'![' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + oldNote.key + path.sep + 'image.jpg](imageName}) \n' + '![' + 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
'Test input' + 'Test input' +
'![' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + newNote.key + path.sep + 'image.jpg](imageName}) \n' + '![' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + newNote.key + path.sep + 'image.jpg](imageName}) \n' +
'[' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + newNote.key + path.sep + 'pdf.pdf](pdf})' '[' + 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) expect(newNote.content).toBe(expectedOutput)
}) })
it('should test that cloneAttachments finds all attachments and copies them to the new location', function () { it('should test that cloneAttachments finds all attachments and copies them to the new location', function () {
const storageKey = 'storageKey' const storagePathOld = 'storagePathOld'
const storagePath = 'storagePath' const storagePathNew = 'storagePathNew'
const dummyStorage = {path: storagePath} const dummyStorageOld = {path: storagePathOld}
const oldNote = {key: 'oldNoteKey', content: 'oldNoteContent'} const dummyStorageNew = {path: storagePathNew}
const newNote = {key: 'newNoteKey', content: 'oldNoteContent'} const oldNote = {key: 'oldNoteKey', content: 'oldNoteContent', storage: 'storageKeyOldNote'}
const newNote = {key: 'newNoteKey', content: 'oldNoteContent', storage: 'storageKeyNewNote'}
const testInput = const testInput =
'Test input' + 'Test input' +
'![' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + oldNote.key + path.sep + 'image.jpg](imageName}) \n' + '![' + 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
const copyFileSyncResp = {to: jest.fn()} const copyFileSyncResp = {to: jest.fn()}
sander.copyFileSync = jest.fn() sander.copyFileSync = jest.fn()
sander.copyFileSync.mockReturnValue(copyFileSyncResp) 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 pathAttachmentOneFrom = path.join(storagePathOld, systemUnderTest.DESTINATION_FOLDER, oldNote.key, 'image.jpg')
const pathAttachmentOneTo = path.join(storagePath, systemUnderTest.DESTINATION_FOLDER, newNote.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 pathAttachmentTwoFrom = path.join(storagePathOld, systemUnderTest.DESTINATION_FOLDER, oldNote.key, 'pdf.pdf')
const pathAttachmentTwoTo = path.join(storagePath, systemUnderTest.DESTINATION_FOLDER, newNote.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(sander.copyFileSync).toHaveBeenCalledTimes(2)
expect(copyFileSyncResp.to).toHaveBeenCalledTimes(2) expect(copyFileSyncResp.to).toHaveBeenCalledTimes(2)
expect(sander.copyFileSync.mock.calls[0][0]).toBe(pathAttachmentOneFrom) expect(sander.copyFileSync.mock.calls[0][0]).toBe(pathAttachmentOneFrom)