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)
|
||||
}
|
||||
18
internal/httpDelivery/handler.go
Normal file
18
internal/httpDelivery/handler.go
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
package httpDelivery
|
||||
|
||||
import "imageStorage/internal/interfaces"
|
||||
|
||||
type Handler struct {
|
||||
controller *controller
|
||||
service *service
|
||||
}
|
||||
|
||||
func NewHandler(utils interfaces.Utils) *Handler {
|
||||
s := newService(utils)
|
||||
c := newController(s)
|
||||
|
||||
return &Handler{
|
||||
controller: c,
|
||||
service: s,
|
||||
}
|
||||
}
|
||||
9
internal/httpDelivery/msg.go
Normal file
9
internal/httpDelivery/msg.go
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
package httpDelivery
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
const logGetImage = "Image delivery | Get image"
|
||||
|
||||
var notFound = errors.New("not found")
|
||||
32
internal/httpDelivery/service.go
Normal file
32
internal/httpDelivery/service.go
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
package httpDelivery
|
||||
|
||||
import (
|
||||
"imageStorage/internal/interfaces"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type service struct {
|
||||
utils interfaces.Utils
|
||||
}
|
||||
|
||||
func newService(utils interfaces.Utils) *service {
|
||||
return &service{
|
||||
utils: utils,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *service) GetImage(userUuid, merchUuid, imgType string) (string, error) {
|
||||
path := s.utils.MerchImageVolumePath(userUuid, merchUuid)
|
||||
file := s.utils.FilenameFromType(imgType)
|
||||
|
||||
fullPath := filepath.Join(path, file)
|
||||
|
||||
if _, err := os.Stat(fullPath); err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return "", notFound
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
return fullPath, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue