fix:rollback ai chat viewer
This commit is contained in:
parent
05d2ebc03a
commit
c8ed293819
|
|
@ -4,7 +4,9 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"math/rand"
|
||||
"mime"
|
||||
"mime/multipart"
|
||||
"narasi-ahli-be/app/module/ai_chat_files/mapper"
|
||||
"narasi-ahli-be/app/module/ai_chat_files/repository"
|
||||
|
|
@ -294,8 +296,8 @@ func (_i *aiChatFilesService) Delete(id uint) error {
|
|||
return _i.Repo.Update(id, result)
|
||||
}
|
||||
|
||||
func (_i *aiChatFilesService) Viewer(c *fiber.Ctx) error {
|
||||
filename := c.Params("filename")
|
||||
func (_i *aiChatFilesService) Viewer(c *fiber.Ctx) (err error) {
|
||||
filename := c.Params("filename")
|
||||
|
||||
result, err := _i.Repo.FindByFilename(filename)
|
||||
if err != nil {
|
||||
|
|
@ -306,52 +308,41 @@ func (_i *aiChatFilesService) Viewer(c *fiber.Ctx) error {
|
|||
bucketName := _i.MinioStorage.Cfg.ObjectStorage.MinioStorage.BucketName
|
||||
objectName := *result.FilePath
|
||||
|
||||
_i.Log.Info().Str("timestamp", time.Now().
|
||||
Format(time.RFC3339)).Str("Service:Resource", "AiChat:Uploads").
|
||||
Interface("data", objectName).Msg("")
|
||||
|
||||
// Create minio connection.
|
||||
|
||||
|
||||
minioClient, err := _i.MinioStorage.ConnectMinio()
|
||||
if err != nil {
|
||||
return err
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"error": true,
|
||||
"msg": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
object, err := minioClient.GetObject(
|
||||
ctx,
|
||||
bucketName,
|
||||
objectName,
|
||||
minio.GetObjectOptions{},
|
||||
)
|
||||
fileContent, err := minioClient.GetObject(ctx, bucketName, objectName, minio.GetObjectOptions{})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
log.Fatalln(err)
|
||||
}
|
||||
defer object.Close()
|
||||
|
||||
stat, err := object.Stat()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ext := strings.ToLower(filepath.Ext(objectName))
|
||||
|
||||
contentType := map[string]string{
|
||||
".mp3": "audio/mpeg",
|
||||
".wav": "audio/wav",
|
||||
".ogg": "audio/ogg",
|
||||
".webm": "audio/webm",
|
||||
".mp4": "video/mp4",
|
||||
".png": "image/png",
|
||||
".jpg": "image/jpeg",
|
||||
".jpeg": "image/jpeg",
|
||||
".gif": "image/gif",
|
||||
".pdf": "application/pdf",
|
||||
}[ext]
|
||||
defer fileContent.Close()
|
||||
|
||||
// Tentukan Content-Type berdasarkan ekstensi file
|
||||
contentType := mime.TypeByExtension("." + getFileExtension(objectName))
|
||||
if contentType == "" {
|
||||
contentType = "application/octet-stream"
|
||||
contentType = "application/octet-stream" // fallback jika tidak ada tipe MIME yang cocok
|
||||
}
|
||||
|
||||
c.Set("Content-Type", contentType)
|
||||
c.Set("Content-Disposition", "inline")
|
||||
c.Set("Accept-Ranges", "bytes")
|
||||
c.Set("Content-Length", strconv.FormatInt(stat.Size, 10))
|
||||
|
||||
return c.SendStream(object, int(stat.Size))
|
||||
if _, err := io.Copy(c.Response().BodyWriter(), fileContent); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue