Skip to content

Commit 26576f7

Browse files
committed
Improve optimaziation
1 parent aabc327 commit 26576f7

File tree

3 files changed

+36
-42
lines changed

3 files changed

+36
-42
lines changed

modules/charset/charset.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,6 @@ import (
2323
// UTF8BOM is the utf-8 byte-order marker
2424
var UTF8BOM = []byte{'\xef', '\xbb', '\xbf'}
2525

26-
// ToUTF8WithErr converts content to UTF8 encoding
27-
func ToUTF8WithErr(content []byte) (string, error) {
28-
charsetLabel, err := DetectEncoding(content)
29-
if err != nil {
30-
return "", err
31-
} else if charsetLabel == "UTF-8" {
32-
return string(RemoveBOMIfPresent(content)), nil
33-
}
34-
35-
encoding, _ := charset.Lookup(charsetLabel)
36-
if encoding == nil {
37-
return string(content), fmt.Errorf("Unknown encoding: %s", charsetLabel)
38-
}
39-
40-
// If there is an error, we concatenate the nicely decoded part and the
41-
// original left over. This way we won't lose much data.
42-
result, n, err := transform.Bytes(encoding.NewDecoder(), content)
43-
if err != nil {
44-
result = append(result, content[n:]...)
45-
}
46-
47-
result = RemoveBOMIfPresent(result)
48-
49-
return string(result), err
50-
}
51-
5226
// ToUTF8WithFallbackReader detects the encoding of content and coverts to UTF-8 reader if possible
5327
func ToUTF8WithFallbackReader(rd io.Reader) io.Reader {
5428
var buf = make([]byte, 2048)
@@ -76,6 +50,32 @@ func ToUTF8WithFallbackReader(rd io.Reader) io.Reader {
7650
)
7751
}
7852

53+
// ToUTF8WithErr converts content to UTF8 encoding
54+
func ToUTF8WithErr(content []byte) (string, error) {
55+
charsetLabel, err := DetectEncoding(content)
56+
if err != nil {
57+
return "", err
58+
} else if charsetLabel == "UTF-8" {
59+
return string(RemoveBOMIfPresent(content)), nil
60+
}
61+
62+
encoding, _ := charset.Lookup(charsetLabel)
63+
if encoding == nil {
64+
return string(content), fmt.Errorf("Unknown encoding: %s", charsetLabel)
65+
}
66+
67+
// If there is an error, we concatenate the nicely decoded part and the
68+
// original left over. This way we won't lose much data.
69+
result, n, err := transform.Bytes(encoding.NewDecoder(), content)
70+
if err != nil {
71+
result = append(result, content[n:]...)
72+
}
73+
74+
result = RemoveBOMIfPresent(result)
75+
76+
return string(result), err
77+
}
78+
7979
// ToUTF8WithFallback detects the encoding of content and coverts to UTF-8 if possible
8080
func ToUTF8WithFallback(content []byte) []byte {
8181
bs, _ := ioutil.ReadAll(ToUTF8WithFallbackReader(bytes.NewReader(content)))

routers/repo/lfs.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -296,20 +296,13 @@ func LFSFileGet(ctx *context.Context) {
296296
break
297297
}
298298

299-
d, _ := ioutil.ReadAll(dataRc)
300-
buf = charset.ToUTF8WithFallback(append(buf, d...))
299+
buf := charset.ToUTF8WithFallbackReader(io.MultiReader(bytes.NewReader(buf), dataRc))
301300

302301
// Building code view blocks with line number on server side.
303-
var fileContent string
304-
if content, err := charset.ToUTF8WithErr(buf); err != nil {
305-
log.Error("ToUTF8WithErr: %v", err)
306-
fileContent = string(buf)
307-
} else {
308-
fileContent = content
309-
}
302+
fileContent, _ := ioutil.ReadAll(buf)
310303

311304
var output bytes.Buffer
312-
lines := strings.Split(fileContent, "\n")
305+
lines := strings.Split(string(fileContent), "\n")
313306
//Remove blank line at the end of file
314307
if len(lines) > 0 && lines[len(lines)-1] == "" {
315308
lines = lines[:len(lines)-1]

routers/repo/view.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,7 @@ func renderDirectory(ctx *context.Context, treeLink string) {
324324
ctx.Data["IsTextFile"] = true
325325
ctx.Data["FileSize"] = fileSize
326326
} else {
327-
d, _ := ioutil.ReadAll(dataRc)
328-
buf = charset.ToUTF8WithFallback(append(buf, d...))
327+
rd := charset.ToUTF8WithFallbackReader(io.MultiReader(bytes.NewReader(buf), dataRc))
329328

330329
if markupType := markup.Type(readmeFile.name); markupType != "" {
331330
ctx.Data["IsMarkup"] = true
@@ -335,12 +334,14 @@ func renderDirectory(ctx *context.Context, treeLink string) {
335334
Filename: readmeFile.name,
336335
URLPrefix: readmeTreelink,
337336
Metas: ctx.Repo.Repository.ComposeDocumentMetas(),
338-
}, bytes.NewReader(buf), &result)
337+
}, rd, &result)
339338
if err != nil {
340-
ctx.ServerError("Render", err)
341-
return
339+
log.Error("Render failed: %v then fallback", err)
340+
bs, _ := ioutil.ReadAll(rd)
341+
ctx.Data["FileContent"] = string(bs)
342+
} else {
343+
ctx.Data["FileContent"] = result.String()
342344
}
343-
ctx.Data["FileContent"] = result.String()
344345
} else {
345346
ctx.Data["IsRenderedHTML"] = true
346347
ctx.Data["FileContent"] = strings.ReplaceAll(

0 commit comments

Comments
 (0)