diff --git a/utils/ipfs/config.go b/pkgs/ipfs/config.go similarity index 100% rename from utils/ipfs/config.go rename to pkgs/ipfs/config.go diff --git a/utils/ipfs/ipfs.go b/pkgs/ipfs/ipfs.go similarity index 70% rename from utils/ipfs/ipfs.go rename to pkgs/ipfs/ipfs.go index a2fb292..142b09a 100644 --- a/utils/ipfs/ipfs.go +++ b/pkgs/ipfs/ipfs.go @@ -9,11 +9,11 @@ import ( myio "gitlink.org.cn/cloudream/common/utils/io" ) -type IPFS struct { +type Client struct { shell *shell.Shell } -func NewIPFS(cfg *Config) (*IPFS, error) { +func NewClient(cfg *Config) (*Client, error) { ipfsAddr := fmt.Sprintf("localhost:%d", cfg.Port) sh := shell.NewShell(ipfsAddr) @@ -22,16 +22,16 @@ func NewIPFS(cfg *Config) (*IPFS, error) { return nil, fmt.Errorf("cannot connect to %s", ipfsAddr) } - return &IPFS{ + return &Client{ shell: sh, }, nil } -func (fs *IPFS) IsUp() bool { +func (fs *Client) IsUp() bool { return fs.shell.IsUp() } -func (fs *IPFS) CreateFile() (myio.PromiseWriteCloser[string], error) { +func (fs *Client) CreateFileStream() (myio.PromiseWriteCloser[string], error) { pr, pw := io.Pipe() ipfsWriter := ipfsWriter{ @@ -50,23 +50,27 @@ func (fs *IPFS) CreateFile() (myio.PromiseWriteCloser[string], error) { return &ipfsWriter, nil } -func (fs *IPFS) OpenRead(hash string) (io.ReadCloser, error) { +func (fs *Client) CreateFile(file io.Reader) (string, error) { + return fs.shell.Add(file) +} + +func (fs *Client) OpenRead(hash string) (io.ReadCloser, error) { return fs.shell.Cat(hash) } -func (fs *IPFS) Pin(hash string) error { +func (fs *Client) Pin(hash string) error { return fs.shell.Pin(hash) } -func (fs *IPFS) Unpin(hash string) error { +func (fs *Client) Unpin(hash string) error { return fs.shell.Unpin(hash) } -func (fs *IPFS) GetPinnedFiles() (map[string]shell.PinInfo, error) { +func (fs *Client) GetPinnedFiles() (map[string]shell.PinInfo, error) { return fs.shell.PinsOfType(context.Background(), shell.RecursivePin) } -func (fs *IPFS) List(hash string) ([]*shell.LsLink, error) { +func (fs *Client) List(hash string) ([]*shell.LsLink, error) { return fs.shell.List(hash) } @@ -94,7 +98,3 @@ func (w *ipfsWriter) Finish() (string, error) { return w.fileHash, w.finishErr } - -func IPFSRemoteDeamonDetector() { //探测本地IPFS Deamon与目的地IPFS Deamon的连接状态 - -} diff --git a/pkgs/ipfs/pool.go b/pkgs/ipfs/pool.go new file mode 100644 index 0000000..7c9f4e5 --- /dev/null +++ b/pkgs/ipfs/pool.go @@ -0,0 +1,34 @@ +package ipfs + +type PoolClient struct { + *Client + owner *Pool +} + +func (c *PoolClient) Close() { + c.owner.Release(c) +} + +type Pool struct { + cfg *Config +} + +func NewPool(cfg *Config) *Pool { + return &Pool{ + cfg: cfg, + } +} +func (p *Pool) Acquire() (*PoolClient, error) { + cli, err := NewClient(p.cfg) + if err != nil { + return nil, err + } + + return &PoolClient{ + Client: cli, + owner: p, + }, nil +} + +func (p *Pool) Release(cli *PoolClient) { +}