package handlers import ( "net/http" "github.com/gin-gonic/gin" "github.com/treytartt/mycrib-api/internal/services" ) // UploadHandler handles file upload endpoints type UploadHandler struct { storageService *services.StorageService } // NewUploadHandler creates a new upload handler func NewUploadHandler(storageService *services.StorageService) *UploadHandler { return &UploadHandler{storageService: storageService} } // UploadImage handles POST /api/uploads/image // Accepts multipart/form-data with "file" field func (h *UploadHandler) UploadImage(c *gin.Context) { file, err := c.FormFile("file") if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "No file provided"}) return } // Get category from query param (default: images) category := c.DefaultQuery("category", "images") result, err := h.storageService.Upload(file, category) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, result) } // UploadDocument handles POST /api/uploads/document // Accepts multipart/form-data with "file" field func (h *UploadHandler) UploadDocument(c *gin.Context) { file, err := c.FormFile("file") if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "No file provided"}) return } result, err := h.storageService.Upload(file, "documents") if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, result) } // UploadCompletion handles POST /api/uploads/completion // For task completion photos func (h *UploadHandler) UploadCompletion(c *gin.Context) { file, err := c.FormFile("file") if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "No file provided"}) return } result, err := h.storageService.Upload(file, "completions") if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, result) } // DeleteFile handles DELETE /api/uploads // Expects JSON body with "url" field func (h *UploadHandler) DeleteFile(c *gin.Context) { var req struct { URL string `json:"url" binding:"required"` } if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } if err := h.storageService.Delete(req.URL); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"message": "File deleted successfully"}) }