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 2.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package ops
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "gitlink.org.cn/cloudream/common/pkgs/future"
  7. "gitlink.org.cn/cloudream/common/pkgs/ipfs"
  8. "gitlink.org.cn/cloudream/common/pkgs/logger"
  9. "gitlink.org.cn/cloudream/common/utils/io2"
  10. stgglb "gitlink.org.cn/cloudream/storage/common/globals"
  11. "gitlink.org.cn/cloudream/storage/common/pkgs/ioswitch"
  12. )
  13. type IPFSRead struct {
  14. Output ioswitch.StreamID `json:"output"`
  15. FileHash string `json:"fileHash"`
  16. Option ipfs.ReadOption `json:"option"`
  17. }
  18. func (o *IPFSRead) Execute(sw *ioswitch.Switch, planID ioswitch.PlanID) error {
  19. logger.
  20. WithField("FileHash", o.FileHash).
  21. WithField("Output", o.Output).
  22. Debugf("ipfs read op")
  23. defer logger.Debugf("ipfs read op finished")
  24. ipfsCli, err := stgglb.IPFSPool.Acquire()
  25. if err != nil {
  26. return fmt.Errorf("new ipfs client: %w", err)
  27. }
  28. defer stgglb.IPFSPool.Release(ipfsCli)
  29. file, err := ipfsCli.OpenRead(o.FileHash, o.Option)
  30. if err != nil {
  31. return fmt.Errorf("reading ipfs: %w", err)
  32. }
  33. fut := future.NewSetVoid()
  34. file = io2.AfterReadClosedOnce(file, func(closer io.ReadCloser) {
  35. fut.SetVoid()
  36. })
  37. sw.StreamReady(planID, ioswitch.NewStream(o.Output, file))
  38. // TODO context
  39. fut.Wait(context.TODO())
  40. return nil
  41. }
  42. type IPFSWrite struct {
  43. Input ioswitch.StreamID `json:"input"`
  44. ResultKey string `json:"resultKey"`
  45. }
  46. func (o *IPFSWrite) Execute(sw *ioswitch.Switch, planID ioswitch.PlanID) error {
  47. logger.
  48. WithField("ResultKey", o.ResultKey).
  49. WithField("Input", o.Input).
  50. Debugf("ipfs write op")
  51. ipfsCli, err := stgglb.IPFSPool.Acquire()
  52. if err != nil {
  53. return fmt.Errorf("new ipfs client: %w", err)
  54. }
  55. defer stgglb.IPFSPool.Release(ipfsCli)
  56. strs, err := sw.WaitStreams(planID, o.Input)
  57. if err != nil {
  58. return err
  59. }
  60. defer strs[0].Stream.Close()
  61. fileHash, err := ipfsCli.CreateFile(strs[0].Stream)
  62. if err != nil {
  63. return fmt.Errorf("creating ipfs file: %w", err)
  64. }
  65. if o.ResultKey != "" {
  66. sw.AddResultValue(planID, ioswitch.ResultKV{
  67. Key: o.ResultKey,
  68. Value: fileHash,
  69. })
  70. }
  71. return nil
  72. }
  73. func init() {
  74. OpUnion.AddT((*IPFSRead)(nil))
  75. OpUnion.AddT((*IPFSWrite)(nil))
  76. }

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