Skip to content

Commit 321e339

Browse files
committed
🐛 Fix: windows cli uploading bug
ISSUES CLOSED: #657
1 parent 0a986c8 commit 321e339

File tree

3 files changed

+67
-41
lines changed

3 files changed

+67
-41
lines changed

src/main/lifeCycle/index.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,27 @@ import { getUploadFiles } from '~/main/utils/handleArgv'
3131
import db from '#/datastore'
3232
import bus from '@core/bus'
3333
import { privacyManager } from '~/main/utils/privacyManager'
34+
import logger from 'apis/core/picgo/logger'
3435

3536
const isDevelopment = process.env.NODE_ENV !== 'production'
37+
38+
const handleStartUpFiles = (argv: string[], cwd: string) => {
39+
const files = getUploadFiles(argv, cwd, logger)
40+
if (files === null || files.length > 0) { // 如果有文件列表作为参数,说明是命令行启动
41+
if (files === null) {
42+
logger.info('cli -> uploading file from clipboard')
43+
uploadClipboardFiles()
44+
} else {
45+
logger.info('cli -> uploading files from cli', ...files.map(item => item.path))
46+
const win = windowManager.getAvailableWindow()
47+
uploadChoosedFiles(win.webContents, files)
48+
}
49+
return true
50+
} else {
51+
return false
52+
}
53+
}
54+
3655
class LifeCycle {
3756
private beforeReady () {
3857
protocol.registerSchemesAsPrivileged([{ scheme: 'picgo', privileges: { secure: true, standard: true } }])
@@ -69,15 +88,7 @@ class LifeCycle {
6988
})
7089
server.startup()
7190
if (process.env.NODE_ENV !== 'development') {
72-
let files = getUploadFiles()
73-
if (files === null || files.length > 0) { // 如果有文件列表作为参数,说明是命令行启动
74-
if (files === null) {
75-
uploadClipboardFiles()
76-
} else {
77-
const win = windowManager.getAvailableWindow()
78-
uploadChoosedFiles(win.webContents, files)
79-
}
80-
}
91+
handleStartUpFiles(process.argv, process.cwd())
8192
}
8293

8394
if (global.notificationList?.length > 0) {
@@ -91,15 +102,9 @@ class LifeCycle {
91102
}
92103
private onRunning () {
93104
app.on('second-instance', (event, commandLine, workingDirectory) => {
94-
let files = getUploadFiles(commandLine, workingDirectory)
95-
if (files === null || files.length > 0) { // 如果有文件列表作为参数,说明是命令行启动
96-
if (files === null) {
97-
uploadClipboardFiles()
98-
} else {
99-
const win = windowManager.getAvailableWindow()
100-
uploadChoosedFiles(win.webContents, files)
101-
}
102-
} else {
105+
logger.info('detect second instance')
106+
const result = handleStartUpFiles(commandLine, workingDirectory)
107+
if (!result) {
103108
if (windowManager.has(IWindowList.SETTING_WINDOW)) {
104109
const settingWindow = windowManager.get(IWindowList.SETTING_WINDOW)!
105110
if (settingWindow.isMinimized()) {

src/main/migrate/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const updateShortKeyFromVersion212 = (db: typeof DB, shortKeyConfig: IShortKeyCo
2020
name: 'upload',
2121
label: '快捷上传'
2222
}
23+
// @ts-ignore
2324
delete shortKeyConfig.upload
2425
db.set('settings.shortKey', shortKeyConfig)
2526
return true

src/main/utils/handleArgv.ts

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,55 @@
11
import path from 'path'
22
import fs from 'fs-extra'
3-
type ClipboardFileObject = {
3+
import Logger from 'picgo/dist/src/lib/Logger'
4+
interface IResultFileObject {
45
path: string
56
}
6-
type Result = ClipboardFileObject[]
7-
const getUploadFiles = (argv = process.argv, cwd = process.cwd()) => {
8-
let files = argv.slice(1)
9-
if (files.length > 0 && files[0] === 'upload') {
10-
if (files.length === 1) {
7+
type Result = IResultFileObject[]
8+
9+
interface IHandleArgvResult {
10+
success: boolean
11+
fileList?: string[]
12+
}
13+
14+
const handleArgv = (argv: string[]): IHandleArgvResult => {
15+
const uploadIndex = argv.indexOf('upload')
16+
if (uploadIndex !== -1) {
17+
const fileList = argv.slice(1 + uploadIndex)
18+
return {
19+
success: true,
20+
fileList
21+
}
22+
}
23+
return {
24+
success: false
25+
}
26+
}
27+
28+
const getUploadFiles = (argv = process.argv, cwd = process.cwd(), logger: Logger) => {
29+
const { success, fileList } = handleArgv(argv)
30+
if (!success) {
31+
return []
32+
} else {
33+
if (fileList?.length === 0) {
1134
return null // for uploading images in clipboard
12-
} else if (files.length > 1) {
13-
files = argv.slice(1)
14-
let result: Result = []
15-
if (files.length > 0) {
16-
result = files.map(item => {
17-
if (path.isAbsolute(item)) {
35+
} else if ((fileList?.length || 0) > 0) {
36+
const result = fileList!.map(item => {
37+
if (path.isAbsolute(item)) {
38+
return {
39+
path: item
40+
}
41+
} else {
42+
let tempPath = path.join(cwd, item)
43+
if (fs.existsSync(tempPath)) {
1844
return {
19-
path: item
45+
path: tempPath
2046
}
2147
} else {
22-
let tempPath = path.join(cwd, item)
23-
if (fs.existsSync(tempPath)) {
24-
return {
25-
path: tempPath
26-
}
27-
} else {
28-
return null
29-
}
48+
logger.warn(`cli -> can't get file: ${tempPath}, invalid path`)
49+
return null
3050
}
31-
}).filter(item => item !== null) as Result
32-
}
51+
}
52+
}).filter(item => item !== null) as Result
3353
return result
3454
}
3555
}

0 commit comments

Comments
 (0)