fix
This commit is contained in:
parent
42e856f068
commit
976b7bc021
|
|
@ -307,52 +307,71 @@ func (_i *productsService) Delete(id uint) (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_i *productsService) Viewer(c *fiber.Ctx) error {
|
func (_i *productsService) Viewer(c *fiber.Ctx) (err error) {
|
||||||
// ambil full path setelah /viewer/
|
filename := c.Params("filename")
|
||||||
objectPath := strings.TrimPrefix(c.Params("*"), "/")
|
|
||||||
|
|
||||||
if objectPath == "" {
|
// Find product by filename (repository will search using LIKE pattern)
|
||||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
result, err := _i.Repo.FindByThumbnailPath(filename)
|
||||||
|
if err != nil {
|
||||||
|
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{
|
||||||
"error": true,
|
"error": true,
|
||||||
"msg": "Invalid file path",
|
"msg": "Product file not found",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
bucketName := _i.MinioStorage.Cfg.ObjectStorage.MinioStorage.BucketName
|
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
|
||||||
|
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(500).JSON(fiber.Map{
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||||
"error": true,
|
"error": true,
|
||||||
"msg": err.Error(),
|
"msg": err.Error(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
obj, err := minioClient.GetObject(
|
fileContent, err := minioClient.GetObject(ctx, bucketName, objectName, minio.GetObjectOptions{})
|
||||||
context.Background(),
|
|
||||||
bucketName,
|
|
||||||
objectPath,
|
|
||||||
minio.GetObjectOptions{},
|
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.Status(404).JSON(fiber.Map{
|
_i.Log.Error().Str("timestamp", time.Now().
|
||||||
|
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": "Product file not found",
|
"msg": "Failed to retrieve file",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
defer obj.Close()
|
defer fileContent.Close()
|
||||||
|
|
||||||
contentType := mime.TypeByExtension(filepath.Ext(objectPath))
|
// Determine Content-Type based on file extension
|
||||||
|
contentType := mime.TypeByExtension("." + getFileExtension(objectName))
|
||||||
if contentType == "" {
|
if contentType == "" {
|
||||||
contentType = "application/octet-stream"
|
contentType = "application/octet-stream" // fallback if no MIME type matches
|
||||||
}
|
}
|
||||||
|
|
||||||
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, ".")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue