fix:viewer ai chat file

This commit is contained in:
Rama Priyanto 2026-01-08 15:22:20 +07:00
parent b6bb1f4007
commit 05d2ebc03a
1 changed files with 22 additions and 14 deletions

View File

@ -308,10 +308,7 @@ func (_i *aiChatFilesService) Viewer(c *fiber.Ctx) error {
minioClient, err := _i.MinioStorage.ConnectMinio()
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": true,
"msg": err.Error(),
})
return err
}
object, err := minioClient.GetObject(
@ -325,29 +322,40 @@ func (_i *aiChatFilesService) Viewer(c *fiber.Ctx) error {
}
defer object.Close()
// 🔥 Ambil metadata object (INI PENTING)
stat, err := object.Stat()
if err != nil {
return err
}
// 🔥 Content-Type yang BENAR
if stat.ContentType != "" {
c.Set("Content-Type", stat.ContentType)
} else {
// fallback kalau metadata kosong
c.Type(filepath.Ext(objectName))
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]
if contentType == "" {
contentType = "application/octet-stream"
}
// 🔥 WAJIB untuk preview media
c.Set("Content-Type", contentType)
c.Set("Content-Disposition", "inline")
c.Set("Accept-Ranges", "bytes")
c.Set("Content-Length", strconv.FormatInt(stat.Size, 10))
// 🔥 BIARKAN FIBER HANDLE STREAMING
return c.SendStream(object)
return c.SendStream(object, int(stat.Size))
}
func getFileExtension(filename string) string {
// split file name
parts := strings.Split(filename, ".")