[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
This commit is contained in:
Kim Carlbäcker 2016-07-12 00:21:26 +02:00 committed by 无闻
parent de10387f41
commit f4ab50501e
6 changed files with 730 additions and 934 deletions

View file

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

View file

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

File diff suppressed because one or more lines are too long

View file

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

View file

@ -19,6 +19,7 @@ import (
"github.com/gogits/gogs/modules/context" "github.com/gogits/gogs/modules/context"
"github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/markdown" "github.com/gogits/gogs/modules/markdown"
"github.com/gogits/gogs/modules/setting"
"github.com/gogits/gogs/modules/template" "github.com/gogits/gogs/modules/template"
"github.com/gogits/gogs/modules/template/highlight" "github.com/gogits/gogs/modules/template/highlight"
) )
@ -104,6 +105,10 @@ func Home(ctx *context.Context) {
case isImageFile: case isImageFile:
ctx.Data["IsImageFile"] = true ctx.Data["IsImageFile"] = true
case isTextFile: case isTextFile:
if blob.Size() >= setting.MaxDisplayFileSize {
ctx.Data["IsFileTooLarge"] = true
} else {
ctx.Data["IsFileTooLarge"] = false
d, _ := ioutil.ReadAll(dataRc) d, _ := ioutil.ReadAll(dataRc)
buf = append(buf, d...) buf = append(buf, d...)
readmeExist := markdown.IsMarkdownFile(blob.Name()) || markdown.IsReadmeFile(blob.Name()) readmeExist := markdown.IsMarkdownFile(blob.Name()) || markdown.IsReadmeFile(blob.Name())
@ -122,6 +127,7 @@ func Home(ctx *context.Context) {
} }
} }
} }
}
} else { } else {
// Directory and file list. // Directory and file list.
tree, err := ctx.Repo.Commit.SubTree(treename) tree, err := ctx.Repo.Commit.SubTree(treename)

View file

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