lfs.go 2.8 KB

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