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

allow to copy images without generating new name

This commit is contained in:
Nikolay Lopin
2018-02-25 02:45:59 +03:00
parent 8d59bd9f71
commit e5825e898d

View File

@@ -3,19 +3,20 @@ const path = require('path')
const { findStorage } = require('browser/lib/findStorage') const { findStorage } = require('browser/lib/findStorage')
/** /**
* @description To copy an image and return the path. * @description Copy an image and return the path.
* @param {String} filePath * @param {String} filePath
* @param {String} storageKey * @param {String} storageKey
* @return {String} an image path * @param {Boolean} rename create new filename or leave the old one
* @return {Promise<any>} an image path
*/ */
function copyImage (filePath, storageKey) { function copyImage (filePath, storageKey, rename = true) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
try { try {
const targetStorage = findStorage(storageKey) const targetStorage = findStorage(storageKey)
const inputImage = fs.createReadStream(filePath) const inputImage = fs.createReadStream(filePath)
const imageExt = path.extname(filePath) const imageExt = path.extname(filePath)
const imageName = Math.random().toString(36).slice(-16) const imageName = rename ? Math.random().toString(36).slice(-16) : path.basename(filePath, imageExt)
const basename = `${imageName}${imageExt}` const basename = `${imageName}${imageExt}`
const imageDir = path.join(targetStorage.path, 'images') const imageDir = path.join(targetStorage.path, 'images')
if (!fs.existsSync(imageDir)) fs.mkdirSync(imageDir) if (!fs.existsSync(imageDir)) fs.mkdirSync(imageDir)