Partly: added picgo package to PicGo

This commit is contained in:
Molunerfinn
2018-09-14 17:31:57 +08:00
parent f309d18aed
commit 65dfb4ac05
3 changed files with 384 additions and 79 deletions

View File

@@ -1,6 +1,12 @@
import db from '../../datastore/index'
import { Notification } from 'electron'
import picBeds from '../../datastore/pic-bed-handler'
// 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')
// const checkUploader = (type) => {
// const currentUploader = db.read().get(`picBed.${type}`).value()
@@ -12,24 +18,68 @@ import picBeds from '../../datastore/pic-bed-handler'
// }
const uploader = (img, type, webContents) => {
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
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
}
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
// }
// } else {
// return false
// }
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)
}
})
})
}
export default uploader