PicList/src/main/utils/uploader.js

159 lines
4.3 KiB
JavaScript
Raw Normal View History

import {
app,
Notification,
BrowserWindow,
ipcMain
} from 'electron'
2018-09-14 05:31:57 -04:00
import path from 'path'
import dayjs from 'dayjs'
2018-09-14 05:31:57 -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')
const renameURL = process.env.NODE_ENV === 'development' ? `http://localhost:9080/#rename-page` : `file://${__dirname}/index.html#rename-page`
const createRenameWindow = (win) => {
let options = {
height: 175,
width: 300,
show: true,
fullscreenable: false,
resizable: false,
vibrancy: 'ultra-dark',
webPreferences: {
nodeIntegration: true,
nodeIntegrationInWorker: true,
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)
// 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)
}
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
2018-12-18 05:58:31 -05:00
class Uploader {
2019-01-12 00:47:44 -05:00
constructor (img, webContents, picgo = undefined) {
2018-12-18 05:58:31 -05:00
this.img = img
this.webContents = webContents
2019-01-12 00:47:44 -05:00
this.picgo = picgo
2018-12-18 05:58:31 -05:00
}
2018-12-18 05:58:31 -05:00
upload () {
const win = BrowserWindow.fromWebContents(this.webContents)
2019-01-12 00:47:44 -05:00
const picgo = this.picgo || new PicGo(CONFIG_PATH)
2018-12-18 05:58:31 -05:00
picgo.config.debug = true
// for picgo-core
picgo.config.PICGO_ENV = 'GUI'
2018-12-18 05:58:31 -05:00
let input = this.img
2018-12-18 05:58:31 -05:00
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('beforeTransform', ctx => {
if (ctx.getConfig('settings.uploadNotification')) {
const notification = new Notification({
title: '上传进度',
body: '正在上传'
})
notification.show()
}
})
2018-12-18 05:58:31 -05:00
picgo.upload(input)
2018-09-14 05:31:57 -04:00
2018-12-18 05:58:31 -05:00
picgo.on('notification', message => {
const notification = new Notification(message)
notification.show()
})
2018-09-14 05:31:57 -04:00
2018-12-18 05:58:31 -05:00
picgo.on('uploadProgress', progress => {
this.webContents.send('uploadProgress', progress)
})
2018-09-14 05:31:57 -04:00
2018-12-18 05:58:31 -05:00
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 => {
2019-01-11 04:22:33 -05:00
const notification = new Notification({
title: '上传失败',
body: '请检查配置和上传的文件是否符合要求'
})
notification.show()
2018-09-14 05:31:57 -04:00
resolve(false)
2018-12-18 05:58:31 -05:00
})
})
2018-12-18 05:58:31 -05:00
}
2017-11-29 10:13:35 -05:00
}
2018-12-18 05:58:31 -05:00
export default Uploader