From 6b49198d19486367b6e26cf5c72fcb1a5d93a6f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=90=8C=E8=90=8C=E5=93=92=E8=B5=AB=E8=90=9D?= Date: Tue, 18 Apr 2023 19:33:49 +0800 Subject: [PATCH] :sparkles: Feature: add delete local file after uploading, fix compatibility with auto-delete ISSUES CLOSED: #40 --- package.json | 2 +- public/i18n/en.yml | 1 + public/i18n/zh-CN.yml | 1 + public/i18n/zh-TW.yml | 1 + src/main/apis/app/system/index.ts | 6 ++++++ src/main/apis/app/uploader/apis.ts | 11 +++++++++++ src/main/apis/gui/index.ts | 7 +++++++ src/renderer/pages/PicGoSetting.vue | 20 +++++++++++++++++++- src/renderer/pages/Upload.vue | 1 - src/universal/types/i18n.d.ts | 1 + src/universal/types/view.d.ts | 3 ++- yarn.lock | 8 ++++---- 12 files changed, 54 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 9401557..95834fe 100644 --- a/package.json +++ b/package.json @@ -68,7 +68,7 @@ "mime-types": "^2.1.35", "mitt": "^3.0.0", "nodejs-file-downloader": "^4.10.6", - "piclist": "^0.5.1", + "piclist": "^0.5.2", "pinia": "^2.0.32", "pinia-plugin-persistedstate": "^3.1.0", "qiniu": "^7.8.0", diff --git a/public/i18n/en.yml b/public/i18n/en.yml index 4ad14bd..ad52a5f 100644 --- a/public/i18n/en.yml +++ b/public/i18n/en.yml @@ -197,6 +197,7 @@ SETTINGS_ISHIDEDOCK_TIPS: Not support hide dock and tray at the same time SETTINGS_ENCODE_OUTPUT_URL: Encode Output(or Copyed) URL SETTINGS_WATCH_CLIPBOARD: Watch clipboard when software start SETTINGS_SHORT_URL: Use short url +SETTINGS_DELETE_LOCAL_FILE_AFTER_UPLOAD: Delete local file after upload # shortcut-page BUILTIN_CLIPBOARD_TIPS: Use builtin clipboard function to upload instead of using scripts diff --git a/public/i18n/zh-CN.yml b/public/i18n/zh-CN.yml index 98d2e50..8102309 100644 --- a/public/i18n/zh-CN.yml +++ b/public/i18n/zh-CN.yml @@ -199,6 +199,7 @@ SETTINGS_ISHIDEDOCK_TIPS: 不支持同时隐藏dock和托盘 SETTINGS_ENCODE_OUTPUT_URL: 输出(复制) URL 时进行转义 SETTINGS_WATCH_CLIPBOARD: 软件启动时自动监听剪贴板上传 SETTINGS_SHORT_URL: 使用短链接 +SETTINGS_DELETE_LOCAL_FILE_AFTER_UPLOAD: 上传后删除本地文件 # shortcut-page SHORTCUT_NAME: 快捷键名称 diff --git a/public/i18n/zh-TW.yml b/public/i18n/zh-TW.yml index 1ca0cc8..19e6d00 100644 --- a/public/i18n/zh-TW.yml +++ b/public/i18n/zh-TW.yml @@ -197,6 +197,7 @@ SETTINGS_ISHIDEDOCK_TIPS: 不支持同時隱藏dock和托盘 SETTINGS_ENCODE_OUTPUT_URL: 輸出(複製) URL 時進行轉義 SETTINGS_WATCH_CLIPBOARD: 軟體啟動時自動監聽剪貼簿上傳 SETTINGS_SHORT_URL: 使用短網址 +SETTINGS_DELETE_LOCAL_FILE_AFTER_UPLOAD: 上傳後刪除本地檔案 # shortcut-page SHORTCUT_NAME: 快捷鍵名稱 diff --git a/src/main/apis/app/system/index.ts b/src/main/apis/app/system/index.ts index c0fea69..7aab4d3 100644 --- a/src/main/apis/app/system/index.ts +++ b/src/main/apis/app/system/index.ts @@ -23,6 +23,7 @@ import clipboardListener from 'clipboard-event' import clipboardPoll from '~/main/utils/clipboardPoll' import picgo from '../../core/picgo' import { uploadClipboardFiles } from '../uploader/apis' +import { cloneDeep } from 'lodash' let contextMenu: Menu | null let tray: Tray | null @@ -394,13 +395,18 @@ export function createTray () { // so the tray window must be available tray.on('drop-files', async (event: Event, files: string[]) => { const pasteStyle = db.get('settings.pasteStyle') || 'markdown' + const rawInput = cloneDeep(files) const trayWindow = windowManager.get(IWindowList.TRAY_WINDOW)! const imgs = await uploader .setWebContents(trayWindow.webContents) .upload(files) + const deleteLocalFile = db.get('settings.deleteLocalFile') || false if (imgs !== false) { const pasteText: string[] = [] for (let i = 0; i < imgs.length; i++) { + if (deleteLocalFile) { + await fs.remove(rawInput[i]) + } pasteText.push(await (pasteTemplate(pasteStyle, imgs[i], db.get('settings.customLink')))) const notification = new Notification({ title: T('UPLOAD_SUCCEED'), diff --git a/src/main/apis/app/uploader/apis.ts b/src/main/apis/app/uploader/apis.ts index 8b76582..3298978 100644 --- a/src/main/apis/app/uploader/apis.ts +++ b/src/main/apis/app/uploader/apis.ts @@ -13,6 +13,8 @@ import { T } from '~/main/i18n/index' import ALLApi from '@/apis/allApi' import picgo from '@core/picgo' import GuiApi from '../../gui' +import fs from 'fs-extra' +import { cloneDeep } from 'lodash' const handleClipboardUploading = async (): Promise => { const useBuiltinClipboard = db.get('settings.useBuiltinClipboard') === undefined ? true : !!db.get('settings.useBuiltinClipboard') @@ -71,12 +73,21 @@ export const uploadClipboardFiles = async (): Promise => { export const uploadChoosedFiles = async (webContents: WebContents, files: IFileWithPath[]): Promise => { const input = files.map(item => item.path) + const rawInput = cloneDeep(input) const imgs = await uploader.setWebContents(webContents).upload(input) const result = [] if (imgs !== false) { const pasteStyle = db.get('settings.pasteStyle') || 'markdown' + const deleteLocalFile = db.get('settings.deleteLocalFile') || false const pasteText: string[] = [] for (let i = 0; i < imgs.length; i++) { + if (deleteLocalFile) { + fs.remove(rawInput[i]).then(() => { + picgo.log.info(`delete local file: ${rawInput[i]}`) + }).catch((err: Error) => { + picgo.log.error(err) + }) + } pasteText.push(await (pasteTemplate(pasteStyle, imgs[i], db.get('settings.customLink')))) const notification = new Notification({ title: T('UPLOAD_SUCCEED'), diff --git a/src/main/apis/gui/index.ts b/src/main/apis/gui/index.ts index f65e5f2..b3aad3e 100644 --- a/src/main/apis/gui/index.ts +++ b/src/main/apis/gui/index.ts @@ -18,6 +18,8 @@ import { } from '~/universal/events/constants' import { DBStore } from '@picgo/store' import { T } from '~/main/i18n' +import fs from 'fs-extra' +import { cloneDeep } from 'lodash' // Cross-process support may be required in the future class GuiApi implements IGuiApi { @@ -76,11 +78,16 @@ class GuiApi implements IGuiApi { async upload (input: IUploadOption) { this.windowId = await getWindowId() const webContents = this.getWebcontentsByWindowId(this.windowId) + const rawInput = cloneDeep(input) const imgs = await uploader.setWebContents(webContents!).upload(input) if (imgs !== false) { const pasteStyle = db.get('settings.pasteStyle') || 'markdown' + const deleteLocalFile = db.get('settings.deleteLocalFile') || false const pasteText: string[] = [] for (let i = 0; i < imgs.length; i++) { + if (deleteLocalFile) { + await fs.remove(rawInput[i]) + } pasteText.push(await (pasteTemplate(pasteStyle, imgs[i], db.get('settings.customLink')))) const notification = new Notification({ title: T('UPLOAD_SUCCEED'), diff --git a/src/renderer/pages/PicGoSetting.vue b/src/renderer/pages/PicGoSetting.vue index 766c5cb..34b0b84 100644 --- a/src/renderer/pages/PicGoSetting.vue +++ b/src/renderer/pages/PicGoSetting.vue @@ -225,6 +225,16 @@ @change="handleDeleteCloudFile" /> + + + @@ -1073,7 +1083,8 @@ const form = reactive({ isHideDock: false, encodeOutputURL: true, isAutoListenClipboard: false, - useShortUrl: false + useShortUrl: false, + deleteLocalFile: false }) const languageList = i18nManager.languageList.map(item => ({ @@ -1172,6 +1183,7 @@ async function initData () { form.customMiniIcon = settings.customMiniIcon || '' form.isHideDock = settings.isHideDock || false form.useShortUrl = settings.useShortUrl || false + form.deleteLocalFile = settings.deleteLocalFile || false currentLanguage.value = settings.language ?? 'zh-CN' currentStartMode.value = settings.startMode || 'quiet' customLink.value = settings.customLink || '![$fileName]($url)' @@ -1331,6 +1343,12 @@ function handleDeleteCloudFile (val: ICheckBoxValueType) { }) } +function handleDeleteLocalFile (val: ICheckBoxValueType) { + saveConfig({ + 'settings.deleteLocalFile': val + }) +} + function handleRename (val: ICheckBoxValueType) { saveConfig({ 'settings.rename': val diff --git a/src/renderer/pages/Upload.vue b/src/renderer/pages/Upload.vue index 391cfb5..289049a 100644 --- a/src/renderer/pages/Upload.vue +++ b/src/renderer/pages/Upload.vue @@ -630,7 +630,6 @@ async function getUseShortUrl () { } async function handleUseShortUrlChange () { - console.log(useShortUrl.value) saveConfig({ 'settings.useShortUrl': useShortUrl.value }) diff --git a/src/universal/types/i18n.d.ts b/src/universal/types/i18n.d.ts index b28b014..58fb97b 100644 --- a/src/universal/types/i18n.d.ts +++ b/src/universal/types/i18n.d.ts @@ -193,6 +193,7 @@ interface ILocales { SETTINGS_ENCODE_OUTPUT_URL: string SETTINGS_WATCH_CLIPBOARD: string SETTINGS_SHORT_URL: string + SETTINGS_DELETE_LOCAL_FILE_AFTER_UPLOAD: string SHORTCUT_NAME: string SHORTCUT_BIND: string SHORTCUT_STATUS: string diff --git a/src/universal/types/view.d.ts b/src/universal/types/view.d.ts index d7e5f1c..316bade 100644 --- a/src/universal/types/view.d.ts +++ b/src/universal/types/view.d.ts @@ -18,7 +18,8 @@ interface ISettingForm { isHideDock: boolean, encodeOutputURL: boolean, isAutoListenClipboard: boolean, - useShortUrl: boolean + useShortUrl: boolean, + deleteLocalFile: boolean } interface IShortKeyMap { diff --git a/yarn.lock b/yarn.lock index 62b505a..cef542b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -11044,10 +11044,10 @@ performance-now@^2.1.0: resolved "https://registry.npmmirror.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b" integrity sha512-7EAHlyLHI56VEIdK57uwHdHKIaAGbnXPiw0yWbarQZOKaKpvUIgW0jWRVLiatnM+XXlSwsanIBH/hzGMJulMow== -piclist@^0.5.1: - version "0.5.1" - resolved "https://registry.npmjs.org/piclist/-/piclist-0.5.1.tgz#0d2b48c503e3a9eba73b26863c162642b0351b34" - integrity sha512-miF3+vunzE7uzAMuTaPoGYjgSrQWBrVLUOYa+V2NK2Y02CZ1d9AiFQMz73DsrQuch8GCXyDoauTpLZFQjbR5Bg== +piclist@^0.5.2: + version "0.5.2" + resolved "https://registry.npmjs.org/piclist/-/piclist-0.5.2.tgz#bb366e7c07f93b4dbe615ad5e1489647eb8508c7" + integrity sha512-j3s92ZzalSNjG1xu8doBjojTjWs8o1iRtAUqb0BLFnrtpWZ+91zcp6zb3wTMyKbH7WTXR+X35yYIvoNF/E0cvw== dependencies: "@picgo/i18n" "^1.0.0" "@picgo/store" "^2.0.4"