PicList/src/main/utils/uploader.ts

183 lines
5.1 KiB
TypeScript
Raw Normal View History

import {
app,
Notification,
BrowserWindow,
2019-12-19 06:17:21 -05:00
ipcMain,
WebContents
} from 'electron'
2018-09-14 05:31:57 -04:00
import path from 'path'
import dayjs from 'dayjs'
2019-12-19 06:17:21 -05:00
import PicGoCore from '~/universal/types/picgo'
2018-09-14 05:31:57 -04:00
// eslint-disable-next-line
const requireFunc = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require
2019-12-19 06:17:21 -05:00
const PicGo = requireFunc('picgo') as typeof PicGoCore
2018-09-20 02:49:20 -04:00
const STORE_PATH = app.getPath('userData')
const CONFIG_PATH = path.join(STORE_PATH, '/data.json')
2019-12-20 11:50:13 -05:00
const renameURL = process.env.NODE_ENV === 'development'
? `${(process.env.WEBPACK_DEV_SERVER_URL as string)}#rename-page`
: `picgo://./index.html#rename-page`
2019-12-19 06:17:21 -05:00
// type BrowserWindowOptions = {
// height: number,
// width: number,
// show: boolean,
// fullscreenable: boolean,
// resizable: boolean,
// vibrancy: string | any,
// webPreferences: {
// nodeIntegration: boolean,
// nodeIntegrationInWorker: boolean,
// backgroundThrottling: boolean
// },
// backgroundColor?: string
// autoHideMenuBar?: boolean
// transparent?: boolean
// }
const createRenameWindow = (win: BrowserWindow) => {
let options: IBrowserWindowOptions = {
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
}
2019-12-19 06:17:21 -05:00
const waitForShow = (webcontent: WebContents) => {
return new Promise((resolve, reject) => {
2019-12-20 11:50:13 -05:00
webcontent.on('did-finish-load', () => {
resolve()
})
})
}
2019-12-19 06:17:21 -05:00
const waitForRename = (window: BrowserWindow, id: number): Promise<string|null> => {
return new Promise((resolve, reject) => {
2019-12-19 06:17:21 -05:00
ipcMain.once(`rename${id}`, (evt: Event, newName: string) => {
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-12-19 06:17:21 -05:00
private picgo: PicGoCore
private webContents: WebContents
private img: undefined | string[]
constructor (img: undefined | string[], webContents: WebContents, picgo: PicGoCore | undefined = undefined) {
2018-12-18 05:58:31 -05:00
this.img = img
this.webContents = webContents
2019-12-19 06:17:21 -05:00
this.picgo = picgo || new PicGo(CONFIG_PATH)
2018-12-18 05:58:31 -05:00
}
2019-12-19 06:17:21 -05:00
upload (): Promise<ImgInfo[]|false> {
2018-12-18 05:58:31 -05:00
const win = BrowserWindow.fromWebContents(this.webContents)
2019-12-19 06:17:21 -05:00
const picgo = this.picgo
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) => {
2019-12-19 06:17:21 -05:00
let name: undefined | string | null
let fileName: string | undefined
2018-12-18 05:58:31 -05:00
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 => {
2019-12-19 06:17:21 -05:00
if (ctx.output.every((item: ImgInfo) => item.imgUrl)) {
2018-12-18 05:58:31 -05:00
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