1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-14 10:16:26 +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