diff options
author | FuXiaoHei <fuxiaohei@hexiaz.com> | 2014-03-25 00:02:37 +0800 |
---|---|---|
committer | FuXiaoHei <fuxiaohei@hexiaz.com> | 2014-03-25 00:02:37 +0800 |
commit | 8909935a1049dea3783021f0460118ad838b1dfd (patch) | |
tree | 00d23ec41174cda54d5bf2de1175a9d635b75f9a | |
parent | 62b05c1e228f1a570a97e6957c45e018262f812f (diff) | |
parent | 6302f5462890b455cb306fc3925c71850a6e1dfb (diff) | |
download | gitea-8909935a1049dea3783021f0460118ad838b1dfd.tar.gz gitea-8909935a1049dea3783021f0460118ad838b1dfd.zip |
Merge branch 'master' of https://github.com/gogits/gogs
-rw-r--r-- | README.md | 1 | ||||
-rw-r--r-- | README_ZH.md | 1 | ||||
-rw-r--r-- | models/repo.go | 2 | ||||
-rw-r--r-- | modules/base/conf.go | 4 | ||||
-rw-r--r-- | modules/base/markdown.go | 9 | ||||
-rw-r--r-- | routers/repo/repo.go | 74 | ||||
-rw-r--r-- | serve.go | 2 | ||||
-rw-r--r-- | templates/repo/single_file.tmpl | 16 | ||||
-rw-r--r-- | web.go | 4 |
9 files changed, 87 insertions, 26 deletions
@@ -53,6 +53,7 @@ There are two ways to install Gogs: - Mail Service, modules design is inspired by [WeTalk](https://github.com/beego/wetalk). - System Monitor Status is inspired by [GoBlog](https://github.com/fuxiaohei/goblog). - Usage and modification from [beego](http://beego.me) modules. +- Thanks [gobuild.io](http://gobuild.io) for providing binary compile and download service. ## Contributors diff --git a/README_ZH.md b/README_ZH.md index 838e30a0ef..440f952f86 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -49,6 +49,7 @@ Gogs 完全使用 Go 语言来实现对 Git 数据的操作,实现 **零** 依 - 基于 [GoBlog](https://github.com/fuxiaohei/goblog) 修改的系统监视状态。 - [beego](http://beego.me) 模块的使用与修改。 - [martini](http://martini.codegangsta.io/) 的路由与中间件机制。 +- 感谢 [gobuild.io](http://gobuild.io) 提供二进制编译与下载服务。 ## 贡献成员 diff --git a/models/repo.go b/models/repo.go index 6cbfaf1059..d5f9be72ac 100644 --- a/models/repo.go +++ b/models/repo.go @@ -109,7 +109,7 @@ func IsRepositoryExist(user *User, repoName string) (bool, error) { var ( // Define as all lower case!! - illegalPatterns = []string{"[.][Gg][Ii][Tt]", "user", "help", "stars", "issues", "pulls", "commits", "admin", "repo", "template", "admin"} + illegalPatterns = []string{"[.][Gg][Ii][Tt]", "raw", "user", "help", "stars", "issues", "pulls", "commits", "admin", "repo", "template", "admin"} ) // IsLegalName returns false if name contains illegal characters. diff --git a/modules/base/conf.go b/modules/base/conf.go index b4e0de97bd..d3122a5339 100644 --- a/modules/base/conf.go +++ b/modules/base/conf.go @@ -100,7 +100,7 @@ func newService() { Service.EnableCacheAvatar = Cfg.MustBool("service", "ENABLE_CACHE_AVATAR", false) } -func NewLogService() { +func newLogService() { // Get and check log mode. LogMode = Cfg.MustValue("log", "MODE", "console") modeSec := "log." + LogMode @@ -296,7 +296,7 @@ func NewConfigContext() { func NewServices() { newService() - NewLogService() + newLogService() newCacheService() newSessionService() newMailService() diff --git a/modules/base/markdown.go b/modules/base/markdown.go index 05ce0c833c..c722f04b2e 100644 --- a/modules/base/markdown.go +++ b/modules/base/markdown.go @@ -6,6 +6,7 @@ package base import ( "bytes" + "net/http" "path" "path/filepath" "strings" @@ -42,6 +43,14 @@ func IsMarkdownFile(name string) bool { return false } +func IsTextFile(data []byte) (string, bool) { + contentType := http.DetectContentType(data) + if strings.Index(contentType, "text/") != -1 { + return contentType, true + } + return contentType, false +} + func IsReadmeFile(name string) bool { name = strings.ToLower(name) if len(name) < 6 { diff --git a/routers/repo/repo.go b/routers/repo/repo.go index a055b416c2..76d55554bc 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -6,11 +6,11 @@ package repo import ( "path" + "path/filepath" "strings" "github.com/codegangsta/martini" - "github.com/gogits/git" "github.com/gogits/webdav" "github.com/gogits/gogs/models" @@ -96,6 +96,7 @@ func Single(ctx *middleware.Context, params martini.Params) { } branchLink := "/" + ctx.Repo.Owner.LowerName + "/" + ctx.Repo.Repository.Name + "/src/" + params["branchname"] + rawLink := "/" + ctx.Repo.Owner.LowerName + "/" + ctx.Repo.Repository.Name + "/raw/" + params["branchname"] if len(treename) != 0 && repoFile == nil { ctx.Handle(404, "repo.Single", nil) @@ -103,12 +104,10 @@ func Single(ctx *middleware.Context, params martini.Params) { } if repoFile != nil && repoFile.IsFile() { - if repoFile.Size > 1024*1024 || repoFile.Filemode != git.FileModeBlob { - ctx.Data["FileIsLarge"] = true - } else if blob, err := repoFile.LookupBlob(); err != nil { - //log.Error("repo.Single(repoFile.LookupBlob): %v", err) + if blob, err := repoFile.LookupBlob(); err != nil { ctx.Handle(404, "repo.Single(repoFile.LookupBlob)", err) } else { + ctx.Data["FileSize"] = repoFile.Size ctx.Data["IsFile"] = true ctx.Data["FileName"] = repoFile.Name ext := path.Ext(repoFile.Name) @@ -116,13 +115,20 @@ func Single(ctx *middleware.Context, params martini.Params) { ext = ext[1:] } ctx.Data["FileExt"] = ext + ctx.Data["FileLink"] = rawLink + "/" + treename + + data := blob.Contents() + _, isTextFile := base.IsTextFile(data) + ctx.Data["FileIsText"] = isTextFile readmeExist := base.IsMarkdownFile(repoFile.Name) || base.IsReadmeFile(repoFile.Name) ctx.Data["ReadmeExist"] = readmeExist if readmeExist { - ctx.Data["FileContent"] = string(base.RenderMarkdown(blob.Contents(), "")) + ctx.Data["FileContent"] = string(base.RenderMarkdown(data, "")) } else { - ctx.Data["FileContent"] = string(blob.Contents()) + if isTextFile { + ctx.Data["FileContent"] = string(data) + } } } @@ -151,17 +157,19 @@ func Single(ctx *middleware.Context, params martini.Params) { if readmeFile != nil { ctx.Data["ReadmeExist"] = true - // if file large than 1M not show it - if readmeFile.Size > 1024*1024 || readmeFile.Filemode != git.FileModeBlob { - ctx.Data["FileIsLarge"] = true - } else if blob, err := readmeFile.LookupBlob(); err != nil { + if blob, err := readmeFile.LookupBlob(); err != nil { ctx.Handle(404, "repo.Single(readmeFile.LookupBlob)", err) return } else { - // current repo branch link - + ctx.Data["FileSize"] = readmeFile.Size + ctx.Data["FileLink"] = rawLink + "/" + treename + data := blob.Contents() + _, isTextFile := base.IsTextFile(data) + ctx.Data["FileIsText"] = isTextFile ctx.Data["FileName"] = readmeFile.Name - ctx.Data["FileContent"] = string(base.RenderMarkdown(blob.Contents(), branchLink)) + if isTextFile { + ctx.Data["FileContent"] = string(base.RenderMarkdown(data, branchLink)) + } } } } @@ -201,6 +209,44 @@ func Single(ctx *middleware.Context, params martini.Params) { ctx.HTML(200, "repo/single") } +func SingleDownload(ctx *middleware.Context, params martini.Params) { + if !ctx.Repo.IsValid { + ctx.Handle(404, "repo.SingleDownload", nil) + return + } + + if len(params["branchname"]) == 0 { + params["branchname"] = "master" + } + + // Get tree path + treename := params["_1"] + + repoFile, err := models.GetTargetFile(params["username"], params["reponame"], + params["branchname"], params["commitid"], treename) + + if err != nil { + ctx.Handle(404, "repo.SingleDownload(GetTargetFile)", err) + return + } + + blob, err := repoFile.LookupBlob() + if err != nil { + ctx.Handle(404, "repo.SingleDownload(LookupBlob)", err) + return + } + + data := blob.Contents() + contentType, isTextFile := base.IsTextFile(data) + ctx.Res.Header().Set("Content-Type", contentType) + if !isTextFile { + ctx.Res.Header().Set("Content-Type", contentType) + ctx.Res.Header().Set("Content-Disposition", "attachment; filename="+filepath.Base(treename)) + ctx.Res.Header().Set("Content-Transfer-Encoding", "binary") + } + ctx.Res.Write(data) +} + func Http(ctx *middleware.Context, params martini.Params) { /*if !ctx.Repo.IsValid { return @@ -45,6 +45,7 @@ gogs serv provide access auth for repositories`, } func init() { + os.MkdirAll("log", os.ModePerm) log.NewLogger(10000, "file", fmt.Sprintf(`{"filename":"%s"}`, "log/serv.log")) } @@ -72,7 +73,6 @@ func runServ(k *cli.Context) { base.NewConfigContext() models.LoadModelsConfig() models.NewEngine() - base.NewLogService() keys := strings.Split(os.Args[2], "-") if len(keys) != 2 { diff --git a/templates/repo/single_file.tmpl b/templates/repo/single_file.tmpl index 7f07033703..d00bffa956 100644 --- a/templates/repo/single_file.tmpl +++ b/templates/repo/single_file.tmpl @@ -2,20 +2,22 @@ <div class="panel-heading file-head"> {{if .ReadmeExist}} <i class="icon fa fa-book"></i> + {{.FileName}} {{else}} + <i class="icon fa fa-file-text-o"></i> + {{.FileName}} {{FileSize .FileSize}} <div class="btn-group pull-right"> <a class="btn btn-default hidden" href="#">Edit</a> - <a class="btn btn-default" href="#">Raw</a> + <a class="btn btn-default" href="{{.FileLink}}">Raw</a> <a class="btn btn-default hidden" href="#">Blame</a> <a class="btn btn-default hidden" href="#">History</a> <a class="btn btn-danger hidden" href="#">Delete</a> </div> - <i class="icon fa fa-file-text-o"></i> - {{end}}{{.FileName}} + {{end}} </div> - {{if .FileIsLarge}} - <div class="panel-footer"> - Large file size 1000kb + {{if not .FileIsText}} + <div class="panel-footer text-center"> + <a href="{{.FileLink}}" class="btn btn-default">View Raw</a> </div> {{else}} {{if .ReadmeExist}} @@ -28,7 +30,7 @@ <tbody> <tr> <td class="lines-num"></td> - <td class="lines-code markdown"><pre class="prettyprint linenums lang-{{.FileExt}}">{{.FileContent}}</pre></td> + <td class="lines-code markdown"><pre class="prettyprint linenums{{if .FileExt}} lang-{{.FileExt}}{{end}}">{{.FileContent}}</pre></td> </tr> </tbody> </table> @@ -89,7 +89,7 @@ func runWeb(*cli.Context) { reqSignOut := middleware.Toggle(&middleware.ToggleOptions{SignOutRequire: true}) // Routers. - m.Get("/", reqSignIn, routers.Home) + m.Get("/", ignSignIn, routers.Home) m.Get("/issues", reqSignIn, user.Issues) m.Get("/pulls", reqSignIn, user.Pulls) m.Get("/stars", reqSignIn, user.Stars) @@ -152,6 +152,7 @@ func runWeb(*cli.Context) { r.Get("/branches", repo.Branches) r.Get("/src/:branchname", repo.Single) r.Get("/src/:branchname/**", repo.Single) + r.Get("/raw/:branchname/**", repo.SingleDownload) r.Get("/commits/:branchname", repo.Commits) r.Get("/commits/:branchname", repo.Commits) }, ignSignIn, middleware.RepoAssignment(true)) @@ -162,6 +163,7 @@ func runWeb(*cli.Context) { m.Group("/:username", func(r martini.Router) { r.Get("/:reponame", middleware.RepoAssignment(true), repo.Single) + r.Get("/:reponame", middleware.RepoAssignment(true), repo.Single) r.Any("/:reponame/**", repo.Http) }, ignSignIn) |