-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Export note with local images #1306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
83da07a
Export note with local images
3da4bb6
Export images together with document
f678a17
Export images together with document
98c9132
Merge branch 'master' into export-note-with-images
338f9fb
Semicolon fix
12447ef
Reject promise if write to file failed
5ec541c
Manipulate left margin of task item to hide circle
Rokt33r File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| const fs = require('fs') | ||
| const path = require('path') | ||
|
|
||
| /** | ||
| * @description Copy a file from source to destination | ||
| * @param {String} srcPath | ||
| * @param {String} dstPath | ||
| * @return {Promise} an image path | ||
| */ | ||
| function copyFile (srcPath, dstPath) { | ||
| if (!path.extname(dstPath)) { | ||
| dstPath = path.join(dstPath, path.basename(srcPath)) | ||
| } | ||
|
|
||
| return new Promise((resolve, reject) => { | ||
| const dstFolder = path.dirname(dstPath) | ||
| if (!fs.existsSync(dstFolder)) fs.mkdirSync(dstFolder) | ||
|
|
||
| const input = fs.createReadStream(srcPath) | ||
| const output = fs.createWriteStream(dstPath) | ||
|
|
||
| output.on('error', reject) | ||
| input.on('error', reject) | ||
| input.on('end', () => { | ||
| resolve(dstPath) | ||
| }) | ||
| input.pipe(output) | ||
| }) | ||
| } | ||
|
|
||
| module.exports = copyFile |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| import copyFile from 'browser/main/lib/dataApi/copyFile' | ||
| import {findStorage} from 'browser/lib/findStorage' | ||
|
|
||
| const fs = require('fs') | ||
| const path = require('path') | ||
|
|
||
| const LOCAL_STORED_REGEX = /!\[(.*?)\]\(\s*?\/:storage\/(.*\.\S*?)\)/gi | ||
| const IMAGES_FOLDER_NAME = 'images' | ||
|
|
||
| /** | ||
| * Export note together with images | ||
| * | ||
| * If images is stored in the storage, creates 'images' subfolder in target directory | ||
| * and copies images to it. Changes links to images in the content of the note | ||
| * | ||
| * @param {String} storageKey or storage path | ||
| * @param {String} noteContent Content to export | ||
| * @param {String} targetPath Path to exported file | ||
| * @param {function} outputFormatter | ||
| * @return {Promise.<*[]>} | ||
| */ | ||
| function exportNote (storageKey, noteContent, targetPath, outputFormatter) { | ||
| const storagePath = path.isAbsolute(storageKey) ? storageKey : findStorage(storageKey).path | ||
| const exportTasks = [] | ||
|
|
||
| if (!storagePath) { | ||
| throw new Error('Storage path is not found') | ||
| } | ||
|
|
||
| let exportedData = noteContent.replace(LOCAL_STORED_REGEX, (match, dstFilename, srcFilename) => { | ||
| if (!path.extname(dstFilename)) { | ||
| dstFilename += path.extname(srcFilename) | ||
| } | ||
|
|
||
| const dstRelativePath = path.join(IMAGES_FOLDER_NAME, dstFilename) | ||
|
|
||
| exportTasks.push({ | ||
| src: path.join(IMAGES_FOLDER_NAME, srcFilename), | ||
| dst: dstRelativePath | ||
| }) | ||
|
|
||
| return `` | ||
| }) | ||
|
|
||
| if (outputFormatter) { | ||
| exportedData = outputFormatter(exportedData, exportTasks) | ||
| } | ||
|
|
||
| const tasks = prepareTasks(exportTasks, storagePath, path.dirname(targetPath)) | ||
|
|
||
| return Promise.all(tasks.map((task) => copyFile(task.src, task.dst))) | ||
| .then(() => { | ||
| return saveToFile(exportedData, targetPath) | ||
| }).catch((err) => { | ||
| rollbackExport(tasks) | ||
| throw err | ||
| }) | ||
| } | ||
|
|
||
| function prepareTasks (tasks, storagePath, targetPath) { | ||
| return tasks.map((task) => { | ||
| if (!path.isAbsolute(task.src)) { | ||
| task.src = path.join(storagePath, task.src) | ||
| } | ||
|
|
||
| if (!path.isAbsolute(task.dst)) { | ||
| task.dst = path.join(targetPath, task.dst) | ||
| } | ||
|
|
||
| return task | ||
| }) | ||
| } | ||
|
|
||
| function saveToFile (data, filename) { | ||
| return new Promise((resolve, reject) => { | ||
| fs.writeFile(filename, data, (err) => { | ||
| if (err) throw err | ||
|
|
||
| resolve(filename) | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| /** | ||
| * Remove exported files | ||
| * @param tasks Array of copy task objects. Object consists of two mandatory fields – `src` and `dst` | ||
| */ | ||
| function rollbackExport (tasks) { | ||
| const folders = new Set() | ||
| tasks.forEach((task) => { | ||
| let fullpath = task.dst | ||
|
|
||
| if (!path.extname(task.dst)) { | ||
| fullpath = path.join(task.dst, path.basename(task.src)) | ||
| } | ||
|
|
||
| if (fs.existsSync(fullpath)) { | ||
| fs.unlink(fullpath) | ||
| folders.add(path.dirname(fullpath)) | ||
| } | ||
| }) | ||
|
|
||
| folders.forEach((folder) => { | ||
| if (fs.readdirSync(folder).length === 0) { | ||
| fs.rmdir(folder) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| export default exportNote | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume this makes the error go limbo.
return reject(err)should be right.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed