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

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>