fix:rollback ai chat viewer
This commit is contained in:
parent
05d2ebc03a
commit
c8ed293819
|
|
@ -4,7 +4,9 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"mime"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"narasi-ahli-be/app/module/ai_chat_files/mapper"
|
"narasi-ahli-be/app/module/ai_chat_files/mapper"
|
||||||
"narasi-ahli-be/app/module/ai_chat_files/repository"
|
"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)
|
return _i.Repo.Update(id, result)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_i *aiChatFilesService) Viewer(c *fiber.Ctx) error {
|
func (_i *aiChatFilesService) Viewer(c *fiber.Ctx) (err error) {
|
||||||
filename := c.Params("filename")
|
filename := c.Params("filename")
|
||||||
|
|
||||||
result, err := _i.Repo.FindByFilename(filename)
|
result, err := _i.Repo.FindByFilename(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -306,52 +308,41 @@ func (_i *aiChatFilesService) Viewer(c *fiber.Ctx) error {
|
||||||
bucketName := _i.MinioStorage.Cfg.ObjectStorage.MinioStorage.BucketName
|
bucketName := _i.MinioStorage.Cfg.ObjectStorage.MinioStorage.BucketName
|
||||||
objectName := *result.FilePath
|
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()
|
minioClient, err := _i.MinioStorage.ConnectMinio()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||||
|
"error": true,
|
||||||
|
"msg": err.Error(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
object, err := minioClient.GetObject(
|
fileContent, err := minioClient.GetObject(ctx, bucketName, objectName, minio.GetObjectOptions{})
|
||||||
ctx,
|
|
||||||
bucketName,
|
|
||||||
objectName,
|
|
||||||
minio.GetObjectOptions{},
|
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
defer object.Close()
|
defer fileContent.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]
|
|
||||||
|
|
||||||
|
// Tentukan Content-Type berdasarkan ekstensi file
|
||||||
|
contentType := mime.TypeByExtension("." + getFileExtension(objectName))
|
||||||
if contentType == "" {
|
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-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