From 751997ad34fdd52b9f3956b14395560b059c9ac1 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Mon, 1 Apr 2024 21:11:30 +0800 Subject: [PATCH] Refactor file view & render (#30227) The old code is inconsistent and fragile, and the UI isn't right. --- routers/web/repo/blame.go | 10 +++++++++- routers/web/repo/setting/lfs.go | 17 ++++++++--------- routers/web/repo/view.go | 12 +++++++----- templates/repo/blame.tmpl | 4 ++++ templates/repo/settings/lfs_file.tmpl | 10 ++++------ templates/repo/view_file.tmpl | 16 ++++------------ templates/shared/filetoolarge.tmpl | 4 ++++ 7 files changed, 40 insertions(+), 33 deletions(-) create mode 100644 templates/shared/filetoolarge.tmpl diff --git a/routers/web/repo/blame.go b/routers/web/repo/blame.go index 935e6d78fc..1887e4d95d 100644 --- a/routers/web/repo/blame.go +++ b/routers/web/repo/blame.go @@ -16,6 +16,7 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/highlight" "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/templates" "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/util" @@ -87,9 +88,16 @@ func RefBlame(ctx *context.Context) { ctx.Data["IsBlame"] = true - ctx.Data["FileSize"] = blob.Size() + fileSize := blob.Size() + ctx.Data["FileSize"] = fileSize ctx.Data["FileName"] = blob.Name() + if fileSize >= setting.UI.MaxDisplayFileSize { + ctx.Data["IsFileTooLarge"] = true + ctx.HTML(http.StatusOK, tplRepoHome) + return + } + ctx.Data["NumLines"], err = blob.GetBlobLineCount() ctx.Data["NumLinesSet"] = true diff --git a/routers/web/repo/setting/lfs.go b/routers/web/repo/setting/lfs.go index 32049cf0a4..6dddade066 100644 --- a/routers/web/repo/setting/lfs.go +++ b/routers/web/repo/setting/lfs.go @@ -287,22 +287,19 @@ func LFSFileGet(ctx *context.Context) { st := typesniffer.DetectContentType(buf) ctx.Data["IsTextFile"] = st.IsText() - isRepresentableAsText := st.IsRepresentableAsText() - - fileSize := meta.Size ctx.Data["FileSize"] = meta.Size 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") switch { - case isRepresentableAsText: - if st.IsSvgImage() { - ctx.Data["IsImageFile"] = true - } - - if fileSize >= setting.UI.MaxDisplayFileSize { + case st.IsRepresentableAsText(): + if meta.Size >= setting.UI.MaxDisplayFileSize { ctx.Data["IsFileTooLarge"] = true break } + if st.IsSvgImage() { + ctx.Data["IsImageFile"] = true + } + rd := charset.ToUTF8WithFallbackReader(io.MultiReader(bytes.NewReader(buf), dataRc), charset.ConvertOpts{}) // Building code view blocks with line number on server side. @@ -338,6 +335,8 @@ func LFSFileGet(ctx *context.Context) { ctx.Data["IsAudioFile"] = true case st.IsImage() && (setting.UI.SVG.Enabled || !st.IsSvgImage()): ctx.Data["IsImageFile"] = true + default: + // TODO: the logic is not the same as "renderFile" in "view.go" } ctx.HTML(http.StatusOK, tplSettingsLFSFile) } diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index 93e0f5bcbd..8aa9dbb1be 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -482,17 +482,17 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry) { switch { case isRepresentableAsText: + if fInfo.fileSize >= setting.UI.MaxDisplayFileSize { + ctx.Data["IsFileTooLarge"] = true + break + } + if fInfo.st.IsSvgImage() { ctx.Data["IsImageFile"] = true ctx.Data["CanCopyContent"] = true ctx.Data["HasSourceRenderedToggle"] = true } - if fInfo.fileSize >= setting.UI.MaxDisplayFileSize { - ctx.Data["IsFileTooLarge"] = true - break - } - rd := charset.ToUTF8WithFallbackReader(io.MultiReader(bytes.NewReader(buf), dataRc), charset.ConvertOpts{}) shouldRenderSource := ctx.FormString("display") == "source" @@ -606,6 +606,8 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry) { break } + // TODO: this logic seems strange, it duplicates with "isRepresentableAsText=true", it is not the same as "LFSFileGet" in "lfs.go" + // maybe for this case, the file is a binary file, and shouldn't be rendered? if markupType := markup.Type(blob.Name()); markupType != "" { rd := io.MultiReader(bytes.NewReader(buf), dataRc) ctx.Data["IsMarkup"] = true diff --git a/templates/repo/blame.tmpl b/templates/repo/blame.tmpl index 1a148a2d1c..30d1a3d78d 100644 --- a/templates/repo/blame.tmpl +++ b/templates/repo/blame.tmpl @@ -30,6 +30,9 @@
+ {{if .IsFileTooLarge}} + {{template "shared/filetoolarge" dict "RawFileLink" .RawFileLink}} + {{else}} {{range $row := .BlameRows}} @@ -75,6 +78,7 @@ {{end}}
+ {{end}}{{/* end if .IsFileTooLarge */}}
{{if $.Permission.CanRead $.UnitTypeIssues}} {{ctx.Locale.Tr "repo.issues.context.reference_issue"}} diff --git a/templates/repo/settings/lfs_file.tmpl b/templates/repo/settings/lfs_file.tmpl index 43afba96c3..cb65236f23 100644 --- a/templates/repo/settings/lfs_file.tmpl +++ b/templates/repo/settings/lfs_file.tmpl @@ -14,7 +14,9 @@
{{template "repo/unicode_escape_prompt" dict "EscapeStatus" .EscapeStatus "root" $}}
- {{if .IsMarkup}} + {{if .IsFileTooLarge}} + {{template "shared/filetoolarge" dict "RawFileLink" .RawFileLink}} + {{else if .IsMarkup}} {{if .FileContent}}{{.FileContent | SafeHTML}}{{end}} {{else if .IsPlainText}}
{{if .FileContent}}{{.FileContent | SafeHTML}}{{end}}
@@ -33,19 +35,15 @@ {{else if .IsPDFFile}}
{{else}} - {{ctx.Locale.Tr "repo.file_view_raw"}} + {{ctx.Locale.Tr "repo.file_view_raw"}} {{end}}
{{else if .FileSize}} - {{if .IsFileTooLarge}} - - {{else}} - {{end}}
{{ctx.Locale.Tr "repo.file_too_large"}}{{.LineNums}}
    {{.FileContent}}
diff --git a/templates/repo/view_file.tmpl b/templates/repo/view_file.tmpl index b7c1b9eeae..9c5bd9094d 100644 --- a/templates/repo/view_file.tmpl +++ b/templates/repo/view_file.tmpl @@ -89,7 +89,9 @@ {{template "repo/unicode_escape_prompt" dict "EscapeStatus" .EscapeStatus "root" $}} {{end}}
- {{if .IsMarkup}} + {{if .IsFileTooLarge}} + {{template "shared/filetoolarge" dict "RawFileLink" .RawFileLink}} + {{else if .IsMarkup}} {{if .FileContent}}{{.FileContent}}{{end}} {{else if .IsPlainText}}
{{if .FileContent}}{{.FileContent}}{{end}}
@@ -108,19 +110,10 @@ {{else if .IsPDFFile}}
{{else}} - {{ctx.Locale.Tr "repo.file_view_raw"}} + {{ctx.Locale.Tr "repo.file_view_raw"}} {{end}}
{{else if .FileSize}} - {{if .IsFileTooLarge}} - - - - - - -
{{ctx.Locale.Tr "repo.file_too_large"}}
- {{else}} {{range $idx, $code := .FileContent}} @@ -142,7 +135,6 @@ {{ctx.Locale.Tr "repo.view_git_blame"}}{{ctx.Locale.Tr "repo.file_copy_permalink"}} - {{end}} {{end}} diff --git a/templates/shared/filetoolarge.tmpl b/templates/shared/filetoolarge.tmpl new file mode 100644 index 0000000000..8842fb1b91 --- /dev/null +++ b/templates/shared/filetoolarge.tmpl @@ -0,0 +1,4 @@ +
+ {{ctx.Locale.Tr "repo.file_too_large"}} + {{if .RawFileLink}}{{ctx.Locale.Tr "repo.file_view_raw"}}{{end}} +