diff options
author | Unknwon <u@gogs.io> | 2016-08-30 02:08:38 -0700 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2016-08-30 02:08:38 -0700 |
commit | 780cc2d11093e048e41f2d6da9d76f6c6ac4a5e2 (patch) | |
tree | 1657d9fecdcf70d4e0067724344f531d608efb71 /modules | |
parent | 2a13f682e0fa039a610e7817495d87685f9632f2 (diff) | |
download | gitea-780cc2d11093e048e41f2d6da9d76f6c6ac4a5e2.tar.gz gitea-780cc2d11093e048e41f2d6da9d76f6c6ac4a5e2.zip |
router/repo: code refactoring
Diffstat (limited to 'modules')
-rw-r--r-- | modules/base/tool.go | 24 | ||||
-rw-r--r-- | modules/context/context.go | 11 | ||||
-rw-r--r-- | modules/context/repo.go | 13 |
3 files changed, 21 insertions, 27 deletions
diff --git a/modules/base/tool.go b/modules/base/tool.go index cf8fece027..e321d2b868 100644 --- a/modules/base/tool.go +++ b/modules/base/tool.go @@ -518,26 +518,14 @@ func IsLetter(ch rune) bool { return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_' || ch >= 0x80 && unicode.IsLetter(ch) } -func IsTextFile(data []byte) (string, bool) { - contentType := http.DetectContentType(data) - if strings.Index(contentType, "text/") != -1 { - return contentType, true - } - return contentType, false +func IsTextFile(data []byte) bool { + return strings.Index(http.DetectContentType(data), "text/") != -1 } -func IsImageFile(data []byte) (string, bool) { - contentType := http.DetectContentType(data) - if strings.Index(contentType, "image/") != -1 { - return contentType, true - } - return contentType, false +func IsImageFile(data []byte) bool { + return strings.Index(http.DetectContentType(data), "image/") != -1 } -func IsPDFFile(data []byte) (string, bool) { - contentType := http.DetectContentType(data) - if strings.Index(contentType, "application/pdf") != -1 { - return contentType, true - } - return contentType, false +func IsPDFFile(data []byte) bool { + return strings.Index(http.DetectContentType(data), "application/pdf") != -1 } diff --git a/modules/context/context.go b/modules/context/context.go index 971d075d0f..ac46bb38fb 100644 --- a/modules/context/context.go +++ b/modules/context/context.go @@ -105,13 +105,12 @@ func (ctx *Context) Handle(status int, title string, err error) { ctx.HTML(status, base.TplName(fmt.Sprintf("status/%d", status))) } -// HandleError use error check function to determine if server should -// response as client input error or server internal error. -// It responses with given status code for client error, -// or error context description for logging purpose of server error. -func (ctx *Context) HandleError(title string, errck func(error) bool, err error, status int) { +// NotFoundOrServerError use error check function to determine if the error +// is about not found. It responses with 404 status code for not found error, +// or error context description for logging purpose of 500 server error. +func (ctx *Context) NotFoundOrServerError(title string, errck func(error) bool, err error) { if errck(err) { - ctx.Error(status, err.Error()) + ctx.Handle(404, title, err) return } diff --git a/modules/context/repo.go b/modules/context/repo.go index f5f26b1e51..d72e64e42a 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -48,7 +48,7 @@ type Repository struct { CommitsCount int64 Mirror *models.Mirror - PullRequest *PullRequest + PullRequest *PullRequest } // IsOwner returns true if current user is the owner of repository. @@ -71,6 +71,11 @@ func (r *Repository) HasAccess() bool { return r.AccessMode >= models.ACCESS_MODE_READ } +// CanEnableEditor returns true if repository is editable and user has proper access level. +func (r *Repository) CanEnableEditor() bool { + return r.Repository.CanEnableEditor() && r.IsViewBranch && r.IsWriter() +} + // GetEditorconfig returns the .editorconfig definition if found in the // HEAD of the default repo branch. func (r *Repository) GetEditorconfig() (*editorconfig.Editorconfig, error) { @@ -167,6 +172,7 @@ func RepoAssignment(args ...bool) macaron.Handler { } } ctx.Repo.Owner = owner + ctx.Data["Username"] = ctx.Repo.Owner.Name // Get repository. repo, err := models.GetRepositoryByName(owner.ID, repoName) @@ -221,6 +227,7 @@ func RepoAssignment(args ...bool) macaron.Handler { } ctx.Repo.Repository = repo + ctx.Data["RepoName"] = ctx.Repo.Repository.Name ctx.Data["IsBareRepo"] = ctx.Repo.Repository.IsBare gitRepo, err := git.OpenRepository(models.RepoPath(userName, repoName)) @@ -348,12 +355,11 @@ func RepoRef() macaron.Handler { // For API calls. if ctx.Repo.GitRepo == nil { repoPath := models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name) - gitRepo, err := git.OpenRepository(repoPath) + ctx.Repo.GitRepo, err = git.OpenRepository(repoPath) if err != nil { ctx.Handle(500, "RepoRef Invalid repo "+repoPath, err) return } - ctx.Repo.GitRepo = gitRepo } // Get default branch. @@ -431,6 +437,7 @@ func RepoRef() macaron.Handler { ctx.Repo.BranchName = refName ctx.Data["BranchName"] = ctx.Repo.BranchName ctx.Data["CommitID"] = ctx.Repo.CommitID + ctx.Data["TreePath"] = ctx.Repo.TreePath ctx.Data["IsViewBranch"] = ctx.Repo.IsViewBranch ctx.Data["IsViewTag"] = ctx.Repo.IsViewTag ctx.Data["IsViewCommit"] = ctx.Repo.IsViewCommit |