diff --git a/src/main/apis/app/system/index.ts b/src/main/apis/app/system/index.ts index 380a680..35b2efb 100644 --- a/src/main/apis/app/system/index.ts +++ b/src/main/apis/app/system/index.ts @@ -326,7 +326,13 @@ export function createTray(tooltip: string) { if (deleteLocalFile) { await fs.remove(rawInput[i]) } - pasteText.push(await pasteTemplate(pasteStyle, imgs[i], db.get(configPaths.settings.customLink))) + const [pasteTextItem, shortUrl] = await pasteTemplate( + pasteStyle, + imgs[i], + db.get(configPaths.settings.customLink) + ) + imgs[i].shortUrl = shortUrl + pasteText.push(pasteTextItem) const isShowResultNotification = db.get(configPaths.settings.uploadResultNotification) === undefined ? true @@ -334,7 +340,7 @@ export function createTray(tooltip: string) { if (isShowResultNotification) { const notification = new Notification({ title: T('UPLOAD_SUCCEED'), - body: imgs[i].imgUrl! + body: shortUrl || imgs[i].imgUrl! // icon: files[i] }) setTimeout(() => { diff --git a/src/main/apis/app/uploader/apis.ts b/src/main/apis/app/uploader/apis.ts index 068ff1f..13c87bf 100644 --- a/src/main/apis/app/uploader/apis.ts +++ b/src/main/apis/app/uploader/apis.ts @@ -54,7 +54,9 @@ export const uploadClipboardFiles = async (): Promise => { if (img.length > 0) { const trayWindow = windowManager.get(IWindowList.TRAY_WINDOW) const pasteStyle = db.get(configPaths.settings.pasteStyle) || IPasteStyle.MARKDOWN - handleCopyUrl(await pasteTemplate(pasteStyle, img[0], db.get(configPaths.settings.customLink))) + const [pastedText, shortUrl] = await pasteTemplate(pasteStyle, img[0], db.get(configPaths.settings.customLink)) + img[0].shortUrl = shortUrl + handleCopyUrl(pastedText) const isShowResultNotification = db.get(configPaths.settings.uploadResultNotification) === undefined ? true @@ -62,7 +64,7 @@ export const uploadClipboardFiles = async (): Promise => { if (isShowResultNotification) { const notification = new Notification({ title: T('UPLOAD_SUCCEED'), - body: img[0].imgUrl! + body: shortUrl || img[0].imgUrl! // icon: img[0].imgUrl }) setTimeout(() => { @@ -128,7 +130,13 @@ export const uploadChoosedFiles = async ( picgo.log.error(err) }) } - pasteText.push(await pasteTemplate(pasteStyle, imgs[i], db.get(configPaths.settings.customLink))) + const [pasteTextItem, shortUrl] = await pasteTemplate( + pasteStyle, + imgs[i], + db.get(configPaths.settings.customLink) + ) + imgs[i].shortUrl = shortUrl + pasteText.push(pasteTextItem) const isShowResultNotification = db.get(configPaths.settings.uploadResultNotification) === undefined ? true @@ -136,7 +144,7 @@ export const uploadChoosedFiles = async ( if (isShowResultNotification) { const notification = new Notification({ title: T('UPLOAD_SUCCEED'), - body: imgs[i].imgUrl! + body: shortUrl || imgs[i].imgUrl! // icon: files[i].path }) setTimeout(() => { diff --git a/src/main/apis/gui/index.ts b/src/main/apis/gui/index.ts index 2d571b1..a2b2537 100644 --- a/src/main/apis/gui/index.ts +++ b/src/main/apis/gui/index.ts @@ -95,7 +95,13 @@ class GuiApi implements IGuiApi { if (deleteLocalFile) { await fs.remove(rawInput[i]) } - pasteText.push(await pasteTemplate(pasteStyle, imgs[i], db.get(configPaths.settings.customLink))) + const [pasteTextItem, shortUrl] = await pasteTemplate( + pasteStyle, + imgs[i], + db.get(configPaths.settings.customLink) + ) + imgs[i].shortUrl = shortUrl + pasteText.push(pasteTextItem) const isShowResultNotification = db.get(configPaths.settings.uploadResultNotification) === undefined ? true @@ -103,7 +109,7 @@ class GuiApi implements IGuiApi { if (isShowResultNotification) { const notification = new Notification({ title: T('UPLOAD_SUCCEED'), - body: imgs[i].imgUrl as string + body: shortUrl || (imgs[i].imgUrl! as string) // icon: imgs[i].imgUrl }) setTimeout(() => { diff --git a/src/main/events/rpc/routes/gallery/index.ts b/src/main/events/rpc/routes/gallery/index.ts index fc049b8..ec66114 100644 --- a/src/main/events/rpc/routes/gallery/index.ts +++ b/src/main/events/rpc/routes/gallery/index.ts @@ -27,11 +27,11 @@ const galleryRoutes = [ const [item, copy = true] = args const pasteStyle = picgo.getConfig(configPaths.settings.pasteStyle) || IPasteStyle.MARKDOWN const customLink = picgo.getConfig(configPaths.settings.customLink) - const txt = await pasteTemplate(pasteStyle, item, customLink) + const [txt, shortUrl] = await pasteTemplate(pasteStyle, item, customLink) if (copy) { clipboard.writeText(txt) } - return txt + return [txt, shortUrl] }, type: IRPCType.INVOKE }, diff --git a/src/main/events/rpc/routes/tray/index.ts b/src/main/events/rpc/routes/tray/index.ts index a48cc2e..930d3c9 100644 --- a/src/main/events/rpc/routes/tray/index.ts +++ b/src/main/events/rpc/routes/tray/index.ts @@ -40,7 +40,9 @@ const trayRoutes = [ const img = await uploader.setWebContents(trayWindow.webContents).uploadWithBuildInClipboard() if (img !== false) { const pasteStyle = db.get(configPaths.settings.pasteStyle) || IPasteStyle.MARKDOWN - handleCopyUrl(await pasteTemplate(pasteStyle, img[0], db.get(configPaths.settings.customLink))) + const [pasteText, shortUrl] = await pasteTemplate(pasteStyle, img[0], db.get(configPaths.settings.customLink)) + img[0].shortUrl = shortUrl + handleCopyUrl(pasteText) const isShowResultNotification = db.get(configPaths.settings.uploadResultNotification) === undefined ? true @@ -48,7 +50,7 @@ const trayRoutes = [ if (isShowResultNotification) { const notification = new Notification({ title: T('UPLOAD_SUCCEED'), - body: img[0].imgUrl! + body: shortUrl || img[0].imgUrl! // icon: file[0] // icon: img[0].imgUrl }) diff --git a/src/main/server/routerManager.ts b/src/main/server/routerManager.ts index 71718e9..32d9cae 100644 --- a/src/main/server/routerManager.ts +++ b/src/main/server/routerManager.ts @@ -54,6 +54,7 @@ router.post( const picbed = urlparams?.get('picbed') const passedKey = urlparams?.get('key') const serverKey = picgo.getConfig(configPaths.settings.serverKey) || '' + const useShortUrl = picgo.getConfig(configPaths.settings.useShortUrl) if (serverKey && passedKey !== serverKey) { handleResponse({ response, @@ -92,8 +93,9 @@ router.post( // upload with clipboard logger.info('[PicList Server] upload clipboard file') const result = await uploadClipboardFiles() - const res = result.url + const res = useShortUrl ? result.fullResult.shortUrl || result.url : result.url const fullResult = result.fullResult + fullResult.imgUrl = useShortUrl ? fullResult.shortUrl || fullResult.imgUrl : fullResult.imgUrl logger.info('[PicList Server] upload result:', res) if (res) { const treatedFullResult = { @@ -130,7 +132,7 @@ router.post( const win = windowManager.getAvailableWindow() const result = await uploadChoosedFiles(win.webContents, pathList) const res = result.map(item => { - return item.url + return useShortUrl ? item.fullResult.shortUrl || item.url : item.url }) const fullResult = result.map((item: any) => { const treatedItem = { @@ -139,6 +141,7 @@ router.post( ...item.fullResult } delete treatedItem.config + treatedItem.imgUrl = useShortUrl ? treatedItem.shortUrl || treatedItem.imgUrl : treatedItem.imgUrl return treatedItem }) logger.info('[PicList Server] upload result', res.join(' ; ')) diff --git a/src/main/utils/pasteTemplate.ts b/src/main/utils/pasteTemplate.ts index 605706d..ee02ca8 100644 --- a/src/main/utils/pasteTemplate.ts +++ b/src/main/utils/pasteTemplate.ts @@ -32,7 +32,7 @@ export default async (style: IPasteStyle, item: ImgInfo, customLink: string | un url = handleUrlEncodeWithSetting(url) const useShortUrl = db.get(configPaths.settings.useShortUrl) || false if (useShortUrl) { - url = await generateShortUrl(url) + url = item.shortUrl && item.shortUrl !== url ? item.shortUrl : await generateShortUrl(url) } const _customLink = customLink || '![$fileName]($url)' const tpl = { @@ -45,5 +45,5 @@ export default async (style: IPasteStyle, item: ImgInfo, customLink: string | un url }) } - return tpl[style] + return [tpl[style], useShortUrl ? url : ''] } diff --git a/src/renderer/pages/Gallery.vue b/src/renderer/pages/Gallery.vue index 7e6f8c0..0b9a5ee 100644 --- a/src/renderer/pages/Gallery.vue +++ b/src/renderer/pages/Gallery.vue @@ -546,15 +546,21 @@ function handleClose() { async function copy(item: ImgInfo) { item.config = JSON.parse(JSON.stringify(item.config) || '{}') - const copyLink = await triggerRPC(IRPCActionType.GALLERY_PASTE_TEXT, item) + const result = await triggerRPC<[string, string]>(IRPCActionType.GALLERY_PASTE_TEXT, item) + if (result && result[1] && item.id) { + await $$db.updateById(item.id, { + shortUrl: result[1] + }) + } const obj = { title: $T('COPY_LINK_SUCCEED'), - body: copyLink + body: result ? result[0] : '' } const myNotification = new Notification(obj.title, obj) myNotification.onclick = () => { return true } + updateGallery() } function remove(item: ImgInfo) { @@ -743,8 +749,13 @@ async function multiCopy() { if (choosedList[key]) { const item = await $$db.getById(key) if (item) { - const txt = await triggerRPC(IRPCActionType.GALLERY_PASTE_TEXT, item) - copyString.push(txt!) + const result = await triggerRPC(IRPCActionType.GALLERY_PASTE_TEXT, item) + copyString.push(result ? result[0] : '') + if (result && result[1] && item.id) { + await $$db.updateById(item.id, { + shortUrl: result[1] + }) + } choosedList[key] = false } } @@ -758,6 +769,7 @@ async function multiCopy() { myNotification.onclick = () => { return true } + updateGallery() } }