lfs.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2020 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 db
  5. import (
  6. "fmt"
  7. "time"
  8. "github.com/jinzhu/gorm"
  9. "gogs.io/gogs/internal/errutil"
  10. "gogs.io/gogs/internal/lfsutil"
  11. )
  12. // LFSStore is the persistent interface for LFS objects.
  13. //
  14. // NOTE: All methods are sorted in alphabetical order.
  15. type LFSStore interface {
  16. // CreateObject creates a LFS object record in database.
  17. CreateObject(repoID int64, oid lfsutil.OID, size int64, storage lfsutil.Storage) error
  18. // GetObjectByOID returns the LFS object with given OID. It returns ErrLFSObjectNotExist
  19. // when not found.
  20. GetObjectByOID(repoID int64, oid lfsutil.OID) (*LFSObject, error)
  21. // GetObjectsByOIDs returns LFS objects found within "oids". The returned list could have
  22. // less elements if some oids were not found.
  23. GetObjectsByOIDs(repoID int64, oids ...lfsutil.OID) ([]*LFSObject, error)
  24. }
  25. var LFS LFSStore
  26. type lfs struct {
  27. *gorm.DB
  28. }
  29. // LFSObject is the relation between an LFS object and a repository.
  30. type LFSObject struct {
  31. RepoID int64 `gorm:"PRIMARY_KEY;AUTO_INCREMENT:false"`
  32. OID lfsutil.OID `gorm:"PRIMARY_KEY;COLUMN:oid"`
  33. Size int64 `gorm:"NOT NULL"`
  34. Storage lfsutil.Storage `gorm:"NOT NULL"`
  35. CreatedAt time.Time `gorm:"NOT NULL"`
  36. }
  37. func (db *lfs) CreateObject(repoID int64, oid lfsutil.OID, size int64, storage lfsutil.Storage) error {
  38. object := &LFSObject{
  39. RepoID: repoID,
  40. OID: oid,
  41. Size: size,
  42. Storage: storage,
  43. }
  44. return db.DB.Create(object).Error
  45. }
  46. type ErrLFSObjectNotExist struct {
  47. args errutil.Args
  48. }
  49. func IsErrLFSObjectNotExist(err error) bool {
  50. _, ok := err.(ErrLFSObjectNotExist)
  51. return ok
  52. }
  53. func (err ErrLFSObjectNotExist) Error() string {
  54. return fmt.Sprintf("LFS object does not exist: %v", err.args)
  55. }
  56. func (ErrLFSObjectNotExist) NotFound() bool {
  57. return true
  58. }
  59. func (db *lfs) GetObjectByOID(repoID int64, oid lfsutil.OID) (*LFSObject, error) {
  60. object := new(LFSObject)
  61. err := db.Where("repo_id = ? AND oid = ?", repoID, oid).First(object).Error
  62. if err != nil {
  63. if gorm.IsRecordNotFoundError(err) {
  64. return nil, ErrLFSObjectNotExist{args: errutil.Args{"repoID": repoID, "oid": oid}}
  65. }
  66. return nil, err
  67. }
  68. return object, err
  69. }
  70. func (db *lfs) GetObjectsByOIDs(repoID int64, oids ...lfsutil.OID) ([]*LFSObject, error) {
  71. if len(oids) == 0 {
  72. return []*LFSObject{}, nil
  73. }
  74. objects := make([]*LFSObject, 0, len(oids))
  75. err := db.Where("repo_id = ? AND oid IN (?)", repoID, oids).Find(&objects).Error
  76. if err != nil && err != gorm.ErrRecordNotFound {
  77. return nil, err
  78. }
  79. return objects, nil
  80. }