mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-13 09:46:22 +00:00
Merge branch 'master' into OnBlur_Throws_Exceptions_On_Snippet_Notes
This commit is contained in:
@@ -42,7 +42,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)}`
|
||||
@@ -52,8 +52,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)
|
||||
}
|
||||
@@ -149,7 +151,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')
|
||||
const imageMd = generateAttachmentMarkdown(imageName, imagePath, true)
|
||||
codeEditor.insertAttachmentMd(imageMd)
|
||||
}
|
||||
@@ -174,7 +176,7 @@ function getAttachmentsInContent (markdownContent) {
|
||||
* @returns {String[]} Absolute paths of the referenced attachments
|
||||
*/
|
||||
function getAbsolutePathsOfAttachmentsInContent (markdownContent, storagePath) {
|
||||
const temp = getAttachmentsInContent(markdownContent)
|
||||
const temp = getAttachmentsInContent(markdownContent) || []
|
||||
const result = []
|
||||
for (const relativePath of temp) {
|
||||
result.push(relativePath.replace(new RegExp(STORAGE_FOLDER_PLACEHOLDER, 'g'), path.join(storagePath, DESTINATION_FOLDER)))
|
||||
@@ -198,8 +200,19 @@ function moveAttachments (oldPath, newPath, noteKey, newNoteKey, noteContent) {
|
||||
if (fse.existsSync(src)) {
|
||||
fse.moveSync(src, dest)
|
||||
}
|
||||
return replaceNoteKeyWithNewNoteKey(noteContent, noteKey, newNoteKey)
|
||||
}
|
||||
|
||||
/**
|
||||
* Modifies the given content so that in all attachment references the oldNoteKey is replaced by the new one
|
||||
* @param noteContent content that should be modified
|
||||
* @param oldNoteKey note key to be replaced
|
||||
* @param newNoteKey note key serving as a replacement
|
||||
* @returns {String} modified note content
|
||||
*/
|
||||
function replaceNoteKeyWithNewNoteKey (noteContent, oldNoteKey, newNoteKey) {
|
||||
if (noteContent) {
|
||||
return noteContent.replace(new RegExp(STORAGE_FOLDER_PLACEHOLDER + escapeStringRegexp(path.sep) + noteKey, 'g'), path.join(STORAGE_FOLDER_PLACEHOLDER, newNoteKey))
|
||||
return noteContent.replace(new RegExp(STORAGE_FOLDER_PLACEHOLDER + escapeStringRegexp(path.sep) + oldNoteKey, 'g'), path.join(STORAGE_FOLDER_PLACEHOLDER, newNoteKey))
|
||||
}
|
||||
return noteContent
|
||||
}
|
||||
@@ -270,6 +283,33 @@ 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 oldNote Note that is being cloned
|
||||
* @param newNote Clone of the note
|
||||
*/
|
||||
function cloneAttachments (oldNote, newNote) {
|
||||
if (newNote.type === 'MARKDOWN_NOTE') {
|
||||
const oldStorage = findStorage.findStorage(oldNote.storage)
|
||||
const newStorage = findStorage.findStorage(newNote.storage)
|
||||
const attachmentsPaths = getAbsolutePathsOfAttachmentsInContent(oldNote.content, oldStorage.path) || []
|
||||
|
||||
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(newStorage.path, DESTINATION_FOLDER, newNote.key, path.basename(attachment))
|
||||
sander.copyFileSync(attachment).to(destination)
|
||||
}
|
||||
newNote.content = replaceNoteKeyWithNewNoteKey(newNote.content, oldNote.key, newNote.key)
|
||||
} else {
|
||||
console.debug('Cloning of the attachment was skipped since it only works for MARKDOWN_NOTEs')
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
copyAttachment,
|
||||
fixLocalURLS,
|
||||
@@ -282,6 +322,7 @@ module.exports = {
|
||||
deleteAttachmentFolder,
|
||||
deleteAttachmentsNotPresentInNote,
|
||||
moveAttachments,
|
||||
cloneAttachments,
|
||||
STORAGE_FOLDER_PLACEHOLDER,
|
||||
DESTINATION_FOLDER
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user