Skip to content

Commit 8afa373

Browse files
committed
Attachments should be visible immediately
2 parents 0ca4e6c + 16794b9 commit 8afa373

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

browser/main/lib/dataApi/attachmentManagement.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function copyAttachment (sourceFilePath, storageKey, noteKey, useRandomName = tr
4242

4343
const targetStorage = findStorage.findStorage(storageKey)
4444

45-
const inputFile = fs.createReadStream(sourceFilePath)
45+
const inputFileStream = fs.createReadStream(sourceFilePath)
4646
let destinationName
4747
if (useRandomName) {
4848
destinationName = `${uniqueSlug()}${path.extname(sourceFilePath)}`
@@ -52,8 +52,10 @@ function copyAttachment (sourceFilePath, storageKey, noteKey, useRandomName = tr
5252
const destinationDir = path.join(targetStorage.path, DESTINATION_FOLDER, noteKey)
5353
createAttachmentDestinationFolder(targetStorage.path, noteKey)
5454
const outputFile = fs.createWriteStream(path.join(destinationDir, destinationName))
55-
inputFile.pipe(outputFile)
56-
resolve(destinationName)
55+
inputFileStream.pipe(outputFile)
56+
inputFileStream.on('end', () => {
57+
resolve(destinationName)
58+
})
5759
} catch (e) {
5860
return reject(e)
5961
}
@@ -149,7 +151,7 @@ function handlePastImageEvent (codeEditor, storageKey, noteKey, dataTransferItem
149151
base64data = reader.result.replace(/^data:image\/png;base64,/, '')
150152
base64data += base64data.replace('+', ' ')
151153
const binaryData = new Buffer(base64data, 'base64').toString('binary')
152-
fs.writeFile(imagePath, binaryData, 'binary')
154+
fs.writeFileSync(imagePath, binaryData, 'binary')
153155
const imageMd = generateAttachmentMarkdown(imageName, imagePath, true)
154156
codeEditor.insertAttachmentMd(imageMd)
155157
}
@@ -174,7 +176,7 @@ function getAttachmentsInContent (markdownContent) {
174176
* @returns {String[]} Absolute paths of the referenced attachments
175177
*/
176178
function getAbsolutePathsOfAttachmentsInContent (markdownContent, storagePath) {
177-
const temp = getAttachmentsInContent(markdownContent)
179+
const temp = getAttachmentsInContent(markdownContent) || []
178180
const result = []
179181
for (const relativePath of temp) {
180182
result.push(relativePath.replace(new RegExp(STORAGE_FOLDER_PLACEHOLDER, 'g'), path.join(storagePath, DESTINATION_FOLDER)))

tests/dataApi/attachmentManagement.test.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,13 @@ it('should test that copyAttachment works correctly assuming correct working of
5050
const noteKey = 'noteKey'
5151
const dummyUniquePath = 'dummyPath'
5252
const dummyStorage = {path: 'dummyStoragePath'}
53+
const dummyReadStream = {}
5354

55+
dummyReadStream.pipe = jest.fn()
56+
dummyReadStream.on = jest.fn((event, callback) => { callback() })
5457
fs.existsSync = jest.fn()
5558
fs.existsSync.mockReturnValue(true)
56-
fs.createReadStream = jest.fn()
57-
fs.createReadStream.mockReturnValue({pipe: jest.fn()})
59+
fs.createReadStream = jest.fn(() => dummyReadStream)
5860
fs.createWriteStream = jest.fn()
5961

6062
findStorage.findStorage = jest.fn()
@@ -77,7 +79,11 @@ it('should test that copyAttachment creates a new folder if the attachment folde
7779
const noteKey = 'noteKey'
7880
const attachmentFolderPath = path.join(dummyStorage.path, systemUnderTest.DESTINATION_FOLDER)
7981
const attachmentFolderNoteKyPath = path.join(dummyStorage.path, systemUnderTest.DESTINATION_FOLDER, noteKey)
82+
const dummyReadStream = {}
8083

84+
dummyReadStream.pipe = jest.fn()
85+
dummyReadStream.on = jest.fn()
86+
fs.createReadStream = jest.fn(() => dummyReadStream)
8187
fs.existsSync = jest.fn()
8288
fs.existsSync.mockReturnValueOnce(true)
8389
fs.existsSync.mockReturnValueOnce(false)
@@ -99,7 +105,11 @@ it('should test that copyAttachment creates a new folder if the attachment folde
99105

100106
it('should test that copyAttachment don\'t uses a random file name if not intended ', function () {
101107
const dummyStorage = {path: 'dummyStoragePath'}
108+
const dummyReadStream = {}
102109

110+
dummyReadStream.pipe = jest.fn()
111+
dummyReadStream.on = jest.fn()
112+
fs.createReadStream = jest.fn(() => dummyReadStream)
103113
fs.existsSync = jest.fn()
104114
fs.existsSync.mockReturnValueOnce(true)
105115
fs.existsSync.mockReturnValueOnce(false)

0 commit comments

Comments
 (0)