1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-13 17:56:25 +00:00

Export note with local images

Looks through the note and searches for local images. Copies them to ‘images’ folder in the export path and replaces affected links.

#1261
This commit is contained in:
Nikolay Lopin
2017-12-17 21:39:34 +03:00
parent e72a7ceaea
commit 83da07a941
4 changed files with 143 additions and 32 deletions

View File

@@ -0,0 +1,37 @@
const fs = require('fs')
const path = require('path')
/**
* @description Export an image
* @param {String} storagePath
* @param {String} srcFilename
* @param {String} dstPath
* @param {String} dstFilename if not present, destination filename will be equal to srcFilename
* @return {Promise} an image path
*/
function exportImage (storagePath, srcFilename, dstPath, dstFilename = '') {
dstFilename = dstFilename || srcFilename
const src = path.join(storagePath, 'images', srcFilename)
if (!path.extname(dstFilename)) {
dstFilename += path.extname(srcFilename)
}
const dstImagesFolder = path.join(dstPath, 'images')
const dst = path.join(dstImagesFolder, dstFilename)
return new Promise((resolve, reject) => {
if (!fs.existsSync(dstImagesFolder)) fs.mkdirSync(dstImagesFolder)
const input = fs.createReadStream(src)
const output = fs.createWriteStream(dst)
output.on('error', reject)
input.on('error', reject)
input.on('end', resolve)
input.pipe(output)
})
}
module.exports = exportImage

View File

@@ -0,0 +1,67 @@
import exportImage from 'browser/main/lib/dataApi/exportImage'
import {findStorage} from 'browser/lib/findStorage'
const fs = require('fs')
const path = require('path')
/**
* 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
* @param {String} noteContent Content to export
* @param {String} targetPath Path to exported file
* @return {Promise.<*[]>}
*/
function exportNote (storageKey, noteContent, targetPath) {
const targetStorage = findStorage(storageKey)
const storagedImagesRe = /!\[(.*?)\]\(\s*?\/:storage\/(.*\.\S*?)\)/gi
const exportTasks = []
const images = []
const exportedData = noteContent.replace(storagedImagesRe, (match, dstFilename, srcFilename) => {
if (!path.extname(dstFilename)) {
dstFilename += path.extname(srcFilename)
}
const imagePath = path.join('images', dstFilename)
exportTasks.push(
exportImage(targetStorage.path, srcFilename, path.dirname(targetPath), dstFilename)
)
images.push(imagePath)
return `![${dstFilename}](${imagePath})`
})
exportTasks.push(exportFile(exportedData, targetPath))
return Promise.all(exportTasks)
.catch((err) => {
rollbackExport(images)
throw err
})
}
function exportFile (data, filename) {
return new Promise((resolve, reject) => {
fs.writeFile(filename, data, (err) => {
if (err) throw err
resolve(filename)
})
})
}
/**
* Remove exported images
* @param imagesPaths
*/
function rollbackExport (imagesPaths) {
imagesPaths.forEach((path) => {
if (fs.existsSync(path)) {
fs.unlink(path)
}
})
}
export default exportNote