Spaces:
Runtime error
Runtime error
File size: 1,457 Bytes
4a66f20 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | package controllers
import (
"net/http"
"abdanhafidz.com/go-boilerplate/services"
"github.com/gin-gonic/gin"
)
type UploadController interface {
UploadMedia(ctx *gin.Context)
}
type uploadController struct {
uploadService services.UploadService
}
func NewUploadController(uploadService services.UploadService) UploadController {
return &uploadController{uploadService: uploadService}
}
// UploadMedia godoc
// @Summary Upload a media file
// @Description Upload an image or file to storage and receive a public URL back.
// @Tags Upload
// @Accept multipart/form-data
// @Produce json
// @Param file formData file true "File to upload"
// @Param folder formData string false "Folder name (default: uploads)"
// @Success 200 {object} map[string]string
// @Failure 400 {object} dto.ErrorResponse
// @Security BearerAuth
// @Router /api/v1/upload [post]
func (c *uploadController) UploadMedia(ctx *gin.Context) {
file, header, err := ctx.Request.FormFile("file")
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "file is required"})
return
}
defer file.Close()
folder := ctx.PostForm("folder")
if folder == "" {
folder = "uploads"
}
url, err := c.uploadService.UploadFile(file, header, folder)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
ctx.JSON(http.StatusOK, gin.H{"url": url})
}
|