upload.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Package handlers
  2. package handlers
  3. import (
  4. "encoding/json"
  5. "net/http"
  6. "time"
  7. "github.com/google/uuid"
  8. "github.com/minio/minio-go/v7"
  9. "github.com/minio/minio-go/v7/pkg/credentials"
  10. )
  11. type UploadHandler struct {
  12. minioClient *minio.Client
  13. bucket string
  14. endpoint string
  15. publicEndpoint string
  16. }
  17. func NewUploadHandler(endpoint, publicEndpoint, accessKey, secretKey, bucket string, useSSL bool) (*UploadHandler, error) {
  18. client, err := minio.New(endpoint, &minio.Options{
  19. Creds: credentials.NewStaticV4(accessKey, secretKey, ""),
  20. Secure: useSSL,
  21. })
  22. if err != nil {
  23. return nil, err
  24. }
  25. return &UploadHandler{
  26. minioClient: client,
  27. bucket: bucket,
  28. endpoint: endpoint,
  29. publicEndpoint: publicEndpoint,
  30. }, nil
  31. }
  32. type presignedURLRequest struct {
  33. ContentType string `json:"content_type"`
  34. MaxSizeMB int `json:"max_size_mb"`
  35. }
  36. type presignedURLResponse struct {
  37. UploadURL string `json:"upload_url"`
  38. FileURL string `json:"file_url"`
  39. Fields map[string]string `json:"fields"`
  40. }
  41. func (h *UploadHandler) PresignedURL(w http.ResponseWriter, r *http.Request) {
  42. var req presignedURLRequest
  43. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  44. writeError(w, http.StatusBadRequest, "invalid request body")
  45. return
  46. }
  47. if req.MaxSizeMB <= 0 || req.MaxSizeMB > 50 {
  48. req.MaxSizeMB = 10
  49. }
  50. validContentTypes := map[string]bool{
  51. "image/jpeg": true, "image/png": true, "image/webp": true, "image/heic": true,
  52. }
  53. if !validContentTypes[req.ContentType] {
  54. writeError(w, http.StatusBadRequest, "unsupported content type")
  55. return
  56. }
  57. fileID := uuid.New().String()
  58. ext := ".jpg"
  59. switch req.ContentType {
  60. case "image/png":
  61. ext = ".png"
  62. case "image/webp":
  63. ext = ".webp"
  64. case "image/heic":
  65. ext = ".heic"
  66. }
  67. objectName := "uploads/" + fileID + ext
  68. policy := minio.NewPostPolicy()
  69. policy.SetBucket(h.bucket)
  70. policy.SetKey(objectName)
  71. policy.SetExpires(time.Now().Add(15 * time.Minute))
  72. policy.SetContentType(req.ContentType)
  73. policy.SetContentLengthRange(1, int64(req.MaxSizeMB)*1024*1024)
  74. url, formData, err := h.minioClient.PresignedPostPolicy(r.Context(), policy)
  75. if err != nil {
  76. writeError(w, http.StatusInternalServerError, "failed to generate upload URL")
  77. return
  78. }
  79. fileURL := h.publicEndpoint + "/" + h.bucket + "/" + objectName
  80. writeJSON(w, http.StatusOK, presignedURLResponse{
  81. UploadURL: url.String(),
  82. FileURL: fileURL,
  83. Fields: formData,
  84. })
  85. }