From 4bc0cccb246e3146538b96f9c99c6c520bf3bfb6 Mon Sep 17 00:00:00 2001 From: ehhc Date: Mon, 21 May 2018 15:17:38 +0200 Subject: [PATCH] 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 }