package upload import ( "clapclap/internal/env" "context" "fmt" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/s3" ) func GetFileMaxSize(objectKey string) int { switch { case IsObjectImg(objectKey): size := env.C.MAX_UPLOAD_IMG_MB return size case IsObjectTrack(objectKey): size := env.C.MAX_UPLOAD_TRACK_MB return size } return -1 } func GeneratePresignedUrl(ctx context.Context, contentType string, s3c *s3.Client, objectKey string) (*s3.PresignedPostRequest, error) { urlExpireTime := env.C.PRESIGNED_URL_EXPIRE_TIME_MIN maxFileSize := int64(GetFileMaxSize(objectKey) * 1024 * 1024) if maxFileSize < 0 { return nil, fmt.Errorf("Invalid path for objectKey: %s", objectKey) } presigner := s3.NewPresignClient(s3c) postReq, err := presigner.PresignPostObject( ctx, &s3.PutObjectInput{ Bucket: aws.String(env.C.BUCKET_NAME), Key: aws.String(objectKey), ContentType: aws.String(contentType), }, func(opts *s3.PresignPostOptions) { opts.Expires = urlExpireTime opts.Conditions = append(opts.Conditions, []any{"content-length-range", 1, maxFileSize}, ) }, ) if err != nil { return nil, err } return postReq, nil }