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 8 years ago
parent
commit
f4ab50501e

+ 2 - 0
conf/app.ini

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

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

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

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


+ 2 - 0
modules/setting/setting.go

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

+ 18 - 12
routers/repo/view.go

@@ -19,6 +19,7 @@ import (
 	"github.com/gogits/gogs/modules/context"
 	"github.com/gogits/gogs/modules/log"
 	"github.com/gogits/gogs/modules/markdown"
+	"github.com/gogits/gogs/modules/setting"
 	"github.com/gogits/gogs/modules/template"
 	"github.com/gogits/gogs/modules/template/highlight"
 )
@@ -104,20 +105,25 @@ func Home(ctx *context.Context) {
 			case isImageFile:
 				ctx.Data["IsImageFile"] = true
 			case isTextFile:
-				d, _ := ioutil.ReadAll(dataRc)
-				buf = append(buf, d...)
-				readmeExist := markdown.IsMarkdownFile(blob.Name()) || markdown.IsReadmeFile(blob.Name())
-				ctx.Data["ReadmeExist"] = readmeExist
-				if readmeExist {
-					ctx.Data["FileContent"] = string(markdown.Render(buf, path.Dir(treeLink), ctx.Repo.Repository.ComposeMetas()))
+				if blob.Size() >= setting.MaxDisplayFileSize {
+					ctx.Data["IsFileTooLarge"] = true
 				} else {
-					if err, content := template.ToUtf8WithErr(buf); err != nil {
-						if err != nil {
-							log.Error(4, "Convert content encoding: %s", err)
-						}
-						ctx.Data["FileContent"] = string(buf)
+					ctx.Data["IsFileTooLarge"] = false
+					d, _ := ioutil.ReadAll(dataRc)
+					buf = append(buf, d...)
+					readmeExist := markdown.IsMarkdownFile(blob.Name()) || markdown.IsReadmeFile(blob.Name())
+					ctx.Data["ReadmeExist"] = readmeExist
+					if readmeExist {
+						ctx.Data["FileContent"] = string(markdown.Render(buf, path.Dir(treeLink), ctx.Repo.Repository.ComposeMetas()))
 					} else {
-						ctx.Data["FileContent"] = content
+						if err, content := template.ToUtf8WithErr(buf); err != nil {
+							if err != nil {
+								log.Error(4, "Convert content encoding: %s", err)
+							}
+							ctx.Data["FileContent"] = string(buf)
+						} else {
+							ctx.Data["FileContent"] = content
+						}
 					}
 				}
 			}

+ 4 - 0
templates/repo/view_file.tmpl

@@ -41,8 +41,12 @@
 				<table>
 					<tbody>
 						<tr>
+						{{if .IsFileTooLarge}}
+							<td><strong>{{.i18n.Tr "repo.file_too_large"}}</strong></td>
+						{{else}}
 							<td class="lines-num"></td>
 							<td class="lines-code"><pre><code class="{{.HighlightClass}}"><ol class="linenums">{{.FileContent}}</ol></code></pre></td>
+						{{end}}
 						</tr>
 					</tbody>
 				</table>

Some files were not shown because too many files changed in this diff