Skip to content

Commit ff4ec86

Browse files
committed
🐛 Fix: handle clipboard file path error
ISSUES CLOSED: #97
1 parent 57fce75 commit ff4ec86

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

src/core/PicGo.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,18 +182,18 @@ class PicGo extends EventEmitter implements IPicGo {
182182
// upload from clipboard
183183
if (input === undefined || input.length === 0) {
184184
try {
185-
const { imgPath, isExistFile } = await getClipboardImage(this)
185+
const { imgPath, shouldKeepAfterUploading } = await getClipboardImage(this)
186186
if (imgPath === 'no image') {
187187
throw new Error('image not found in clipboard')
188188
} else {
189189
this.once(IBuildInEvent.FAILED, () => {
190-
if (!isExistFile) {
190+
if (!shouldKeepAfterUploading) {
191191
// 删除 picgo 生成的图片文件,例如 `~/.picgo/20200621205720.png`
192192
fs.remove(imgPath).catch((e) => { this.log.error(e) })
193193
}
194194
})
195195
this.once('finished', () => {
196-
if (!isExistFile) {
196+
if (!shouldKeepAfterUploading) {
197197
fs.remove(imgPath).catch((e) => { this.log.error(e) })
198198
}
199199
})

src/types/index.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,11 @@ interface IOptions {
437437
*/
438438
interface IClipboardImage {
439439
imgPath: string
440-
isExistFile: boolean
440+
/**
441+
* if the path is generate by picgo -> false
442+
* if the path is a real file path in system -> true
443+
*/
444+
shouldKeepAfterUploading: boolean
441445
}
442446

443447
/**

src/utils/getClipboardImage.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const getCurrentPlatform = (): string => {
2727
// Thanks to vs-picgo: https://github.com/Spades-S/vs-picgo/blob/master/src/extension.ts
2828
const getClipboardImage = async (ctx: IPicGo): Promise<IClipboardImage> => {
2929
const imagePath = path.join(ctx.baseDir, `${dayjs().format('YYYYMMDDHHmmss')}.png`)
30-
return await new Promise<IClipboardImage>((resolve: Function): void => {
30+
return await new Promise<IClipboardImage>((resolve: Function, reject: Function): void => {
3131
const platform: string = getCurrentPlatform()
3232
let execution
3333
// for PicGo GUI
@@ -65,23 +65,35 @@ const getClipboardImage = async (ctx: IPicGo): Promise<IClipboardImage> => {
6565
execution.stdout.on('data', (data: Buffer) => {
6666
if (platform === 'linux') {
6767
if (data.toString().trim() === 'no xclip') {
68-
return ctx.emit(IBuildInEvent.NOTIFICATION, {
68+
ctx.emit(IBuildInEvent.NOTIFICATION, {
6969
title: 'xclip not found',
7070
body: 'Please install xclip before run picgo'
7171
})
72+
return reject(new Error('Please install xclip before run picgo'))
7273
}
7374
}
7475
const imgPath = data.toString().trim()
75-
let isExistFile = false
76+
77+
// if the filePath is the real file in system
78+
// we should keep it instead of removing
79+
let shouldKeepAfterUploading = false
80+
7681
// in macOS if your copy the file in system, it's basename will not equal to our default basename
7782
if (path.basename(imgPath) !== path.basename(imagePath)) {
83+
// if the path is not generate by picgo
84+
// but the path exists, we should keep it
7885
if (fs.existsSync(imgPath)) {
79-
isExistFile = true
86+
shouldKeepAfterUploading = true
8087
}
8188
}
89+
// if the imgPath is invalid
90+
if (!fs.existsSync(imgPath)) {
91+
return reject(new Error(`Can't find ${imgPath}`))
92+
}
93+
8294
resolve({
8395
imgPath,
84-
isExistFile
96+
shouldKeepAfterUploading
8597
})
8698
})
8799
})

0 commit comments

Comments
 (0)