You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
2.8 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package context
  5. import (
  6. "fmt"
  7. "strings"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/base"
  10. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/modules/setting"
  12. "github.com/Unknwon/paginater"
  13. macaron "gopkg.in/macaron.v1"
  14. )
  15. // APIContext is a specific macaron context for API service
  16. type APIContext struct {
  17. *Context
  18. Org *APIOrganization
  19. }
  20. // Error responses error message to client with given message.
  21. // If status is 500, also it prints error to log.
  22. func (ctx *APIContext) Error(status int, title string, obj interface{}) {
  23. var message string
  24. if err, ok := obj.(error); ok {
  25. message = err.Error()
  26. } else {
  27. message = obj.(string)
  28. }
  29. if status == 500 {
  30. log.Error(4, "%s: %s", title, message)
  31. }
  32. ctx.JSON(status, map[string]string{
  33. "message": message,
  34. "url": base.DocURL,
  35. })
  36. }
  37. // SetLinkHeader sets pagination link header by given totol number and page size.
  38. func (ctx *APIContext) SetLinkHeader(total, pageSize int) {
  39. page := paginater.New(total, pageSize, ctx.QueryInt("page"), 0)
  40. links := make([]string, 0, 4)
  41. if page.HasNext() {
  42. links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"next\"", setting.AppURL, ctx.Req.URL.Path[1:], page.Next()))
  43. }
  44. if !page.IsLast() {
  45. links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"last\"", setting.AppURL, ctx.Req.URL.Path[1:], page.TotalPages()))
  46. }
  47. if !page.IsFirst() {
  48. links = append(links, fmt.Sprintf("<%s%s?page=1>; rel=\"first\"", setting.AppURL, ctx.Req.URL.Path[1:]))
  49. }
  50. if page.HasPrevious() {
  51. links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"prev\"", setting.AppURL, ctx.Req.URL.Path[1:], page.Previous()))
  52. }
  53. if len(links) > 0 {
  54. ctx.Header().Set("Link", strings.Join(links, ","))
  55. }
  56. }
  57. // APIContexter returns apicontext as macaron middleware
  58. func APIContexter() macaron.Handler {
  59. return func(c *Context) {
  60. ctx := &APIContext{
  61. Context: c,
  62. }
  63. c.Map(ctx)
  64. }
  65. }
  66. // ExtractOwnerAndRepo returns a handler that populates the `Repo.Owner` and
  67. // `Repo.Repository` fields of an APIContext
  68. func ExtractOwnerAndRepo() macaron.Handler {
  69. return func(ctx *APIContext) {
  70. owner, err := models.GetUserByName(ctx.Params(":username"))
  71. if err != nil {
  72. if models.IsErrUserNotExist(err) {
  73. ctx.Error(422, "", err)
  74. } else {
  75. ctx.Error(500, "GetUserByName", err)
  76. }
  77. return
  78. }
  79. repo, err := models.GetRepositoryByName(owner.ID, ctx.Params(":reponame"))
  80. if err != nil {
  81. if models.IsErrRepoNotExist(err) {
  82. ctx.Status(404)
  83. } else {
  84. ctx.Error(500, "GetRepositoryByName", err)
  85. }
  86. return
  87. }
  88. ctx.Repo.Owner = owner
  89. ctx.Data["Owner"] = owner
  90. ctx.Repo.Repository = repo
  91. ctx.Data["Repository"] = repo
  92. }
  93. }