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.

space_syncer.go 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package http
  2. import (
  3. "net/http"
  4. "github.com/gin-gonic/gin"
  5. "gitlink.org.cn/cloudream/common/pkgs/logger"
  6. "gitlink.org.cn/cloudream/jcs-pub/client/internal/http/types"
  7. cliapi "gitlink.org.cn/cloudream/jcs-pub/client/sdk/api/v1"
  8. clitypes "gitlink.org.cn/cloudream/jcs-pub/client/types"
  9. "gitlink.org.cn/cloudream/jcs-pub/common/ecode"
  10. )
  11. type SpaceSyncerService struct {
  12. *Server
  13. }
  14. func (s *Server) SpaceSyncer() *SpaceSyncerService {
  15. return &SpaceSyncerService{s}
  16. }
  17. func (s *SpaceSyncerService) CreateTask(ctx *gin.Context) {
  18. log := logger.WithField("HTTP", "SpaceSyncer.CreateTask")
  19. req, err := types.ShouldBindJSONEx[cliapi.SpaceSyncerCreateTask](ctx)
  20. if err != nil {
  21. log.Warnf("binding body: %s", err.Error())
  22. ctx.JSON(http.StatusBadRequest, types.Failed(ecode.BadArgument, "missing argument or invalid argument"))
  23. return
  24. }
  25. if len(req.DestPathes) != len(req.DestUserSpaceIDs) {
  26. log.Warnf("destPathes and destUserSpaceIDs should have the same length")
  27. ctx.JSON(http.StatusBadRequest, types.Failed(ecode.BadArgument, "destPathes and destUserSpaceIDs should have the same length"))
  28. return
  29. }
  30. if len(req.DestPathes) == 0 {
  31. log.Warnf("must have at least one dest")
  32. ctx.JSON(http.StatusBadRequest, types.Failed(ecode.BadArgument, "must have at least one dest"))
  33. return
  34. }
  35. dests := make([]clitypes.SpaceSyncDest, 0, len(req.DestUserSpaceIDs))
  36. for _, id := range req.DestUserSpaceIDs {
  37. dests = append(dests, clitypes.SpaceSyncDest{
  38. DestUserSpaceID: clitypes.UserSpaceID(id),
  39. DestPath: clitypes.PathFromJcsPathString(req.DestPathes[0]),
  40. })
  41. }
  42. info, err := s.svc.SpaceSyncer.CreateTask(clitypes.SpaceSyncTask{
  43. Trigger: req.Trigger,
  44. Mode: req.Mode,
  45. Filters: req.Filters,
  46. Options: req.Options,
  47. SrcUserSpaceID: req.SrcUserSpaceID,
  48. SrcPath: clitypes.PathFromJcsPathString(req.SrcPath),
  49. Dests: dests,
  50. })
  51. if err != nil {
  52. log.Warnf("start task: %s", err.Error())
  53. ctx.JSON(http.StatusInternalServerError, types.Failed(ecode.OperationFailed, "start task: %v", err))
  54. return
  55. }
  56. ctx.JSON(http.StatusOK, types.OK(cliapi.SpaceSyncerCreateTaskResp{
  57. Task: info.Task,
  58. }))
  59. }
  60. func (s *SpaceSyncerService) CancelTask(ctx *gin.Context) {
  61. log := logger.WithField("HTTP", "SpaceSyncer.CancelTask")
  62. var req cliapi.SpaceSyncerCancelTask
  63. if err := ctx.ShouldBindJSON(&req); err != nil {
  64. log.Warnf("binding body: %s", err.Error())
  65. ctx.JSON(http.StatusBadRequest, types.Failed(ecode.BadArgument, "missing argument or invalid argument"))
  66. return
  67. }
  68. s.svc.SpaceSyncer.CancelTask(req.TaskID)
  69. ctx.JSON(http.StatusOK, types.OK(cliapi.SpaceSyncerCancelTaskResp{}))
  70. }
  71. func (s *SpaceSyncerService) GetTask(ctx *gin.Context) {
  72. log := logger.WithField("HTTP", "SpaceSyncer.GetTask")
  73. var req cliapi.SpaceSyncerGetTask
  74. if err := ctx.ShouldBindQuery(&req); err != nil {
  75. log.Warnf("binding query: %s", err.Error())
  76. ctx.JSON(http.StatusBadRequest, types.Failed(ecode.BadArgument, "missing argument or invalid argument"))
  77. return
  78. }
  79. task := s.svc.SpaceSyncer.GetTask(req.TaskID)
  80. if task == nil {
  81. ctx.JSON(http.StatusOK, types.Failed(ecode.DataNotFound, "task not found"))
  82. return
  83. }
  84. ctx.JSON(http.StatusOK, types.OK(cliapi.SpaceSyncerGetTaskResp{Task: *task}))
  85. }

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