mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-13 17:56:25 +00:00
Add dataApi.copyImage for copying image to boostnote storage on an image to boostnote storage on an image doropped into CodeEditor
This commit is contained in:
27
browser/main/lib/dataApi/copyImage.js
Normal file
27
browser/main/lib/dataApi/copyImage.js
Normal file
@@ -0,0 +1,27 @@
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const _ = require('lodash')
|
||||
const sander = require('sander')
|
||||
|
||||
function copyImage (filePath, storageKey) {
|
||||
let targetStorage
|
||||
try {
|
||||
let cachedStorageList = JSON.parse(localStorage.getItem('storages'))
|
||||
if (!_.isArray(cachedStorageList)) throw new Error('Target storage doesn\'t exist.')
|
||||
|
||||
targetStorage = _.find(cachedStorageList, {key: storageKey})
|
||||
if (targetStorage == null) throw new Error('Target storage doesn\'t exist.')
|
||||
} catch (e) {
|
||||
return Promise.reject(e)
|
||||
}
|
||||
|
||||
//return resolveStorageData(targetStorage)
|
||||
|
||||
const inputImage = fs.createReadStream(filePath)
|
||||
const imageName = path.basename(filePath)
|
||||
sander.mkdirSync(`${targetStorage.path}/images`)
|
||||
const outputImage = fs.createWriteStream(path.join(targetStorage.path, 'images', imageName))
|
||||
inputImage.pipe(outputImage)
|
||||
}
|
||||
|
||||
module.exports = copyImage
|
||||
Reference in New Issue
Block a user