mirror of
https://github.com/thomiceli/opengist.git
synced 2025-05-13 15:52:11 +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")
|
return ctx.NotFound("File not found")
|
||||||
}
|
}
|
||||||
contentType := handlers.GetContentTypeFromFilename(file.Filename)
|
contentType := handlers.GetContentTypeFromFilename(file.Filename)
|
||||||
|
ContentDisposition := handlers.GetContentDisposition(file.Filename)
|
||||||
ctx.Response().Header().Set("Content-Type", contentType)
|
ctx.Response().Header().Set("Content-Type", contentType)
|
||||||
|
ctx.Response().Header().Set("Content-Disposition", ContentDisposition)
|
||||||
return ctx.PlainText(200, file.Content)
|
return ctx.PlainText(200, file.Content)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,13 +2,14 @@ package handlers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/gorilla/schema"
|
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/url"
|
"net/url"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/gorilla/schema"
|
||||||
|
|
||||||
"github.com/thomiceli/opengist/internal/web/context"
|
"github.com/thomiceli/opengist/internal/web/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -141,12 +142,21 @@ func ParseSearchQueryStr(query string) (string, map[string]string) {
|
|||||||
return content, metadata
|
return content, metadata
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetContentTypeFromFilename(filename string) string {
|
func GetContentTypeFromFilename(filename string) (ret string) {
|
||||||
ext := strings.ToLower(filepath.Ext(filename))
|
ext := strings.ToLower(filepath.Ext(filename))
|
||||||
|
|
||||||
switch ext {
|
switch ext {
|
||||||
case ".css":
|
case ".css":
|
||||||
return "text/css"
|
ret = "text/css"
|
||||||
default:
|
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