Closed
Description
- Gitea version (or commit ref): v1.5.1
- Git version: git version 2.18.0
- Operating system: CentOS Linux release 7.5.1804
- Database (use
[x]
):- PostgreSQL
- MySQL
- MSSQL
- SQLite
- Can you reproduce the bug at https://try.gitea.io:
- Yes (provide example URL): https://try.gitea.io/lunny/testGiteaChardet/src/branch/master/readme.txt
- No
- Not relevant
- Log gist: N/A
Description
If the file is readme.txt and the encoding is not UTF-8, the file rendering displays the garbled code.
The reason for the garbled is that it has not been converted to UTF-8.
The code is as follows:
https://github.com/go-gitea/gitea/blob/master/routers/repo/view.go#L214
The solutions are as follows:
Change lines 211-248 into:
} else {
// Building code view blocks with line number on server side.
var fileContent string
if content, err := templates.ToUTF8WithErr(buf); err != nil {
if err != nil {
log.Error(4, "ToUTF8WithErr: %v", err)
}
fileContent = string(buf)
} else {
fileContent = content
}
if readmeExist {
ctx.Data["IsRenderedHTML"] = true
ctx.Data["FileContent"] = strings.Replace(
gotemplate.HTMLEscapeString(fileContent), "\n", `<br>`, -1,
)
} else {
var output bytes.Buffer
lines := strings.Split(fileContent, "\n")
//Remove blank line at the end of file
if len(lines) > 0 && lines[len(lines)-1] == "" {
lines = lines[:len(lines)-1]
}
for index, line := range lines {
line = gotemplate.HTMLEscapeString(line)
if index != len(lines)-1 {
line += "\n"
}
output.WriteString(fmt.Sprintf(`<li class="L%d" rel="L%d">%s</li>`, index+1, index+1, line))
}
ctx.Data["FileContent"] = gotemplate.HTML(output.String())
output.Reset()
for i := 0; i < len(lines); i++ {
output.WriteString(fmt.Sprintf(`<span id="L%d">%d</span>`, i+1, i+1))
}
ctx.Data["LineNums"] = gotemplate.HTML(output.String())
}
}