1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-13 01:36:22 +00:00

Attachments should be visible immediately

This commit is contained in:
ehhc
2018-05-20 13:54:24 +02:00
2 changed files with 19 additions and 7 deletions

View File

@@ -42,7 +42,7 @@ function copyAttachment (sourceFilePath, storageKey, noteKey, useRandomName = tr
const targetStorage = findStorage.findStorage(storageKey) const targetStorage = findStorage.findStorage(storageKey)
const inputFile = fs.createReadStream(sourceFilePath) const inputFileStream = fs.createReadStream(sourceFilePath)
let destinationName let destinationName
if (useRandomName) { if (useRandomName) {
destinationName = `${uniqueSlug()}${path.extname(sourceFilePath)}` destinationName = `${uniqueSlug()}${path.extname(sourceFilePath)}`
@@ -52,8 +52,10 @@ function copyAttachment (sourceFilePath, storageKey, noteKey, useRandomName = tr
const destinationDir = path.join(targetStorage.path, DESTINATION_FOLDER, noteKey) const destinationDir = path.join(targetStorage.path, DESTINATION_FOLDER, noteKey)
createAttachmentDestinationFolder(targetStorage.path, noteKey) createAttachmentDestinationFolder(targetStorage.path, noteKey)
const outputFile = fs.createWriteStream(path.join(destinationDir, destinationName)) const outputFile = fs.createWriteStream(path.join(destinationDir, destinationName))
inputFile.pipe(outputFile) inputFileStream.pipe(outputFile)
resolve(destinationName) inputFileStream.on('end', () => {
resolve(destinationName)
})
} catch (e) { } catch (e) {
return reject(e) return reject(e)
} }
@@ -149,7 +151,7 @@ function handlePastImageEvent (codeEditor, storageKey, noteKey, dataTransferItem
base64data = reader.result.replace(/^data:image\/png;base64,/, '') base64data = reader.result.replace(/^data:image\/png;base64,/, '')
base64data += base64data.replace('+', ' ') base64data += base64data.replace('+', ' ')
const binaryData = new Buffer(base64data, 'base64').toString('binary') const binaryData = new Buffer(base64data, 'base64').toString('binary')
fs.writeFile(imagePath, binaryData, 'binary') fs.writeFileSync(imagePath, binaryData, 'binary')
const imageMd = generateAttachmentMarkdown(imageName, imagePath, true) const imageMd = generateAttachmentMarkdown(imageName, imagePath, true)
codeEditor.insertAttachmentMd(imageMd) codeEditor.insertAttachmentMd(imageMd)
} }
@@ -174,7 +176,7 @@ function getAttachmentsInContent (markdownContent) {
* @returns {String[]} Absolute paths of the referenced attachments * @returns {String[]} Absolute paths of the referenced attachments
*/ */
function getAbsolutePathsOfAttachmentsInContent (markdownContent, storagePath) { function getAbsolutePathsOfAttachmentsInContent (markdownContent, storagePath) {
const temp = getAttachmentsInContent(markdownContent) const temp = getAttachmentsInContent(markdownContent) || []
const result = [] const result = []
for (const relativePath of temp) { for (const relativePath of temp) {
result.push(relativePath.replace(new RegExp(STORAGE_FOLDER_PLACEHOLDER, 'g'), path.join(storagePath, DESTINATION_FOLDER))) result.push(relativePath.replace(new RegExp(STORAGE_FOLDER_PLACEHOLDER, 'g'), path.join(storagePath, DESTINATION_FOLDER)))

View File

@@ -50,11 +50,13 @@ it('should test that copyAttachment works correctly assuming correct working of
const noteKey = 'noteKey' const noteKey = 'noteKey'
const dummyUniquePath = 'dummyPath' const dummyUniquePath = 'dummyPath'
const dummyStorage = {path: 'dummyStoragePath'} const dummyStorage = {path: 'dummyStoragePath'}
const dummyReadStream = {}
dummyReadStream.pipe = jest.fn()
dummyReadStream.on = jest.fn((event, callback) => { callback() })
fs.existsSync = jest.fn() fs.existsSync = jest.fn()
fs.existsSync.mockReturnValue(true) fs.existsSync.mockReturnValue(true)
fs.createReadStream = jest.fn() fs.createReadStream = jest.fn(() => dummyReadStream)
fs.createReadStream.mockReturnValue({pipe: jest.fn()})
fs.createWriteStream = jest.fn() fs.createWriteStream = jest.fn()
findStorage.findStorage = jest.fn() findStorage.findStorage = jest.fn()
@@ -77,7 +79,11 @@ it('should test that copyAttachment creates a new folder if the attachment folde
const noteKey = 'noteKey' const noteKey = 'noteKey'
const attachmentFolderPath = path.join(dummyStorage.path, systemUnderTest.DESTINATION_FOLDER) const attachmentFolderPath = path.join(dummyStorage.path, systemUnderTest.DESTINATION_FOLDER)
const attachmentFolderNoteKyPath = path.join(dummyStorage.path, systemUnderTest.DESTINATION_FOLDER, noteKey) 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 = jest.fn()
fs.existsSync.mockReturnValueOnce(true) fs.existsSync.mockReturnValueOnce(true)
fs.existsSync.mockReturnValueOnce(false) fs.existsSync.mockReturnValueOnce(false)
@@ -99,7 +105,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 () { it('should test that copyAttachment don\'t uses a random file name if not intended ', function () {
const dummyStorage = {path: 'dummyStoragePath'} 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 = jest.fn()
fs.existsSync.mockReturnValueOnce(true) fs.existsSync.mockReturnValueOnce(true)
fs.existsSync.mockReturnValueOnce(false) fs.existsSync.mockReturnValueOnce(false)