feat: 更新资源迁移逻辑,优化用户 ID 处理,删除冗余代码
This commit is contained in:
@@ -2,7 +2,7 @@ import { App as AppType, AppList, AppData } from './module/app-drizzle.ts';
|
||||
import { app, db, oss, schema } from '@/app.ts';
|
||||
import { uniqBy } from 'es-toolkit';
|
||||
import { getUidByUsername, prefixFix } from './util.ts';
|
||||
import { deleteFiles, getMinioList, getMinioListAndSetToAppList } from '../file/index.ts';
|
||||
import { deleteFileByPrefix, getMinioList, getMinioListAndSetToAppList } from '../file/index.ts';
|
||||
import { setExpire } from './revoke.ts';
|
||||
import { User } from '@/models/user.ts';
|
||||
import { callDetectAppVersion } from './export.ts';
|
||||
@@ -186,12 +186,10 @@ app
|
||||
ctx.throw('app not found');
|
||||
}
|
||||
if (am.version === app.version) {
|
||||
ctx.throw('app is published');
|
||||
ctx.throw('app处于于发布状态,无法删除');
|
||||
}
|
||||
const appData = app.data as AppData;
|
||||
const files = appData.files || [];
|
||||
if (deleteFile && files.length > 0) {
|
||||
await deleteFiles(files.map((item) => item.path));
|
||||
if (deleteFile) {
|
||||
await deleteFileByPrefix(`data/${app.uid}/${app.key}/${app.version}`);
|
||||
}
|
||||
await db.delete(schema.kvAppList).where(eq(schema.kvAppList.id, id));
|
||||
ctx.body = 'success';
|
||||
|
||||
@@ -190,8 +190,8 @@ app
|
||||
await db.delete(schema.kvApp).where(eq(schema.kvApp.id, id));
|
||||
await Promise.all(list.map((item) => db.delete(schema.kvAppList).where(eq(schema.kvAppList.id, item.id))));
|
||||
if (deleteFile) {
|
||||
const username = tokenUser.username;
|
||||
await deleteFileByPrefix(`${username}/${am.key}`);
|
||||
const id = tokenUser.id;
|
||||
await deleteFileByPrefix(`data/${id}/${am.key}`);
|
||||
}
|
||||
ctx.body = 'success';
|
||||
return ctx;
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { app } from '@/app.ts';
|
||||
import { getFileStat, getMinioList, deleteFile, updateFileStat, deleteFiles } from './module/get-minio-list.ts';
|
||||
import { getFileStat, getMinioList, deleteFile, updateFileStat, deleteFileByPrefix } from './module/get-minio-list.ts';
|
||||
import path from 'path';
|
||||
import { CustomError } from '@kevisual/router';
|
||||
import { callDetectAppVersion } from '../app-manager/export.ts';
|
||||
import { UserId } from '../user/modules/user-id.ts';
|
||||
|
||||
/**
|
||||
* 清理prefix中的'..'
|
||||
@@ -23,8 +24,9 @@ const handlePrefix = (prefix: string) => {
|
||||
* @param tokenUser
|
||||
* @returns
|
||||
*/
|
||||
const getPrefixByUser = (data: { prefix: string }, tokenUser: { username: string }) => {
|
||||
const prefixBase = '/' + tokenUser.username;
|
||||
const getPrefixByUser = async (data: { prefix: string }, tokenUser: { username: string }) => {
|
||||
const uid = await UserId.getUserIdByName(tokenUser.username);
|
||||
const prefixBase = '/data/' + uid;
|
||||
const _prefix = handlePrefix(data.prefix);
|
||||
return {
|
||||
len: prefixBase.length,
|
||||
@@ -40,7 +42,7 @@ app
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const data = ctx.query.data || {};
|
||||
const { len, prefix } = getPrefixByUser(data, tokenUser);
|
||||
const { len, prefix } = await getPrefixByUser(data, tokenUser);
|
||||
const recursive = data.recursive;
|
||||
const list = await getMinioList({ prefix: prefix.slice(1), recursive: recursive });
|
||||
|
||||
@@ -67,7 +69,7 @@ app
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const data = ctx.query.data || {};
|
||||
const { prefix } = getPrefixByUser(data, tokenUser);
|
||||
const { prefix } = await getPrefixByUser(data, tokenUser);
|
||||
console.log('prefix', prefix);
|
||||
const stat = await getFileStat(prefix.slice(1));
|
||||
ctx.body = stat;
|
||||
@@ -83,7 +85,8 @@ app
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const list = await getMinioList({ prefix: '' + tokenUser.username, recursive: true });
|
||||
const uid = await UserId.getUserIdByName(tokenUser.username);
|
||||
const list = await getMinioList({ prefix: `data/${uid}/`, recursive: true });
|
||||
const size = list.reduce((acc, item) => {
|
||||
if ('size' in item) {
|
||||
return acc + item.size;
|
||||
@@ -109,7 +112,7 @@ app
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const data = ctx.query.data || {};
|
||||
const { prefix } = getPrefixByUser(data, tokenUser);
|
||||
const { prefix } = await getPrefixByUser(data, tokenUser);
|
||||
const [username, appKey, version] = prefix.slice(1).split('/');
|
||||
const res = await deleteFile(prefix.slice(1));
|
||||
if (res.code === 200) {
|
||||
@@ -136,7 +139,7 @@ app
|
||||
if (!data.metadata || JSON.stringify(data.metadata) === '{}') {
|
||||
ctx.throw(400, 'metadata is required');
|
||||
}
|
||||
const { prefix } = getPrefixByUser(data, tokenUser);
|
||||
const { prefix } = await getPrefixByUser(data, tokenUser);
|
||||
const res = await updateFileStat(prefix.slice(1), data.metadata);
|
||||
if (res.code === 200) {
|
||||
ctx.body = 'update metadata success';
|
||||
@@ -155,6 +158,7 @@ app
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const uid = await UserId.getUserIdByName(tokenUser.username);
|
||||
let directory = ctx.query.data?.directory as string;
|
||||
if (!directory) {
|
||||
ctx.throw(400, 'directory is required');
|
||||
@@ -165,12 +169,15 @@ app
|
||||
if (directory.endsWith('/')) {
|
||||
ctx.throw(400, 'directory is invalid, cannot end with /');
|
||||
}
|
||||
const prefix = tokenUser.username + '/' + directory + '/';
|
||||
const prefix = `data/${uid}/${directory}/`;
|
||||
const list = await getMinioList<true>({ prefix, recursive: true });
|
||||
if (list.length === 0) {
|
||||
ctx.throw(400, 'directory is empty');
|
||||
}
|
||||
const res = await deleteFiles(list.map((item) => item.name));
|
||||
if(list.length > 5000) {
|
||||
ctx.throw(400, '删除文件数量过多,请分批删除');
|
||||
}
|
||||
const res = await deleteFileByPrefix(prefix);
|
||||
if (!res) {
|
||||
ctx.throw(500, 'delete all failed');
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import dayjs from 'dayjs';
|
||||
import { oss } from '@/modules/s3.ts';
|
||||
import { StatObjectResult } from '@kevisual/oss';
|
||||
import { UserId } from '@/routes/user/modules/user-id.ts';
|
||||
type MinioListOpt = {
|
||||
prefix: string;
|
||||
recursive?: boolean;
|
||||
@@ -62,23 +63,14 @@ export const deleteFile = async (prefix: string): Promise<{ code: number; messag
|
||||
}
|
||||
};
|
||||
|
||||
// 批量删除文件
|
||||
export const deleteFiles = async (prefixs: string[]): Promise<any> => {
|
||||
try {
|
||||
for (const prefix of prefixs) {
|
||||
await oss.deleteObject(prefix);
|
||||
}
|
||||
return true;
|
||||
} catch (e) {
|
||||
console.error('delete Files Error not handle', e);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
export const deleteFileByPrefix = async (prefix: string): Promise<any> => {
|
||||
try {
|
||||
const allFiles = await getMinioList<true>({ prefix, recursive: true });
|
||||
const files = allFiles.filter((item) => item.name.startsWith(prefix));
|
||||
await deleteFiles(files.map((item) => item.name));
|
||||
const objects = await oss.listObjects<true>(prefix, { recursive: true });
|
||||
if (objects.length > 0) {
|
||||
for (const obj of objects) {
|
||||
await oss.deleteObject(obj.name);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} catch (e) {
|
||||
console.error('delete File Error not handle', e);
|
||||
@@ -93,7 +85,8 @@ type GetMinioListAndSetToAppListOpts = {
|
||||
// 批量列出文件,并设置到appList的files中
|
||||
export const getMinioListAndSetToAppList = async (opts: GetMinioListAndSetToAppListOpts) => {
|
||||
const { username, appKey, version } = opts;
|
||||
const minioList = await getMinioList({ prefix: `${username}/${appKey}/${version}`, recursive: true });
|
||||
const uid = await UserId.getUserIdByName(username);
|
||||
const minioList = await getMinioList({ prefix: `data/${uid}/${appKey}/${version}`, recursive: true });
|
||||
const files = minioList;
|
||||
return files as MinioFile[];
|
||||
};
|
||||
@@ -178,7 +171,8 @@ export const backupUserA = async (usernameA: string, id: string, backName?: stri
|
||||
* @param username
|
||||
*/
|
||||
export const deleteUser = async (username: string) => {
|
||||
const list = await getMinioList<true>({ prefix: `${username}/`, recursive: true });
|
||||
const uid = await UserId.getUserIdByName(username);
|
||||
const list = await getMinioList<true>({ prefix: `data/${uid}/`, recursive: true });
|
||||
for (const item of list) {
|
||||
await oss.deleteObject(item.name);
|
||||
}
|
||||
|
||||
@@ -2,10 +2,8 @@ import { app, db, schema } from '@/app.ts';
|
||||
import { User } from '@/models/user.ts';
|
||||
import { nanoid } from 'nanoid';
|
||||
import { CustomError } from '@kevisual/router';
|
||||
import { backupUserA, deleteUser, mvUserAToUserB } from '@/routes/file/index.ts';
|
||||
import { AppHelper } from '@/routes/app-manager/module/index.ts';
|
||||
import { eq } from 'drizzle-orm';
|
||||
// import { mvAppFromUserAToUserB } from '@/routes/app-manager/admin/mv-user-app.ts';
|
||||
|
||||
export const checkUsername = (username: string) => {
|
||||
if (username.length > 30) {
|
||||
@@ -45,10 +43,6 @@ export const toChangeName = async (opts: { id: string; newName: string; admin?:
|
||||
user.data = data;
|
||||
try {
|
||||
await user.save();
|
||||
// 迁移文件数据
|
||||
await backupUserA(oldName, user.id); // 备份文件数据
|
||||
await mvUserAToUserB(oldName, newName, true); // 迁移文件数据
|
||||
// await mvAppFromUserAToUserB(oldName, newName); // 迁移应用数据
|
||||
|
||||
if (['org', 'user'].includes(user.type)) {
|
||||
const type = user.type === 'org' ? 'org' : 'user';
|
||||
@@ -175,8 +169,8 @@ app
|
||||
ctx.throw(404, { message: 'User not found' });
|
||||
}
|
||||
await db.delete(schema.cfUser).where(eq(schema.cfUser.id, user.id));
|
||||
backupUserA(user.username, user.id);
|
||||
deleteUser(user.username);
|
||||
// backupUserA(user.username, user.id);
|
||||
// deleteUser(user.username);
|
||||
// TODO: EXPIRE 删除token
|
||||
ctx.body = {
|
||||
id: user.id,
|
||||
|
||||
Reference in New Issue
Block a user