feat: fixing upload product
This commit is contained in:
parent
976b7bc021
commit
95f48ba56b
|
|
@ -30,7 +30,6 @@ import (
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
type productsService struct {
|
type productsService struct {
|
||||||
Repo repository.ProductsRepository
|
Repo repository.ProductsRepository
|
||||||
Log zerolog.Logger
|
Log zerolog.Logger
|
||||||
|
|
@ -310,25 +309,63 @@ func (_i *productsService) Delete(id uint) (err error) {
|
||||||
func (_i *productsService) Viewer(c *fiber.Ctx) (err error) {
|
func (_i *productsService) Viewer(c *fiber.Ctx) (err error) {
|
||||||
filename := c.Params("filename")
|
filename := c.Params("filename")
|
||||||
|
|
||||||
// Find product by filename (repository will search using LIKE pattern)
|
var objectName string
|
||||||
|
var found bool
|
||||||
|
|
||||||
|
// First, try to find by ThumbnailPath (for main product images)
|
||||||
result, err := _i.Repo.FindByThumbnailPath(filename)
|
result, err := _i.Repo.FindByThumbnailPath(filename)
|
||||||
if err != nil {
|
if err == nil && result != nil && result.ThumbnailPath != nil && *result.ThumbnailPath != "" {
|
||||||
|
// Check if the filename matches the thumbnail
|
||||||
|
if strings.Contains(*result.ThumbnailPath, filename) {
|
||||||
|
objectName = *result.ThumbnailPath
|
||||||
|
found = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If not found in ThumbnailPath, search in Colors JSON field
|
||||||
|
if !found {
|
||||||
|
// Create a query request with large limit to search all products
|
||||||
|
queryReq := request.ProductsQueryRequest{
|
||||||
|
Pagination: &paginator.Pagination{
|
||||||
|
Page: 1,
|
||||||
|
Limit: 1000, // Large limit to search all products
|
||||||
|
},
|
||||||
|
}
|
||||||
|
allProducts, _, err := _i.Repo.GetAll(queryReq)
|
||||||
|
if err == nil {
|
||||||
|
for _, product := range allProducts {
|
||||||
|
if product.Colors != nil && *product.Colors != "" {
|
||||||
|
var rawColors []struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
ImagePath *string `json:"image_path"`
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.Unmarshal([]byte(*product.Colors), &rawColors); err == nil {
|
||||||
|
for _, color := range rawColors {
|
||||||
|
if color.ImagePath != nil && strings.Contains(*color.ImagePath, filename) {
|
||||||
|
objectName = *color.ImagePath
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if found {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !found || objectName == "" {
|
||||||
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{
|
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{
|
||||||
"error": true,
|
"error": true,
|
||||||
"msg": "Product file not found",
|
"msg": "Product file not found",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if result.ThumbnailPath == nil || *result.ThumbnailPath == "" {
|
|
||||||
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{
|
|
||||||
"error": true,
|
|
||||||
"msg": "Product thumbnail path not found",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
bucketName := _i.MinioStorage.Cfg.ObjectStorage.MinioStorage.BucketName
|
bucketName := _i.MinioStorage.Cfg.ObjectStorage.MinioStorage.BucketName
|
||||||
objectName := *result.ThumbnailPath
|
|
||||||
|
|
||||||
_i.Log.Info().Str("timestamp", time.Now().
|
_i.Log.Info().Str("timestamp", time.Now().
|
||||||
Format(time.RFC3339)).Str("Service:Resource", "Products:Viewer").
|
Format(time.RFC3339)).Str("Service:Resource", "Products:Viewer").
|
||||||
|
|
@ -370,8 +407,6 @@ func (_i *productsService) Viewer(c *fiber.Ctx) (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
func getFileExtension(filename string) string {
|
func getFileExtension(filename string) string {
|
||||||
// split file name
|
// split file name
|
||||||
parts := strings.Split(filename, ".")
|
parts := strings.Split(filename, ".")
|
||||||
|
|
|
||||||
2
go.mod
2
go.mod
|
|
@ -13,6 +13,7 @@ require (
|
||||||
github.com/go-playground/validator/v10 v10.17.0
|
github.com/go-playground/validator/v10 v10.17.0
|
||||||
github.com/gofiber/fiber/v2 v2.52.4
|
github.com/gofiber/fiber/v2 v2.52.4
|
||||||
github.com/golang-jwt/jwt/v5 v5.2.1
|
github.com/golang-jwt/jwt/v5 v5.2.1
|
||||||
|
github.com/google/uuid v1.6.0
|
||||||
github.com/minio/minio-go/v7 v7.0.68
|
github.com/minio/minio-go/v7 v7.0.68
|
||||||
github.com/pelletier/go-toml/v2 v2.1.1
|
github.com/pelletier/go-toml/v2 v2.1.1
|
||||||
github.com/rs/zerolog v1.31.0
|
github.com/rs/zerolog v1.31.0
|
||||||
|
|
@ -34,7 +35,6 @@ require (
|
||||||
github.com/go-openapi/spec v0.20.15 // indirect
|
github.com/go-openapi/spec v0.20.15 // indirect
|
||||||
github.com/go-openapi/swag v0.22.10 // indirect
|
github.com/go-openapi/swag v0.22.10 // indirect
|
||||||
github.com/go-resty/resty/v2 v2.7.0 // indirect
|
github.com/go-resty/resty/v2 v2.7.0 // indirect
|
||||||
github.com/google/uuid v1.6.0 // indirect
|
|
||||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||||
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
|
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
|
||||||
github.com/jackc/pgx/v5 v5.4.3 // indirect
|
github.com/jackc/pgx/v5 v5.4.3 // indirect
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue