fix: 修复 fileid索引
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import * as Minio from 'minio'
|
||||
import { randomUUID } from 'crypto'
|
||||
import { Readable } from 'stream'
|
||||
import { minioClient, BUCKET_NAME, initializeMinIO } from './minioClient'
|
||||
|
||||
export interface StoredFile {
|
||||
@@ -20,6 +21,19 @@ export interface FileMetadata {
|
||||
objectName: string
|
||||
}
|
||||
|
||||
export interface StoredFileStream {
|
||||
stream: Readable
|
||||
contentType: string
|
||||
filename: string
|
||||
size?: number
|
||||
}
|
||||
|
||||
export interface StoredFileStreamMetadata {
|
||||
contentType?: string | null
|
||||
filename?: string | null
|
||||
size?: number | null
|
||||
}
|
||||
|
||||
// 生成对象名称,使用分层结构优化性能
|
||||
function generateObjectName(type: 'screenshot' | 'version' | 'other', hostname?: string): string {
|
||||
const now = new Date()
|
||||
@@ -168,6 +182,27 @@ export async function getFileByObjectName(objectName: string): Promise<{ buffer:
|
||||
}
|
||||
}
|
||||
|
||||
export async function getFileStreamByObjectName(
|
||||
objectName: string,
|
||||
metadata: StoredFileStreamMetadata = {}
|
||||
): Promise<StoredFileStream | null> {
|
||||
try {
|
||||
await ensureMinIOReady()
|
||||
|
||||
const stream = await minioClient.getObject(BUCKET_NAME, objectName)
|
||||
|
||||
return {
|
||||
stream,
|
||||
contentType: metadata.contentType || 'application/octet-stream',
|
||||
filename: metadata.filename || objectName.split('/').pop() || 'unknown',
|
||||
size: metadata.size ?? undefined
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('❌ 获取文件流失败:', error)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export async function deleteFile(objectName: string): Promise<boolean> {
|
||||
try {
|
||||
await ensureMinIOReady()
|
||||
|
||||
Reference in New Issue
Block a user