You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

storage_object.go 3.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package db
  2. import (
  3. "fmt"
  4. "github.com/jmoiron/sqlx"
  5. "gitlink.org.cn/cloudream/common/consts"
  6. "gitlink.org.cn/cloudream/storage-common/pkgs/db/model"
  7. )
  8. type StorageObjectDB struct {
  9. *DB
  10. }
  11. func (db *DB) StorageObject() *StorageObjectDB {
  12. return &StorageObjectDB{DB: db}
  13. }
  14. func (*StorageObjectDB) Get(ctx SQLContext, storageID int64, objectID int64, userID int64) (model.StorageObject, error) {
  15. var ret model.StorageObject
  16. err := sqlx.Get(ctx, &ret, "select * from StorageObject where StorageID = ? and ObjectID = ? and UserID = ?", storageID, objectID, userID)
  17. return ret, err
  18. }
  19. func (*StorageObjectDB) GetAllByStorageAndObjectID(ctx SQLContext, storageID int64, objectID int64) ([]model.StorageObject, error) {
  20. var ret []model.StorageObject
  21. err := sqlx.Select(ctx, &ret, "select * from StorageObject where StorageID = ? and ObjectID = ?", storageID, objectID)
  22. return ret, err
  23. }
  24. func (*StorageObjectDB) GetAllByStorageID(ctx SQLContext, storageID int64) ([]model.StorageObject, error) {
  25. var ret []model.StorageObject
  26. err := sqlx.Select(ctx, &ret, "select * from StorageObject where StorageID = ?", storageID)
  27. return ret, err
  28. }
  29. func (*StorageObjectDB) MoveObjectTo(ctx SQLContext, objectID int64, storageID int64, userID int64) error {
  30. _, err := ctx.Exec("insert into StorageObject values(?,?,?,?)", objectID, storageID, userID, consts.StorageObjectStateNormal)
  31. return err
  32. }
  33. func (*StorageObjectDB) ChangeState(ctx SQLContext, storageID int64, objectID int64, userID int64, state string) error {
  34. _, err := ctx.Exec("update StorageObject set State = ? where StorageID = ? and ObjectID = ? and UserID = ?", state, storageID, objectID, userID)
  35. return err
  36. }
  37. // SetStateNormal 将状态设置为Normal,如果记录状态是Deleted,则不进行操作
  38. func (*StorageObjectDB) SetStateNormal(ctx SQLContext, storageID int64, objectID int64, userID int64) error {
  39. _, err := ctx.Exec("update StorageObject set State = ? where StorageID = ? and ObjectID = ? and UserID = ? and State <> ?",
  40. consts.StorageObjectStateNormal,
  41. storageID,
  42. objectID,
  43. userID,
  44. consts.StorageObjectStateDeleted,
  45. )
  46. return err
  47. }
  48. func (*StorageObjectDB) SetAllObjectState(ctx SQLContext, objectID int64, state string) (int64, error) {
  49. ret, err := ctx.Exec(
  50. "update StorageObject set State = ? where ObjectID = ?",
  51. state,
  52. objectID,
  53. )
  54. if err != nil {
  55. return 0, err
  56. }
  57. cnt, err := ret.RowsAffected()
  58. if err != nil {
  59. return 0, fmt.Errorf("get affected rows failed, err: %w", err)
  60. }
  61. return cnt, nil
  62. }
  63. // SetAllObjectOutdated 将Storage中指定对象设置为已过期。
  64. // 注:只会设置Normal状态的对象
  65. func (*StorageObjectDB) SetAllObjectOutdated(ctx SQLContext, objectID int64) (int64, error) {
  66. ret, err := ctx.Exec(
  67. "update StorageObject set State = ? where State = ? and ObjectID = ?",
  68. consts.StorageObjectStateOutdated,
  69. consts.StorageObjectStateNormal,
  70. objectID,
  71. )
  72. if err != nil {
  73. return 0, err
  74. }
  75. cnt, err := ret.RowsAffected()
  76. if err != nil {
  77. return 0, fmt.Errorf("get affected rows failed, err: %w", err)
  78. }
  79. return cnt, nil
  80. }
  81. func (db *StorageObjectDB) SetAllObjectDeleted(ctx SQLContext, objectID int64) (int64, error) {
  82. return db.SetAllObjectState(ctx, objectID, consts.StorageObjectStateDeleted)
  83. }
  84. func (*StorageObjectDB) Delete(ctx SQLContext, storageID int64, objectID int64, userID int64) error {
  85. _, err := ctx.Exec("delete from StorageObject where StorageID = ? and ObjectID = ? and UserID = ?", storageID, objectID, userID)
  86. return err
  87. }
  88. // FindObjectStorages 查询存储了指定对象的Storage
  89. func (*StorageObjectDB) FindObjectStorages(ctx SQLContext, objectID int64) ([]model.Storage, error) {
  90. var ret []model.Storage
  91. err := sqlx.Select(ctx, &ret,
  92. "select Storage.* from StorageObject, Storage where ObjectID = ? and "+
  93. "StorageObject.StorageID = Storage.StorageID",
  94. objectID,
  95. )
  96. return ret, err
  97. }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。