lfs.go 2.5 KB

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