Fix garbled/mojibake text display issues for non-English Unicode characters in browsers. (#441)

* Update util.go

Fix garbled/mojibake text display issues for non-English Unicode characters in browsers.

* add Content-Disposition, help handle file name on download

Author:    awkj <hzzbiu@gmail.com>
This commit is contained in:
awkj 2025-03-17 23:22:54 +08:00 committed by GitHub
parent d53885c541
commit da0b440360
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 4 deletions

View File

@ -21,7 +21,9 @@ func RawFile(ctx *context.Context) error {
return ctx.NotFound("File not found")
}
contentType := handlers.GetContentTypeFromFilename(file.Filename)
ContentDisposition := handlers.GetContentDisposition(file.Filename)
ctx.Response().Header().Set("Content-Type", contentType)
ctx.Response().Header().Set("Content-Disposition", ContentDisposition)
return ctx.PlainText(200, file.Content)
}

View File

@ -2,13 +2,14 @@ package handlers
import (
"errors"
"github.com/gorilla/schema"
"html/template"
"net/url"
"path/filepath"
"strconv"
"strings"
"github.com/gorilla/schema"
"github.com/thomiceli/opengist/internal/web/context"
)
@ -141,12 +142,21 @@ func ParseSearchQueryStr(query string) (string, map[string]string) {
return content, metadata
}
func GetContentTypeFromFilename(filename string) string {
func GetContentTypeFromFilename(filename string) (ret string) {
ext := strings.ToLower(filepath.Ext(filename))
switch ext {
case ".css":
return "text/css"
ret = "text/css"
default:
return "text/plain"
ret = "text/plain"
}
// add charset=utf-8, if not, unicode charset will be broken
ret += "; charset=utf-8"
return
}
func GetContentDisposition(filename string) string {
return "inline; filename=\"" + filename + "\""
}