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

lfs.go 3.1KB

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