🐛 Fix: windows cli uploading bug

ISSUES CLOSED: #657
This commit is contained in:
PiEgg 2021-04-24 00:44:49 +08:00
parent 0a986c8933
commit 321e33967c
3 changed files with 67 additions and 41 deletions

View File

@ -31,8 +31,27 @@ import { getUploadFiles } from '~/main/utils/handleArgv'
import db from '#/datastore' import db from '#/datastore'
import bus from '@core/bus' import bus from '@core/bus'
import { privacyManager } from '~/main/utils/privacyManager' import { privacyManager } from '~/main/utils/privacyManager'
import logger from 'apis/core/picgo/logger'
const isDevelopment = process.env.NODE_ENV !== 'production' const isDevelopment = process.env.NODE_ENV !== 'production'
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
} else {
return false
}
}
class LifeCycle { class LifeCycle {
private beforeReady () { private beforeReady () {
protocol.registerSchemesAsPrivileged([{ scheme: 'picgo', privileges: { secure: true, standard: true } }]) protocol.registerSchemesAsPrivileged([{ scheme: 'picgo', privileges: { secure: true, standard: true } }])
@ -69,15 +88,7 @@ class LifeCycle {
}) })
server.startup() server.startup()
if (process.env.NODE_ENV !== 'development') { if (process.env.NODE_ENV !== 'development') {
let files = getUploadFiles() handleStartUpFiles(process.argv, process.cwd())
if (files === null || files.length > 0) { // 如果有文件列表作为参数,说明是命令行启动
if (files === null) {
uploadClipboardFiles()
} else {
const win = windowManager.getAvailableWindow()
uploadChoosedFiles(win.webContents, files)
}
}
} }
if (global.notificationList?.length > 0) { if (global.notificationList?.length > 0) {
@ -91,15 +102,9 @@ class LifeCycle {
} }
private onRunning () { private onRunning () {
app.on('second-instance', (event, commandLine, workingDirectory) => { app.on('second-instance', (event, commandLine, workingDirectory) => {
let files = getUploadFiles(commandLine, workingDirectory) logger.info('detect second instance')
if (files === null || files.length > 0) { // 如果有文件列表作为参数,说明是命令行启动 const result = handleStartUpFiles(commandLine, workingDirectory)
if (files === null) { if (!result) {
uploadClipboardFiles()
} else {
const win = windowManager.getAvailableWindow()
uploadChoosedFiles(win.webContents, files)
}
} else {
if (windowManager.has(IWindowList.SETTING_WINDOW)) { if (windowManager.has(IWindowList.SETTING_WINDOW)) {
const settingWindow = windowManager.get(IWindowList.SETTING_WINDOW)! const settingWindow = windowManager.get(IWindowList.SETTING_WINDOW)!
if (settingWindow.isMinimized()) { if (settingWindow.isMinimized()) {

View File

@ -20,6 +20,7 @@ const updateShortKeyFromVersion212 = (db: typeof DB, shortKeyConfig: IShortKeyCo
name: 'upload', name: 'upload',
label: '快捷上传' label: '快捷上传'
} }
// @ts-ignore
delete shortKeyConfig.upload delete shortKeyConfig.upload
db.set('settings.shortKey', shortKeyConfig) db.set('settings.shortKey', shortKeyConfig)
return true return true

View File

@ -1,35 +1,55 @@
import path from 'path' import path from 'path'
import fs from 'fs-extra' import fs from 'fs-extra'
type ClipboardFileObject = { import Logger from 'picgo/dist/src/lib/Logger'
interface IResultFileObject {
path: string path: string
} }
type Result = ClipboardFileObject[] type Result = IResultFileObject[]
const getUploadFiles = (argv = process.argv, cwd = process.cwd()) => {
let files = argv.slice(1) interface IHandleArgvResult {
if (files.length > 0 && files[0] === 'upload') { success: boolean
if (files.length === 1) { fileList?: string[]
}
const handleArgv = (argv: string[]): IHandleArgvResult => {
const uploadIndex = argv.indexOf('upload')
if (uploadIndex !== -1) {
const fileList = argv.slice(1 + uploadIndex)
return {
success: true,
fileList
}
}
return {
success: false
}
}
const getUploadFiles = (argv = process.argv, cwd = process.cwd(), logger: Logger) => {
const { success, fileList } = handleArgv(argv)
if (!success) {
return []
} else {
if (fileList?.length === 0) {
return null // for uploading images in clipboard return null // for uploading images in clipboard
} else if (files.length > 1) { } else if ((fileList?.length || 0) > 0) {
files = argv.slice(1) const result = fileList!.map(item => {
let result: Result = [] if (path.isAbsolute(item)) {
if (files.length > 0) { return {
result = files.map(item => { path: item
if (path.isAbsolute(item)) { }
} else {
let tempPath = path.join(cwd, item)
if (fs.existsSync(tempPath)) {
return { return {
path: item path: tempPath
} }
} else { } else {
let tempPath = path.join(cwd, item) logger.warn(`cli -> can't get file: ${tempPath}, invalid path`)
if (fs.existsSync(tempPath)) { return null
return {
path: tempPath
}
} else {
return null
}
} }
}).filter(item => item !== null) as Result }
} }).filter(item => item !== null) as Result
return result return result
} }
} }