diff options
Diffstat (limited to 'modules')
-rw-r--r-- | modules/auth/repo.go | 62 | ||||
-rw-r--r-- | modules/base/template.go | 1 | ||||
-rw-r--r-- | modules/base/tool.go | 46 | ||||
-rw-r--r-- | modules/middleware/context.go | 7 | ||||
-rw-r--r-- | modules/middleware/repo.go | 78 |
5 files changed, 132 insertions, 62 deletions
diff --git a/modules/auth/repo.go b/modules/auth/repo.go index 5fe091d6..ac1b6b69 100644 --- a/modules/auth/repo.go +++ b/modules/auth/repo.go @@ -12,11 +12,8 @@ import ( "github.com/gogits/binding" - "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/log" - "github.com/martini-contrib/render" - "github.com/martini-contrib/sessions" ) type CreateRepoForm struct { @@ -61,62 +58,3 @@ type DeleteRepoForm struct { UserName string `form:"userName" binding:"Required"` RepoId int64 `form:"repoId" binding:"Required"` } - -func RepoAssignment(redirect bool) martini.Handler { - return func(params martini.Params, r render.Render, data base.TmplData, session sessions.Session) { - // assign false first - data["IsRepositoryValid"] = false - - var ( - user *models.User - err error - ) - // get repository owner - isOwner := (data["SignedUserName"] == params["username"]) - if !isOwner { - user, err = models.GetUserByName(params["username"]) - if err != nil { - if redirect { - r.Redirect("/") - return - } - //data["ErrorMsg"] = err - //log.Error("repo.Single: %v", err) - //r.HTML(200, "base/error", data) - return - } - } else { - user = SignedInUser(session) - } - if user == nil { - if redirect { - r.Redirect("/") - return - } - //data["ErrorMsg"] = "invliad user account for single repository" - //log.Error("repo.Single: %v", err) - //r.HTML(200, "base/error", data) - return - } - data["IsRepositoryOwner"] = isOwner - - // get repository - repo, err := models.GetRepositoryByName(user, params["reponame"]) - if err != nil { - if redirect { - r.Redirect("/") - return - } - //data["ErrorMsg"] = err - //log.Error("repo.Single: %v", err) - //r.HTML(200, "base/error", data) - return - } - - data["Repository"] = repo - data["Owner"] = user - data["Title"] = user.Name + "/" + repo.Name - data["RepositoryLink"] = data["Title"] - data["IsRepositoryValid"] = true - } -} diff --git a/modules/base/template.go b/modules/base/template.go index 620bf4a1..b38ab140 100644 --- a/modules/base/template.go +++ b/modules/base/template.go @@ -21,6 +21,7 @@ var TemplateFuncs template.FuncMap = map[string]interface{}{ }, "str2html": Str2html, "TimeSince": TimeSince, + "FileSize": FileSize, "Subtract": Subtract, "ActionIcon": ActionIcon, "ActionDesc": ActionDesc, diff --git a/modules/base/tool.go b/modules/base/tool.go index 4802c413..3f8b8ffa 100644 --- a/modules/base/tool.go +++ b/modules/base/tool.go @@ -8,6 +8,7 @@ import ( "crypto/md5" "encoding/hex" "fmt" + "math" "strings" "time" ) @@ -80,6 +81,51 @@ func TimeSince(then time.Time) string { return then.String() } +const ( + Byte = 1 + KByte = Byte * 1024 + MByte = KByte * 1024 + GByte = MByte * 1024 + TByte = GByte * 1024 + PByte = TByte * 1024 + EByte = PByte * 1024 +) + +var bytesSizeTable = map[string]uint64{ + "b": Byte, + "kb": KByte, + "mb": MByte, + "gb": GByte, + "tb": TByte, + "pb": PByte, + "eb": EByte, +} + +func logn(n, b float64) float64 { + return math.Log(n) / math.Log(b) +} + +func humanateBytes(s uint64, base float64, sizes []string) string { + if s < 10 { + return fmt.Sprintf("%dB", s) + } + e := math.Floor(logn(float64(s), base)) + suffix := sizes[int(e)] + val := float64(s) / math.Pow(base, math.Floor(e)) + f := "%.0f" + if val < 10 { + f = "%.1f" + } + + return fmt.Sprintf(f+"%s", val, suffix) +} + +// FileSize calculates the file size and generate user-friendly string. +func FileSize(s int64) string { + sizes := []string{"B", "KB", "MB", "GB", "TB", "PB", "EB"} + return humanateBytes(uint64(s), 1024, sizes) +} + // Subtract deals with subtraction of all types of number. func Subtract(left interface{}, right interface{}) interface{} { var rleft, rright int64 diff --git a/modules/middleware/context.go b/modules/middleware/context.go index d3fe7bbf..d002d3c2 100644 --- a/modules/middleware/context.go +++ b/modules/middleware/context.go @@ -29,6 +29,13 @@ type Context struct { Render render.Render User *models.User IsSigned bool + + Repo struct { + IsValid bool + IsOwner bool + Repository *models.Repository + Owner *models.User + } } // Query querys form parameter. diff --git a/modules/middleware/repo.go b/modules/middleware/repo.go new file mode 100644 index 00000000..948713ef --- /dev/null +++ b/modules/middleware/repo.go @@ -0,0 +1,78 @@ +// Copyright 2014 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package middleware + +import ( + "github.com/codegangsta/martini" + + "github.com/gogits/gogs/models" +) + +func RepoAssignment(redirect bool) martini.Handler { + return func(ctx *Context, params martini.Params) { + // assign false first + ctx.Data["IsRepositoryValid"] = false + + var ( + user *models.User + err error + ) + + // get repository owner + ctx.Repo.IsOwner = ctx.IsSigned && ctx.User.LowerName == params["username"] + ctx.Data["IsRepositoryOwner"] = ctx.Repo.IsOwner + + if !ctx.Repo.IsOwner { + user, err = models.GetUserByName(params["username"]) + if err != nil { + if redirect { + ctx.Render.Redirect("/") + return + } + //data["ErrorMsg"] = err + //log.Error("repo.Single: %v", err) + //r.HTML(200, "base/error", data) + return + } + } else { + user = ctx.User + } + + if user == nil { + if redirect { + ctx.Render.Redirect("/") + return + } + //data["ErrorMsg"] = "invliad user account for single repository" + //log.Error("repo.Single: %v", err) + //r.HTML(200, "base/error", data) + return + } + + ctx.Repo.Owner = user + + // get repository + repo, err := models.GetRepositoryByName(user, params["reponame"]) + if err != nil { + if redirect { + ctx.Render.Redirect("/") + return + } + //data["ErrorMsg"] = err + //log.Error("repo.Single: %v", err) + //r.HTML(200, "base/error", data) + return + } + + ctx.Repo.IsValid = true + ctx.Repo.Repository = repo + + ctx.Data["IsRepositoryValid"] = true + ctx.Data["Repository"] = repo + ctx.Data["Owner"] = user + ctx.Data["Title"] = user.Name + "/" + repo.Name + ctx.Data["RepositoryLink"] = ctx.Data["Title"] + } +} |