From a76aed2d4efd31f99e49230717808479280544c0 Mon Sep 17 00:00:00 2001 From: ehhc Date: Sat, 21 Apr 2018 14:49:43 +0200 Subject: [PATCH 1/2] Fixes #1822 --- browser/main/lib/dataApi/attachmentManagement.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/browser/main/lib/dataApi/attachmentManagement.js b/browser/main/lib/dataApi/attachmentManagement.js index 6d4d7406..d1e57b75 100644 --- a/browser/main/lib/dataApi/attachmentManagement.js +++ b/browser/main/lib/dataApi/attachmentManagement.js @@ -39,7 +39,7 @@ function copyAttachment (sourceFilePath, storageKey, noteKey, useRandomName = tr const targetStorage = findStorage.findStorage(storageKey) - const inputFile = fs.createReadStream(sourceFilePath) + const inputFileStream = fs.createReadStream(sourceFilePath) let destinationName if (useRandomName) { destinationName = `${uniqueSlug()}${path.extname(sourceFilePath)}` @@ -49,8 +49,10 @@ function copyAttachment (sourceFilePath, storageKey, noteKey, useRandomName = tr const destinationDir = path.join(targetStorage.path, DESTINATION_FOLDER, noteKey) createAttachmentDestinationFolder(targetStorage.path, noteKey) const outputFile = fs.createWriteStream(path.join(destinationDir, destinationName)) - inputFile.pipe(outputFile) - resolve(destinationName) + inputFileStream.pipe(outputFile) + inputFileStream.on('end', () => { + resolve(destinationName) + }) } catch (e) { return reject(e) } @@ -146,7 +148,7 @@ function handlePastImageEvent (codeEditor, storageKey, noteKey, dataTransferItem base64data = reader.result.replace(/^data:image\/png;base64,/, '') base64data += base64data.replace('+', ' ') const binaryData = new Buffer(base64data, 'base64').toString('binary') - fs.writeFile(imagePath, binaryData, 'binary') + fs.writeFileSync(imagePath, binaryData, 'binary') let imageMd = generateAttachmentMarkdown(imageName, imagePath, true) codeEditor.insertAttachmentMd(imageMd) } From 16794b9d78b8b30fc664b166bb9d56a4a76d7987 Mon Sep 17 00:00:00 2001 From: ehhc Date: Sat, 21 Apr 2018 16:32:27 +0200 Subject: [PATCH 2/2] Fixes #1822 -> fix for the broken tests --- tests/dataApi/attachmentManagement.test.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tests/dataApi/attachmentManagement.test.js b/tests/dataApi/attachmentManagement.test.js index 7e97cf80..1f40cd60 100644 --- a/tests/dataApi/attachmentManagement.test.js +++ b/tests/dataApi/attachmentManagement.test.js @@ -48,11 +48,13 @@ it('should test that copyAttachment works correctly assuming correct working of const noteKey = 'noteKey' const dummyUniquePath = 'dummyPath' const dummyStorage = {path: 'dummyStoragePath'} + const dummyReadStream = {} + dummyReadStream.pipe = jest.fn() + dummyReadStream.on = jest.fn((event, callback) => { callback() }) fs.existsSync = jest.fn() fs.existsSync.mockReturnValue(true) - fs.createReadStream = jest.fn() - fs.createReadStream.mockReturnValue({pipe: jest.fn()}) + fs.createReadStream = jest.fn(() => dummyReadStream) fs.createWriteStream = jest.fn() findStorage.findStorage = jest.fn() @@ -75,7 +77,11 @@ it('should test that copyAttachment creates a new folder if the attachment folde const noteKey = 'noteKey' const attachmentFolderPath = path.join(dummyStorage.path, systemUnderTest.DESTINATION_FOLDER) const attachmentFolderNoteKyPath = path.join(dummyStorage.path, systemUnderTest.DESTINATION_FOLDER, noteKey) + const dummyReadStream = {} + dummyReadStream.pipe = jest.fn() + dummyReadStream.on = jest.fn() + fs.createReadStream = jest.fn(() => dummyReadStream) fs.existsSync = jest.fn() fs.existsSync.mockReturnValueOnce(true) fs.existsSync.mockReturnValueOnce(false) @@ -97,7 +103,11 @@ it('should test that copyAttachment creates a new folder if the attachment folde it('should test that copyAttachment don\'t uses a random file name if not intended ', function () { const dummyStorage = {path: 'dummyStoragePath'} + const dummyReadStream = {} + dummyReadStream.pipe = jest.fn() + dummyReadStream.on = jest.fn() + fs.createReadStream = jest.fn(() => dummyReadStream) fs.existsSync = jest.fn() fs.existsSync.mockReturnValueOnce(true) fs.existsSync.mockReturnValueOnce(false)