2022-09-24 19:33:38 +00:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
|
2022-10-30 04:05:38 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/core/services"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent/attachment"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/repo"
|
2022-10-30 02:15:35 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/sys/validate"
|
2023-04-09 18:39:43 +00:00
|
|
|
"github.com/hay-kot/httpkit/errchain"
|
|
|
|
"github.com/hay-kot/httpkit/server"
|
2022-09-24 19:33:38 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
)
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
type (
|
|
|
|
ItemAttachmentToken struct {
|
|
|
|
Token string `json:"token"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2023-03-07 06:18:58 +00:00
|
|
|
// HandleItemAttachmentCreate godocs
|
|
|
|
//
|
|
|
|
// @Summary Create Item Attachment
|
|
|
|
// @Tags Items Attachments
|
|
|
|
// @Produce json
|
|
|
|
// @Param id path string true "Item ID"
|
|
|
|
// @Param file formData file true "File attachment"
|
|
|
|
// @Param type formData string true "Type of file"
|
|
|
|
// @Param name formData string true "name of the file including extension"
|
|
|
|
// @Success 200 {object} repo.ItemOut
|
2023-07-17 01:37:56 +00:00
|
|
|
// @Failure 422 {object} validate.ErrorResponse
|
2023-03-07 06:18:58 +00:00
|
|
|
// @Router /v1/items/{id}/attachments [POST]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleItemAttachmentCreate() errchain.HandlerFunc {
|
2022-10-30 02:15:35 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) error {
|
2022-09-24 19:33:38 +00:00
|
|
|
err := r.ParseMultipartForm(ctrl.maxUploadSize << 20)
|
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("failed to parse multipart form")
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(errors.New("failed to parse multipart form"), http.StatusBadRequest)
|
|
|
|
|
2022-09-24 19:33:38 +00:00
|
|
|
}
|
|
|
|
|
2022-10-30 02:15:35 +00:00
|
|
|
errs := validate.NewFieldErrors()
|
2022-09-24 19:33:38 +00:00
|
|
|
|
|
|
|
file, _, err := r.FormFile("file")
|
|
|
|
if err != nil {
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, http.ErrMissingFile):
|
|
|
|
log.Debug().Msg("file for attachment is missing")
|
|
|
|
errs = errs.Append("file", "file is required")
|
|
|
|
default:
|
|
|
|
log.Err(err).Msg("failed to get file from form")
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(err, http.StatusInternalServerError)
|
2022-09-24 19:33:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
attachmentName := r.FormValue("name")
|
|
|
|
if attachmentName == "" {
|
|
|
|
log.Debug().Msg("failed to get name from form")
|
|
|
|
errs = errs.Append("name", "name is required")
|
|
|
|
}
|
|
|
|
|
2022-10-30 02:15:35 +00:00
|
|
|
if !errs.Nil() {
|
2023-03-21 04:32:10 +00:00
|
|
|
return server.JSON(w, http.StatusUnprocessableEntity, errs)
|
2022-09-24 19:33:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
attachmentType := r.FormValue("type")
|
|
|
|
if attachmentType == "" {
|
|
|
|
attachmentType = attachment.TypeAttachment.String()
|
|
|
|
}
|
|
|
|
|
2022-10-30 02:15:35 +00:00
|
|
|
id, err := ctrl.routeID(r)
|
2022-09-24 19:33:38 +00:00
|
|
|
if err != nil {
|
2022-10-30 02:15:35 +00:00
|
|
|
return err
|
2022-09-24 19:33:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx := services.NewContext(r.Context())
|
|
|
|
|
|
|
|
item, err := ctrl.svc.Items.AttachmentAdd(
|
|
|
|
ctx,
|
|
|
|
id,
|
|
|
|
attachmentName,
|
|
|
|
attachment.Type(attachmentType),
|
|
|
|
file,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("failed to add attachment")
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(err, http.StatusInternalServerError)
|
2022-09-24 19:33:38 +00:00
|
|
|
}
|
|
|
|
|
2023-03-21 04:32:10 +00:00
|
|
|
return server.JSON(w, http.StatusCreated, item)
|
2022-09-24 19:33:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleItemAttachmentGet godocs
|
2023-03-07 06:18:58 +00:00
|
|
|
//
|
|
|
|
// @Summary Get Item Attachment
|
|
|
|
// @Tags Items Attachments
|
|
|
|
// @Produce application/octet-stream
|
|
|
|
// @Param id path string true "Item ID"
|
|
|
|
// @Param attachment_id path string true "Attachment ID"
|
|
|
|
// @Success 200 {object} ItemAttachmentToken
|
|
|
|
// @Router /v1/items/{id}/attachments/{attachment_id} [GET]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleItemAttachmentGet() errchain.HandlerFunc {
|
2022-09-24 19:33:38 +00:00
|
|
|
return ctrl.handleItemAttachmentsHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleItemAttachmentDelete godocs
|
2023-03-07 06:18:58 +00:00
|
|
|
//
|
|
|
|
// @Summary Delete Item Attachment
|
|
|
|
// @Tags Items Attachments
|
|
|
|
// @Param id path string true "Item ID"
|
|
|
|
// @Param attachment_id path string true "Attachment ID"
|
|
|
|
// @Success 204
|
|
|
|
// @Router /v1/items/{id}/attachments/{attachment_id} [DELETE]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleItemAttachmentDelete() errchain.HandlerFunc {
|
2022-09-24 19:33:38 +00:00
|
|
|
return ctrl.handleItemAttachmentsHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleItemAttachmentUpdate godocs
|
2023-03-07 06:18:58 +00:00
|
|
|
//
|
|
|
|
// @Summary Update Item Attachment
|
|
|
|
// @Tags Items Attachments
|
|
|
|
// @Param id path string true "Item ID"
|
|
|
|
// @Param attachment_id path string true "Attachment ID"
|
|
|
|
// @Param payload body repo.ItemAttachmentUpdate true "Attachment Update"
|
|
|
|
// @Success 200 {object} repo.ItemOut
|
|
|
|
// @Router /v1/items/{id}/attachments/{attachment_id} [PUT]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleItemAttachmentUpdate() errchain.HandlerFunc {
|
2022-09-24 19:33:38 +00:00
|
|
|
return ctrl.handleItemAttachmentsHandler
|
|
|
|
}
|
|
|
|
|
2022-10-30 02:15:35 +00:00
|
|
|
func (ctrl *V1Controller) handleItemAttachmentsHandler(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
ID, err := ctrl.routeID(r)
|
2022-09-24 19:33:38 +00:00
|
|
|
if err != nil {
|
2022-10-30 02:15:35 +00:00
|
|
|
return err
|
2022-09-24 19:33:38 +00:00
|
|
|
}
|
|
|
|
|
2022-10-30 02:15:35 +00:00
|
|
|
attachmentID, err := ctrl.routeUUID(r, "attachment_id")
|
2022-09-24 19:33:38 +00:00
|
|
|
if err != nil {
|
2022-10-30 02:15:35 +00:00
|
|
|
return err
|
2022-09-24 19:33:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx := services.NewContext(r.Context())
|
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
2022-12-03 19:55:00 +00:00
|
|
|
doc, err := ctrl.svc.Items.AttachmentPath(r.Context(), attachmentID)
|
2022-09-24 19:33:38 +00:00
|
|
|
if err != nil {
|
2022-12-03 19:55:00 +00:00
|
|
|
log.Err(err).Msg("failed to get attachment path")
|
|
|
|
return validate.NewRequestError(err, http.StatusInternalServerError)
|
2022-09-24 19:33:38 +00:00
|
|
|
}
|
|
|
|
|
2022-12-03 19:55:00 +00:00
|
|
|
http.ServeFile(w, r, doc.Path)
|
|
|
|
return nil
|
2022-09-24 19:33:38 +00:00
|
|
|
|
|
|
|
// Delete Attachment Handler
|
|
|
|
case http.MethodDelete:
|
2022-10-30 02:15:35 +00:00
|
|
|
err = ctrl.svc.Items.AttachmentDelete(r.Context(), ctx.GID, ID, attachmentID)
|
2022-09-24 19:33:38 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("failed to delete attachment")
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(err, http.StatusInternalServerError)
|
2022-09-24 19:33:38 +00:00
|
|
|
}
|
|
|
|
|
2023-03-21 04:32:10 +00:00
|
|
|
return server.JSON(w, http.StatusNoContent, nil)
|
2022-09-24 19:33:38 +00:00
|
|
|
|
|
|
|
// Update Attachment Handler
|
|
|
|
case http.MethodPut:
|
2022-09-27 23:52:13 +00:00
|
|
|
var attachment repo.ItemAttachmentUpdate
|
2022-09-24 19:33:38 +00:00
|
|
|
err = server.Decode(r, &attachment)
|
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("failed to decode attachment")
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(err, http.StatusBadRequest)
|
2022-09-24 19:33:38 +00:00
|
|
|
}
|
|
|
|
|
2022-10-30 02:15:35 +00:00
|
|
|
attachment.ID = attachmentID
|
2022-10-17 02:50:44 +00:00
|
|
|
val, err := ctrl.svc.Items.AttachmentUpdate(ctx, ID, &attachment)
|
2022-09-24 19:33:38 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("failed to delete attachment")
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(err, http.StatusInternalServerError)
|
2022-09-24 19:33:38 +00:00
|
|
|
}
|
|
|
|
|
2023-03-21 04:32:10 +00:00
|
|
|
return server.JSON(w, http.StatusOK, val)
|
2022-09-24 19:33:38 +00:00
|
|
|
}
|
2022-10-30 02:15:35 +00:00
|
|
|
|
|
|
|
return nil
|
2022-09-24 19:33:38 +00:00
|
|
|
}
|