1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-14 10:16:26 +00:00

Fix from reviews

This commit is contained in:
asmsuechan
2017-03-19 12:34:31 -07:00
parent 0f3230110c
commit 893a92c87b

View File

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