Skip to content

Commit cade640

Browse files
committed
fix
1 parent d217dd9 commit cade640

File tree

18 files changed

+417
-309
lines changed

18 files changed

+417
-309
lines changed

modules/markup/console/console.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ package console
66
import (
77
"bytes"
88
"io"
9-
"path"
109

1110
"code.gitea.io/gitea/modules/markup"
1211
"code.gitea.io/gitea/modules/setting"
1312

1413
trend "github.com/buildkite/terminal-to-html/v3"
15-
"github.com/go-enry/go-enry/v2"
1614
)
1715

1816
func init() {
@@ -41,14 +39,19 @@ func (Renderer) SanitizerRules() []setting.MarkupSanitizerRule {
4139

4240
// CanRender implements markup.RendererContentDetector
4341
func (Renderer) CanRender(filename string, input io.Reader) bool {
44-
buf, err := io.ReadAll(input)
45-
if err != nil {
46-
return false
47-
}
48-
if enry.GetLanguage(path.Base(filename), buf) != enry.OtherLanguage {
49-
return false
50-
}
51-
return bytes.ContainsRune(buf, '\x1b')
42+
/*
43+
buf, err := io.ReadAll(input)
44+
if err != nil {
45+
return false
46+
}
47+
if enry.GetLanguage(path.Base(filename), buf) != enry.OtherLanguage {
48+
return false
49+
}
50+
return bytes.ContainsRune(buf, '\x1b')
51+
*/
52+
// FIXME: this check is not right, it is too broad and will match any file containing ANSI escape codes.
53+
// So only use the defined "Extensions" to avoid conflicts with other renderers.
54+
return false
5255
}
5356

5457
// Render renders terminal colors to HTML with all specific handling stuff.

modules/typesniffer/typesniffer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ var (
3232
svgTagInXMLRegex = regexp.MustCompile(`(?si)\A<\?xml\b.*?\?>\s*(?:(<!DOCTYPE\s+svg([\s:]+.*?>|>))\s*)*<svg\b`)
3333
)
3434

35-
// SniffedType contains information about a blobs type.
35+
// SniffedType contains information about a blob's type.
3636
type SniffedType struct {
3737
contentType string
3838
}
3939

40-
// IsText etects if content format is plain text.
40+
// IsText detects if the content format is plain text.
4141
func (ct SniffedType) IsText() bool {
4242
return strings.Contains(ct.contentType, "text/")
4343
}

routers/web/repo/editor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ func editFileOpenExisting(ctx *context.Context) (prefetch []byte, dataRc io.Read
244244
return nil, nil, nil
245245
}
246246

247-
if fInfo.isLFSFile {
247+
if fInfo.isLFSFile() {
248248
lfsLock, err := git_model.GetTreePathLock(ctx, ctx.Repo.Repository.ID, ctx.Repo.TreePath)
249249
if err != nil {
250250
_ = dataRc.Close()
@@ -298,7 +298,7 @@ func EditFile(ctx *context.Context) {
298298
ctx.Data["FileSize"] = fInfo.fileSize
299299

300300
// Only some file types are editable online as text.
301-
if fInfo.isLFSFile {
301+
if fInfo.isLFSFile() {
302302
ctx.Data["NotEditableReason"] = ctx.Tr("repo.editor.cannot_edit_lfs_files")
303303
} else if !fInfo.st.IsRepresentableAsText() {
304304
ctx.Data["NotEditableReason"] = ctx.Tr("repo.editor.cannot_edit_non_text_files")

routers/web/repo/setting/lfs.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,10 @@ func LFSFileGet(ctx *context.Context) {
267267
buf = buf[:n]
268268

269269
st := typesniffer.DetectContentType(buf)
270+
// FIXME: there is no IsPlainText set, but template uses it
270271
ctx.Data["IsTextFile"] = st.IsText()
271272
ctx.Data["FileSize"] = meta.Size
273+
// FIXME: the last field is the URL-base64-encoded filename, it should not be "direct"
272274
ctx.Data["RawFileLink"] = fmt.Sprintf("%s%s/%s.git/info/lfs/objects/%s/%s", setting.AppURL, url.PathEscape(ctx.Repo.Repository.OwnerName), url.PathEscape(ctx.Repo.Repository.Name), url.PathEscape(meta.Oid), "direct")
273275
switch {
274276
case st.IsRepresentableAsText():

routers/web/repo/view.go

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -59,60 +59,63 @@ const (
5959
)
6060

6161
type fileInfo struct {
62-
isTextFile bool
63-
isLFSFile bool
64-
fileSize int64
65-
lfsMeta *lfs.Pointer
66-
st typesniffer.SniffedType
62+
fileSize int64
63+
lfsMeta *lfs.Pointer
64+
st typesniffer.SniffedType
6765
}
6866

69-
func getFileReader(ctx gocontext.Context, repoID int64, blob *git.Blob) ([]byte, io.ReadCloser, *fileInfo, error) {
70-
dataRc, err := blob.DataAsync()
67+
func (fi *fileInfo) isLFSFile() bool {
68+
return fi.lfsMeta != nil && fi.lfsMeta.Oid != ""
69+
}
70+
71+
func getFileReader(ctx gocontext.Context, repoID int64, blob *git.Blob) (buf []byte, dataRc io.ReadCloser, fi *fileInfo, err error) {
72+
dataRc, err = blob.DataAsync()
7173
if err != nil {
7274
return nil, nil, nil, err
7375
}
7476

75-
buf := make([]byte, 1024)
77+
const prefetchSize = lfs.MetaFileMaxSize
78+
79+
buf = make([]byte, prefetchSize)
7680
n, _ := util.ReadAtMost(dataRc, buf)
7781
buf = buf[:n]
7882

79-
st := typesniffer.DetectContentType(buf)
80-
isTextFile := st.IsText()
83+
fi = &fileInfo{fileSize: blob.Size(), st: typesniffer.DetectContentType(buf)}
8184

8285
// FIXME: what happens when README file is an image?
83-
if !isTextFile || !setting.LFS.StartServer {
84-
return buf, dataRc, &fileInfo{isTextFile, false, blob.Size(), nil, st}, nil
86+
if !fi.st.IsText() || !setting.LFS.StartServer {
87+
return buf, dataRc, fi, nil
8588
}
8689

8790
pointer, _ := lfs.ReadPointerFromBuffer(buf)
88-
if !pointer.IsValid() { // fallback to plain file
89-
return buf, dataRc, &fileInfo{isTextFile, false, blob.Size(), nil, st}, nil
91+
if !pointer.IsValid() { // fallback to a plain file
92+
return buf, dataRc, fi, nil
9093
}
9194

9295
meta, err := git_model.GetLFSMetaObjectByOid(ctx, repoID, pointer.Oid)
93-
if err != nil { // fallback to plain file
96+
if err != nil { // fallback to a plain file
9497
log.Warn("Unable to access LFS pointer %s in repo %d: %v", pointer.Oid, repoID, err)
95-
return buf, dataRc, &fileInfo{isTextFile, false, blob.Size(), nil, st}, nil
98+
return buf, dataRc, fi, nil
9699
}
97100

98-
dataRc.Close()
99-
101+
// close the old dataRc and open the real LFS target
102+
_ = dataRc.Close()
100103
dataRc, err = lfs.ReadMetaObject(pointer)
101104
if err != nil {
102105
return nil, nil, nil, err
103106
}
104107

105-
buf = make([]byte, 1024)
108+
buf = make([]byte, prefetchSize)
106109
n, err = util.ReadAtMost(dataRc, buf)
107110
if err != nil {
108-
dataRc.Close()
109-
return nil, nil, nil, err
111+
_ = dataRc.Close()
112+
return nil, nil, fi, err
110113
}
111114
buf = buf[:n]
112-
113-
st = typesniffer.DetectContentType(buf)
114-
115-
return buf, dataRc, &fileInfo{st.IsText(), true, meta.Size, &meta.Pointer, st}, nil
115+
fi.st = typesniffer.DetectContentType(buf)
116+
fi.fileSize = blob.Size()
117+
fi.lfsMeta = &meta.Pointer
118+
return buf, dataRc, fi, nil
116119
}
117120

118121
func loadLatestCommitData(ctx *context.Context, latestCommit *git.Commit) bool {

0 commit comments

Comments
 (0)