package created
This commit is contained in:
parent
79d9ae5ab5
commit
d5610fbb30
4 changed files with 122 additions and 0 deletions
63
internal/httpDelivery/controller.go
Normal file
63
internal/httpDelivery/controller.go
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
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)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue