2018-09-17 03:50:24 -04:00
|
|
|
import {
|
|
|
|
app,
|
|
|
|
Notification,
|
|
|
|
BrowserWindow,
|
|
|
|
ipcMain
|
|
|
|
} from 'electron'
|
2018-09-14 05:31:57 -04:00
|
|
|
import path from 'path'
|
|
|
|
import fecha from 'fecha'
|
|
|
|
|
2018-09-18 06:48:55 -04:00
|
|
|
// eslint-disable-next-line
|
|
|
|
const requireFunc = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require
|
|
|
|
const PicGo = requireFunc('picgo')
|
2018-09-20 02:49:20 -04:00
|
|
|
const STORE_PATH = app.getPath('userData')
|
|
|
|
const CONFIG_PATH = path.join(STORE_PATH, '/data.json')
|
2018-09-17 03:50:24 -04:00
|
|
|
const renameURL = process.env.NODE_ENV === 'development' ? `http://localhost:9080/#rename-page` : `file://${__dirname}/index.html#rename-page`
|
|
|
|
|
|
|
|
const createRenameWindow = () => {
|
|
|
|
let options = {
|
|
|
|
height: 175,
|
|
|
|
width: 300,
|
|
|
|
show: true,
|
|
|
|
fullscreenable: false,
|
|
|
|
resizable: false,
|
|
|
|
vibrancy: 'ultra-dark',
|
|
|
|
webPreferences: {
|
|
|
|
backgroundThrottling: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
options.show = true
|
|
|
|
options.backgroundColor = '#3f3c37'
|
|
|
|
options.autoHideMenuBar = true
|
|
|
|
options.transparent = false
|
|
|
|
}
|
|
|
|
|
|
|
|
const window = new BrowserWindow(options)
|
|
|
|
window.loadURL(renameURL)
|
|
|
|
return window
|
|
|
|
}
|
|
|
|
|
|
|
|
const waitForShow = (webcontent) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
webcontent.on('dom-ready', () => {
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const waitForRename = (window, id) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
ipcMain.once(`rename${id}`, (evt, newName) => {
|
|
|
|
resolve(newName)
|
|
|
|
window.hide()
|
|
|
|
})
|
|
|
|
window.on('close', () => {
|
|
|
|
resolve(null)
|
|
|
|
ipcMain.removeAllListeners(`rename${id}`)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
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)
|
2018-10-12 02:56:25 -04:00
|
|
|
picgo.config.debug = true
|
2018-09-14 05:31:57 -04:00
|
|
|
let input = []
|
|
|
|
switch (type) {
|
|
|
|
case 'imgFromClipboard':
|
|
|
|
if (img !== null) {
|
|
|
|
const today = fecha.format(new Date(), 'YYYYMMDDHHmmss') + '.png'
|
|
|
|
input = [
|
|
|
|
{
|
|
|
|
base64Image: img.imgUrl.replace(/^data\S+,/, ''),
|
2018-09-17 03:50:24 -04:00
|
|
|
fileName: today,
|
2018-09-14 05:31:57 -04:00
|
|
|
width: img.width,
|
|
|
|
height: img.height,
|
|
|
|
extname: '.png'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
break
|
|
|
|
default:
|
2018-09-17 03:50:24 -04:00
|
|
|
input = img
|
2018-09-14 05:31:57 -04:00
|
|
|
break
|
2017-11-29 10:13:35 -05:00
|
|
|
}
|
2018-09-17 03:50:24 -04:00
|
|
|
|
|
|
|
picgo.helper.beforeUploadPlugins.register('renameFn', {
|
|
|
|
handle: async ctx => {
|
2018-09-27 05:22:14 -04:00
|
|
|
const rename = picgo.getConfig('settings.rename')
|
|
|
|
const autoRename = picgo.getConfig('settings.autoRename')
|
2018-09-17 03:50:24 -04:00
|
|
|
await Promise.all(ctx.output.map(async item => {
|
|
|
|
let name
|
|
|
|
let fileName
|
|
|
|
if (autoRename) {
|
|
|
|
fileName = fecha.format(new Date(), 'YYYYMMDDHHmmss') + item.extname
|
|
|
|
} else {
|
|
|
|
fileName = item.fileName
|
|
|
|
}
|
|
|
|
if (rename) {
|
|
|
|
const window = createRenameWindow()
|
|
|
|
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('beforeTransform', ctx => {
|
2018-09-27 05:22:14 -04:00
|
|
|
if (ctx.getConfig('settings.uploadNotification')) {
|
2018-09-17 03:50:24 -04:00
|
|
|
const notification = new Notification({
|
|
|
|
title: '上传进度',
|
|
|
|
body: '正在上传'
|
|
|
|
})
|
|
|
|
notification.show()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-09-14 05:31:57 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
2018-09-17 03:50:24 -04:00
|
|
|
picgo.on('failed', ctx => {
|
|
|
|
resolve(false)
|
|
|
|
})
|
2018-09-14 05:31:57 -04:00
|
|
|
})
|
2017-11-29 10:13:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export default uploader
|