Skip to content

Commit 2fcec70

Browse files
committed
✨ Feature: upload api now support url query picbed and configname
ISSUES CLOSED: #93
1 parent f9a3f24 commit 2fcec70

File tree

8 files changed

+88
-18
lines changed

8 files changed

+88
-18
lines changed

public/i18n/en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ GALLERY_SEARCH_FILENAME: Search by Filename
9999
GALLERY_SEARCH_URL: Search by URL
100100
GALLERY_MATCHED: ' Matched: '
101101

102+
UPLOAD_PAGE_COPY_UPLOAD_API: Copy Upload API
102103
UPLOAD_PAGE_IMAGE_PROCESS_NAME: Image Processing
103104
UPLOAD_PAGE_IMAGE_PROCESS_DIALOG_TITLE: Image Processing Settings
104105
UPLOAD_PAGE_IMAGE_PROCESS_ISADDWM: Add Watermark

public/i18n/zh-CN.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ GALLERY_SEARCH_FILENAME: 搜索文件名
9999
GALLERY_SEARCH_URL: 搜索URL
100100
GALLERY_MATCHED: ' 匹配到: '
101101

102+
UPLOAD_PAGE_COPY_UPLOAD_API: 复制上传API
102103
UPLOAD_PAGE_IMAGE_PROCESS_NAME: 图片处理
103104
UPLOAD_PAGE_IMAGE_PROCESS_DIALOG_TITLE: 图片处理设置
104105
UPLOAD_PAGE_IMAGE_PROCESS_ISADDWM: 是否添加水印

public/i18n/zh-TW.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ GALLERY_SEARCH_FILENAME: 搜尋文件名
9999
GALLERY_SEARCH_URL: 搜尋URL
100100
GALLERY_MATCHED: ' 匹配到: '
101101

102+
UPLOAD_PAGE_COPY_UPLOAD_API: 複製上傳API
102103
UPLOAD_PAGE_IMAGE_PROCESS_NAME: 圖片處理
103104
UPLOAD_PAGE_IMAGE_PROCESS_DIALOG_TITLE: 圖片處理設置
104105
UPLOAD_PAGE_IMAGE_PROCESS_ISADDWM: 是否添加水印

src/main/server/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ class Server {
4444
}
4545

4646
if (request.method === 'POST') {
47-
if (!routers.getHandler(request.url!)) {
48-
logger.warn(`[PicList Server] don't support [${request.url}] url`)
47+
const [url, query] = request.url!.split('?')
48+
if (!routers.getHandler(url!)) {
49+
logger.warn(`[PicList Server] don't support [${url}] url`)
4950
handleResponse({
5051
response,
5152
statusCode: 404,
@@ -73,10 +74,11 @@ class Server {
7374
})
7475
}
7576
logger.info('[PicList Server] get the request', body)
76-
const handler = routers.getHandler(request.url!)
77+
const handler = routers.getHandler(url!)?.handler
7778
handler!({
7879
...postObj,
79-
response
80+
response,
81+
urlparams: query ? new URLSearchParams(query) : undefined
8082
})
8183
})
8284
}

