Skip to content

Commit

Permalink
✨ Feature: add delete local file after uploading, fix compatibility w…
Browse files Browse the repository at this point in the history
…ith auto-delete

ISSUES CLOSED: #40
  • Loading branch information
Kuingsmile committed Apr 18, 2023
1 parent 34997f5 commit 6b49198
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions public/i18n/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions public/i18n/zh-CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: 快捷键名称
Expand Down
1 change: 1 addition & 0 deletions public/i18n/zh-TW.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: 快捷鍵名稱
Expand Down
6 changes: 6 additions & 0 deletions src/main/apis/app/system/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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'),
Expand Down
11 changes: 11 additions & 0 deletions src/main/apis/app/uploader/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<false | ImgInfo[]> => {
const useBuiltinClipboard = db.get('settings.useBuiltinClipboard') === undefined ? true : !!db.get('settings.useBuiltinClipboard')
Expand Down Expand Up @@ -71,12 +73,21 @@ export const uploadClipboardFiles = async (): Promise<IStringKeyMap> => {

export const uploadChoosedFiles = async (webContents: WebContents, files: IFileWithPath[]): Promise<IStringKeyMap[]> => {
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'),
Expand Down
7 changes: 7 additions & 0 deletions src/main/apis/gui/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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'),
Expand Down
20 changes: 19 additions & 1 deletion src/renderer/pages/PicGoSetting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,16 @@
@change="handleDeleteCloudFile"
/>
</el-form-item>
<el-form-item
:label="$T('SETTINGS_DELETE_LOCAL_FILE_AFTER_UPLOAD')"
>
<el-switch
v-model="form.deleteLocalFile"
:active-text="$T('SETTINGS_OPEN')"
:inactive-text="$T('SETTINGS_CLOSE')"
@change="handleDeleteLocalFile"
/>
</el-form-item>
<el-form-item
:label="$T('SETTINGS_RENAME_BEFORE_UPLOAD')"
>
Expand Down Expand Up @@ -1073,7 +1083,8 @@ const form = reactive<ISettingForm>({
isHideDock: false,
encodeOutputURL: true,
isAutoListenClipboard: false,
useShortUrl: false
useShortUrl: false,
deleteLocalFile: false
})
const languageList = i18nManager.languageList.map(item => ({
Expand Down Expand Up @@ -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)'
Expand Down Expand Up @@ -1331,6 +1343,12 @@ function handleDeleteCloudFile (val: ICheckBoxValueType) {
})
}
function handleDeleteLocalFile (val: ICheckBoxValueType) {
saveConfig({
'settings.deleteLocalFile': val
})
}
function handleRename (val: ICheckBoxValueType) {
saveConfig({
'settings.rename': val
Expand Down
1 change: 0 additions & 1 deletion src/renderer/pages/Upload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,6 @@ async function getUseShortUrl () {
}
async function handleUseShortUrlChange () {
console.log(useShortUrl.value)
saveConfig({
'settings.useShortUrl': useShortUrl.value
})
Expand Down
1 change: 1 addition & 0 deletions src/universal/types/i18n.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/universal/types/view.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ interface ISettingForm {
isHideDock: boolean,
encodeOutputURL: boolean,
isAutoListenClipboard: boolean,
useShortUrl: boolean
useShortUrl: boolean,
deleteLocalFile: boolean
}

interface IShortKeyMap {
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 6b49198

Please sign in to comment.