63 lines
1.4 KiB
Go
63 lines
1.4 KiB
Go
package httpDelivery
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/gin-gonic/gin"
|
|
log "github.com/sirupsen/logrus"
|
|
"net/http"
|
|
)
|
|
|
|
type controller struct {
|
|
service *service
|
|
}
|
|
|
|
func newController(s *service) *controller {
|
|
return &controller{
|
|
service: s,
|
|
}
|
|
}
|
|
|
|
func (h *Handler) RegisterRoutes(r *gin.RouterGroup) {
|
|
merchImages := r.Group("/merchImages")
|
|
merchImages.GET("/:userUuid/:merchUuid", h.controller.GetImage)
|
|
log.Debug("Image delivery | Register routes")
|
|
}
|
|
|
|
func (co *controller) GetImage(c *gin.Context) {
|
|
userUuid := c.Param("userUuid")
|
|
merchUuid := c.Param("merchUuid")
|
|
imgType := c.Query("type")
|
|
|
|
if userUuid == "" || merchUuid == "" || imgType == "" {
|
|
log.WithFields(log.Fields{
|
|
"msg": "empty path/query param(s)",
|
|
"userUuid": userUuid,
|
|
"merchUuid": merchUuid,
|
|
"imgType": imgType,
|
|
}).Debug(logGetImage)
|
|
c.AbortWithStatus(http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
if imgType != "thumbnail" && imgType != "full" {
|
|
log.WithField("msg", "invalid image type").Debug(logGetImage)
|
|
c.AbortWithStatus(http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
link, err := co.service.GetImage(userUuid, merchUuid, imgType)
|
|
if err != nil {
|
|
if errors.Is(err, notFound) {
|
|
log.WithField("msg", err.Error()).Debug(logGetImage)
|
|
c.AbortWithStatus(http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
log.WithField("msg", err.Error()).Debug(logGetImage)
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
log.WithField("msg", "success").Info(logGetImage)
|
|
c.File(link)
|
|
}
|