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) { if (deleteLocalFile) {
await fs.remove(rawInput[i]) 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 = const isShowResultNotification =
db.get(configPaths.settings.uploadResultNotification) === undefined db.get(configPaths.settings.uploadResultNotification) === undefined
? true ? true
@ -334,7 +340,7 @@ export function createTray(tooltip: string) {
if (isShowResultNotification) { if (isShowResultNotification) {
const notification = new Notification({ const notification = new Notification({
title: T('UPLOAD_SUCCEED'), title: T('UPLOAD_SUCCEED'),
body: imgs[i].imgUrl! body: shortUrl || imgs[i].imgUrl!
// icon: files[i] // icon: files[i]
}) })
setTimeout(() => { setTimeout(() => {

View File

@ -54,7 +54,9 @@ export const uploadClipboardFiles = async (): Promise<IStringKeyMap> => {
if (img.length > 0) { if (img.length > 0) {
const trayWindow = windowManager.get(IWindowList.TRAY_WINDOW) const trayWindow = windowManager.get(IWindowList.TRAY_WINDOW)
const pasteStyle = db.get(configPaths.settings.pasteStyle) || IPasteStyle.MARKDOWN 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 = const isShowResultNotification =
db.get(configPaths.settings.uploadResultNotification) === undefined db.get(configPaths.settings.uploadResultNotification) === undefined
? true ? true
@ -62,7 +64,7 @@ export const uploadClipboardFiles = async (): Promise<IStringKeyMap> => {
if (isShowResultNotification) { if (isShowResultNotification) {
const notification = new Notification({ const notification = new Notification({
title: T('UPLOAD_SUCCEED'), title: T('UPLOAD_SUCCEED'),
body: img[0].imgUrl! body: shortUrl || img[0].imgUrl!
// icon: img[0].imgUrl // icon: img[0].imgUrl
}) })
setTimeout(() => { setTimeout(() => {
@ -128,7 +130,13 @@ export const uploadChoosedFiles = async (
picgo.log.error(err) 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 = const isShowResultNotification =
db.get(configPaths.settings.uploadResultNotification) === undefined db.get(configPaths.settings.uploadResultNotification) === undefined
? true ? true
@ -136,7 +144,7 @@ export const uploadChoosedFiles = async (
if (isShowResultNotification) { if (isShowResultNotification) {
const notification = new Notification({ const notification = new Notification({
title: T('UPLOAD_SUCCEED'), title: T('UPLOAD_SUCCEED'),
body: imgs[i].imgUrl! body: shortUrl || imgs[i].imgUrl!
// icon: files[i].path // icon: files[i].path
}) })
setTimeout(() => { setTimeout(() => {

View File

@ -95,7 +95,13 @@ class GuiApi implements IGuiApi {
if (deleteLocalFile) { if (deleteLocalFile) {
await fs.remove(rawInput[i]) 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 = const isShowResultNotification =
db.get(configPaths.settings.uploadResultNotification) === undefined db.get(configPaths.settings.uploadResultNotification) === undefined
? true ? true
@ -103,7 +109,7 @@ class GuiApi implements IGuiApi {
if (isShowResultNotification) { if (isShowResultNotification) {
const notification = new Notification({ const notification = new Notification({
title: T('UPLOAD_SUCCEED'), title: T('UPLOAD_SUCCEED'),
body: imgs[i].imgUrl as string body: shortUrl || (imgs[i].imgUrl! as string)
// icon: imgs[i].imgUrl // icon: imgs[i].imgUrl
}) })
setTimeout(() => { setTimeout(() => {

View File

@ -27,11 +27,11 @@ const galleryRoutes = [
const [item, copy = true] = args const [item, copy = true] = args
const pasteStyle = picgo.getConfig<IPasteStyle>(configPaths.settings.pasteStyle) || IPasteStyle.MARKDOWN const pasteStyle = picgo.getConfig<IPasteStyle>(configPaths.settings.pasteStyle) || IPasteStyle.MARKDOWN
const customLink = picgo.getConfig<string>(configPaths.settings.customLink) 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) { if (copy) {
clipboard.writeText(txt) clipboard.writeText(txt)
} }
return txt return [txt, shortUrl]
}, },
type: IRPCType.INVOKE type: IRPCType.INVOKE
}, },

View File

@ -40,7 +40,9 @@ const trayRoutes = [
const img = await uploader.setWebContents(trayWindow.webContents).uploadWithBuildInClipboard() const img = await uploader.setWebContents(trayWindow.webContents).uploadWithBuildInClipboard()
if (img !== false) { if (img !== false) {
const pasteStyle = db.get(configPaths.settings.pasteStyle) || IPasteStyle.MARKDOWN 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 = const isShowResultNotification =
db.get(configPaths.settings.uploadResultNotification) === undefined db.get(configPaths.settings.uploadResultNotification) === undefined
? true ? true
@ -48,7 +50,7 @@ const trayRoutes = [
if (isShowResultNotification) { if (isShowResultNotification) {
const notification = new Notification({ const notification = new Notification({
title: T('UPLOAD_SUCCEED'), title: T('UPLOAD_SUCCEED'),
body: img[0].imgUrl! body: shortUrl || img[0].imgUrl!
// icon: file[0] // icon: file[0]
// icon: img[0].imgUrl // icon: img[0].imgUrl
}) })

View File

@ -54,6 +54,7 @@ router.post(
const picbed = urlparams?.get('picbed') const picbed = urlparams?.get('picbed')
const passedKey = urlparams?.get('key') const passedKey = urlparams?.get('key')
const serverKey = picgo.getConfig<string>(configPaths.settings.serverKey) || '' const serverKey = picgo.getConfig<string>(configPaths.settings.serverKey) || ''
const useShortUrl = picgo.getConfig<boolean>(configPaths.settings.useShortUrl)
if (serverKey && passedKey !== serverKey) { if (serverKey && passedKey !== serverKey) {
handleResponse({ handleResponse({
response, response,
@ -92,8 +93,9 @@ router.post(
// upload with clipboard // upload with clipboard
logger.info('[PicList Server] upload clipboard file') logger.info('[PicList Server] upload clipboard file')
const result = await uploadClipboardFiles() const result = await uploadClipboardFiles()
const res = result.url const res = useShortUrl ? result.fullResult.shortUrl || result.url : result.url
const fullResult = result.fullResult const fullResult = result.fullResult
fullResult.imgUrl = useShortUrl ? fullResult.shortUrl || fullResult.imgUrl : fullResult.imgUrl
logger.info('[PicList Server] upload result:', res) logger.info('[PicList Server] upload result:', res)
if (res) { if (res) {
const treatedFullResult = { const treatedFullResult = {
@ -130,7 +132,7 @@ router.post(
const win = windowManager.getAvailableWindow() const win = windowManager.getAvailableWindow()
const result = await uploadChoosedFiles(win.webContents, pathList) const result = await uploadChoosedFiles(win.webContents, pathList)
const res = result.map(item => { const res = result.map(item => {
return item.url return useShortUrl ? item.fullResult.shortUrl || item.url : item.url
}) })
const fullResult = result.map((item: any) => { const fullResult = result.map((item: any) => {
const treatedItem = { const treatedItem = {
@ -139,6 +141,7 @@ router.post(
...item.fullResult ...item.fullResult
} }
delete treatedItem.config delete treatedItem.config
treatedItem.imgUrl = useShortUrl ? treatedItem.shortUrl || treatedItem.imgUrl : treatedItem.imgUrl
return treatedItem return treatedItem
}) })
logger.info('[PicList Server] upload result', res.join(' ; ')) 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) url = handleUrlEncodeWithSetting(url)
const useShortUrl = db.get(configPaths.settings.useShortUrl) || false const useShortUrl = db.get(configPaths.settings.useShortUrl) || false
if (useShortUrl) { if (useShortUrl) {
url = await generateShortUrl(url) url = item.shortUrl && item.shortUrl !== url ? item.shortUrl : await generateShortUrl(url)
} }
const _customLink = customLink || '![$fileName]($url)' const _customLink = customLink || '![$fileName]($url)'
const tpl = { const tpl = {
@ -45,5 +45,5 @@ export default async (style: IPasteStyle, item: ImgInfo, customLink: string | un
url url
}) })
} }
return tpl[style] return [tpl[style], useShortUrl ? url : '']
} }

View File

@ -546,15 +546,21 @@ function handleClose() {
async function copy(item: ImgInfo) { async function copy(item: ImgInfo) {
item.config = JSON.parse(JSON.stringify(item.config) || '{}') 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 = { const obj = {
title: $T('COPY_LINK_SUCCEED'), title: $T('COPY_LINK_SUCCEED'),
body: copyLink body: result ? result[0] : ''
} }
const myNotification = new Notification(obj.title, obj) const myNotification = new Notification(obj.title, obj)
myNotification.onclick = () => { myNotification.onclick = () => {
return true return true
} }
updateGallery()
} }
function remove(item: ImgInfo) { function remove(item: ImgInfo) {
@ -743,8 +749,13 @@ async function multiCopy() {
if (choosedList[key]) { if (choosedList[key]) {
const item = await $$db.getById<ImgInfo>(key) const item = await $$db.getById<ImgInfo>(key)
if (item) { if (item) {
const txt = await triggerRPC<string>(IRPCActionType.GALLERY_PASTE_TEXT, item) const result = await triggerRPC<string>(IRPCActionType.GALLERY_PASTE_TEXT, item)
copyString.push(txt!) copyString.push(result ? result[0] : '')
if (result && result[1] && item.id) {
await $$db.updateById(item.id, {
shortUrl: result[1]
})
}
choosedList[key] = false choosedList[key] = false
} }
} }
@ -758,6 +769,7 @@ async function multiCopy() {
myNotification.onclick = () => { myNotification.onclick = () => {
return true return true
} }
updateGallery()
} }
} }