diff --git a/app/internal/logic/uploadpresignedurllogic.go b/app/internal/logic/uploadpresignedurllogic.go index d4a1dae..8c7d10a 100644 --- a/app/internal/logic/uploadpresignedurllogic.go +++ b/app/internal/logic/uploadpresignedurllogic.go @@ -4,13 +4,13 @@ import ( "context" "fmt" "path/filepath" + "strings" "time" "git.cialloo.com/CiallooWeb/Blog/app/internal/svc" "git.cialloo.com/CiallooWeb/Blog/app/internal/types" "github.com/aws/aws-sdk-go-v2/service/s3" - "github.com/google/uuid" "github.com/zeromicro/go-zero/core/logx" ) @@ -29,10 +29,61 @@ func NewUploadPresignedURLLogic(ctx context.Context, svcCtx *svc.ServiceContext) } } +// getFileType categorizes file extensions into type folders +func getFileType(ext string) string { + ext = strings.ToLower(ext) + + imageExts := map[string]bool{ + ".jpg": true, ".jpeg": true, ".png": true, ".gif": true, ".bmp": true, + ".webp": true, ".svg": true, ".ico": true, ".tiff": true, ".tif": true, + } + + videoExts := map[string]bool{ + ".mp4": true, ".avi": true, ".mov": true, ".wmv": true, ".flv": true, + ".mkv": true, ".webm": true, ".m4v": true, ".mpg": true, ".mpeg": true, + } + + audioExts := map[string]bool{ + ".mp3": true, ".wav": true, ".flac": true, ".aac": true, ".ogg": true, + ".wma": true, ".m4a": true, ".opus": true, ".ape": true, + } + + archiveExts := map[string]bool{ + ".zip": true, ".rar": true, ".7z": true, ".tar": true, ".gz": true, + ".bz2": true, ".xz": true, ".iso": true, + } + + documentExts := map[string]bool{ + ".pdf": true, ".doc": true, ".docx": true, ".xls": true, ".xlsx": true, + ".ppt": true, ".pptx": true, ".txt": true, ".rtf": true, ".odt": true, + ".ods": true, ".odp": true, + } + + switch { + case imageExts[ext]: + return "images" + case videoExts[ext]: + return "videos" + case audioExts[ext]: + return "audios" + case archiveExts[ext]: + return "archives" + case documentExts[ext]: + return "documents" + default: + return "others" + } +} + func (l *UploadPresignedURLLogic) UploadPresignedURL(req *types.UploadPresignedURLReq) (resp *types.UploadPresignedURLResp, err error) { // Generate unique file key ext := filepath.Ext(req.FileName) - fileKey := fmt.Sprintf("uploads/%s%s", uuid.New().String(), ext) + + // Determine file type based on extension + fileType := getFileType(ext) + + // Generate file key grouped by type: {type}/{time}{ext} + fileKey := fmt.Sprintf("%s/%d%s", fileType, time.Now().UnixMilli(), ext) // Calculate expiration time expiration := time.Duration(l.svcCtx.Config.S3.PresignedURLExpiration) * time.Second