Added: picgo plugin handle states

This commit is contained in:
Molunerfinn
2018-12-18 18:58:31 +08:00
parent 72f2669cce
commit be6cce9501
14 changed files with 175 additions and 131 deletions

View File

@@ -37,6 +37,7 @@ const handleGetPluginList = (ipcMain, STORE_PATH, CONFIG_PATH) => {
ipcMain.on('getPluginList', event => {
const picgo = new PicGo(CONFIG_PATH)
const pluginList = picgo.pluginLoader.getList()
// console.log(pluginList.length)
const list = []
for (let i in pluginList) {
const plugin = picgo.pluginLoader.getPlugin(pluginList[i])
@@ -65,7 +66,8 @@ const handleGetPluginList = (ipcMain, STORE_PATH, CONFIG_PATH) => {
}
},
enabled: picgo.getConfig(`plugins.${pluginList[i]}`),
homepage: pluginPKG.homepage ? pluginPKG.homepage : ''
homepage: pluginPKG.homepage ? pluginPKG.homepage : '',
ing: false
}
list.push(obj)
}
@@ -91,7 +93,7 @@ const handlePluginUninstall = (ipcMain, STORE_PATH, CONFIG_PATH) => {
const picgo = new PicGo(CONFIG_PATH)
const pluginHandler = new PluginHandler(picgo)
picgo.on('uninstallSuccess', notice => {
event.sender.send('installSuccess', notice.body[0].replace(/picgo-plugin-/, ''))
event.sender.send('uninstallSuccess', notice.body[0].replace(/picgo-plugin-/, ''))
})
pluginHandler.uninstall([`picgo-plugin-${msg}`])
picgo.cmd.program.removeAllListeners()

View File

@@ -74,69 +74,78 @@ const waitForRename = (window, id) => {
})
}
const uploader = (img, type, webContents) => {
const win = BrowserWindow.fromWebContents(webContents)
const picgo = new PicGo(CONFIG_PATH)
picgo.config.debug = true
let input = img
class Uploader {
constructor (img, type, webContents) {
this.img = img
this.type = type
this.webContents = webContents
}
picgo.helper.beforeUploadPlugins.register('renameFn', {
handle: async ctx => {
const rename = picgo.getConfig('settings.rename')
const autoRename = picgo.getConfig('settings.autoRename')
await Promise.all(ctx.output.map(async (item, index) => {
let name
let fileName
if (autoRename) {
fileName = dayjs().add(index, 'second').format('YYYYMMDDHHmmss') + item.extname
} else {
fileName = item.fileName
}
if (rename) {
const window = createRenameWindow(win)
await waitForShow(window.webContents)
window.webContents.send('rename', fileName, window.webContents.id)
name = await waitForRename(window, window.webContents.id)
}
item.fileName = name || fileName
}))
}
})
upload () {
const win = BrowserWindow.fromWebContents(this.webContents)
const picgo = new PicGo(CONFIG_PATH)
console.log(picgo.pluginLoader.getList())
picgo.config.debug = true
let input = this.img
picgo.on('beforeTransform', ctx => {
if (ctx.getConfig('settings.uploadNotification')) {
const notification = new Notification({
title: '上传进度',
body: '正在上传'
})
notification.show()
}
})
picgo.upload(input)
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)
picgo.helper.beforeUploadPlugins.register('renameFn', {
handle: async ctx => {
const rename = picgo.getConfig('settings.rename')
const autoRename = picgo.getConfig('settings.autoRename')
await Promise.all(ctx.output.map(async (item, index) => {
let name
let fileName
if (autoRename) {
fileName = dayjs().add(index, 'second').format('YYYYMMDDHHmmss') + item.extname
} else {
fileName = item.fileName
}
if (rename) {
const window = createRenameWindow(win)
await waitForShow(window.webContents)
window.webContents.send('rename', fileName, window.webContents.id)
name = await waitForRename(window, window.webContents.id)
}
item.fileName = name || fileName
}))
}
})
picgo.on('failed', ctx => {
console.log(ctx)
resolve(false)
picgo.on('beforeTransform', ctx => {
if (ctx.getConfig('settings.uploadNotification')) {
const notification = new Notification({
title: '上传进度',
body: '正在上传'
})
notification.show()
}
})
})
picgo.upload(input)
picgo.on('notification', message => {
const notification = new Notification(message)
notification.show()
})
picgo.on('uploadProgress', progress => {
this.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)
}
})
picgo.on('failed', ctx => {
console.log(ctx)
resolve(false)
})
})
}
}
export default uploader
export default Uploader