add_image.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package image
  2. import (
  3. "database/sql"
  4. "mime/multipart"
  5. "path/filepath"
  6. "strconv"
  7. "time"
  8. API "git.dmitriygnatenko.ru/dima/homethings/internal/api/v1"
  9. "git.dmitriygnatenko.ru/dima/homethings/internal/factory"
  10. "git.dmitriygnatenko.ru/dima/homethings/internal/helpers"
  11. "git.dmitriygnatenko.ru/dima/homethings/internal/interfaces"
  12. "git.dmitriygnatenko.ru/dima/homethings/internal/mappers"
  13. "github.com/gofiber/fiber/v2"
  14. )
  15. const fileDateLayout = "2006-01-02-15-04-05"
  16. // @Router /api/v1/images [post]
  17. // @Param place_id formData int false "Place ID"
  18. // @Param thing_id formData int false "Thing ID"
  19. // @Param files formData []file true "Files"
  20. // @Success 200 {object} dto.EmptyResponse
  21. // @Failure 400 {object} dto.ErrorResponse
  22. // @Failure 500 {object} dto.ErrorResponse
  23. // @Summary Add images
  24. // @Tags Images
  25. // @security APIKey
  26. // @Accept mpfd
  27. // @Produce json
  28. func AddImageHandler(sp interfaces.ServiceProvider) fiber.Handler {
  29. return func(fctx *fiber.Ctx) error {
  30. var form *multipart.Form
  31. var placeID, thingID int
  32. var files []string
  33. var err error
  34. ctx := fctx.Context()
  35. if form, err = fctx.MultipartForm(); err != nil {
  36. return fiber.NewError(fiber.StatusBadRequest, err.Error())
  37. }
  38. if formPlace := form.Value["place_id"]; len(formPlace) > 0 {
  39. placeID, err = strconv.Atoi(formPlace[0])
  40. if err != nil {
  41. return fiber.NewError(fiber.StatusBadRequest, err.Error())
  42. }
  43. }
  44. if formThing := form.Value["thing_id"]; len(formThing) > 0 {
  45. thingID, err = strconv.Atoi(formThing[0])
  46. if err != nil {
  47. return fiber.NewError(fiber.StatusBadRequest, err.Error())
  48. }
  49. }
  50. date := time.Now().Format(fileDateLayout)
  51. for _, file := range form.File["files"] {
  52. filename := "/files/" + date + "_" + helpers.GenerateRandomString(10) + filepath.Ext(file.Filename)
  53. if err = sp.GetFileRepository().Save(fctx, file, filename); err != nil {
  54. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  55. }
  56. files = append(files, filename)
  57. }
  58. if (placeID == 0 && thingID == 0) || len(files) == 0 {
  59. return fiber.NewError(fiber.StatusBadRequest, "")
  60. }
  61. var tx *sql.Tx
  62. if thingID > 0 {
  63. tx, err = sp.GetThingImageRepository().BeginTx(ctx, API.DefaultTxLevel)
  64. if err != nil {
  65. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  66. }
  67. for _, file := range files {
  68. if err = sp.GetThingImageRepository().Add(ctx, mappers.ConvertToAddThingImageRequestModel(thingID, file), tx); err != nil {
  69. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  70. }
  71. }
  72. if err = sp.GetThingImageRepository().CommitTx(tx); err != nil {
  73. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  74. }
  75. } else {
  76. tx, err = sp.GetPlaceImageRepository().BeginTx(ctx, API.DefaultTxLevel)
  77. if err != nil {
  78. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  79. }
  80. for _, file := range files {
  81. if err = sp.GetPlaceImageRepository().Add(ctx, mappers.ConvertToAddPlaceImageRequestModel(placeID, file), tx); err != nil {
  82. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  83. }
  84. }
  85. if err = sp.GetPlaceImageRepository().CommitTx(tx); err != nil {
  86. return fiber.NewError(fiber.StatusInternalServerError, err.Error())
  87. }
  88. }
  89. return fctx.JSON(factory.CreateEmptyResponse())
  90. }
  91. }