From 4bc0cccb246e3146538b96f9c99c6c520bf3bfb6 Mon Sep 17 00:00:00 2001 From: ehhc Date: Mon, 21 May 2018 15:17:38 +0200 Subject: [PATCH 1/3] Migrate attachments from /images to /attachments -> fix #1941 --- browser/components/MarkdownPreview.js | 5 ++-- .../main/lib/dataApi/attachmentManagement.js | 30 +++++++++++++++++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/browser/components/MarkdownPreview.js b/browser/components/MarkdownPreview.js index 4fa3841a..104ea708 100755 --- a/browser/components/MarkdownPreview.js +++ b/browser/components/MarkdownPreview.js @@ -391,7 +391,7 @@ export default class MarkdownPreview extends React.Component { el.removeEventListener('click', this.linkClickHandler) }) - const { theme, indentSize, showCopyNotification, storagePath } = this.props + const { theme, indentSize, showCopyNotification, storagePath, noteKey } = this.props let { value, codeBlockTheme } = this.props this.refs.root.contentWindow.document.body.setAttribute('data-theme', theme) @@ -402,7 +402,8 @@ export default class MarkdownPreview extends React.Component { value = value.replace(codeBlock, htmlTextHelper.encodeEntities(codeBlock)) }) } - const renderedHTML = this.markdown.render(value) + let renderedHTML = this.markdown.render(value) + attachmentManagement.migrateAttachments(renderedHTML, storagePath, noteKey) this.refs.root.contentWindow.document.body.innerHTML = attachmentManagement.fixLocalURLS(renderedHTML, storagePath) _.forEach(this.refs.root.contentWindow.document.querySelectorAll('input[type="checkbox"]'), (el) => { diff --git a/browser/main/lib/dataApi/attachmentManagement.js b/browser/main/lib/dataApi/attachmentManagement.js index 893e03d1..7e0bc8ee 100644 --- a/browser/main/lib/dataApi/attachmentManagement.js +++ b/browser/main/lib/dataApi/attachmentManagement.js @@ -73,6 +73,31 @@ function createAttachmentDestinationFolder (destinationStoragePath, noteKey) { } } +/** + * @description Moves attachments from the old location ('/images') to the new one ('/attachments/noteKey) + * @param renderedHTML HTML of the current note + * @param storagePath Storage path of the current note + * @param noteKey Key of the current note + */ +function migrateAttachments (renderedHTML, storagePath, noteKey) { + if (sander.existsSync(path.join(storagePath, 'images'))) { + const attachments = getAttachmentsInContent(renderedHTML) || [] + if (attachments !== []) { + createAttachmentDestinationFolder(storagePath, noteKey) + } + for (const attachment of attachments) { + let attachmentBaseName = path.basename(attachment) + const possibleLegacyPath = path.join(storagePath, 'images', attachmentBaseName) + if (sander.existsSync(possibleLegacyPath)) { + const destinationPath = path.join(storagePath, DESTINATION_FOLDER, attachmentBaseName) + if (!sander.existsSync(destinationPath)) { + sander.copyFileSync(possibleLegacyPath).to(destinationPath) + } + } + } + } +} + /** * @description Fixes the URLs embedded in the generated HTML so that they again refer actual local files. * @param {String} renderedHTML HTML in that the links should be fixed @@ -165,7 +190,7 @@ function handlePastImageEvent (codeEditor, storageKey, noteKey, dataTransferItem */ function getAttachmentsInContent (markdownContent) { const preparedInput = markdownContent.replace(new RegExp(mdurl.encode(path.sep), 'g'), path.sep) - const regexp = new RegExp(STORAGE_FOLDER_PLACEHOLDER + escapeStringRegexp(path.sep) + '([a-zA-Z0-9]|-)+' + escapeStringRegexp(path.sep) + '[a-zA-Z0-9]+(\\.[a-zA-Z0-9]+)?', 'g') + const regexp = new RegExp(STORAGE_FOLDER_PLACEHOLDER + escapeStringRegexp(path.sep) + '?([a-zA-Z0-9]|-)*' + escapeStringRegexp(path.sep) + '[a-zA-Z0-9]+(\\.[a-zA-Z0-9]+)?', 'g') return preparedInput.match(regexp) } @@ -224,7 +249,7 @@ function replaceNoteKeyWithNewNoteKey (noteContent, oldNoteKey, newNoteKey) { * @returns {String} Input without the references */ function removeStorageAndNoteReferences (input, noteKey) { - return input.replace(new RegExp(mdurl.encode(path.sep), 'g'), path.sep).replace(new RegExp(STORAGE_FOLDER_PLACEHOLDER + escapeStringRegexp(path.sep) + noteKey, 'g'), DESTINATION_FOLDER) + return input.replace(new RegExp(mdurl.encode(path.sep), 'g'), path.sep).replace(new RegExp(STORAGE_FOLDER_PLACEHOLDER + '(' + escapeStringRegexp(path.sep) + noteKey + ')?', 'g'), DESTINATION_FOLDER) } /** @@ -321,6 +346,7 @@ module.exports = { deleteAttachmentsNotPresentInNote, moveAttachments, cloneAttachments, + migrateAttachments, STORAGE_FOLDER_PLACEHOLDER, DESTINATION_FOLDER } From ddd1522e190b7f8452bfcf7252bb6d1b02ec35b8 Mon Sep 17 00:00:00 2001 From: ehhc Date: Tue, 22 May 2018 09:08:37 +0200 Subject: [PATCH 2/3] Pasting an image should insert a relative attachment file link -> fixes #1953 --- browser/main/lib/dataApi/attachmentManagement.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/browser/main/lib/dataApi/attachmentManagement.js b/browser/main/lib/dataApi/attachmentManagement.js index 7e0bc8ee..161a48a4 100644 --- a/browser/main/lib/dataApi/attachmentManagement.js +++ b/browser/main/lib/dataApi/attachmentManagement.js @@ -177,7 +177,8 @@ function handlePastImageEvent (codeEditor, storageKey, noteKey, dataTransferItem base64data += base64data.replace('+', ' ') const binaryData = new Buffer(base64data, 'base64').toString('binary') fs.writeFileSync(imagePath, binaryData, 'binary') - const imageMd = generateAttachmentMarkdown(imageName, imagePath, true) + const imageReferencePath = path.join(STORAGE_FOLDER_PLACEHOLDER, noteKey, imageName) + const imageMd = generateAttachmentMarkdown(imageName, imageReferencePath, true) codeEditor.insertAttachmentMd(imageMd) } reader.readAsDataURL(blob) From ffae53326a51a58e1c75b067fb1de27f4f70a4b5 Mon Sep 17 00:00:00 2001 From: ehhc Date: Mon, 28 May 2018 09:04:35 +0200 Subject: [PATCH 3/3] Fix for the issues raised in the code review --- browser/main/lib/dataApi/attachmentManagement.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/browser/main/lib/dataApi/attachmentManagement.js b/browser/main/lib/dataApi/attachmentManagement.js index 161a48a4..8cdc164b 100644 --- a/browser/main/lib/dataApi/attachmentManagement.js +++ b/browser/main/lib/dataApi/attachmentManagement.js @@ -86,7 +86,7 @@ function migrateAttachments (renderedHTML, storagePath, noteKey) { createAttachmentDestinationFolder(storagePath, noteKey) } for (const attachment of attachments) { - let attachmentBaseName = path.basename(attachment) + const attachmentBaseName = path.basename(attachment) const possibleLegacyPath = path.join(storagePath, 'images', attachmentBaseName) if (sander.existsSync(possibleLegacyPath)) { const destinationPath = path.join(storagePath, DESTINATION_FOLDER, attachmentBaseName)