user-manager change

This commit is contained in:
2025-04-03 20:08:40 +08:00
parent 8fafe74fa3
commit d97053a443
12 changed files with 297 additions and 22 deletions

View File

@@ -1,5 +1,6 @@
import { minioClient } from '@/app.ts';
import { bucketName } from '@/modules/minio.ts';
import dayjs from 'dayjs';
import { minioClient } from '../../../modules/minio.ts';
import { bucketName } from '../../../modules/minio.ts';
import { CopyDestinationOptions, CopySourceOptions } from 'minio';
type MinioListOpt = {
prefix: string;
@@ -95,7 +96,17 @@ export const deleteFiles = async (prefixs: string[]): Promise<any> => {
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));
return true;
} catch (e) {
console.error('delete File Error not handle', e);
return false;
}
};
type GetMinioListAndSetToAppListOpts = {
username: string;
appKey: string;
@@ -148,3 +159,61 @@ export const updateFileStat = async (
};
}
};
/**
* 将用户A的文件移动到用户B
* @param usernameA
* @param usernameB
* @param clearOldUser 是否清除用户A的文件
*/
export const mvUserAToUserB = async (usernameA: string, usernameB: string, clearOldUser = false) => {
const oldPrefix = `${usernameA}/`;
const newPrefix = `${usernameB}/`;
const listSource = await getMinioList<true>({ prefix: oldPrefix, recursive: true });
for (const item of listSource) {
const source = new CopySourceOptions({ Bucket: bucketName, Object: item.name });
const stat = await getFileStat(item.name);
const newName = item.name.slice(oldPrefix.length);
const metadata = stat?.userMetadata;
const destination = new CopyDestinationOptions({
Bucket: bucketName,
Object: `${newPrefix}${newName}`,
UserMetadata: metadata,
MetadataDirective: 'COPY',
});
await minioClient.copyObject(source, destination);
}
if (clearOldUser) {
const files = await getMinioList<true>({ prefix: oldPrefix, recursive: true });
for (const file of files) {
await minioClient.removeObject(bucketName, file.name);
}
}
};
export const backupUserA = async (usernameA: string, id: string, backName?: string) => {
const today = backName || dayjs().format('YYYY-MM-DD-HH-mm');
const backupAllPrefix = `private/backup/${id}/`;
const backupPrefix = `private/backup/${id}/${today}`;
const backupList = await getMinioList<false>({ prefix: backupAllPrefix });
const backupListSort = backupList.sort((a, b) => -a.prefix.localeCompare(b.prefix));
if (backupListSort.length > 2) {
const deleteBackup = backupListSort.slice(2);
for (const item of deleteBackup) {
const files = await getMinioList<true>({ prefix: item.prefix, recursive: true });
for (const file of files) {
await minioClient.removeObject(bucketName, file.name);
}
}
}
await mvUserAToUserB(usernameA, backupPrefix, false);
};
/**
* 删除用户
* @param username
*/
export const deleteUser = async (username: string) => {
const list = await getMinioList<true>({ prefix: `${username}/`, recursive: true });
for (const item of list) {
await minioClient.removeObject(bucketName, item.name);
}
};