mirror of
https://github.com/thomiceli/opengist.git
synced 2025-05-11 23:10:02 +02:00
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:
parent
d53885c541
commit
da0b440360
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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 + "\""
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user