Skip to content

Commit 751997a

Browse files
authored
Refactor file view & render (#30227)
The old code is inconsistent and fragile, and the UI isn't right.
1 parent a008486 commit 751997a

File tree

7 files changed

+40
-33
lines changed

7 files changed

+40
-33
lines changed

routers/web/repo/blame.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"code.gitea.io/gitea/modules/git"
1717
"code.gitea.io/gitea/modules/highlight"
1818
"code.gitea.io/gitea/modules/log"
19+
"code.gitea.io/gitea/modules/setting"
1920
"code.gitea.io/gitea/modules/templates"
2021
"code.gitea.io/gitea/modules/timeutil"
2122
"code.gitea.io/gitea/modules/util"
@@ -87,9 +88,16 @@ func RefBlame(ctx *context.Context) {
8788

8889
ctx.Data["IsBlame"] = true
8990

90-
ctx.Data["FileSize"] = blob.Size()
91+
fileSize := blob.Size()
92+
ctx.Data["FileSize"] = fileSize
9193
ctx.Data["FileName"] = blob.Name()
9294

95+
if fileSize >= setting.UI.MaxDisplayFileSize {
96+
ctx.Data["IsFileTooLarge"] = true
97+
ctx.HTML(http.StatusOK, tplRepoHome)
98+
return
99+
}
100+
93101
ctx.Data["NumLines"], err = blob.GetBlobLineCount()
94102
ctx.Data["NumLinesSet"] = true
95103

routers/web/repo/setting/lfs.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -287,22 +287,19 @@ func LFSFileGet(ctx *context.Context) {
287287

288288
st := typesniffer.DetectContentType(buf)
289289
ctx.Data["IsTextFile"] = st.IsText()
290-
isRepresentableAsText := st.IsRepresentableAsText()
291-
292-
fileSize := meta.Size
293290
ctx.Data["FileSize"] = meta.Size
294291
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")
295292
switch {
296-
case isRepresentableAsText:
297-
if st.IsSvgImage() {
298-
ctx.Data["IsImageFile"] = true
299-
}
300-
301-
if fileSize >= setting.UI.MaxDisplayFileSize {
293+
case st.IsRepresentableAsText():
294+
if meta.Size >= setting.UI.MaxDisplayFileSize {
302295
ctx.Data["IsFileTooLarge"] = true
303296
break
304297
}
305298

299+
if st.IsSvgImage() {
300+
ctx.Data["IsImageFile"] = true
301+
}
302+
306303
rd := charset.ToUTF8WithFallbackReader(io.MultiReader(bytes.NewReader(buf), dataRc), charset.ConvertOpts{})
307304

308305
// Building code view blocks with line number on server side.
@@ -338,6 +335,8 @@ func LFSFileGet(ctx *context.Context) {
338335
ctx.Data["IsAudioFile"] = true
339336
case st.IsImage() && (setting.UI.SVG.Enabled || !st.IsSvgImage()):
340337
ctx.Data["IsImageFile"] = true
338+
default:
339+
// TODO: the logic is not the same as "renderFile" in "view.go"
341340
}
342341
ctx.HTML(http.StatusOK, tplSettingsLFSFile)
343342
}

routers/web/repo/view.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -482,17 +482,17 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry) {
482482

483483
switch {
484484
case isRepresentableAsText:
485+
if fInfo.fileSize >= setting.UI.MaxDisplayFileSize {
486+
ctx.Data["IsFileTooLarge"] = true
487+
break
488+
}
489+
485490
if fInfo.st.IsSvgImage() {
486491
ctx.Data["IsImageFile"] = true
487492
ctx.Data["CanCopyContent"] = true
488493
ctx.Data["HasSourceRenderedToggle"] = true
489494
}
490495

491-
if fInfo.fileSize >= setting.UI.MaxDisplayFileSize {
492-
ctx.Data["IsFileTooLarge"] = true
493-
break
494-
}
495-
496496
rd := charset.ToUTF8WithFallbackReader(io.MultiReader(bytes.NewReader(buf), dataRc), charset.ConvertOpts{})
497497

498498
shouldRenderSource := ctx.FormString("display") == "source"
@@ -606,6 +606,8 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry) {
606606
break
607607
}
608608

609+
// TODO: this logic seems strange, it duplicates with "isRepresentableAsText=true", it is not the same as "LFSFileGet" in "lfs.go"
610+
// maybe for this case, the file is a binary file, and shouldn't be rendered?
609611
if markupType := markup.Type(blob.Name()); markupType != "" {
610612
rd := io.MultiReader(bytes.NewReader(buf), dataRc)
611613
ctx.Data["IsMarkup"] = true

templates/repo/blame.tmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
</h4>
3131
<div class="ui attached table unstackable segment">
3232
<div class="file-view code-view unicode-escaped">
33+
{{if .IsFileTooLarge}}
34+
{{template "shared/filetoolarge" dict "RawFileLink" .RawFileLink}}
35+
{{else}}
3336
<table>
3437
<tbody>
3538
{{range $row := .BlameRows}}
@@ -75,6 +78,7 @@
7578
{{end}}
7679
</tbody>
7780
</table>
81+
{{end}}{{/* end if .IsFileTooLarge */}}
7882
<div class="code-line-menu tippy-target">
7983
{{if $.Permission.CanRead $.UnitTypeIssues}}
8084
<a class="item ref-in-new-issue" role="menuitem" data-url-issue-new="{{.RepoLink}}/issues/new" data-url-param-body-link="{{.Repository.Link}}/src/commit/{{PathEscape .CommitID}}/{{PathEscapeSegments .TreePath}}{{if $.HasSourceRenderedToggle}}?display=source{{end}}" rel="nofollow noindex">{{ctx.Locale.Tr "repo.issues.context.reference_issue"}}</a>

templates/repo/settings/lfs_file.tmpl

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
<div class="ui attached table unstackable segment">
1515
{{template "repo/unicode_escape_prompt" dict "EscapeStatus" .EscapeStatus "root" $}}
1616
<div class="file-view{{if .IsMarkup}} markup {{.MarkupType}}{{else if .IsPlainText}} plain-text{{else if .IsTextFile}} code-view{{end}}">
17-
{{if .IsMarkup}}
17+
{{if .IsFileTooLarge}}
18+
{{template "shared/filetoolarge" dict "RawFileLink" .RawFileLink}}
19+
{{else if .IsMarkup}}
1820
{{if .FileContent}}{{.FileContent | SafeHTML}}{{end}}
1921
{{else if .IsPlainText}}
2022
<pre>{{if .FileContent}}{{.FileContent | SafeHTML}}{{end}}</pre>
@@ -33,19 +35,15 @@
3335
{{else if .IsPDFFile}}
3436
<div class="pdf-content is-loading" data-src="{{$.RawFileLink}}" data-fallback-button-text="{{ctx.Locale.Tr "diff.view_file"}}"></div>
3537
{{else}}
36-
<a href="{{$.RawFileLink}}" rel="nofollow">{{ctx.Locale.Tr "repo.file_view_raw"}}</a>
38+
<a href="{{$.RawFileLink}}" rel="nofollow" class="tw-p-4">{{ctx.Locale.Tr "repo.file_view_raw"}}</a>
3739
{{end}}
3840
</div>
3941
{{else if .FileSize}}
4042
<table>
4143
<tbody>
4244
<tr>
43-
{{if .IsFileTooLarge}}
44-
<td><strong>{{ctx.Locale.Tr "repo.file_too_large"}}</strong></td>
45-
{{else}}
4645
<td class="lines-num">{{.LineNums}}</td>
4746
<td class="lines-code"><pre><code class="{{.HighlightClass}}"><ol>{{.FileContent}}</ol></code></pre></td>
48-
{{end}}
4947
</tr>
5048
</tbody>
5149
</table>

templates/repo/view_file.tmpl

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@
8989
{{template "repo/unicode_escape_prompt" dict "EscapeStatus" .EscapeStatus "root" $}}
9090
{{end}}
9191
<div class="file-view{{if .IsMarkup}} markup {{.MarkupType}}{{else if .IsPlainText}} plain-text{{else if .IsTextSource}} code-view{{end}}">
92-
{{if .IsMarkup}}
92+
{{if .IsFileTooLarge}}
93+
{{template "shared/filetoolarge" dict "RawFileLink" .RawFileLink}}
94+
{{else if .IsMarkup}}
9395
{{if .FileContent}}{{.FileContent}}{{end}}
9496
{{else if .IsPlainText}}
9597
<pre>{{if .FileContent}}{{.FileContent}}{{end}}</pre>
@@ -108,19 +110,10 @@
108110
{{else if .IsPDFFile}}
109111
<div class="pdf-content is-loading" data-src="{{$.RawFileLink}}" data-fallback-button-text="{{ctx.Locale.Tr "repo.diff.view_file"}}"></div>
110112
{{else}}
111-
<a href="{{$.RawFileLink}}" rel="nofollow">{{ctx.Locale.Tr "repo.file_view_raw"}}</a>
113+
<a href="{{$.RawFileLink}}" rel="nofollow" class="tw-p-4">{{ctx.Locale.Tr "repo.file_view_raw"}}</a>
112114
{{end}}
113115
</div>
114116
{{else if .FileSize}}
115-
{{if .IsFileTooLarge}}
116-
<table>
117-
<tbody>
118-
<tr>
119-
<td><strong>{{ctx.Locale.Tr "repo.file_too_large"}}</strong></td>
120-
</tr>
121-
</tbody>
122-
</table>
123-
{{else}}
124117
<table>
125118
<tbody>
126119
{{range $idx, $code := .FileContent}}
@@ -142,7 +135,6 @@
142135
<a class="item view_git_blame" role="menuitem" href="{{.Repository.Link}}/blame/commit/{{PathEscape .CommitID}}/{{PathEscapeSegments .TreePath}}">{{ctx.Locale.Tr "repo.view_git_blame"}}</a>
143136
<a class="item copy-line-permalink" role="menuitem" data-url="{{.Repository.Link}}/src/commit/{{PathEscape .CommitID}}/{{PathEscapeSegments .TreePath}}{{if $.HasSourceRenderedToggle}}?display=source{{end}}">{{ctx.Locale.Tr "repo.file_copy_permalink"}}</a>
144137
</div>
145-
{{end}}
146138
{{end}}
147139
</div>
148140
</div>

templates/shared/filetoolarge.tmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<div class="tw-p-4">
2+
{{ctx.Locale.Tr "repo.file_too_large"}}
3+
{{if .RawFileLink}}<a href="{{.RawFileLink}}" rel="nofollow">{{ctx.Locale.Tr "repo.file_view_raw"}}</a>{{end}}
4+
</div>

0 commit comments

Comments
 (0)