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
+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)