Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

lfs.go 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package models
  2. import (
  3. "errors"
  4. "time"
  5. )
  6. // LFSMetaObject stores metadata for LFS tracked files.
  7. type LFSMetaObject struct {
  8. ID int64 `xorm:"pk autoincr"`
  9. Oid string `xorm:"UNIQUE(s) INDEX NOT NULL"`
  10. Size int64 `xorm:"NOT NULL"`
  11. RepositoryID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
  12. Existing bool `xorm:"-"`
  13. Created time.Time `xorm:"-"`
  14. CreatedUnix int64 `xorm:"created"`
  15. }
  16. // LFSTokenResponse defines the JSON structure in which the JWT token is stored.
  17. // This structure is fetched via SSH and passed by the Git LFS client to the server
  18. // endpoint for authorization.
  19. type LFSTokenResponse struct {
  20. Header map[string]string `json:"header"`
  21. Href string `json:"href"`
  22. }
  23. var (
  24. // ErrLFSObjectNotExist is returned from lfs models functions in order
  25. // to differentiate between database and missing object errors.
  26. ErrLFSObjectNotExist = errors.New("LFS Meta object does not exist")
  27. )
  28. const (
  29. // LFSMetaFileIdentifier is the string appearing at the first line of LFS pointer files.
  30. // https://github.com/git-lfs/git-lfs/blob/master/docs/spec.md
  31. LFSMetaFileIdentifier = "version https://git-lfs.github.com/spec/v1"
  32. // LFSMetaFileOidPrefix appears in LFS pointer files on a line before the sha256 hash.
  33. LFSMetaFileOidPrefix = "oid sha256:"
  34. )
  35. // NewLFSMetaObject stores a given populated LFSMetaObject structure in the database
  36. // if it is not already present.
  37. func NewLFSMetaObject(m *LFSMetaObject) (*LFSMetaObject, error) {
  38. var err error
  39. has, err := x.Get(m)
  40. if err != nil {
  41. return nil, err
  42. }
  43. if has {
  44. m.Existing = true
  45. return m, nil
  46. }
  47. sess := x.NewSession()
  48. defer sess.Close()
  49. if err = sess.Begin(); err != nil {
  50. return nil, err
  51. }
  52. if _, err = sess.Insert(m); err != nil {
  53. return nil, err
  54. }
  55. return m, sess.Commit()
  56. }
  57. // GetLFSMetaObjectByOid selects a LFSMetaObject entry from database by its OID.
  58. // It may return ErrLFSObjectNotExist or a database error. If the error is nil,
  59. // the returned pointer is a valid LFSMetaObject.
  60. func (repo *Repository) GetLFSMetaObjectByOid(oid string) (*LFSMetaObject, error) {
  61. if len(oid) == 0 {
  62. return nil, ErrLFSObjectNotExist
  63. }
  64. m := &LFSMetaObject{Oid: oid, RepositoryID: repo.ID}
  65. has, err := x.Get(m)
  66. if err != nil {
  67. return nil, err
  68. } else if !has {
  69. return nil, ErrLFSObjectNotExist
  70. }
  71. return m, nil
  72. }
  73. // RemoveLFSMetaObjectByOid removes a LFSMetaObject entry from database by its OID.
  74. // It may return ErrLFSObjectNotExist or a database error.
  75. func (repo *Repository) RemoveLFSMetaObjectByOid(oid string) error {
  76. if len(oid) == 0 {
  77. return ErrLFSObjectNotExist
  78. }
  79. sess := x.NewSession()
  80. defer sess.Close()
  81. if err := sess.Begin(); err != nil {
  82. return err
  83. }
  84. m := &LFSMetaObject{Oid: oid, RepositoryID: repo.ID}
  85. if _, err := sess.Delete(m); err != nil {
  86. return err
  87. }
  88. return sess.Commit()
  89. }
  90. // AfterLoad stores the LFSMetaObject creation time in the database as local time.
  91. func (m *LFSMetaObject) AfterLoad() {
  92. m.Created = time.Unix(m.CreatedUnix, 0).Local()
  93. }