feat: 更新资源迁移逻辑,优化用户 ID 处理,删除冗余代码

This commit is contained in:
xiongxiao
2026-03-25 02:36:27 +08:00
committed by cnb
parent 55378f4c94
commit b39f7d6028
11 changed files with 107 additions and 181 deletions

View File

@@ -4,15 +4,14 @@ import { filterKeys } from './http-proxy.ts';
import { getUserFromRequest } from '../utils.ts';
import { UserPermission, Permission } from '@kevisual/permission';
import { getLoginUser } from '@/modules/auth.ts';
import busboy from 'busboy';
import { getContentType, getTextContentType } from '../get-content-type.ts';
import { OssBase } from '@kevisual/oss';
import { parseSearchValue } from '@kevisual/router/src/server/parse-body.ts';
import { logger } from '@/modules/logger.ts';
import { pipeBusboy } from '../pipe-busboy.ts';
import { pipeMinioStream } from '../pipe.ts';
import { Readable } from 'stream';
import { postChunkProxy, postProxy } from './ai-proxy-chunk/post-proxy.ts'
import { UserId } from '@/routes/user/modules/user-id.ts';
type FileList = {
name: string;
prefix?: string;
@@ -24,28 +23,18 @@ type FileList = {
url?: string;
pathname?: string;
};
export const getFileList = async (list: any, opts?: { objectName: string; app: string; host?: string }) => {
const { app, host } = opts || {};
export const getFileList = async (list: any, opts?: { objectName: string; app: string; host?: string; uid?: string, username?: string }) => {
const { app, host, uid, username } = opts || {};
const objectName = opts?.objectName || '';
const [user] = objectName.split('/');
let replaceUser = user + '/';
if (app === 'resources') {
replaceUser = `${user}/resources/`;
}
let beforePath = `data/${uid}`;
let replaceUser = `${username}/resources/`;
return list.map((item: FileList) => {
if (item.name) {
item.path = item.name?.replace?.(objectName, '');
item.pathname = '/' + item.name.replace(`${user}/`, replaceUser);
item.pathname = '/' + item.name.replace(`${beforePath}/`, 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.pathname = '/' + item.prefix.replace(`${beforePath}/`, replaceUser);
}
item.url = new URL(item.pathname, `https://${host}`).toString();
return item;
@@ -71,7 +60,7 @@ const getAiProxy = async (req: IncomingMessage, res: ServerResponse, opts: Proxy
const edit = !!params.get('edit');
const recursive = !!params.get('recursive');
const showStat = !!params.get('stat');
const { objectName, app, owner, loginUser, isOwner } = await getObjectName(req);
const { objectName, app, owner, loginUser, isOwner, uid, user } = await getObjectName(req);
if (!dir && _u.pathname.endsWith('/')) {
dir = true; // 如果是目录请求强制设置为true
}
@@ -98,6 +87,8 @@ const getAiProxy = async (req: IncomingMessage, res: ServerResponse, opts: Proxy
objectName: objectName,
app: app,
host,
uid,
username: user,
}),
}),
);
@@ -198,33 +189,24 @@ export const getObjectByPathname = (opts: {
const [_, user, app] = opts.pathname.split('/');
let prefix = '';
let replaceKey = '';
if (app === 'ai') {
const version = opts?.version || '1.0.0';
replaceKey = `/${user}/${app}/`;
prefix = `${user}/${app}/${version}/`;
} else {
replaceKey = `/${user}/${app}/`;
prefix = `${user}/`; // root/resources
}
replaceKey = `/${user}/${app}/`;
prefix = `${user}/`; // root/resources
let objectName = opts.pathname.replace(replaceKey, prefix);
// 解码decodeURIComponent编码的路径
objectName = decodeURIComponent(objectName);
return { prefix, replaceKey, objectName, user, app };
}
export const getObjectName = async (req: IncomingMessage, opts?: { checkOwner?: boolean }) => {
export const getObjectName = async (req: IncomingMessage, opts?: { checkOwner?: boolean, uid?: string }) => {
const _u = new URL(req.url, 'http://localhost');
const pathname = decodeURIComponent(_u.pathname);
const params = _u.searchParams;
const { user, app } = getUserFromRequest(req);
const checkOwner = opts?.checkOwner ?? true;
const uid = opts?.uid || await UserId.getUserIdByName(user);
let objectName = '';
let owner = '';
if (app === 'ai') {
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}/`); // root/resources
}
objectName = pathname.replace(`/${user}/${app}/`, `data/${uid}/`); // root/resources
// 解码decodeURIComponent编码的路径
objectName = decodeURIComponent(objectName);
owner = user;
@@ -242,6 +224,7 @@ export const getObjectName = async (req: IncomingMessage, opts?: { checkOwner?:
isOwner,
app,
user,
uid,
};
};
export const deleteProxy = async (req: IncomingMessage, res: ServerResponse, opts: ProxyOptions) => {