PicList/src/main/utils/uploader.js

86 lines
2.1 KiB
JavaScript
Raw Normal View History

2018-09-14 05:31:57 -04:00
// import db from '../../datastore/index'
import { app, Notification } from 'electron'
// import picBeds from '../../datastore/pic-bed-handler'
import PicGo from 'picgo'
import path from 'path'
import fecha from 'fecha'
const STORE_PATH = app.getPath('userData')
const CONFIG_PATH = path.join(STORE_PATH, '/data.json')
2017-12-22 09:30:16 -05:00
2018-07-10 22:52:02 -04:00
// const checkUploader = (type) => {
// const currentUploader = db.read().get(`picBed.${type}`).value()
// if (currentUploader) {
// return true
// } else {
// return false
// }
// }
2017-12-22 09:30:16 -05:00
2017-11-29 10:13:35 -05:00
const uploader = (img, type, webContents) => {
2018-09-14 05:31:57 -04:00
const picgo = new PicGo(CONFIG_PATH)
let input = []
switch (type) {
case 'imgFromPath':
input = img
break
case 'imgFromClipboard':
if (img !== null) {
const today = fecha.format(new Date(), 'YYYYMMDDHHmmss') + '.png'
input = [
{
base64Image: img.imgUrl.replace(/^data\S+,/, ''),
fileName: name || today,
width: img.width,
height: img.height,
extname: '.png'
}
]
}
break
default:
input = img.map(item => item.path)
break
2017-11-29 10:13:35 -05:00
}
2018-09-14 05:31:57 -04:00
picgo.upload(input)
// if (db.read().get('picBed.uploadNotification').value()) {
// const notification = new Notification({
// title: '上传进度',
// body: '正在上传'
// })
// notification.show()
// }
// const uploadType = db.read().get('picBed.current').value()
// // if (checkUploader(uploadType)) {
// try {
// return picBeds[uploadType](img, type, webContents)
// } catch (e) {
// console.log(e)
// return false
// }
2018-07-10 22:52:02 -04:00
// } else {
// return false
// }
2018-09-14 05:31:57 -04:00
picgo.on('notification', message => {
const notification = new Notification(message)
notification.show()
})
picgo.on('uploadProgress', progress => {
webContents.send('uploadProgress', progress)
})
return new Promise((resolve) => {
picgo.on('finished', ctx => {
if (ctx.output.every(item => item.imgUrl)) {
resolve(ctx.output)
} else {
resolve(false)
}
})
})
2017-11-29 10:13:35 -05:00
}
export default uploader