1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-15 02:36:36 +00:00

Merge pull request #293 from asmsuechan/copy-image-on-dropped-into-CodeEditor

Add dataApi.copyImage for copying image to boostnote storage on an image dropped into CodeEditor
This commit is contained in:
Sota Sugiura
2017-06-05 01:29:11 +09:00
committed by GitHub
4 changed files with 44 additions and 6 deletions

View File

@@ -0,0 +1,32 @@
const fs = require('fs')
const path = require('path')
const _ = require('lodash')
const sander = require('sander')
/**
* @description To copy an image and return the path.
* @param {String} filePath
* @param {String} storageKey
* @return {String} an image path
*/
function copyImage (filePath, storageKey) {
return new Promise((resolve, reject) => {
try {
const cachedStorageList = JSON.parse(localStorage.getItem('storages'))
if (!_.isArray(cachedStorageList)) throw new Error('Target storage doesn\'t exist.')
const storage = _.find(cachedStorageList, {key: storageKey})
if (storage === undefined) throw new Error('Target storage doesn\'t exist.')
const targetStorage = storage
const inputImage = fs.createReadStream(filePath)
const imageName = path.basename(filePath)
const outputImage = fs.createWriteStream(path.join(targetStorage.path, 'images', imageName))
inputImage.pipe(outputImage)
resolve(`${encodeURI(targetStorage.path)}/images/${encodeURI(imageName)}`)
} catch (e) {
return reject(e)
}
})
}
module.exports = copyImage