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.0 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package services
  2. import (
  3. "fmt"
  4. "gitlink.org.cn/cloudream/client/internal/config"
  5. agtcli "gitlink.org.cn/cloudream/rabbitmq/client/agent"
  6. agtmsg "gitlink.org.cn/cloudream/rabbitmq/message/agent"
  7. coormsg "gitlink.org.cn/cloudream/rabbitmq/message/coordinator"
  8. "gitlink.org.cn/cloudream/utils/consts"
  9. "gitlink.org.cn/cloudream/utils/consts/errorcode"
  10. )
  11. type StorageService struct {
  12. *Service
  13. }
  14. func StorageSvc(svc *Service) *StorageService {
  15. return &StorageService{Service: svc}
  16. }
  17. func (svc *StorageService) MoveObjectToStorage(userID int, objectID int, storageID int) error {
  18. // 先向协调端请求文件相关的元数据
  19. preMoveResp, err := svc.coordinator.PreMoveObjectToStorage(coormsg.NewPreMoveObjectToStorageBody(objectID, storageID, userID))
  20. if err != nil {
  21. return fmt.Errorf("request to coordinator failed, err: %w", err)
  22. }
  23. if preMoveResp.ErrorCode != errorcode.OK {
  24. return fmt.Errorf("coordinator PreMoveObjectToStorage failed, code: %s, message: %s", preMoveResp.ErrorCode, preMoveResp.ErrorMessage)
  25. }
  26. // 然后向代理端发送移动文件的请求
  27. agentClient, err := agtcli.NewAgentClient(preMoveResp.Body.NodeID, &config.Cfg().RabbitMQ)
  28. if err != nil {
  29. return fmt.Errorf("create agent client to %d failed, err: %w", preMoveResp.Body.NodeID, err)
  30. }
  31. defer agentClient.Close()
  32. switch preMoveResp.Body.Redundancy {
  33. case consts.REDUNDANCY_REP:
  34. agentMoveResp, err := agentClient.RepMove(agtmsg.NewRepMoveCommandBody(preMoveResp.Body.Directory, preMoveResp.Body.Hashes, objectID, userID, preMoveResp.Body.FileSizeInBytes))
  35. if err != nil {
  36. return fmt.Errorf("request to agent %d failed, err: %w", preMoveResp.Body.NodeID, err)
  37. }
  38. if agentMoveResp.ErrorCode != errorcode.OK {
  39. return fmt.Errorf("agent %d operation failed, code: %s, messsage: %s", preMoveResp.Body.NodeID, agentMoveResp.ErrorCode, agentMoveResp.ErrorMessage)
  40. }
  41. case consts.REDUNDANCY_EC:
  42. agentMoveResp, err := agentClient.ECMove(agtmsg.NewECMoveCommandBody(preMoveResp.Body.Directory, preMoveResp.Body.Hashes, preMoveResp.Body.IDs, *preMoveResp.Body.ECName, objectID, userID, preMoveResp.Body.FileSizeInBytes))
  43. if err != nil {
  44. return fmt.Errorf("request to agent %d failed, err: %w", preMoveResp.Body.NodeID, err)
  45. }
  46. if agentMoveResp.ErrorCode != errorcode.OK {
  47. return fmt.Errorf("agent %d operation failed, code: %s, messsage: %s", preMoveResp.Body.NodeID, agentMoveResp.ErrorCode, agentMoveResp.ErrorMessage)
  48. }
  49. }
  50. moveResp, err := svc.coordinator.MoveObjectToStorage(coormsg.NewMoveObjectToStorageBody(objectID, storageID, userID))
  51. if err != nil {
  52. return fmt.Errorf("request to coordinator failed, err: %w", err)
  53. }
  54. if preMoveResp.ErrorCode != errorcode.OK {
  55. return fmt.Errorf("coordinator MoveObjectToStorage failed, code: %s, message: %s", moveResp.ErrorCode, moveResp.ErrorMessage)
  56. }
  57. return nil
  58. }
  59. func (svc *StorageService) DeleteStorageObject(userID int, objectID int, storageID int) error {
  60. // TODO
  61. panic("not implement yet")
  62. }

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