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.go 3.7 kB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package http
  2. import (
  3. "net/http"
  4. "path/filepath"
  5. "time"
  6. "github.com/gin-gonic/gin"
  7. "gitlink.org.cn/cloudream/common/consts/errorcode"
  8. "gitlink.org.cn/cloudream/common/pkgs/logger"
  9. cdssdk "gitlink.org.cn/cloudream/common/sdks/storage"
  10. )
  11. type StorageService struct {
  12. *Server
  13. }
  14. func (s *Server) Storage() *StorageService {
  15. return &StorageService{
  16. Server: s,
  17. }
  18. }
  19. func (s *StorageService) LoadPackage(ctx *gin.Context) {
  20. log := logger.WithField("HTTP", "Storage.LoadPackage")
  21. var req cdssdk.StorageLoadPackageReq
  22. if err := ctx.ShouldBindJSON(&req); err != nil {
  23. log.Warnf("binding body: %s", err.Error())
  24. ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument"))
  25. return
  26. }
  27. nodeID, taskID, err := s.svc.StorageSvc().StartStorageLoadPackage(req.UserID, req.PackageID, req.StorageID)
  28. if err != nil {
  29. log.Warnf("start storage load package: %s", err.Error())
  30. ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "storage load package failed"))
  31. return
  32. }
  33. for {
  34. complete, ret, err := s.svc.StorageSvc().WaitStorageLoadPackage(nodeID, taskID, time.Second*10)
  35. if complete {
  36. if err != nil {
  37. log.Warnf("loading complete with: %s", err.Error())
  38. ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "storage load package failed"))
  39. return
  40. }
  41. ctx.JSON(http.StatusOK, OK(cdssdk.StorageLoadPackageResp{
  42. FullPath: filepath.Join(ret.RemoteBase, ret.PackagePath),
  43. PackagePath: ret.PackagePath,
  44. LocalBase: ret.LocalBase,
  45. RemoteBase: ret.RemoteBase,
  46. }))
  47. return
  48. }
  49. if err != nil {
  50. log.Warnf("wait loadding: %s", err.Error())
  51. ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "storage load package failed"))
  52. return
  53. }
  54. }
  55. }
  56. func (s *StorageService) CreatePackage(ctx *gin.Context) {
  57. log := logger.WithField("HTTP", "Storage.CreatePackage")
  58. var req cdssdk.StorageCreatePackageReq
  59. if err := ctx.ShouldBindJSON(&req); err != nil {
  60. log.Warnf("binding body: %s", err.Error())
  61. ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument"))
  62. return
  63. }
  64. nodeID, taskID, err := s.svc.StorageSvc().StartStorageCreatePackage(
  65. req.UserID, req.BucketID, req.Name, req.StorageID, req.Path, req.NodeAffinity)
  66. if err != nil {
  67. log.Warnf("start storage create package: %s", err.Error())
  68. ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "storage create package failed"))
  69. return
  70. }
  71. for {
  72. complete, packageID, err := s.svc.StorageSvc().WaitStorageCreatePackage(nodeID, taskID, time.Second*10)
  73. if complete {
  74. if err != nil {
  75. log.Warnf("creating complete with: %s", err.Error())
  76. ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "storage create package failed"))
  77. return
  78. }
  79. ctx.JSON(http.StatusOK, OK(cdssdk.StorageCreatePackageResp{
  80. PackageID: packageID,
  81. }))
  82. return
  83. }
  84. if err != nil {
  85. log.Warnf("wait creating: %s", err.Error())
  86. ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "storage create package failed"))
  87. return
  88. }
  89. }
  90. }
  91. func (s *StorageService) Get(ctx *gin.Context) {
  92. log := logger.WithField("HTTP", "Storage.Get")
  93. var req cdssdk.StorageGet
  94. if err := ctx.ShouldBindQuery(&req); err != nil {
  95. log.Warnf("binding query: %s", err.Error())
  96. ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument"))
  97. return
  98. }
  99. info, err := s.svc.StorageSvc().Get(req.UserID, req.StorageID)
  100. if err != nil {
  101. log.Warnf("getting info: %s", err.Error())
  102. ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "get storage inf failed"))
  103. return
  104. }
  105. ctx.JSON(http.StatusOK, OK(cdssdk.StorageGetResp{
  106. Storage: *info,
  107. }))
  108. }

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