From 0609b64cfff0c88f125806f03215524174405e42 Mon Sep 17 00:00:00 2001 From: soup Date: Sun, 26 Jan 2025 23:40:59 +0100 Subject: [PATCH] feat: add MIME type support for raw file serving (#417) --- internal/web/handlers/gist/download.go | 7 +++++-- internal/web/handlers/util.go | 14 +++++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/internal/web/handlers/gist/download.go b/internal/web/handlers/gist/download.go index fed370cb..86593edf 100644 --- a/internal/web/handlers/gist/download.go +++ b/internal/web/handlers/gist/download.go @@ -3,9 +3,11 @@ package gist import ( "archive/zip" "bytes" + "strconv" + "github.com/thomiceli/opengist/internal/db" "github.com/thomiceli/opengist/internal/web/context" - "strconv" + "github.com/thomiceli/opengist/internal/web/handlers" ) func RawFile(ctx *context.Context) error { @@ -18,7 +20,8 @@ func RawFile(ctx *context.Context) error { if file == nil { return ctx.NotFound("File not found") } - + contentType := handlers.GetContentTypeFromFilename(file.Filename) + ctx.Response().Header().Set("Content-Type", contentType) return ctx.PlainText(200, file.Content) } diff --git a/internal/web/handlers/util.go b/internal/web/handlers/util.go index 25b161c3..da29bb6e 100644 --- a/internal/web/handlers/util.go +++ b/internal/web/handlers/util.go @@ -2,10 +2,12 @@ package handlers import ( "errors" - "github.com/thomiceli/opengist/internal/web/context" "html/template" + "path/filepath" "strconv" "strings" + + "github.com/thomiceli/opengist/internal/web/context" ) func GetPage(ctx *context.Context) int { @@ -77,3 +79,13 @@ func ParseSearchQueryStr(query string) (string, map[string]string) { content := strings.TrimSpace(contentBuilder.String()) return content, metadata } + +func GetContentTypeFromFilename(filename string) string { + ext := strings.ToLower(filepath.Ext(filename)) + switch ext { + case ".css": + return "text/css" + default: + return "text/plain" + } +}