|
@@ -25,7 +25,7 @@ export class FileService {
|
|
|
|
|
|
|
|
constructor(app: FastifyInstance) {
|
|
constructor(app: FastifyInstance) {
|
|
|
const config = app.config
|
|
const config = app.config
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
this.bucket = config.OSS_BUCKET
|
|
this.bucket = config.OSS_BUCKET
|
|
|
this.defaultFolder = config.UPLOAD_FOLDER
|
|
this.defaultFolder = config.UPLOAD_FOLDER
|
|
|
this.ossClient = new OSS({
|
|
this.ossClient = new OSS({
|
|
@@ -57,11 +57,7 @@ export class FileService {
|
|
|
* @param options 上传选项
|
|
* @param options 上传选项
|
|
|
* @returns 上传结果
|
|
* @returns 上传结果
|
|
|
*/
|
|
*/
|
|
|
- async uploadZip(
|
|
|
|
|
- buffer: Buffer,
|
|
|
|
|
- originalName: string,
|
|
|
|
|
- options: FileUploadOptions = {}
|
|
|
|
|
- ): Promise<UploadResult> {
|
|
|
|
|
|
|
+ async uploadZip(buffer: Buffer, originalName: string, options: FileUploadOptions = {}): Promise<UploadResult> {
|
|
|
// 验证文件类型
|
|
// 验证文件类型
|
|
|
if (!originalName.toLowerCase().endsWith('.zip')) {
|
|
if (!originalName.toLowerCase().endsWith('.zip')) {
|
|
|
throw new Error('只支持ZIP格式的压缩包')
|
|
throw new Error('只支持ZIP格式的压缩包')
|
|
@@ -75,12 +71,12 @@ export class FileService {
|
|
|
// 生成文件名
|
|
// 生成文件名
|
|
|
const extension = path.extname(originalName)
|
|
const extension = path.extname(originalName)
|
|
|
const filename = options.filename || `${randomUUID()}${extension}`
|
|
const filename = options.filename || `${randomUUID()}${extension}`
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 获取当前日期文件夹
|
|
// 获取当前日期文件夹
|
|
|
const dateFolder = this.getDateFolder()
|
|
const dateFolder = this.getDateFolder()
|
|
|
const baseFolder = options.folder || this.defaultFolder
|
|
const baseFolder = options.folder || this.defaultFolder
|
|
|
const targetFolder = `${baseFolder}/${dateFolder}`
|
|
const targetFolder = `${baseFolder}/${dateFolder}`
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 构建文件路径
|
|
// 构建文件路径
|
|
|
const key = `${targetFolder}/${filename}`
|
|
const key = `${targetFolder}/${filename}`
|
|
|
|
|
|
|
@@ -131,12 +127,12 @@ export class FileService {
|
|
|
// 生成文件名
|
|
// 生成文件名
|
|
|
const extension = path.extname(originalName)
|
|
const extension = path.extname(originalName)
|
|
|
const filename = options.filename || `${randomUUID()}${extension}`
|
|
const filename = options.filename || `${randomUUID()}${extension}`
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 获取当前日期文件夹
|
|
// 获取当前日期文件夹
|
|
|
const dateFolder = this.getDateFolder()
|
|
const dateFolder = this.getDateFolder()
|
|
|
const baseFolder = options.folder || this.defaultFolder
|
|
const baseFolder = options.folder || this.defaultFolder
|
|
|
const targetFolder = `${baseFolder}/${dateFolder}`
|
|
const targetFolder = `${baseFolder}/${dateFolder}`
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 构建文件路径
|
|
// 构建文件路径
|
|
|
const key = `${targetFolder}/${filename}`
|
|
const key = `${targetFolder}/${filename}`
|
|
|
|
|
|
|
@@ -172,20 +168,13 @@ export class FileService {
|
|
|
originalName: string,
|
|
originalName: string,
|
|
|
options: Omit<FileUploadOptions, 'allowedTypes'> = {}
|
|
options: Omit<FileUploadOptions, 'allowedTypes'> = {}
|
|
|
): Promise<UploadResult> {
|
|
): Promise<UploadResult> {
|
|
|
- const imageTypes = [
|
|
|
|
|
- 'image/jpeg',
|
|
|
|
|
- 'image/jpg',
|
|
|
|
|
- 'image/png',
|
|
|
|
|
- 'image/gif',
|
|
|
|
|
- 'image/webp',
|
|
|
|
|
- 'image/svg+xml'
|
|
|
|
|
- ]
|
|
|
|
|
|
|
+ const imageTypes = ['image/jpeg', 'image/jpg', 'image/png', 'image/gif', 'image/webp', 'image/svg+xml']
|
|
|
|
|
|
|
|
return this.uploadFile(buffer, originalName, 'image/jpeg', {
|
|
return this.uploadFile(buffer, originalName, 'image/jpeg', {
|
|
|
...options,
|
|
...options,
|
|
|
folder: options.folder || this.defaultFolder,
|
|
folder: options.folder || this.defaultFolder,
|
|
|
allowedTypes: imageTypes,
|
|
allowedTypes: imageTypes,
|
|
|
- maxSize: options.maxSize || 10 * 1024 * 1024 // 默认10MB
|
|
|
|
|
|
|
+ maxSize: options.maxSize || 15 * 1024 * 1024 // 默认15MB
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -281,7 +270,7 @@ export class FileService {
|
|
|
* @param keys 文件key数组
|
|
* @param keys 文件key数组
|
|
|
* @returns 删除结果
|
|
* @returns 删除结果
|
|
|
*/
|
|
*/
|
|
|
- async deleteFiles(keys: string[]): Promise<{ success: string[], failed: string[] }> {
|
|
|
|
|
|
|
+ async deleteFiles(keys: string[]): Promise<{ success: string[]; failed: string[] }> {
|
|
|
const result = {
|
|
const result = {
|
|
|
success: [] as string[],
|
|
success: [] as string[],
|
|
|
failed: [] as string[]
|
|
failed: [] as string[]
|
|
@@ -312,4 +301,4 @@ export class FileService {
|
|
|
throw new Error(`文件下载失败: ${error instanceof Error ? error.message : '未知错误'}`)
|
|
throw new Error(`文件下载失败: ${error instanceof Error ? error.message : '未知错误'}`)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-}
|
|
|
|
|
|
|
+}
|