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:
36
browser/main/lib/dataApi/copyFile.js
Executable file
36
browser/main/lib/dataApi/copyFile.js
Executable file
@@ -0,0 +1,36 @@
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
|
||||
/**
|
||||
* @description Export a file
|
||||
* @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 exportFile (storagePath, srcFilename, dstPath, dstFilename = '') {
|
||||
dstFilename = dstFilename || srcFilename
|
||||
|
||||
const src = path.join(storagePath, 'images', srcFilename)
|
||||
|
||||
if (!path.extname(dstFilename)) {
|
||||
dstFilename += path.extname(srcFilename)
|
||||
}
|
||||
|
||||
const dst = path.join(dstPath, dstFilename)
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!fs.existsSync(dstPath)) fs.mkdirSync(dstPath)
|
||||
|
||||
const input = fs.createReadStream(src)
|
||||
const output = fs.createWriteStream(dst)
|
||||
|
||||
output.on('error', reject)
|
||||
input.on('error', reject)
|
||||
input.on('end', resolve, dst)
|
||||
input.pipe(output)
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = exportFile
|
||||
Reference in New Issue
Block a user