Skip to content

Commit

Permalink
feat: add MIME type support for raw file serving (#417)
Browse files Browse the repository at this point in the history
  • Loading branch information
s0up4200 authored Jan 26, 2025
1 parent f5b8881 commit 0609b64
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
7 changes: 5 additions & 2 deletions internal/web/handlers/gist/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}

Expand Down
14 changes: 13 additions & 1 deletion internal/web/handlers/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"
}
}

0 comments on commit 0609b64

Please # to comment.