2021-04-24 05:56:56 -04:00
|
|
|
import './errorHandler'
|
2020-04-24 23:34:04 -04:00
|
|
|
import {
|
|
|
|
app,
|
|
|
|
globalShortcut,
|
2020-09-30 05:32:35 -04:00
|
|
|
protocol,
|
2023-04-05 01:02:17 -04:00
|
|
|
Notification,
|
2023-04-06 03:57:36 -04:00
|
|
|
dialog,
|
2024-01-02 03:28:12 -05:00
|
|
|
screen,
|
|
|
|
shell
|
2020-04-24 23:34:04 -04:00
|
|
|
} from 'electron'
|
|
|
|
import {
|
2021-04-24 00:13:58 -04:00
|
|
|
createProtocol
|
2020-04-24 23:34:04 -04:00
|
|
|
} from 'vue-cli-plugin-electron-builder/lib'
|
|
|
|
import beforeOpen from '~/main/utils/beforeOpen'
|
|
|
|
import ipcList from '~/main/events/ipcList'
|
|
|
|
import busEventList from '~/main/events/busEventList'
|
2024-04-09 03:03:32 -04:00
|
|
|
import { II18nLanguage, IRemoteNoticeTriggerHook, ISartMode, IWindowList } from '#/types/enum'
|
2020-04-24 23:34:04 -04:00
|
|
|
import windowManager from 'apis/app/window/windowManager'
|
|
|
|
import {
|
|
|
|
uploadChoosedFiles,
|
|
|
|
uploadClipboardFiles
|
|
|
|
} from 'apis/app/uploader/apis'
|
|
|
|
import {
|
2023-04-10 04:06:37 -04:00
|
|
|
createTray, setDockMenu
|
2020-04-24 23:34:04 -04:00
|
|
|
} from 'apis/app/system'
|
|
|
|
import server from '~/main/server/index'
|
|
|
|
import shortKeyHandler from 'apis/app/shortKey/shortKeyHandler'
|
|
|
|
import { getUploadFiles } from '~/main/utils/handleArgv'
|
2023-08-10 08:30:46 -04:00
|
|
|
import db from '~/main/apis/core/datastore'
|
2020-04-24 23:34:04 -04:00
|
|
|
import bus from '@core/bus'
|
2021-04-23 12:44:49 -04:00
|
|
|
import logger from 'apis/core/picgo/logger'
|
2021-08-01 05:02:54 -04:00
|
|
|
import picgo from 'apis/core/picgo'
|
2022-01-09 21:20:56 -05:00
|
|
|
import fixPath from './fixPath'
|
2023-02-15 10:36:47 -05:00
|
|
|
import { clearTempFolder } from '../manage/utils/common'
|
2022-02-20 08:30:22 -05:00
|
|
|
import { initI18n } from '~/main/utils/handleI18n'
|
2022-10-30 04:08:29 -04:00
|
|
|
import { remoteNoticeHandler } from 'apis/app/remoteNotice'
|
2023-02-15 10:36:47 -05:00
|
|
|
import { manageIpcList } from '../manage/events/ipcList'
|
|
|
|
import getManageApi from '../manage/Main'
|
|
|
|
import UpDownTaskQueue from '../manage/datastore/upDownTaskQueue'
|
2023-04-05 01:02:17 -04:00
|
|
|
import { T } from '~/main/i18n'
|
2023-04-05 10:55:35 -04:00
|
|
|
import { UpdateInfo, autoUpdater } from 'electron-updater'
|
2023-04-06 00:50:07 -04:00
|
|
|
import updateChecker from '../utils/updateChecker'
|
2023-04-10 06:45:04 -04:00
|
|
|
import clipboardPoll from '../utils/clipboardPoll'
|
2023-04-10 04:06:37 -04:00
|
|
|
import path from 'path'
|
|
|
|
import { CLIPBOARD_IMAGE_FOLDER } from '~/universal/utils/static'
|
|
|
|
import fs from 'fs-extra'
|
2023-07-12 07:41:49 -04:00
|
|
|
import { startFileServer } from '../fileServer'
|
2024-04-02 01:29:55 -04:00
|
|
|
import webServer from '../server/webServer'
|
2023-09-30 00:46:01 -04:00
|
|
|
import axios from 'axios'
|
2024-04-09 03:03:32 -04:00
|
|
|
import { configPaths } from '~/universal/utils/configPaths'
|
2020-04-24 23:34:04 -04:00
|
|
|
const isDevelopment = process.env.NODE_ENV !== 'production'
|
2021-04-23 12:44:49 -04:00
|
|
|
|
|
|
|
const handleStartUpFiles = (argv: string[], cwd: string) => {
|
|
|
|
const files = getUploadFiles(argv, cwd, logger)
|
|
|
|
if (files === null || files.length > 0) { // 如果有文件列表作为参数,说明是命令行启动
|
|
|
|
if (files === null) {
|
|
|
|
logger.info('cli -> uploading file from clipboard')
|
|
|
|
uploadClipboardFiles()
|
|
|
|
} else {
|
|
|
|
logger.info('cli -> uploading files from cli', ...files.map(item => item.path))
|
|
|
|
const win = windowManager.getAvailableWindow()
|
|
|
|
uploadChoosedFiles(win.webContents, files)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2023-08-10 03:11:57 -04:00
|
|
|
return false
|
2021-04-23 12:44:49 -04:00
|
|
|
}
|
|
|
|
|
2023-04-05 10:55:35 -04:00
|
|
|
autoUpdater.setFeedURL({
|
|
|
|
provider: 'generic',
|
|
|
|
url: 'https://release.piclist.cn/latest',
|
|
|
|
channel: 'latest'
|
|
|
|
})
|
|
|
|
|
|
|
|
autoUpdater.autoDownload = false
|
|
|
|
|
2023-09-30 00:46:01 -04:00
|
|
|
autoUpdater.on('update-available', async (info: UpdateInfo) => {
|
2024-04-09 03:03:32 -04:00
|
|
|
const lang = db.get(configPaths.settings.language) || II18nLanguage.ZH_CN
|
2023-09-30 00:46:01 -04:00
|
|
|
let updateLog = ''
|
|
|
|
try {
|
2024-04-09 03:03:32 -04:00
|
|
|
const url = lang === II18nLanguage.ZH_CN ? 'https://release.piclist.cn/currentVersion.md' : 'https://release.piclist.cn/currentVersion_en.md'
|
2023-09-30 00:46:01 -04:00
|
|
|
const res = await axios.get(url)
|
|
|
|
updateLog = res.data
|
|
|
|
} catch (e: any) {
|
|
|
|
logger.error(e)
|
|
|
|
}
|
2023-04-05 10:55:35 -04:00
|
|
|
dialog.showMessageBox({
|
|
|
|
type: 'info',
|
|
|
|
title: T('FIND_NEW_VERSION'),
|
2024-01-02 03:28:12 -05:00
|
|
|
buttons: ['Yes', 'Go to download page'],
|
2023-04-05 10:55:35 -04:00
|
|
|
message: T('TIPS_FIND_NEW_VERSION', {
|
|
|
|
v: info.version
|
2023-09-30 00:46:01 -04:00
|
|
|
}) + '\n\n' + updateLog,
|
2023-04-06 00:50:07 -04:00
|
|
|
checkboxLabel: T('NO_MORE_NOTICE'),
|
|
|
|
checkboxChecked: false
|
2023-04-05 10:55:35 -04:00
|
|
|
}).then((result) => {
|
|
|
|
if (result.response === 0) {
|
|
|
|
autoUpdater.downloadUpdate()
|
2024-01-02 03:28:12 -05:00
|
|
|
} else {
|
|
|
|
shell.openExternal('https://github.com/Kuingsmile/PicList/releases/latest')
|
2023-04-05 10:55:35 -04:00
|
|
|
}
|
2024-04-09 03:03:32 -04:00
|
|
|
db.set(configPaths.settings.showUpdateTip, !result.checkboxChecked)
|
2023-08-10 03:11:57 -04:00
|
|
|
}).catch((err) => {
|
|
|
|
logger.error(err)
|
2023-04-05 10:55:35 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-08-10 03:11:57 -04:00
|
|
|
autoUpdater.on('download-progress', (progressObj) => {
|
|
|
|
const percent = {
|
|
|
|
progress: progressObj.percent
|
|
|
|
}
|
|
|
|
const window = windowManager.get(IWindowList.SETTING_WINDOW)!
|
|
|
|
window.webContents.send('updateProgress', percent)
|
|
|
|
})
|
|
|
|
|
2023-04-05 10:55:35 -04:00
|
|
|
autoUpdater.on('update-downloaded', () => {
|
|
|
|
dialog.showMessageBox({
|
|
|
|
type: 'info',
|
|
|
|
title: T('UPDATE_DOWNLOADED'),
|
|
|
|
buttons: ['Yes', 'No'],
|
|
|
|
message: T('TIPS_UPDATE_DOWNLOADED')
|
|
|
|
}).then((result) => {
|
2023-08-10 03:11:57 -04:00
|
|
|
const window = windowManager.get(IWindowList.SETTING_WINDOW)!
|
|
|
|
window.webContents.send('updateProgress', { progress: 100 })
|
2023-04-05 10:55:35 -04:00
|
|
|
if (result.response === 0) {
|
|
|
|
autoUpdater.quitAndInstall()
|
|
|
|
}
|
2023-08-10 03:11:57 -04:00
|
|
|
}).catch((err) => {
|
|
|
|
logger.error(err)
|
2023-04-05 10:55:35 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
autoUpdater.on('error', (err) => {
|
2023-04-05 11:14:20 -04:00
|
|
|
console.log(err)
|
2023-04-05 10:55:35 -04:00
|
|
|
})
|
|
|
|
|
2020-04-24 23:34:04 -04:00
|
|
|
class LifeCycle {
|
2021-08-01 05:02:54 -04:00
|
|
|
private async beforeReady () {
|
2020-04-24 23:34:04 -04:00
|
|
|
protocol.registerSchemesAsPrivileged([{ scheme: 'picgo', privileges: { secure: true, standard: true } }])
|
2022-01-08 21:11:14 -05:00
|
|
|
// fix the $PATH in macOS & linux
|
2020-04-24 23:34:04 -04:00
|
|
|
fixPath()
|
|
|
|
beforeOpen()
|
2022-02-20 08:30:22 -05:00
|
|
|
initI18n()
|
2020-04-24 23:34:04 -04:00
|
|
|
ipcList.listen()
|
2023-02-15 10:36:47 -05:00
|
|
|
getManageApi()
|
|
|
|
UpDownTaskQueue.getInstance()
|
|
|
|
manageIpcList.listen()
|
2020-04-24 23:34:04 -04:00
|
|
|
busEventList.listen()
|
|
|
|
}
|
2022-01-04 10:40:28 -05:00
|
|
|
|
2020-04-24 23:34:04 -04:00
|
|
|
private onReady () {
|
2021-08-01 05:02:54 -04:00
|
|
|
const readyFunction = async () => {
|
2020-04-24 23:34:04 -04:00
|
|
|
createProtocol('picgo')
|
|
|
|
windowManager.create(IWindowList.TRAY_WINDOW)
|
|
|
|
windowManager.create(IWindowList.SETTING_WINDOW)
|
2024-04-09 03:03:32 -04:00
|
|
|
const isAutoListenClipboard = db.get(configPaths.settings.isAutoListenClipboard) || false
|
2023-05-24 05:14:21 -04:00
|
|
|
const ClipboardWatcher = clipboardPoll
|
2023-04-10 06:45:04 -04:00
|
|
|
if (isAutoListenClipboard) {
|
2024-04-09 03:03:32 -04:00
|
|
|
db.set(configPaths.settings.isListeningClipboard, true)
|
2023-04-10 06:45:04 -04:00
|
|
|
ClipboardWatcher.startListening()
|
|
|
|
ClipboardWatcher.on('change', () => {
|
|
|
|
picgo.log.info('clipboard changed')
|
|
|
|
uploadClipboardFiles()
|
|
|
|
})
|
|
|
|
} else {
|
2024-04-09 03:03:32 -04:00
|
|
|
db.set(configPaths.settings.isListeningClipboard, false)
|
2023-04-10 06:45:04 -04:00
|
|
|
}
|
2024-04-09 03:03:32 -04:00
|
|
|
const isHideDock = db.get(configPaths.settings.isHideDock) || false
|
|
|
|
const startMode = db.get(configPaths.settings.startMode) || ISartMode.QUIET
|
2023-04-10 23:19:45 -04:00
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
isHideDock ? app.dock.hide() : setDockMenu()
|
2024-04-09 03:03:32 -04:00
|
|
|
startMode !== ISartMode.NO_TRAY && createTray()
|
2023-04-10 23:19:45 -04:00
|
|
|
} else {
|
2023-04-09 11:45:29 -04:00
|
|
|
createTray()
|
|
|
|
}
|
2024-04-09 03:03:32 -04:00
|
|
|
db.set(configPaths.needReload, false)
|
2023-04-06 00:50:07 -04:00
|
|
|
updateChecker()
|
2020-04-24 23:34:04 -04:00
|
|
|
// 不需要阻塞
|
|
|
|
process.nextTick(() => {
|
|
|
|
shortKeyHandler.init()
|
|
|
|
})
|
|
|
|
server.startup()
|
2023-07-12 07:41:49 -04:00
|
|
|
startFileServer()
|
2024-04-02 01:29:55 -04:00
|
|
|
webServer.start()
|
2020-04-24 23:34:04 -04:00
|
|
|
if (process.env.NODE_ENV !== 'development') {
|
2021-04-23 12:44:49 -04:00
|
|
|
handleStartUpFiles(process.argv, process.cwd())
|
2020-04-24 23:34:04 -04:00
|
|
|
}
|
2020-09-30 05:32:35 -04:00
|
|
|
|
2021-08-20 22:42:52 -04:00
|
|
|
if (global.notificationList && global.notificationList?.length > 0) {
|
|
|
|
while (global.notificationList?.length) {
|
2020-09-30 05:32:35 -04:00
|
|
|
const option = global.notificationList.pop()
|
|
|
|
const notice = new Notification(option!)
|
|
|
|
notice.show()
|
|
|
|
}
|
|
|
|
}
|
2022-10-30 04:08:29 -04:00
|
|
|
await remoteNoticeHandler.init()
|
|
|
|
remoteNoticeHandler.triggerHook(IRemoteNoticeTriggerHook.APP_START)
|
2024-04-09 03:03:32 -04:00
|
|
|
if (startMode === ISartMode.MINI) {
|
2023-04-06 03:57:36 -04:00
|
|
|
windowManager.create(IWindowList.MINI_WINDOW)
|
|
|
|
const miniWindow = windowManager.get(IWindowList.MINI_WINDOW)!
|
2023-04-10 07:50:36 -04:00
|
|
|
miniWindow.removeAllListeners()
|
2024-04-09 03:03:32 -04:00
|
|
|
if (db.get(configPaths.settings.miniWindowOntop)) {
|
2023-04-06 03:57:36 -04:00
|
|
|
miniWindow.setAlwaysOnTop(true)
|
|
|
|
}
|
|
|
|
const { width, height } = screen.getPrimaryDisplay().workAreaSize
|
2024-04-09 03:03:32 -04:00
|
|
|
const lastPosition = db.get(configPaths.settings.miniWindowPosition)
|
2023-04-06 03:57:36 -04:00
|
|
|
if (lastPosition) {
|
|
|
|
miniWindow.setPosition(lastPosition[0], lastPosition[1])
|
|
|
|
} else {
|
|
|
|
miniWindow.setPosition(width - 100, height - 100)
|
|
|
|
}
|
2023-04-10 07:50:36 -04:00
|
|
|
const setPositionFunc = () => {
|
2023-04-06 03:57:36 -04:00
|
|
|
const position = miniWindow.getPosition()
|
2024-04-09 03:03:32 -04:00
|
|
|
db.set(configPaths.settings.miniWindowPosition, position)
|
2023-04-10 07:50:36 -04:00
|
|
|
}
|
|
|
|
miniWindow.on('close', setPositionFunc)
|
|
|
|
miniWindow.on('move', setPositionFunc)
|
2023-04-06 03:57:36 -04:00
|
|
|
miniWindow.show()
|
|
|
|
miniWindow.focus()
|
2024-04-09 03:03:32 -04:00
|
|
|
} else if (startMode === ISartMode.MAIN) {
|
2023-04-06 03:57:36 -04:00
|
|
|
const settingWindow = windowManager.get(IWindowList.SETTING_WINDOW)!
|
|
|
|
settingWindow.show()
|
|
|
|
settingWindow.focus()
|
|
|
|
}
|
2023-04-10 04:06:37 -04:00
|
|
|
const clipboardDir = path.join(picgo.baseDir, CLIPBOARD_IMAGE_FOLDER)
|
|
|
|
fs.ensureDir(clipboardDir)
|
2021-08-01 05:02:54 -04:00
|
|
|
}
|
2023-01-06 04:21:27 -05:00
|
|
|
app.whenReady().then(readyFunction)
|
2020-04-24 23:34:04 -04:00
|
|
|
}
|
2022-01-04 10:40:28 -05:00
|
|
|
|
2020-04-24 23:34:04 -04:00
|
|
|
private onRunning () {
|
|
|
|
app.on('second-instance', (event, commandLine, workingDirectory) => {
|
2021-04-23 12:44:49 -04:00
|
|
|
logger.info('detect second instance')
|
|
|
|
const result = handleStartUpFiles(commandLine, workingDirectory)
|
|
|
|
if (!result) {
|
2020-04-24 23:34:04 -04:00
|
|
|
if (windowManager.has(IWindowList.SETTING_WINDOW)) {
|
|
|
|
const settingWindow = windowManager.get(IWindowList.SETTING_WINDOW)!
|
|
|
|
if (settingWindow.isMinimized()) {
|
|
|
|
settingWindow.restore()
|
|
|
|
}
|
|
|
|
settingWindow.focus()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
app.on('activate', () => {
|
|
|
|
createProtocol('picgo')
|
|
|
|
if (!windowManager.has(IWindowList.TRAY_WINDOW)) {
|
|
|
|
windowManager.create(IWindowList.TRAY_WINDOW)
|
|
|
|
}
|
|
|
|
if (!windowManager.has(IWindowList.SETTING_WINDOW)) {
|
|
|
|
windowManager.create(IWindowList.SETTING_WINDOW)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
app.setLoginItemSettings({
|
2024-04-09 03:03:32 -04:00
|
|
|
openAtLogin: db.get(configPaths.settings.autoStart) || false
|
2020-04-24 23:34:04 -04:00
|
|
|
})
|
|
|
|
if (process.platform === 'win32') {
|
2023-02-15 10:36:47 -05:00
|
|
|
app.setAppUserModelId('com.kuingsmile.piclist')
|
2020-04-24 23:34:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (process.env.XDG_CURRENT_DESKTOP && process.env.XDG_CURRENT_DESKTOP.includes('Unity')) {
|
|
|
|
process.env.XDG_CURRENT_DESKTOP = 'Unity'
|
|
|
|
}
|
|
|
|
}
|
2022-01-04 10:40:28 -05:00
|
|
|
|
2020-04-24 23:34:04 -04:00
|
|
|
private onQuit () {
|
|
|
|
app.on('window-all-closed', () => {
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
app.quit()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
app.on('will-quit', () => {
|
2023-02-15 10:36:47 -05:00
|
|
|
UpDownTaskQueue.getInstance().persist()
|
|
|
|
clearTempFolder()
|
2020-04-24 23:34:04 -04:00
|
|
|
globalShortcut.unregisterAll()
|
|
|
|
bus.removeAllListeners()
|
|
|
|
server.shutdown()
|
|
|
|
})
|
|
|
|
// Exit cleanly on request from parent process in development mode.
|
|
|
|
if (isDevelopment) {
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
process.on('message', data => {
|
|
|
|
if (data === 'graceful-exit') {
|
|
|
|
app.quit()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
process.on('SIGTERM', () => {
|
|
|
|
app.quit()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-04 10:40:28 -05:00
|
|
|
|
2021-07-26 12:15:11 -04:00
|
|
|
async launchApp () {
|
2020-04-24 23:34:04 -04:00
|
|
|
const gotTheLock = app.requestSingleInstanceLock()
|
|
|
|
if (!gotTheLock) {
|
|
|
|
app.quit()
|
|
|
|
} else {
|
2021-08-01 05:02:54 -04:00
|
|
|
await this.beforeReady()
|
2020-04-24 23:34:04 -04:00
|
|
|
this.onReady()
|
|
|
|
this.onRunning()
|
|
|
|
this.onQuit()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const bootstrap = new LifeCycle()
|
|
|
|
|
|
|
|
export {
|
|
|
|
bootstrap
|
|
|
|
}
|