feat: 上传资源和下载资源更新

This commit is contained in:
2025-03-20 02:29:26 +08:00
parent 9b1045d456
commit 0179fe73a3
19 changed files with 747 additions and 225 deletions

View File

@@ -1,6 +1,6 @@
import { minioClient } from '@/app.ts';
import { bucketName } from '@/modules/minio.ts';
import { CopyDestinationOptions, CopySourceOptions } from 'minio';
type MinioListOpt = {
prefix: string;
recursive?: boolean;
@@ -41,9 +41,12 @@ export const getMinioList = async (opts: MinioListOpt): Promise<MinioList> => {
});
});
};
export const getFileStat = async (prefix: string): Promise<any> => {
export const getFileStat = async (prefix: string, isFile?: boolean): Promise<any> => {
try {
const obj = await minioClient.statObject(bucketName, prefix);
if (isFile && obj.size === 0) {
return null;
}
return obj;
} catch (e) {
if (e.code === 'NotFound') {
@@ -54,16 +57,30 @@ export const getFileStat = async (prefix: string): Promise<any> => {
}
};
export const deleteFile = async (prefix: string): Promise<any> => {
export const deleteFile = async (prefix: string): Promise<{ code: number; message: string }> => {
try {
const fileStat = await getFileStat(prefix);
if (!fileStat) {
console.warn(`File not found: ${prefix}`);
return {
code: 404,
message: 'file not found',
};
}
await minioClient.removeObject(bucketName, prefix, {
versionId: 'null',
forceDelete: true,
forceDelete: true, // 强制删除
});
return true;
return {
code: 200,
message: 'delete success',
};
} catch (e) {
console.error('delete File Error not handle', e);
return false;
return {
code: 500,
message: 'delete failed',
};
}
};
@@ -90,3 +107,43 @@ export const getMinioListAndSetToAppList = async (opts: GetMinioListAndSetToAppL
const files = minioList;
return files as MinioFile[];
};
/**
* 更新文件的元数据
* @param prefix 文件前缀
* @param newMetadata 新的元数据
* @returns
*/
export const updateFileStat = async (
prefix: string,
newMetadata: Record<string, string>,
): Promise<{
code: number;
data: any;
message?: string;
}> => {
try {
const source = new CopySourceOptions({ Bucket: bucketName, Object: prefix });
const destination = new CopyDestinationOptions({
Bucket: bucketName,
Object: prefix,
UserMetadata: newMetadata,
MetadataDirective: 'REPLACE',
});
const copyResult = await minioClient.copyObject(source, destination);
console.log('copyResult', copyResult);
console.log(`Metadata for ${prefix} updated successfully.`);
return {
code: 200,
data: copyResult,
message: 'update metadata success',
};
} catch (e) {
console.error('Error updating file stat', e);
return {
code: 500,
data: null,
message: `update metadata failed. ${e.message}`,
};
}
};