diff --git a/src/core/PicGo.ts b/src/core/PicGo.ts index 96b9b38..5ad55c8 100644 --- a/src/core/PicGo.ts +++ b/src/core/PicGo.ts @@ -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) }) } }) diff --git a/src/types/index.d.ts b/src/types/index.d.ts index 9ed80e9..20802c0 100644 --- a/src/types/index.d.ts +++ b/src/types/index.d.ts @@ -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 } /** diff --git a/src/utils/getClipboardImage.ts b/src/utils/getClipboardImage.ts index f16e9d5..0873af3 100644 --- a/src/utils/getClipboardImage.ts +++ b/src/utils/getClipboardImage.ts @@ -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 => { const imagePath = path.join(ctx.baseDir, `${dayjs().format('YYYYMMDDHHmmss')}.png`) - return await new Promise((resolve: Function): void => { + return await new Promise((resolve: Function, reject: Function): void => { const platform: string = getCurrentPlatform() let execution // for PicGo GUI @@ -65,23 +65,35 @@ const getClipboardImage = async (ctx: IPicGo): Promise => { 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 }) }) })