160 lines
3.8 KiB
TypeScript
160 lines
3.8 KiB
TypeScript
/**
|
|
* MinIO 存储管理 API 客户端
|
|
* 基于 /api/v2/storage 接口
|
|
*/
|
|
|
|
import { apiRequest } from '../axios-client';
|
|
|
|
export interface CopyFileRequest {
|
|
source_path: string;
|
|
destination_path: string;
|
|
source_bucket?: string | null;
|
|
destination_bucket?: string | null;
|
|
}
|
|
|
|
export interface CopyFileResponse {
|
|
success: boolean;
|
|
message: string;
|
|
source_path: string;
|
|
destination_path: string;
|
|
}
|
|
|
|
export interface MoveFileRequest {
|
|
source_path: string;
|
|
destination_path: string;
|
|
}
|
|
|
|
export interface MoveFileResponse {
|
|
success: boolean;
|
|
message: string;
|
|
source_path: string;
|
|
destination_path: string;
|
|
}
|
|
|
|
export interface DeleteFileRequest {
|
|
file_path: string;
|
|
}
|
|
|
|
export interface DeleteFileResponse {
|
|
success: boolean;
|
|
message: string;
|
|
file_path: string;
|
|
}
|
|
|
|
/**
|
|
* 复制文件
|
|
* @param request 复制文件请求参数
|
|
* @param token JWT token(可选)
|
|
* @returns 复制结果
|
|
*/
|
|
export async function copyFile(request: CopyFileRequest, token?: string) {
|
|
const response = await apiRequest<CopyFileResponse>(
|
|
'/api/v2/storage/files/copy',
|
|
{
|
|
method: 'POST',
|
|
data: request,
|
|
headers: {
|
|
...(token ? { 'Authorization': `Bearer ${token}` } : {})
|
|
}
|
|
}
|
|
);
|
|
|
|
if (response.error) {
|
|
throw new Error(response.error);
|
|
}
|
|
|
|
return response.data;
|
|
}
|
|
|
|
/**
|
|
* 移动/重命名文件
|
|
* @param request 移动文件请求参数
|
|
* @param token JWT token(可选)
|
|
* @returns 移动结果
|
|
*/
|
|
export async function moveFile(request: MoveFileRequest, token?: string) {
|
|
const response = await apiRequest<MoveFileResponse>(
|
|
'/api/v2/storage/files/move',
|
|
{
|
|
method: 'POST',
|
|
data: request,
|
|
headers: {
|
|
...(token ? { 'Authorization': `Bearer ${token}` } : {})
|
|
}
|
|
}
|
|
);
|
|
|
|
if (response.error) {
|
|
throw new Error(response.error);
|
|
}
|
|
|
|
return response.data;
|
|
}
|
|
|
|
/**
|
|
* 删除文件
|
|
* @param request 删除文件请求参数
|
|
* @param token JWT token(可选)
|
|
* @returns 删除结果
|
|
*/
|
|
export async function deleteFile(request: DeleteFileRequest, token?: string) {
|
|
const response = await apiRequest<DeleteFileResponse>(
|
|
'/api/v2/storage/files',
|
|
{
|
|
method: 'DELETE',
|
|
headers: {
|
|
...(token ? { 'Authorization': `Bearer ${token}` } : {})
|
|
}
|
|
},
|
|
{ file_path: request.file_path }
|
|
);
|
|
|
|
if (response.error) {
|
|
throw new Error(response.error);
|
|
}
|
|
|
|
return response.data;
|
|
}
|
|
|
|
/**
|
|
* 生成新的文件路径(用于复制模板文件)
|
|
* @param originalPath 原始文件路径
|
|
* @param area 地区代码
|
|
* @returns 新文件路径
|
|
*/
|
|
export function generateDraftFilePath(originalPath: string, area: string): string {
|
|
// 提取文件目录和文件名
|
|
const lastSlashIndex = originalPath.lastIndexOf('/');
|
|
const directory = lastSlashIndex >= 0 ? originalPath.substring(0, lastSlashIndex) : '';
|
|
const fileName = lastSlashIndex >= 0 ? originalPath.substring(lastSlashIndex + 1) : originalPath;
|
|
|
|
// 提取文件扩展名
|
|
const lastDotIndex = fileName.lastIndexOf('.');
|
|
const baseName = lastDotIndex >= 0 ? fileName.substring(0, lastDotIndex) : fileName;
|
|
const extension = lastDotIndex >= 0 ? fileName.substring(lastDotIndex) : '';
|
|
|
|
// 生成时间戳和 UUID
|
|
const timestamp = Date.now();
|
|
const uuid = generateUUID();
|
|
|
|
// 构建新文件名:原文件名_地区_时间戳_uuid.扩展名
|
|
const newFileName = `${baseName}_${area}_${timestamp}_${uuid}${extension}`;
|
|
|
|
// 构建完整路径
|
|
const newPath = directory ? `${directory}/${newFileName}` : newFileName;
|
|
|
|
return newPath;
|
|
}
|
|
|
|
/**
|
|
* 生成简单的 UUID(不依赖外部库)
|
|
* @returns UUID 字符串
|
|
*/
|
|
function generateUUID(): string {
|
|
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
|
const r = Math.random() * 16 | 0;
|
|
const v = c === 'x' ? r : (r & 0x3 | 0x8);
|
|
return v.toString(16);
|
|
});
|
|
}
|