store.go 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package lfs
  2. import (
  3. "context"
  4. "gogs.io/gogs/internal/database"
  5. "gogs.io/gogs/internal/lfsutil"
  6. )
  7. // Store is the data layer carrier for LFS endpoints. This interface is meant to
  8. // abstract away and limit the exposure of the underlying data layer to the
  9. // handler through a thin-wrapper.
  10. type Store interface {
  11. // GetAccessTokenBySHA1 returns the access token with given SHA1. It returns
  12. // database.ErrAccessTokenNotExist when not found.
  13. GetAccessTokenBySHA1(ctx context.Context, sha1 string) (*database.AccessToken, error)
  14. // TouchAccessTokenByID updates the updated time of the given access token to
  15. // the current time.
  16. TouchAccessTokenByID(ctx context.Context, id int64) error
  17. // CreateLFSObject creates an LFS object record in database.
  18. CreateLFSObject(ctx context.Context, repoID int64, oid lfsutil.OID, size int64, storage lfsutil.Storage) error
  19. // GetLFSObjectByOID returns the LFS object with given OID. It returns
  20. // database.ErrLFSObjectNotExist when not found.
  21. GetLFSObjectByOID(ctx context.Context, repoID int64, oid lfsutil.OID) (*database.LFSObject, error)
  22. // GetLFSObjectsByOIDs returns LFS objects found within "oids". The returned
  23. // list could have fewer elements if some oids were not found.
  24. GetLFSObjectsByOIDs(ctx context.Context, repoID int64, oids ...lfsutil.OID) ([]*database.LFSObject, error)
  25. // AuthorizeRepositoryAccess returns true if the user has as good as desired
  26. // access mode to the repository.
  27. AuthorizeRepositoryAccess(ctx context.Context, userID, repoID int64, desired database.AccessMode, opts database.AccessModeOptions) bool
  28. // GetRepositoryByName returns the repository with given owner and name. It
  29. // returns database.ErrRepoNotExist when not found.
  30. GetRepositoryByName(ctx context.Context, ownerID int64, name string) (*database.Repository, error)
  31. // IsTwoFactorEnabled returns true if the user has enabled 2FA.
  32. IsTwoFactorEnabled(ctx context.Context, userID int64) bool
  33. }
  34. type store struct{}
  35. // NewStore returns a new Store using the global database handle.
  36. func NewStore() Store {
  37. return &store{}
  38. }
  39. func (*store) GetAccessTokenBySHA1(ctx context.Context, sha1 string) (*database.AccessToken, error) {
  40. return database.Handle.AccessTokens().GetBySHA1(ctx, sha1)
  41. }
  42. func (*store) TouchAccessTokenByID(ctx context.Context, id int64) error {
  43. return database.Handle.AccessTokens().Touch(ctx, id)
  44. }
  45. func (*store) CreateLFSObject(ctx context.Context, repoID int64, oid lfsutil.OID, size int64, storage lfsutil.Storage) error {
  46. return database.Handle.LFS().CreateObject(ctx, repoID, oid, size, storage)
  47. }
  48. func (*store) GetLFSObjectByOID(ctx context.Context, repoID int64, oid lfsutil.OID) (*database.LFSObject, error) {
  49. return database.Handle.LFS().GetObjectByOID(ctx, repoID, oid)
  50. }
  51. func (*store) GetLFSObjectsByOIDs(ctx context.Context, repoID int64, oids ...lfsutil.OID) ([]*database.LFSObject, error) {
  52. return database.Handle.LFS().GetObjectsByOIDs(ctx, repoID, oids...)
  53. }
  54. func (*store) AuthorizeRepositoryAccess(ctx context.Context, userID, repoID int64, desired database.AccessMode, opts database.AccessModeOptions) bool {
  55. return database.Handle.Permissions().Authorize(ctx, userID, repoID, desired, opts)
  56. }
  57. func (*store) GetRepositoryByName(ctx context.Context, ownerID int64, name string) (*database.Repository, error) {
  58. return database.Handle.Repositories().GetByName(ctx, ownerID, name)
  59. }
  60. func (*store) IsTwoFactorEnabled(ctx context.Context, userID int64) bool {
  61. return database.Handle.TwoFactors().IsEnabled(ctx, userID)
  62. }