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.

ipfs.go 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package cmd
  2. import (
  3. "time"
  4. shell "github.com/ipfs/go-ipfs-api"
  5. "gitlink.org.cn/cloudream/agent/internal/config"
  6. "gitlink.org.cn/cloudream/agent/internal/task"
  7. "gitlink.org.cn/cloudream/common/consts"
  8. "gitlink.org.cn/cloudream/common/consts/errorcode"
  9. "gitlink.org.cn/cloudream/common/pkg/logger"
  10. ramsg "gitlink.org.cn/cloudream/rabbitmq/message"
  11. agtmsg "gitlink.org.cn/cloudream/rabbitmq/message/agent"
  12. )
  13. func (svc *Service) CheckIPFS(msg *agtmsg.CheckIPFS) (*agtmsg.CheckIPFSResp, *ramsg.CodeMessage) {
  14. filesMap, err := svc.ipfs.GetPinnedFiles()
  15. if err != nil {
  16. logger.Warnf("get pinned files from ipfs failed, err: %s", err.Error())
  17. return ramsg.ReplyFailed[agtmsg.CheckIPFSResp](errorcode.OPERATION_FAILED, "get pinned files from ipfs failed")
  18. }
  19. // TODO 根据锁定清单过滤被锁定的文件的记录
  20. if msg.IsComplete {
  21. return svc.checkComplete(msg, filesMap)
  22. } else {
  23. return svc.checkIncrement(msg, filesMap)
  24. }
  25. }
  26. func (svc *Service) checkIncrement(msg *agtmsg.CheckIPFS, filesMap map[string]shell.PinInfo) (*agtmsg.CheckIPFSResp, *ramsg.CodeMessage) {
  27. var entries []agtmsg.CheckIPFSRespEntry
  28. for _, cache := range msg.Caches {
  29. _, ok := filesMap[cache.FileHash]
  30. if ok {
  31. if cache.State == consts.CACHE_STATE_PINNED {
  32. // 不处理
  33. } else if cache.State == consts.CACHE_STATE_TEMP {
  34. err := svc.ipfs.Unpin(cache.FileHash)
  35. if err != nil {
  36. logger.WithField("FileHash", cache.FileHash).Warnf("unpin file failed, err: %s", err.Error())
  37. }
  38. }
  39. // 删除map中的记录,表示此记录已被检查过
  40. delete(filesMap, cache.FileHash)
  41. } else {
  42. if cache.State == consts.CACHE_STATE_PINNED {
  43. svc.taskManager.StartComparable(task.NewIPFSPin(cache.FileHash))
  44. } else if cache.State == consts.CACHE_STATE_TEMP {
  45. if time.Since(cache.CacheTime) > time.Duration(config.Cfg().TempFileLifetime)*time.Second {
  46. entries = append(entries, agtmsg.NewCheckIPFSRespEntry(cache.FileHash, agtmsg.CHECK_IPFS_RESP_OP_DELETE_TEMP))
  47. }
  48. }
  49. }
  50. }
  51. // 增量情况下,不需要对filesMap中没检查的记录进行处理
  52. return ramsg.ReplyOK(agtmsg.NewCheckIPFSResp(entries))
  53. }
  54. func (svc *Service) checkComplete(msg *agtmsg.CheckIPFS, filesMap map[string]shell.PinInfo) (*agtmsg.CheckIPFSResp, *ramsg.CodeMessage) {
  55. var entries []agtmsg.CheckIPFSRespEntry
  56. for _, cache := range msg.Caches {
  57. _, ok := filesMap[cache.FileHash]
  58. if ok {
  59. if cache.State == consts.CACHE_STATE_PINNED {
  60. // 不处理
  61. } else if cache.State == consts.CACHE_STATE_TEMP {
  62. err := svc.ipfs.Unpin(cache.FileHash)
  63. if err != nil {
  64. logger.WithField("FileHash", cache.FileHash).Warnf("unpin file failed, err: %s", err.Error())
  65. }
  66. }
  67. // 删除map中的记录,表示此记录已被检查过
  68. delete(filesMap, cache.FileHash)
  69. } else {
  70. if cache.State == consts.CACHE_STATE_PINNED {
  71. svc.taskManager.StartComparable(task.NewIPFSPin(cache.FileHash))
  72. } else if cache.State == consts.CACHE_STATE_TEMP {
  73. if time.Since(cache.CacheTime) > time.Duration(config.Cfg().TempFileLifetime)*time.Second {
  74. entries = append(entries, agtmsg.NewCheckIPFSRespEntry(cache.FileHash, agtmsg.CHECK_IPFS_RESP_OP_DELETE_TEMP))
  75. }
  76. }
  77. }
  78. }
  79. // map中剩下的数据是没有被遍历过,即Cache中没有记录的,那么就Unpin文件,并产生一条Temp记录
  80. for hash := range filesMap {
  81. err := svc.ipfs.Unpin(hash)
  82. if err != nil {
  83. logger.WithField("FileHash", hash).Warnf("unpin file failed, err: %s", err.Error())
  84. }
  85. entries = append(entries, agtmsg.NewCheckIPFSRespEntry(hash, agtmsg.CHECK_IPFS_RESP_OP_CREATE_TEMP))
  86. }
  87. return ramsg.ReplyOK(agtmsg.NewCheckIPFSResp(entries))
  88. }

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