Feature: upload api now support url query picbed and configname

ISSUES CLOSED: #93
This commit is contained in:
萌萌哒赫萝
2023-09-06 08:40:50 -07:00
parent f9a3f24c2f
commit 2fcec70721
8 changed files with 88 additions and 18 deletions

View File

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

View File

@@ -1,12 +1,12 @@
class Router {
private router = new Map<string, routeHandler>()
private router = new Map<string, {handler: routeHandler, urlparams?: URLSearchParams}>()
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) {

View File

@@ -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<void> => {
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<IStringKeyMap>('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<IStringKeyMap>('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({