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'
|
2018-11-22 03:55:34 -05:00
|
|
|
import dayjs from 'dayjs'
|
2018-09-14 05:31:57 -04:00
|
|
|
|
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`
|
|
|
|
|
2018-12-13 04:05:08 -05:00
|
|
|
const createRenameWindow = (win) => {
|
2018-09-17 03:50:24 -04:00
|
|
|
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)
|
2018-12-13 04:05:08 -05:00
|
|
|
// check if this window is visible
|
|
|
|
if (win.isVisible()) {
|
|
|
|
// bounds: { x: 821, y: 75, width: 800, height: 450 }
|
|
|
|
const bounds = win.getBounds()
|
|
|
|
const positionX = bounds.x + bounds.width / 2 - 150
|
|
|
|
let positionY
|
|
|
|
// if is the settingWindow
|
|
|
|
if (bounds.height > 400) {
|
|
|
|
positionY = bounds.y + bounds.height / 2 - 88
|
|
|
|
} else { // if is the miniWindow
|
|
|
|
positionY = bounds.y + bounds.height / 2
|
|
|
|
}
|
|
|
|
window.setPosition(positionX, positionY, false)
|
|
|
|
}
|
2018-09-17 03:50:24 -04:00
|
|
|
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-12-13 04:05:08 -05:00
|
|
|
const win = BrowserWindow.fromWebContents(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-11-28 22:14:45 -05:00
|
|
|
let input = img
|
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-11-22 03:55:34 -05:00
|
|
|
await Promise.all(ctx.output.map(async (item, index) => {
|
2018-09-17 03:50:24 -04:00
|
|
|
let name
|
|
|
|
let fileName
|
|
|
|
if (autoRename) {
|
2018-11-22 03:55:34 -05:00
|
|
|
fileName = dayjs().add(index, 'second').format('YYYYMMDDHHmmss') + item.extname
|
2018-09-17 03:50:24 -04:00
|
|
|
} else {
|
|
|
|
fileName = item.fileName
|
|
|
|
}
|
|
|
|
if (rename) {
|
2018-12-13 04:05:08 -05:00
|
|
|
const window = createRenameWindow(win)
|
2018-09-17 03:50:24 -04:00
|
|
|
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 => {
|
2018-11-25 10:53:26 -05:00
|
|
|
console.log(ctx)
|
2018-09-17 03:50:24 -04:00
|
|
|
resolve(false)
|
|
|
|
})
|
2018-09-14 05:31:57 -04:00
|
|
|
})
|
2017-11-29 10:13:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export default uploader
|