gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package lfs
  4. import (
  5. "errors"
  6. "fmt"
  7. "time"
  8. "code.gitea.io/gitea/modules/util"
  9. )
  10. const (
  11. // MediaType contains the media type for LFS server requests
  12. MediaType = "application/vnd.git-lfs+json"
  13. // AcceptHeader Some LFS servers offer content with other types, so fallback to '*/*' if application/vnd.git-lfs+json cannot be served
  14. AcceptHeader = "application/vnd.git-lfs+json;q=0.9, */*;q=0.8"
  15. // UserAgentHeader Add User-Agent for gitea's self-implemented lfs client,
  16. // and the version is consistent with the latest version of git lfs can be avoided incompatibilities.
  17. // Some lfs servers will check this
  18. UserAgentHeader = "git-lfs/3.6.0 (Gitea)"
  19. )
  20. // BatchRequest contains multiple requests processed in one batch operation.
  21. // https://github.com/git-lfs/git-lfs/blob/main/docs/api/batch.md#requests
  22. type BatchRequest struct {
  23. Operation string `json:"operation"`
  24. Transfers []string `json:"transfers,omitempty"`
  25. Ref *Reference `json:"ref,omitempty"`
  26. Objects []Pointer `json:"objects"`
  27. }
  28. // Reference contains a git reference.
  29. // https://github.com/git-lfs/git-lfs/blob/main/docs/api/batch.md#ref-property
  30. type Reference struct {
  31. Name string `json:"name"`
  32. }
  33. // Pointer contains LFS pointer data
  34. type Pointer struct {
  35. Oid string `json:"oid" xorm:"UNIQUE(s) INDEX NOT NULL"`
  36. Size int64 `json:"size" xorm:"NOT NULL"`
  37. }
  38. // BatchResponse contains multiple object metadata Representation structures
  39. // for use with the batch API.
  40. // https://github.com/git-lfs/git-lfs/blob/main/docs/api/batch.md#successful-responses
  41. type BatchResponse struct {
  42. Transfer string `json:"transfer,omitempty"`
  43. Objects []*ObjectResponse `json:"objects"`
  44. }
  45. // ObjectResponse is object metadata as seen by clients of the LFS server.
  46. type ObjectResponse struct {
  47. Pointer
  48. Actions map[string]*Link `json:"actions,omitempty"`
  49. Links map[string]*Link `json:"_links,omitempty"`
  50. Error *ObjectError `json:"error,omitempty"`
  51. }
  52. // Link provides a structure with information about how to access a object.
  53. type Link struct {
  54. Href string `json:"href"`
  55. Header map[string]string `json:"header,omitempty"`
  56. ExpiresAt *time.Time `json:"expires_at,omitempty"`
  57. }
  58. // ObjectError defines the JSON structure returned to the client in case of an error.
  59. type ObjectError struct {
  60. Code int `json:"code"`
  61. Message string `json:"message"`
  62. }
  63. var (
  64. // See https://github.com/git-lfs/git-lfs/blob/main/docs/api/batch.md#successful-responses
  65. // LFS object error codes should match HTTP status codes where possible:
  66. // 404 - The object does not exist on the server.
  67. // 409 - The specified hash algorithm disagrees with the server's acceptable options.
  68. // 410 - The object was removed by the owner.
  69. // 422 - Validation error.
  70. ErrObjectNotExist = util.ErrNotExist // the object does not exist on the server
  71. ErrObjectHashMismatch = errors.New("the specified hash algorithm disagrees with the server's acceptable options")
  72. ErrObjectRemoved = errors.New("the object was removed by the owner")
  73. ErrObjectValidation = errors.New("validation error")
  74. )
  75. func (e *ObjectError) Error() string {
  76. return fmt.Sprintf("[%d] %s", e.Code, e.Message)
  77. }
  78. func (e *ObjectError) Unwrap() error {
  79. switch e.Code {
  80. case 404:
  81. return ErrObjectNotExist
  82. case 409:
  83. return ErrObjectHashMismatch
  84. case 410:
  85. return ErrObjectRemoved
  86. case 422:
  87. return ErrObjectValidation
  88. default:
  89. return errors.New(e.Message)
  90. }
  91. }
  92. // PointerBlob associates a Git blob with a Pointer.
  93. type PointerBlob struct {
  94. Hash string
  95. Pointer
  96. }
  97. // ErrorResponse describes the error to the client.
  98. type ErrorResponse struct {
  99. Message string
  100. DocumentationURL string `json:"documentation_url,omitempty"`
  101. RequestID string `json:"request_id,omitempty"`
  102. }