Skip to content

Commit

Permalink
🐛 Fix: handle clipboard file path error
Browse files Browse the repository at this point in the history
ISSUES CLOSED: #97
  • Loading branch information
Molunerfinn committed Aug 21, 2021
1 parent 57fce75 commit ff4ec86
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/core/PicGo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,18 @@ class PicGo extends EventEmitter implements IPicGo {
// upload from clipboard
if (input === undefined || input.length === 0) {
try {
const { imgPath, isExistFile } = await getClipboardImage(this)
const { imgPath, shouldKeepAfterUploading } = await getClipboardImage(this)
if (imgPath === 'no image') {
throw new Error('image not found in clipboard')
} else {
this.once(IBuildInEvent.FAILED, () => {
if (!isExistFile) {
if (!shouldKeepAfterUploading) {
// 删除 picgo 生成的图片文件,例如 `~/.picgo/20200621205720.png`
fs.remove(imgPath).catch((e) => { this.log.error(e) })
}
})
this.once('finished', () => {
if (!isExistFile) {
if (!shouldKeepAfterUploading) {
fs.remove(imgPath).catch((e) => { this.log.error(e) })
}
})
Expand Down
6 changes: 5 additions & 1 deletion src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,11 @@ interface IOptions {
*/
interface IClipboardImage {
imgPath: string
isExistFile: boolean
/**
* if the path is generate by picgo -> false
* if the path is a real file path in system -> true
*/
shouldKeepAfterUploading: boolean
}

/**
Expand Down
22 changes: 17 additions & 5 deletions src/utils/getClipboardImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const getCurrentPlatform = (): string => {
// Thanks to vs-picgo: https://github.com/Spades-S/vs-picgo/blob/master/src/extension.ts
const getClipboardImage = async (ctx: IPicGo): Promise<IClipboardImage> => {
const imagePath = path.join(ctx.baseDir, `${dayjs().format('YYYYMMDDHHmmss')}.png`)
return await new Promise<IClipboardImage>((resolve: Function): void => {
return await new Promise<IClipboardImage>((resolve: Function, reject: Function): void => {
const platform: string = getCurrentPlatform()
let execution
// for PicGo GUI
Expand Down Expand Up @@ -65,23 +65,35 @@ const getClipboardImage = async (ctx: IPicGo): Promise<IClipboardImage> => {
execution.stdout.on('data', (data: Buffer) => {
if (platform === 'linux') {
if (data.toString().trim() === 'no xclip') {
return ctx.emit(IBuildInEvent.NOTIFICATION, {
ctx.emit(IBuildInEvent.NOTIFICATION, {
title: 'xclip not found',
body: 'Please install xclip before run picgo'
})
return reject(new Error('Please install xclip before run picgo'))
}
}
const imgPath = data.toString().trim()
let isExistFile = false

// if the filePath is the real file in system
// we should keep it instead of removing
let shouldKeepAfterUploading = false

// in macOS if your copy the file in system, it's basename will not equal to our default basename
if (path.basename(imgPath) !== path.basename(imagePath)) {
// if the path is not generate by picgo
// but the path exists, we should keep it
if (fs.existsSync(imgPath)) {
isExistFile = true
shouldKeepAfterUploading = true
}
}
// if the imgPath is invalid
if (!fs.existsSync(imgPath)) {
return reject(new Error(`Can't find ${imgPath}`))
}

resolve({
imgPath,
isExistFile
shouldKeepAfterUploading
})
})
})
Expand Down

0 comments on commit ff4ec86

Please sign in to comment.