Browse Source

Merge pull request '增加ClientPool,解除对config的依赖' (#5) from feature_gxh into master

pull/7/head
baohan 2 years ago
parent
commit
e31751b532
3 changed files with 48 additions and 14 deletions
  1. +0
    -0
      pkgs/ipfs/config.go
  2. +14
    -14
      pkgs/ipfs/ipfs.go
  3. +34
    -0
      pkgs/ipfs/pool.go

utils/ipfs/config.go → pkgs/ipfs/config.go View File


utils/ipfs/ipfs.go → pkgs/ipfs/ipfs.go View File

@@ -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的连接状态

}

+ 34
- 0
pkgs/ipfs/pool.go View File

@@ -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) {
}

Loading…
Cancel
Save