From f7ed85d2abc43b48fd3b0f029ab8875448e237fd Mon Sep 17 00:00:00 2001 From: Grigorii Dzhanelidze <350804+strlght@users.noreply.github.com> Date: Sun, 5 Sep 2021 13:45:50 +0300 Subject: [PATCH] Send correct Content-Type for podcastitem Need to pass correct Content-Type because Safari won't play any sound passed as application/octet-stream --- controllers/podcast.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/controllers/podcast.go b/controllers/podcast.go index 5fb7282..f784f6f 100644 --- a/controllers/podcast.go +++ b/controllers/podcast.go @@ -289,7 +289,7 @@ func GetPodcastItemFileById(c *gin.Context) { c.Header("Content-Description", "File Transfer") c.Header("Content-Transfer-Encoding", "binary") c.Header("Content-Disposition", "attachment; filename="+path.Base(podcast.DownloadPath)) - c.Header("Content-Type", "application/octet-stream") + c.Header("Content-Type", GetFileContentType(podcast.DownloadPath)) c.File(podcast.DownloadPath) } else { c.Redirect(302, podcast.FileURL) @@ -300,6 +300,19 @@ func GetPodcastItemFileById(c *gin.Context) { } } +func GetFileContentType(filePath string) string { + file, err := os.Open(filePath) + if err != nil { + return "application/octet-stream" + } + defer file.Close() + buffer := make([]byte, 512) + if _, err := file.Read(buffer); err != nil { + return "application/octet-stream" + } + return http.DetectContentType(buffer) +} + func MarkPodcastItemAsUnplayed(c *gin.Context) { var searchByIdQuery SearchByIdQuery