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.

command_service.go 3.0 kB

2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package main
  2. import (
  3. "io"
  4. "os"
  5. "path/filepath"
  6. log "github.com/sirupsen/logrus"
  7. "gitlink.org.cn/cloudream/agent/internal/config"
  8. "gitlink.org.cn/cloudream/utils"
  9. coorcli "gitlink.org.cn/cloudream/rabbitmq/client/coordinator"
  10. ramsg "gitlink.org.cn/cloudream/rabbitmq/message"
  11. agtmsg "gitlink.org.cn/cloudream/rabbitmq/message/agent"
  12. coormsg "gitlink.org.cn/cloudream/rabbitmq/message/coordinator"
  13. "gitlink.org.cn/cloudream/utils/consts/errorcode"
  14. myio "gitlink.org.cn/cloudream/utils/io"
  15. "gitlink.org.cn/cloudream/utils/ipfs"
  16. )
  17. type CommandService struct {
  18. ipfs *ipfs.IPFS
  19. }
  20. func NewCommandService(ipfs *ipfs.IPFS) *CommandService {
  21. return &CommandService{
  22. ipfs: ipfs,
  23. }
  24. }
  25. func (service *CommandService) RepMove(msg *agtmsg.RepMoveCommand) *agtmsg.AgentMoveResp {
  26. outFileName := utils.MakeMoveOperationFileName(msg.Body.ObjectID, msg.Body.UserID)
  27. outFileDir := filepath.Join(config.Cfg().StorageBaseDir, msg.Body.Directory)
  28. outFilePath := filepath.Join(outFileDir, outFileName)
  29. err := os.MkdirAll(outFileDir, 0644)
  30. if err != nil {
  31. log.Warnf("create file directory %s failed, err: %s", outFileDir, err.Error())
  32. return ramsg.ReplyFailed[agtmsg.AgentMoveResp](errorcode.OPERATION_FAILED, "create local file directory failed")
  33. }
  34. outFile, err := os.Create(outFilePath)
  35. if err != nil {
  36. log.Warnf("create file %s failed, err: %s", outFilePath, err.Error())
  37. return ramsg.ReplyFailed[agtmsg.AgentMoveResp](errorcode.OPERATION_FAILED, "create local file failed")
  38. }
  39. defer outFile.Close()
  40. hashs := msg.Body.Hashs
  41. fileHash := hashs[0]
  42. ipfsRd, err := service.ipfs.OpenRead(fileHash)
  43. if err != nil {
  44. log.Warnf("read ipfs file %s failed, err: %s", fileHash, err.Error())
  45. return ramsg.ReplyFailed[agtmsg.AgentMoveResp](errorcode.OPERATION_FAILED, "read ipfs file failed")
  46. }
  47. defer ipfsRd.Close()
  48. buf := make([]byte, 1024)
  49. for {
  50. readCnt, err := ipfsRd.Read(buf)
  51. if readCnt > 0 {
  52. err = myio.WriteAll(outFile, buf[:readCnt])
  53. if err != nil {
  54. log.Warnf("write data to file %s failed, err: %s", outFilePath, err.Error())
  55. return ramsg.ReplyFailed[agtmsg.AgentMoveResp](errorcode.OPERATION_FAILED, "write data to file failed")
  56. }
  57. }
  58. // 文件读取完毕
  59. if err == io.EOF {
  60. break
  61. }
  62. if err != nil {
  63. log.Warnf("read ipfs file %s data failed, err: %s", fileHash, err.Error())
  64. return ramsg.ReplyFailed[agtmsg.AgentMoveResp](errorcode.OPERATION_FAILED, "read ipfs file data failed")
  65. }
  66. }
  67. //向coor报告临时缓存hash
  68. coorClient, err := coorcli.NewCoordinatorClient(&config.Cfg().RabbitMQ)
  69. if err != nil {
  70. log.Warnf("new coordinator client failed, err: %s", err.Error())
  71. return ramsg.ReplyFailed[agtmsg.AgentMoveResp](errorcode.OPERATION_FAILED, "new coordinator client failed")
  72. }
  73. defer coorClient.Close()
  74. // TODO 这里更新失败残留下的文件是否要删除?
  75. coorClient.TempCacheReport(coormsg.NewTempCacheReportBody(config.Cfg().ID, hashs))
  76. return ramsg.ReplyOK(agtmsg.NewAgentMoveRespBody())
  77. }

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