src/main/server/router.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
class Router {
2-
private router = new Map<string, routeHandler>()
2+
private router = new Map<string, {handler: routeHandler, urlparams?: URLSearchParams}>()
33

4-
get (url: string, callback: routeHandler): void {
5-
this.router.set(url, callback)
4+
get (url: string, callback: routeHandler, urlparams?: URLSearchParams): void {
5+
this.router.set(url, { handler: callback, urlparams })
66
}
77

8-
post (url: string, callback: routeHandler): void {
9-
this.router.set(url, callback)
8+
post (url: string, callback: routeHandler, urlparams?: URLSearchParams): void {
9+
this.router.set(url, { handler: callback, urlparams })
1010
}
1111

1212
getHandler (url: string) {

src/main/server/routerManager.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import windowManager from 'apis/app/window/windowManager'
77
import { uploadChoosedFiles, uploadClipboardFiles, deleteChoosedFiles } from 'apis/app/uploader/apis'
88
import path from 'path'
99
import { dbPathDir } from 'apis/core/datastore/dbChecker'
10+
import picgo from '@core/picgo'
11+
import { changeCurrentUploader } from '../utils/handleUploaderConfig'
1012

1113
const STORE_PATH = dbPathDir()
1214
const LOG_PATH = path.join(STORE_PATH, 'piclist.log')
@@ -16,12 +18,39 @@ const deleteErrorMessage = `delete error. see ${LOG_PATH} for more detail.`
1618

1719
router.post('/upload', async ({
1820
response,
19-
list = []
21+
list = [],
22+
urlparams
2023
} : {
2124
response: IHttpResponse,
22-
list?: string[]
25+
list?: string[],
26+
urlparams?: URLSearchParams
2327
}): Promise<void> => {
2428
try {
29+
const picbed = urlparams?.get('picbed')
30+
let currentPicBedType = ''
31+
let currentPicBedConfig = {} as IStringKeyMap
32+
let currentPicBedConfigId = ''
33+
let needRestore = false
34+
if (picbed) {
35+
const configName = urlparams?.get('configName') || 'Default'
36+
const currentPicBed = picgo.getConfig<IStringKeyMap>('picBed') || {} as IStringKeyMap
37+
currentPicBedType = currentPicBed?.current
38+
currentPicBedConfig = currentPicBed?.[currentPicBedType]
39+
currentPicBedConfigId = currentPicBedConfig?._id
40+
if (picbed === currentPicBedType && configName === currentPicBedConfig._configName) {
41+
// do nothing
42+
} else {
43+
needRestore = true
44+
const picBeds = picgo.getConfig<IStringKeyMap>('uploader')
45+
const currentPicBedList = picBeds?.[picbed]?.configList
46+
if (currentPicBedList) {
47+
const currentConfig = currentPicBedList?.find((item: any) => item._configName === configName)
48+
if (currentConfig) {
49+
changeCurrentUploader(picbed, currentConfig, currentConfig._id)
50+
}
51+
}
52+
}
53+
}
2554
if (list.length === 0) {
2655
// upload with clipboard
2756
logger.info('[PicList Server] upload clipboard file')
@@ -83,6 +112,9 @@ router.post('/upload', async ({
83112
})
84113
}
85114
}
115+
if (needRestore) {
116+
changeCurrentUploader(currentPicBedType, currentPicBedConfig, currentPicBedConfigId)
117+
}
86118
} catch (err: any) {
87119
logger.error(err)
88120
handleResponse({

src/renderer/pages/picbeds/index.vue

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,26 @@
1010
>
1111
<div
1212
class="view-title"
13-
@click="handleNameClick"
1413
>
15-
{{ picBedName }} {{ $T('SETTINGS') }}
14+
<span
15+
class="view-title-text"
16+
@click="handleNameClick"
17+
>
18+
{{ picBedName }} {{ $T('SETTINGS') }}</span>
1619
<el-icon
1720
v-if="linkToLogInList.includes(picBedName)"
1821
>
1922
<Link />
2023
</el-icon>
24+
<el-button
25+
type="primary"
26+
round
27+
size="small"
28+
style="margin-left: 6px"
29+
@click="handleCopyApi"
30+
>
31+
{{ $T('UPLOAD_PAGE_COPY_UPLOAD_API') }}
32+
</el-button>
2133
</div>
2234
<config-form
2335
v-if="config.length > 0"
@@ -95,7 +107,7 @@ import { ref, onBeforeUnmount, onBeforeMount } from 'vue'
95107
import { T as $T } from '@/i18n/index'
96108
97109
// 数据发送工具函数
98-
import { sendToMain, triggerRPC } from '@/utils/dataSender'
110+
import { getConfig, sendToMain, triggerRPC } from '@/utils/dataSender'
99111
100112
// Vue Router 相关
101113
import { useRoute, useRouter } from 'vue-router'
@@ -105,6 +117,7 @@ import ConfigForm from '@/components/ConfigForm.vue'
105117
106118
// Electron 相关
107119
import {
120+
clipboard,
108121
ipcRenderer,
109122
IpcRendererEvent
110123
} from 'electron'
@@ -119,7 +132,7 @@ import { Link } from '@element-plus/icons-vue'
119132
import dayjs from 'dayjs'
120133
121134
// Element Plus 下拉菜单组件
122-
import { ElDropdown } from 'element-plus'
135+
import { ElDropdown, ElMessage } from 'element-plus'
123136
124137
const type = ref('')
125138
const config = ref<IPicGoPluginConfig[]>([])
@@ -187,7 +200,7 @@ const handleReset = async () => {
187200
$router.back()
188201
}
189202
190-
const linkToLogInList = ['github', 'tcyun', 'aliyun', 'smms', 'qiniu', 'imgur', 'upyun', 'githubPlus']
203+
const linkToLogInList = ['GitHub', '腾讯云COS', '阿里云OSS', 'SM.MS', '七牛云', 'Imgur', '又拍云', 'githubPlus']
191204
192205
function handleNameClick () {
193206
switch ($route.params.type) {
@@ -218,7 +231,26 @@ function handleNameClick () {
218231
}
219232
}
220233
221-
function getPicBeds (event: IpcRendererEvent, _config: IPicGoPluginConfig[], name: string) {
234+
async function handleCopyApi () {
235+
try {
236+
const serverConfig = await getConfig<IStringKeyMap>('settings.server') || {
237+
port: 36677,
238+
host: '127.0.0.1'
239+
}
240+
const { port, host } = serverConfig
241+
const uploader = await getConfig('uploader') as IStringKeyMap || {}
242+
const picBedConfigList = uploader[$route.params.type as string].configList || []
243+
const picBedConfig = picBedConfigList.find((item: IUploaderConfigListItem) => item._id === $route.params.configId)
244+
const apiUrl = `http://${host}:${port}/upload?picbed=${$route.params.type}&configName=${picBedConfig?._configName}`
245+
clipboard.writeText(apiUrl)
246+
ElMessage.success($T('MANAGE_BUCKET_COPY_SUCCESS') + ' ' + apiUrl)
247+
} catch (error) {
248+
console.log(error)
249+
ElMessage.error('Copy failed')
250+
}
251+
}
252+
253+
function getPicBeds (_event: IpcRendererEvent, _config: IPicGoPluginConfig[], name: string) {
222254
config.value = _config
223255
picBedName.value = name
224256
}
@@ -245,7 +277,7 @@ export default {
245277
height 100%
246278
overflow-y auto
247279
overflow-x hidden
248-
.view-title
280+
.view-title-text
249281
&:hover
250282
cursor pointer
251283
color #409EFF

src/universal/types/i18n.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ interface ILocales {
9696
GALLERY_SEARCH_FILENAME: string
9797
GALLERY_SEARCH_URL: string
9898
GALLERY_MATCHED: string
99+
UPLOAD_PAGE_COPY_UPLOAD_API: string
99100
UPLOAD_PAGE_IMAGE_PROCESS_NAME: string
100101
UPLOAD_PAGE_IMAGE_PROCESS_DIALOG_TITLE: string
101102
UPLOAD_PAGE_IMAGE_PROCESS_ISADDWM: string

0 commit comments

Comments
 (0)