Browse Source

[Fix] Don't display way too large files #1513 (#3253)

* Add MaxDisplayFileSize setting

* Don't show files that are too large

* Localized FileTooLarge

* Change IsFileTooBig => IsFileTooLarge
Kim Carlbäcker 3 years ago
parent
commit
f4ab50501e

+ 2 - 0
conf/app.ini

@@ -31,6 +31,8 @@ FEED_MAX_COMMIT_NUM = 5
31 31
 ; An invalid color like "none" or "disable" will have the default style
32 32
 ; More info: https://developers.google.com/web/updates/2014/11/Support-for-theme-color-in-Chrome-39-for-Android
33 33
 THEME_COLOR_META_TAG = `#ff5343`
34
+; Max size of files to be displayed (defaults is 8MiB)
35
+MAX_DISPLAY_FILE_SIZE = 8388608
34 36
 
35 37
 [ui.admin]
36 38
 ; Number of users that are showed in one page

+ 1 - 0
conf/locale/locale_en-US.ini

@@ -409,6 +409,7 @@ file_raw = Raw
409 409
 file_history = History
410 410
 file_view_raw = View Raw
411 411
 file_permalink = Permalink
412
+file_too_large = This file is too large to be shown
412 413
 
413 414
 commits.commits = Commits
414 415
 commits.search = Search commits

File diff suppressed because it is too large
+ 703 - 922
modules/bindata/bindata.go


+ 2 - 0
modules/setting/setting.go

@@ -123,6 +123,7 @@ var (
123 123
 	AdminNoticePagingNum int
124 124
 	AdminOrgPagingNum    int
125 125
 	ThemeColorMetaTag    string
126
+	MaxDisplayFileSize   int64
126 127
 
127 128
 	// Markdown sttings
128 129
 	Markdown struct {
@@ -441,6 +442,7 @@ func NewContext() {
441 442
 	ExplorePagingNum = sec.Key("EXPLORE_PAGING_NUM").MustInt(20)
442 443
 	IssuePagingNum = sec.Key("ISSUE_PAGING_NUM").MustInt(10)
443 444
 	FeedMaxCommitNum = sec.Key("FEED_MAX_COMMIT_NUM").MustInt(5)
445
+	MaxDisplayFileSize = sec.Key("MAX_DISPLAY_FILE_SIZE").MustInt64(8388608)
444 446
 
445 447
 	sec = Cfg.Section("ui.admin")
446 448
 	AdminUserPagingNum = sec.Key("USER_PAGING_NUM").MustInt(50)

+ 18 - 12
routers/repo/view.go

@@ -19,6 +19,7 @@ import (
19 19
 	"github.com/gogits/gogs/modules/context"
20 20
 	"github.com/gogits/gogs/modules/log"
21 21
 	"github.com/gogits/gogs/modules/markdown"
22
+	"github.com/gogits/gogs/modules/setting"
22 23
 	"github.com/gogits/gogs/modules/template"
23 24
 	"github.com/gogits/gogs/modules/template/highlight"
24 25
 )
@@ -104,20 +105,25 @@ func Home(ctx *context.Context) {
104 105
 			case isImageFile:
105 106
 				ctx.Data["IsImageFile"] = true
106 107
 			case isTextFile:
107
-				d, _ := ioutil.ReadAll(dataRc)
108
-				buf = append(buf, d...)
109
-				readmeExist := markdown.IsMarkdownFile(blob.Name()) || markdown.IsReadmeFile(blob.Name())
110
-				ctx.Data["ReadmeExist"] = readmeExist
111
-				if readmeExist {
112
-					ctx.Data["FileContent"] = string(markdown.Render(buf, path.Dir(treeLink), ctx.Repo.Repository.ComposeMetas()))
108
+				if blob.Size() >= setting.MaxDisplayFileSize {
109
+					ctx.Data["IsFileTooLarge"] = true
113 110
 				} else {
114
-					if err, content := template.ToUtf8WithErr(buf); err != nil {
115
-						if err != nil {
116
-							log.Error(4, "Convert content encoding: %s", err)
117
-						}
118
-						ctx.Data["FileContent"] = string(buf)
111
+					ctx.Data["IsFileTooLarge"] = false
112
+					d, _ := ioutil.ReadAll(dataRc)
113
+					buf = append(buf, d...)
114
+					readmeExist := markdown.IsMarkdownFile(blob.Name()) || markdown.IsReadmeFile(blob.Name())
115
+					ctx.Data["ReadmeExist"] = readmeExist
116
+					if readmeExist {
117
+						ctx.Data["FileContent"] = string(markdown.Render(buf, path.Dir(treeLink), ctx.Repo.Repository.ComposeMetas()))
119 118
 					} else {
120
-						ctx.Data["FileContent"] = content
119
+						if err, content := template.ToUtf8WithErr(buf); err != nil {
120
+							if err != nil {
121
+								log.Error(4, "Convert content encoding: %s", err)
122
+							}
123
+							ctx.Data["FileContent"] = string(buf)
124
+						} else {
125
+							ctx.Data["FileContent"] = content
126
+						}
121 127
 					}
122 128
 				}
123 129
 			}

+ 4 - 0
templates/repo/view_file.tmpl

@@ -41,8 +41,12 @@
41 41
 				<table>
42 42
 					<tbody>
43 43
 						<tr>
44
+						{{if .IsFileTooLarge}}
45
+							<td><strong>{{.i18n.Tr "repo.file_too_large"}}</strong></td>
46
+						{{else}}
44 47
 							<td class="lines-num"></td>
45 48
 							<td class="lines-code"><pre><code class="{{.HighlightClass}}"><ol class="linenums">{{.FileContent}}</ol></code></pre></td>
49
+						{{end}}
46 50
 						</tr>
47 51
 					</tbody>
48 52
 				</table>