update api product
This commit is contained in:
parent
6ac6e6ea9c
commit
13b95c0109
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"jaecoo-be/app/database/entity"
|
"jaecoo-be/app/database/entity"
|
||||||
res "jaecoo-be/app/module/products/response"
|
res "jaecoo-be/app/module/products/response"
|
||||||
"path/filepath"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func ProductsResponseMapper(product *entity.Products, host string) *res.ProductsResponse {
|
func ProductsResponseMapper(product *entity.Products, host string) *res.ProductsResponse {
|
||||||
|
|
@ -14,7 +13,7 @@ func ProductsResponseMapper(product *entity.Products, host string) *res.Products
|
||||||
|
|
||||||
var colors []res.ProductColorResponse
|
var colors []res.ProductColorResponse
|
||||||
|
|
||||||
if product.Colors != nil && *product.Colors != "" {
|
if product.Colors != nil && *product.Colors != "" {
|
||||||
var rawColors []struct {
|
var rawColors []struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
ImagePath *string `json:"image_path"`
|
ImagePath *string `json:"image_path"`
|
||||||
|
|
@ -25,11 +24,9 @@ if product.Colors != nil && *product.Colors != "" {
|
||||||
for _, c := range rawColors {
|
for _, c := range rawColors {
|
||||||
var imageUrl *string
|
var imageUrl *string
|
||||||
|
|
||||||
if c.ImagePath != nil {
|
if c.ImagePath != nil && *c.ImagePath != "" {
|
||||||
filename := filepath.Base(*c.ImagePath)
|
url := host + "/api/products/viewer/" + *c.ImagePath
|
||||||
url := host + "/products/viewer/" + filename
|
|
||||||
imageUrl = &url
|
imageUrl = &url
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
colors = append(colors, res.ProductColorResponse{
|
colors = append(colors, res.ProductColorResponse{
|
||||||
|
|
@ -38,8 +35,7 @@ if product.Colors != nil && *product.Colors != "" {
|
||||||
ImageUrl: imageUrl,
|
ImageUrl: imageUrl,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
response := &res.ProductsResponse{
|
response := &res.ProductsResponse{
|
||||||
ID: product.ID,
|
ID: product.ID,
|
||||||
|
|
@ -56,12 +52,11 @@ if product.Colors != nil && *product.Colors != "" {
|
||||||
}
|
}
|
||||||
|
|
||||||
if product.ThumbnailPath != nil && *product.ThumbnailPath != "" {
|
if product.ThumbnailPath != nil && *product.ThumbnailPath != "" {
|
||||||
// Extract filename from path
|
thumbnailUrl := host + "/api/products/viewer/" + *product.ThumbnailPath
|
||||||
filename := filepath.Base(*product.ThumbnailPath)
|
|
||||||
thumbnailUrl := host + "/products/viewer/" + filename
|
|
||||||
response.ThumbnailUrl = &thumbnailUrl
|
response.ThumbnailUrl = &thumbnailUrl
|
||||||
}
|
}
|
||||||
|
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -307,69 +307,52 @@ func (_i *productsService) Delete(id uint) (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_i *productsService) Viewer(c *fiber.Ctx) (err error) {
|
func (_i *productsService) Viewer(c *fiber.Ctx) error {
|
||||||
filename := c.Params("filename")
|
// ambil full path setelah /viewer/
|
||||||
|
objectPath := strings.TrimPrefix(c.Params("*"), "/")
|
||||||
|
|
||||||
// Find product by filename (repository will search using LIKE pattern)
|
if objectPath == "" {
|
||||||
result, err := _i.Repo.FindByThumbnailPath(filename)
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||||
if err != nil {
|
|
||||||
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{
|
|
||||||
"error": true,
|
"error": true,
|
||||||
"msg": "Product file not found",
|
"msg": "Invalid file path",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
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()
|
|
||||||
bucketName := _i.MinioStorage.Cfg.ObjectStorage.MinioStorage.BucketName
|
bucketName := _i.MinioStorage.Cfg.ObjectStorage.MinioStorage.BucketName
|
||||||
objectName := *result.ThumbnailPath
|
|
||||||
|
|
||||||
_i.Log.Info().Str("timestamp", time.Now().
|
|
||||||
Format(time.RFC3339)).Str("Service:Resource", "Products:Viewer").
|
|
||||||
Interface("data", objectName).Msg("")
|
|
||||||
|
|
||||||
// Create minio connection
|
|
||||||
minioClient, err := _i.MinioStorage.ConnectMinio()
|
minioClient, err := _i.MinioStorage.ConnectMinio()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
return c.Status(500).JSON(fiber.Map{
|
||||||
"error": true,
|
"error": true,
|
||||||
"msg": err.Error(),
|
"msg": err.Error(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fileContent, err := minioClient.GetObject(ctx, bucketName, objectName, minio.GetObjectOptions{})
|
obj, err := minioClient.GetObject(
|
||||||
|
context.Background(),
|
||||||
|
bucketName,
|
||||||
|
objectPath,
|
||||||
|
minio.GetObjectOptions{},
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_i.Log.Error().Str("timestamp", time.Now().
|
return c.Status(404).JSON(fiber.Map{
|
||||||
Format(time.RFC3339)).Str("Service:Resource", "Products:Viewer").
|
|
||||||
Interface("Error getting file", err).Msg("")
|
|
||||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
|
||||||
"error": true,
|
"error": true,
|
||||||
"msg": "Failed to retrieve file",
|
"msg": "Product file not found",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
defer fileContent.Close()
|
defer obj.Close()
|
||||||
|
|
||||||
// Determine Content-Type based on file extension
|
contentType := mime.TypeByExtension(filepath.Ext(objectPath))
|
||||||
contentType := mime.TypeByExtension("." + getFileExtension(objectName))
|
|
||||||
if contentType == "" {
|
if contentType == "" {
|
||||||
contentType = "application/octet-stream" // fallback if no MIME type matches
|
contentType = "application/octet-stream"
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Set("Content-Type", contentType)
|
c.Set("Content-Type", contentType)
|
||||||
|
_, err = io.Copy(c.Response().BodyWriter(), obj)
|
||||||
if _, err := io.Copy(c.Response().BodyWriter(), fileContent); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
|
||||||
|
|
||||||
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, ".")
|
||||||
|
|
|
||||||
BIN
jaecoo-be.exe
BIN
jaecoo-be.exe
Binary file not shown.
Loading…
Reference in New Issue