diff --git a/src/module/proxy/ai-proxy.ts b/src/module/proxy/ai-proxy.ts index 62e43b2..d81db73 100644 --- a/src/module/proxy/ai-proxy.ts +++ b/src/module/proxy/ai-proxy.ts @@ -9,6 +9,47 @@ import { getContentType } from '../get-content-type.ts'; import { OssBase } from '@kevisual/oss'; import { parseSearchValue } from '@kevisual/router/browser'; import { logger } from '../logger.ts'; + +type FileList = { + name: string; + prefix?: string; + size?: number; + etag?: string; + lastModified?: Date; + + path?: string; + url?: string; + pathname?: string; +}; +export const getFileList = async (list: any, opts?: { objectName: string; app: string; host?: string }) => { + const { app, host } = opts || {}; + const objectName = opts?.objectName || ''; + let newObjectName = objectName; + const [user] = objectName.split('/'); + let replaceUser = user + '/'; + if (app === 'resources') { + replaceUser = `${user}/resources/`; + newObjectName = objectName.replace(`${user}/`, replaceUser); + } + return list.map((item: FileList) => { + if (item.name) { + item.path = item.name?.replace?.(objectName, ''); + item.pathname = '/' + item.name.replace(`${user}/`, replaceUser); + } else { + item.path = item.prefix?.replace?.(objectName, ''); + item.pathname = '/' + item.prefix.replace(`${user}/`, replaceUser); + } + if (item.name && app === 'ai') { + const [_user, _app, _version, ...rest] = item.name.split('/'); + item.pathname = item.pathname.replace(`/${_user}/${_app}/${_version}/`, `/${_user}/${_app}/`); + } else if (app === 'ai') { + const [_user, _app, _version, ...rest] = item.prefix?.split('/'); + item.pathname = item.pathname.replace(`/${_user}/${_app}/${_version}/`, `/${_user}/${_app}/`); + } + item.url = new URL(item.pathname, `https://${host}`).toString(); + return item; + }); +}; // import { logger } from '@/module/logger.ts'; const getAiProxy = async (req: IncomingMessage, res: ServerResponse, opts: ProxyOptions) => { const { createNotFoundPage } = opts; @@ -17,10 +58,12 @@ const getAiProxy = async (req: IncomingMessage, res: ServerResponse, opts: Proxy const params = _u.searchParams; const password = params.get('p'); const hash = params.get('hash'); - const dir = !!params.get('dir'); + let dir = !!params.get('dir'); const recursive = !!params.get('recursive'); const { objectName, app, owner, loginUser, isOwner } = await getObjectName(req); - + if (!dir && req.url.endsWith('/')) { + dir = true; // 如果是目录请求,强制设置为true + } logger.debug(`proxy request: ${objectName}`, dir); try { if (dir) { @@ -33,20 +76,10 @@ const getAiProxy = async (req: IncomingMessage, res: ServerResponse, opts: Proxy res.end( JSON.stringify({ code: 200, - data: list.map((item) => { - // @ts-ignore - item.path = item.name?.replace?.(objectName, ''); - // @ts-ignore - item.pathname = '/' + item.name; - if (app === 'ai') { - const [_user, _app, _version, ...rest] = item.name.split('/'); - // @ts-ignore - item.pathname = item.pathname.replace(`/${_user}/${_app}/${_version}/`, `/${_user}/${_app}/`); - // @ts-ignore - } - // @ts-ignore - item.url = new URL(item.pathname, `https://${host}`).toString(); - return item; + data: await getFileList(list, { + objectName: objectName, + app: app, + host, }), }), ); @@ -202,7 +235,7 @@ export const getObjectName = async (req: IncomingMessage, opts?: { checkOwner?: const version = params.get('version') || '1.0.0'; // root/ai objectName = pathname.replace(`/${user}/${app}/`, `${user}/${app}/${version}/`); } else { - objectName = pathname.replace(`/${user}/${app}/`, `${user}/`); // resources/root/ + objectName = pathname.replace(`/${user}/${app}/`, `${user}/`); // root/resources } owner = user; let isOwner = undefined;