lfs.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. "gorm.io/gorm"
  10. "gogs.io/gogs/internal/errutil"
  11. "gogs.io/gogs/internal/lfsutil"
  12. )
  13. // LFSStore is the persistent interface for LFS objects.
  14. type LFSStore interface {
  15. // CreateObject creates a LFS object record in database.
  16. CreateObject(ctx context.Context, repoID int64, oid lfsutil.OID, size int64, storage lfsutil.Storage) error
  17. // GetObjectByOID returns the LFS object with given OID. It returns
  18. // ErrLFSObjectNotExist when not found.
  19. GetObjectByOID(ctx context.Context, repoID int64, oid lfsutil.OID) (*LFSObject, error)
  20. // GetObjectsByOIDs returns LFS objects found within "oids". The returned list
  21. // could have less elements if some oids were not found.
  22. GetObjectsByOIDs(ctx context.Context, repoID int64, oids ...lfsutil.OID) ([]*LFSObject, error)
  23. }
  24. var LFS LFSStore
  25. // LFSObject is the relation between an LFS object and a repository.
  26. type LFSObject struct {
  27. RepoID int64 `gorm:"primaryKey;auto_increment:false"`
  28. OID lfsutil.OID `gorm:"primaryKey;column:oid"`
  29. Size int64 `gorm:"not null"`
  30. Storage lfsutil.Storage `gorm:"not null"`
  31. CreatedAt time.Time `gorm:"not null"`
  32. }
  33. var _ LFSStore = (*lfs)(nil)
  34. type lfs struct {
  35. *gorm.DB
  36. }
  37. func (db *lfs) CreateObject(ctx context.Context, 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.WithContext(ctx).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(ctx context.Context, repoID int64, oid lfsutil.OID) (*LFSObject, error) {
  60. object := new(LFSObject)
  61. err := db.WithContext(ctx).Where("repo_id = ? AND oid = ?", repoID, oid).First(object).Error
  62. if err != nil {
  63. if err == gorm.ErrRecordNotFound {
  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(ctx context.Context, 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.WithContext(ctx).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. }