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