From 2fcec70721a1dd50fa65b51d27887383d136b391 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: Wed, 6 Sep 2023 08:40:50 -0700 Subject: [PATCH] :sparkles: Feature: upload api now support url query picbed and configname ISSUES CLOSED: #93 --- public/i18n/en.yml | 1 + public/i18n/zh-CN.yml | 1 + public/i18n/zh-TW.yml | 1 + src/main/server/index.ts | 10 +++--- src/main/server/router.ts | 10 +++--- src/main/server/routerManager.ts | 36 ++++++++++++++++++++-- src/renderer/pages/picbeds/index.vue | 46 +++++++++++++++++++++++----- src/universal/types/i18n.d.ts | 1 + 8 files changed, 88 insertions(+), 18 deletions(-) diff --git a/public/i18n/en.yml b/public/i18n/en.yml index a892652c..92967df9 100644 --- a/public/i18n/en.yml +++ b/public/i18n/en.yml @@ -99,6 +99,7 @@ GALLERY_SEARCH_FILENAME: Search by Filename GALLERY_SEARCH_URL: Search by URL GALLERY_MATCHED: ' Matched: ' +UPLOAD_PAGE_COPY_UPLOAD_API: Copy Upload API UPLOAD_PAGE_IMAGE_PROCESS_NAME: Image Processing UPLOAD_PAGE_IMAGE_PROCESS_DIALOG_TITLE: Image Processing Settings UPLOAD_PAGE_IMAGE_PROCESS_ISADDWM: Add Watermark diff --git a/public/i18n/zh-CN.yml b/public/i18n/zh-CN.yml index 3f2f1b77..700810ac 100644 --- a/public/i18n/zh-CN.yml +++ b/public/i18n/zh-CN.yml @@ -99,6 +99,7 @@ GALLERY_SEARCH_FILENAME: 搜索文件名 GALLERY_SEARCH_URL: 搜索URL GALLERY_MATCHED: ' 匹配到: ' +UPLOAD_PAGE_COPY_UPLOAD_API: 复制上传API UPLOAD_PAGE_IMAGE_PROCESS_NAME: 图片处理 UPLOAD_PAGE_IMAGE_PROCESS_DIALOG_TITLE: 图片处理设置 UPLOAD_PAGE_IMAGE_PROCESS_ISADDWM: 是否添加水印 diff --git a/public/i18n/zh-TW.yml b/public/i18n/zh-TW.yml index e2e4dc89..2b2adb1d 100644 --- a/public/i18n/zh-TW.yml +++ b/public/i18n/zh-TW.yml @@ -99,6 +99,7 @@ GALLERY_SEARCH_FILENAME: 搜尋文件名 GALLERY_SEARCH_URL: 搜尋URL GALLERY_MATCHED: ' 匹配到: ' +UPLOAD_PAGE_COPY_UPLOAD_API: 複製上傳API UPLOAD_PAGE_IMAGE_PROCESS_NAME: 圖片處理 UPLOAD_PAGE_IMAGE_PROCESS_DIALOG_TITLE: 圖片處理設置 UPLOAD_PAGE_IMAGE_PROCESS_ISADDWM: 是否添加水印 diff --git a/src/main/server/index.ts b/src/main/server/index.ts index 32f78e51..a59b2a43 100644 --- a/src/main/server/index.ts +++ b/src/main/server/index.ts @@ -44,8 +44,9 @@ class Server { } if (request.method === 'POST') { - if (!routers.getHandler(request.url!)) { - logger.warn(`[PicList Server] don't support [${request.url}] url`) + const [url, query] = request.url!.split('?') + if (!routers.getHandler(url!)) { + logger.warn(`[PicList Server] don't support [${url}] url`) handleResponse({ response, statusCode: 404, @@ -73,10 +74,11 @@ class Server { }) } logger.info('[PicList Server] get the request', body) - const handler = routers.getHandler(request.url!) + const handler = routers.getHandler(url!)?.handler handler!({ ...postObj, - response + response, + urlparams: query ? new URLSearchParams(query) : undefined }) }) } diff --git a/src/main/server/router.ts b/src/main/server/router.ts index 4c984aee..1f5dc3e1 100644 --- a/src/main/server/router.ts +++ b/src/main/server/router.ts @@ -1,12 +1,12 @@ class Router { - private router = new Map() + private router = new Map() - get (url: string, callback: routeHandler): void { - this.router.set(url, callback) + get (url: string, callback: routeHandler, urlparams?: URLSearchParams): void { + this.router.set(url, { handler: callback, urlparams }) } - post (url: string, callback: routeHandler): void { - this.router.set(url, callback) + post (url: string, callback: routeHandler, urlparams?: URLSearchParams): void { + this.router.set(url, { handler: callback, urlparams }) } getHandler (url: string) { diff --git a/src/main/server/routerManager.ts b/src/main/server/routerManager.ts index c21eed6d..f391dd72 100644 --- a/src/main/server/routerManager.ts +++ b/src/main/server/routerManager.ts @@ -7,6 +7,8 @@ import windowManager from 'apis/app/window/windowManager' import { uploadChoosedFiles, uploadClipboardFiles, deleteChoosedFiles } from 'apis/app/uploader/apis' import path from 'path' import { dbPathDir } from 'apis/core/datastore/dbChecker' +import picgo from '@core/picgo' +import { changeCurrentUploader } from '../utils/handleUploaderConfig' const STORE_PATH = dbPathDir() const LOG_PATH = path.join(STORE_PATH, 'piclist.log') @@ -16,12 +18,39 @@ const deleteErrorMessage = `delete error. see ${LOG_PATH} for more detail.` router.post('/upload', async ({ response, - list = [] + list = [], + urlparams } : { response: IHttpResponse, - list?: string[] + list?: string[], + urlparams?: URLSearchParams }): Promise => { try { + const picbed = urlparams?.get('picbed') + let currentPicBedType = '' + let currentPicBedConfig = {} as IStringKeyMap + let currentPicBedConfigId = '' + let needRestore = false + if (picbed) { + const configName = urlparams?.get('configName') || 'Default' + const currentPicBed = picgo.getConfig('picBed') || {} as IStringKeyMap + currentPicBedType = currentPicBed?.current + currentPicBedConfig = currentPicBed?.[currentPicBedType] + currentPicBedConfigId = currentPicBedConfig?._id + if (picbed === currentPicBedType && configName === currentPicBedConfig._configName) { + // do nothing + } else { + needRestore = true + const picBeds = picgo.getConfig('uploader') + const currentPicBedList = picBeds?.[picbed]?.configList + if (currentPicBedList) { + const currentConfig = currentPicBedList?.find((item: any) => item._configName === configName) + if (currentConfig) { + changeCurrentUploader(picbed, currentConfig, currentConfig._id) + } + } + } + } if (list.length === 0) { // upload with clipboard logger.info('[PicList Server] upload clipboard file') @@ -83,6 +112,9 @@ router.post('/upload', async ({ }) } } + if (needRestore) { + changeCurrentUploader(currentPicBedType, currentPicBedConfig, currentPicBedConfigId) + } } catch (err: any) { logger.error(err) handleResponse({ diff --git a/src/renderer/pages/picbeds/index.vue b/src/renderer/pages/picbeds/index.vue index b2e317c2..03178711 100644 --- a/src/renderer/pages/picbeds/index.vue +++ b/src/renderer/pages/picbeds/index.vue @@ -10,14 +10,26 @@ >
- {{ picBedName }} {{ $T('SETTINGS') }} + + {{ picBedName }} {{ $T('SETTINGS') }} + + {{ $T('UPLOAD_PAGE_COPY_UPLOAD_API') }} +
([]) @@ -187,7 +200,7 @@ const handleReset = async () => { $router.back() } -const linkToLogInList = ['github', 'tcyun', 'aliyun', 'smms', 'qiniu', 'imgur', 'upyun', 'githubPlus'] +const linkToLogInList = ['GitHub', '腾讯云COS', '阿里云OSS', 'SM.MS', '七牛云', 'Imgur', '又拍云', 'githubPlus'] function handleNameClick () { switch ($route.params.type) { @@ -218,7 +231,26 @@ function handleNameClick () { } } -function getPicBeds (event: IpcRendererEvent, _config: IPicGoPluginConfig[], name: string) { +async function handleCopyApi () { + try { + const serverConfig = await getConfig('settings.server') || { + port: 36677, + host: '127.0.0.1' + } + const { port, host } = serverConfig + const uploader = await getConfig('uploader') as IStringKeyMap || {} + const picBedConfigList = uploader[$route.params.type as string].configList || [] + const picBedConfig = picBedConfigList.find((item: IUploaderConfigListItem) => item._id === $route.params.configId) + const apiUrl = `http://${host}:${port}/upload?picbed=${$route.params.type}&configName=${picBedConfig?._configName}` + clipboard.writeText(apiUrl) + ElMessage.success($T('MANAGE_BUCKET_COPY_SUCCESS') + ' ' + apiUrl) + } catch (error) { + console.log(error) + ElMessage.error('Copy failed') + } +} + +function getPicBeds (_event: IpcRendererEvent, _config: IPicGoPluginConfig[], name: string) { config.value = _config picBedName.value = name } @@ -245,7 +277,7 @@ export default { height 100% overflow-y auto overflow-x hidden - .view-title + .view-title-text &:hover cursor pointer color #409EFF diff --git a/src/universal/types/i18n.d.ts b/src/universal/types/i18n.d.ts index 306081ad..a0615f2d 100644 --- a/src/universal/types/i18n.d.ts +++ b/src/universal/types/i18n.d.ts @@ -96,6 +96,7 @@ interface ILocales { GALLERY_SEARCH_FILENAME: string GALLERY_SEARCH_URL: string GALLERY_MATCHED: string + UPLOAD_PAGE_COPY_UPLOAD_API: string UPLOAD_PAGE_IMAGE_PROCESS_NAME: string UPLOAD_PAGE_IMAGE_PROCESS_DIALOG_TITLE: string UPLOAD_PAGE_IMAGE_PROCESS_ISADDWM: string