Feature(custom): optimize short url

ISSUES CLOSED: #252
This commit is contained in:
Kuingsmile 2024-11-16 10:14:52 +08:00
parent 4209838925
commit fd5316a7b9
8 changed files with 57 additions and 20 deletions

View File

@ -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(() => {

View File

@ -54,7 +54,9 @@ export const uploadClipboardFiles = async (): Promise<IStringKeyMap> => {
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<IStringKeyMap> => {
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(() => {

View File

@ -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(() => {

View File

@ -27,11 +27,11 @@ const galleryRoutes = [
const [item, copy = true] = args
const pasteStyle = picgo.getConfig<IPasteStyle>(configPaths.settings.pasteStyle) || IPasteStyle.MARKDOWN
const customLink = picgo.getConfig<string>(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
},

View File

@ -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
})

View File

@ -54,6 +54,7 @@ router.post(
const picbed = urlparams?.get('picbed')
const passedKey = urlparams?.get('key')
const serverKey = picgo.getConfig<string>(configPaths.settings.serverKey) || ''
const useShortUrl = picgo.getConfig<boolean>(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(' ; '))

View File

@ -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 : '']
}

View File

@ -546,15 +546,21 @@ function handleClose() {
async function copy(item: ImgInfo) {
item.config = JSON.parse(JSON.stringify(item.config) || '{}')
const copyLink = await triggerRPC<string>(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<ImgInfo>(key)
if (item) {
const txt = await triggerRPC<string>(IRPCActionType.GALLERY_PASTE_TEXT, item)
copyString.push(txt!)
const result = await triggerRPC<string>(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()
}
}