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

Export images together with document

This commit is contained in:
Nikolay Lopin
2018-02-05 02:08:33 +03:00
parent 3da4bb69ce
commit f678a17505
3 changed files with 153 additions and 71 deletions

View File

@@ -2,35 +2,30 @@ const fs = require('fs')
const path = require('path')
/**
* @description Export a file
* @param {String} storagePath
* @param {String} srcFilename
* @description Copy a file from source to destination
* @param {String} srcPath
* @param {String} dstPath
* @param {String} dstFilename if not present, destination filename will be equal to srcFilename
* @return {Promise} an image path
*/
function exportFile (storagePath, srcFilename, dstPath, dstFilename = '') {
dstFilename = dstFilename || srcFilename
const src = path.join(storagePath, 'images', srcFilename)
if (!path.extname(dstFilename)) {
dstFilename += path.extname(srcFilename)
function copyFile (srcPath, dstPath) {
if (!path.extname(dstPath)) {
dstPath = path.join(dstPath, path.basename(srcPath))
}
const dst = path.join(dstPath, dstFilename)
return new Promise((resolve, reject) => {
if (!fs.existsSync(dstPath)) fs.mkdirSync(dstPath)
const dstFolder = path.dirname(dstPath)
if (!fs.existsSync(dstFolder)) fs.mkdirSync(dstFolder)
const input = fs.createReadStream(src)
const output = fs.createWriteStream(dst)
const input = fs.createReadStream(srcPath)
const output = fs.createWriteStream(dstPath)
output.on('error', reject)
input.on('error', reject)
input.on('end', resolve, dst)
input.on('end', () => {
resolve(dstPath)
})
input.pipe(output)
})
}
module.exports = exportFile
module.exports = copyFile