fix(storage): auto-detect content-type to resolve image rendering issues

Fixes #995. Added mime type detection in SaveBytes for MinIO, OSS, S3, and TOS to prevent images from being incorrectly saved as text/csv.
This commit is contained in:
jw_mac
2026-04-20 19:14:55 +08:00
committed by lyingbug
parent ede7dd08a2
commit 09e1642335
5 changed files with 64 additions and 59 deletions
+1 -1
View File
@@ -177,7 +177,7 @@ func (s *minioFileService) SaveBytes(ctx context.Context, data []byte, tenantID
// Upload bytes to MinIO
reader := bytes.NewReader(data)
_, err = s.client.PutObject(ctx, s.bucketName, objectName, reader, int64(len(data)), minio.PutObjectOptions{
ContentType: "text/csv; charset=utf-8",
ContentType: utils.GetContentTypeByExt(ext),
})
if err != nil {
return "", fmt.Errorf("failed to upload bytes to MinIO: %w", err)
+2 -2
View File
@@ -172,7 +172,7 @@ func (s *ossFileService) SaveFile(ctx context.Context,
contentType := file.Header.Get("Content-Type")
if contentType == "" {
contentType = getContentTypeByExt(ext)
contentType = utils.GetContentTypeByExt(ext)
}
// Use Uploader for files > 10MB (auto multipart with concurrent uploads)
@@ -233,7 +233,7 @@ func (s *ossFileService) SaveBytes(ctx context.Context, data []byte, tenantID ui
Bucket: oss.Ptr(targetBucket),
Key: oss.Ptr(objectName),
Body: bytes.NewReader(data),
ContentType: oss.Ptr("text/csv; charset=utf-8"),
ContentType: oss.Ptr(utils.GetContentTypeByExt(ext)),
})
if err != nil {
return "", fmt.Errorf("failed to upload bytes to OSS: %w", err)
+2 -54
View File
@@ -172,58 +172,6 @@ func (s *s3FileService) parseS3FilePath(filePath string) (string, error) {
return parts[1], nil
}
// getContentTypeByExt returns the content type based on file extension
func getContentTypeByExt(ext string) string {
switch strings.ToLower(ext) {
case ".csv":
return "text/csv; charset=utf-8"
case ".json":
return "application/json"
case ".pdf":
return "application/pdf"
case ".doc":
return "application/msword"
case ".docx":
return "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
case ".xls":
return "application/vnd.ms-excel"
case ".xlsx":
return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
case ".ppt":
return "application/vnd.ms-powerpoint"
case ".pptx":
return "application/vnd.openxmlformats-officedocument.presentationml.presentation"
case ".txt":
return "text/plain; charset=utf-8"
case ".md":
return "text/markdown"
case ".html":
return "text/html; charset=utf-8"
case ".jpg", ".jpeg":
return "image/jpeg"
case ".png":
return "image/png"
case ".gif":
return "image/gif"
case ".svg":
return "image/svg+xml"
case ".mp3":
return "audio/mpeg"
case ".wav":
return "audio/wav"
case ".m4a":
return "audio/mp4"
case ".flac":
return "audio/flac"
case ".ogg":
return "audio/ogg"
case ".mp4":
return "video/mp4"
default:
return "application/octet-stream"
}
}
// SaveFile saves a file to S3
func (s *s3FileService) SaveFile(ctx context.Context,
file *multipart.FileHeader, tenantID uint64, knowledgeID string,
@@ -242,7 +190,7 @@ func (s *s3FileService) SaveFile(ctx context.Context,
// Determine content type
contentType := file.Header.Get("Content-Type")
if contentType == "" {
contentType = getContentTypeByExt(ext)
contentType = utils.GetContentTypeByExt(ext)
}
// Upload file to S3
@@ -313,7 +261,7 @@ func (s *s3FileService) SaveBytes(ctx context.Context, data []byte, tenantID uin
Key: aws.String(objectName),
Body: reader,
ContentLength: aws.Int64(int64(len(data))),
ContentType: aws.String("text/csv; charset=utf-8"),
ContentType: aws.String(utils.GetContentTypeByExt(ext)),
})
if err != nil {
return "", fmt.Errorf("failed to upload bytes to S3: %w", err)
+6 -2
View File
@@ -166,11 +166,15 @@ func (s *tosFileService) SaveFile(ctx context.Context, file *multipart.FileHeade
}
defer src.Close()
contentType := file.Header.Get("Content-Type")
if contentType == "" {
contentType = utils.GetContentTypeByExt(ext)
}
_, err = s.client.PutObjectV2(ctx, &tos.PutObjectV2Input{
PutObjectBasicInput: tos.PutObjectBasicInput{
Bucket: s.bucketName,
Key: objectName,
ContentType: file.Header.Get("Content-Type"),
ContentType: contentType,
},
Content: src,
})
@@ -210,7 +214,7 @@ func (s *tosFileService) SaveBytes(ctx context.Context, data []byte, tenantID ui
PutObjectBasicInput: tos.PutObjectBasicInput{
Bucket: targetBucket,
Key: objectName,
ContentType: "text/csv; charset=utf-8",
ContentType: utils.GetContentTypeByExt(ext),
},
Content: reader,
})
+53
View File
@@ -0,0 +1,53 @@
package utils
import "strings"
// GetContentTypeByExt returns the content type based on file extension
func GetContentTypeByExt(ext string) string {
switch strings.ToLower(ext) {
case ".csv":
return "text/csv; charset=utf-8"
case ".json":
return "application/json"
case ".pdf":
return "application/pdf"
case ".doc":
return "application/msword"
case ".docx":
return "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
case ".xls":
return "application/vnd.ms-excel"
case ".xlsx":
return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
case ".png":
return "image/png"
case ".jpg", ".jpeg":
return "image/jpeg"
case ".gif":
return "image/gif"
case ".webp":
return "image/webp"
case ".svg":
return "image/svg+xml"
case ".txt":
return "text/plain; charset=utf-8"
case ".html", ".htm":
return "text/html; charset=utf-8"
case ".mp4":
return "video/mp4"
case ".mp3":
return "audio/mpeg"
case ".wav":
return "audio/wav"
case ".zip":
return "application/zip"
case ".tar":
return "application/x-tar"
case ".gz":
return "application/gzip"
case ".md":
return "text/markdown; charset=utf-8"
default:
return "application/octet-stream"
}
}