upload.go 2.7 KB

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