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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.OperationFailed, "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.CacheStatePinned {
  32. // 不处理
  33. } else if cache.State == consts.CacheStateTemp {
  34. logger.WithField("FileHash", cache.FileHash).Debugf("unpin for cache entry state is temp")
  35. err := svc.ipfs.Unpin(cache.FileHash)
  36. if err != nil {
  37. logger.WithField("FileHash", cache.FileHash).Warnf("unpin file failed, err: %s", err.Error())
  38. }
  39. }
  40. // 删除map中的记录,表示此记录已被检查过
  41. delete(filesMap, cache.FileHash)
  42. } else {
  43. if cache.State == consts.CacheStatePinned {
  44. svc.taskManager.StartComparable(task.NewIPFSPin(cache.FileHash))
  45. } else if cache.State == consts.CacheStateTemp {
  46. if time.Since(cache.CacheTime) > time.Duration(config.Cfg().TempFileLifetime)*time.Second {
  47. entries = append(entries, agtmsg.NewCheckIPFSRespEntry(cache.FileHash, agtmsg.CHECK_IPFS_RESP_OP_DELETE_TEMP))
  48. }
  49. }
  50. }
  51. }
  52. // 增量情况下,不需要对filesMap中没检查的记录进行处理
  53. return ramsg.ReplyOK(agtmsg.NewCheckIPFSResp(entries))
  54. }
  55. func (svc *Service) checkComplete(msg *agtmsg.CheckIPFS, filesMap map[string]shell.PinInfo) (*agtmsg.CheckIPFSResp, *ramsg.CodeMessage) {
  56. var entries []agtmsg.CheckIPFSRespEntry
  57. for _, cache := range msg.Caches {
  58. _, ok := filesMap[cache.FileHash]
  59. if ok {
  60. if cache.State == consts.CacheStatePinned {
  61. // 不处理
  62. } else if cache.State == consts.CacheStateTemp {
  63. logger.WithField("FileHash", cache.FileHash).Debugf("unpin for cache entry state is temp")
  64. err := svc.ipfs.Unpin(cache.FileHash)
  65. if err != nil {
  66. logger.WithField("FileHash", cache.FileHash).Warnf("unpin file failed, err: %s", err.Error())
  67. }
  68. }
  69. // 删除map中的记录,表示此记录已被检查过
  70. delete(filesMap, cache.FileHash)
  71. } else {
  72. if cache.State == consts.CacheStatePinned {
  73. svc.taskManager.StartComparable(task.NewIPFSPin(cache.FileHash))
  74. } else if cache.State == consts.CacheStateTemp {
  75. if time.Since(cache.CacheTime) > time.Duration(config.Cfg().TempFileLifetime)*time.Second {
  76. entries = append(entries, agtmsg.NewCheckIPFSRespEntry(cache.FileHash, agtmsg.CHECK_IPFS_RESP_OP_DELETE_TEMP))
  77. }
  78. }
  79. }
  80. }
  81. // map中剩下的数据是没有被遍历过,即Cache中没有记录的,那么就Unpin文件,并产生一条Temp记录
  82. for hash := range filesMap {
  83. logger.WithField("FileHash", hash).Debugf("unpin for no cacah entry")
  84. err := svc.ipfs.Unpin(hash)
  85. if err != nil {
  86. logger.WithField("FileHash", hash).Warnf("unpin file failed, err: %s", err.Error())
  87. }
  88. entries = append(entries, agtmsg.NewCheckIPFSRespEntry(hash, agtmsg.CHECK_IPFS_RESP_OP_CREATE_TEMP))
  89. }
  90. return ramsg.ReplyOK(agtmsg.NewCheckIPFSResp(entries))
  91. }

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