Skip to content

Let's use AI to Earn!

feat(file): enhance file upload handling with content type support (#291)

Sakura6aeacb3b67cd
diff --git a/project/backend/apps/aitoearn-server/src/file/file.service.ts b/project/backend/apps/aitoearn-server/src/file/file.service.ts
index 90e6426a..3cad6bca 100644
--- a/project/backend/apps/aitoearn-server/src/file/file.service.ts
+++ b/project/backend/apps/aitoearn-server/src/file/file.service.ts
@@ -29,7 +29,7 @@ export class FileService {
/**
* 文件上传
* @param {Express.Multer.File} file 文件buffer流对象
- * @param {string | undefined} path 路径,不传就会使用‘nopath’前缀
+ * @param {string | undefined} path 路径,不传就会使用'nopath'前缀
* @param {string | undefined} newName 新的文件名
* @param {string | undefined} permanent 是否为永久目录,默认临时
* @returns
@@ -49,6 +49,7 @@ export class FileService {
const res = await this.s3Service.putObject(
filePath,
file.buffer,
+ file.mimetype,
)
return { key: res.path }
}
@@ -71,9 +72,11 @@ export class FileService {
): Promise<string> {
const { path, permanent, fileType } = option
const objectName = `${config.environment}/${permanent ? '' : 'temp/'}${path || 'nopath'}${`/${dayjs().format('YYYYMM')}/${uuidv4()}.${fileType}`}`
+ const contentType = mime.lookup(fileType) || 'application/octet-stream'
const res = await this.s3Service.putObject(
objectName,
buffer,
+ contentType,
)
return res.path
@@ -90,7 +93,7 @@ export class FileService {
path,
})
const filePath = `${newPath}/${newFileName}.${mime.extension(fileType)}`
- const res = await this.s3Service.initiateMultipartUpload(filePath)
+ const res = await this.s3Service.initiateMultipartUpload(filePath, fileType)
return {
uploadId: res,
fileId: filePath,
@@ -148,15 +151,15 @@ export class FileService {
/**
* 获取分片上传的签名URL
* @param key
- * @param contentType
- * @param expiresIn
* @returns
*/
async getUploadUrl(
key: string,
) {
+ const contentType = mime.lookup(key) || 'application/octet-stream'
const presigned = await this.s3Service.getUploadSign(
key,
+ contentType,
)
return presigned
}
diff --git a/project/backend/libs/aws-s3/src/s3.service.ts b/project/backend/libs/aws-s3/src/s3.service.ts
index 8f83417f..6d74afea 100644
--- a/project/backend/libs/aws-s3/src/s3.service.ts
+++ b/project/backend/libs/aws-s3/src/s3.service.ts
@@ -28,6 +28,7 @@ export class S3Service {
async putObject(
objectPath: string,
file: PutObjectCommandInput['Body'],
+ contentType?: string,
) {
const upload = new Upload({
client: this.client,
@@ -35,6 +36,7 @@ export class S3Service {
Bucket: this.config.bucketName,
Key: objectPath,
Body: file,
+ ContentType: contentType,
},
})
await upload.done()
@@ -67,25 +69,30 @@ export class S3Service {
}
// 生成预签名上传 URL
- getUploadSign(objectPath: string) {
+ getUploadSign(objectPath: string, contentType?: string) {
return createPresignedPost(this.client, {
Bucket: this.config.bucketName,
Key: objectPath,
Expires: this.config.signExpires,
+ Conditions: contentType ? [['eq', '$Content-Type', contentType]] : undefined,
+ Fields: contentType ? { 'Content-Type': contentType } : undefined,
})
}
/**
* 开始分片上传
* @param {string} objectPath - 文件键
+ * @param {string} contentType - 文件MIME类型
* @returns {Promise<string>} - 返回上传ID
*/
async initiateMultipartUpload(
objectPath: string,
+ contentType?: string,
): Promise<string> {
const command = new CreateMultipartUploadCommand({
Bucket: this.config.bucketName,
Key: objectPath,
+ ContentType: contentType,
})
const response = await this.client.send(command)