lotus/node/client/import.go

92 lines
1.9 KiB
Go
Raw Normal View History

2019-07-12 09:59:18 +00:00
package client
import (
"context"
2019-07-16 16:02:51 +00:00
"errors"
2019-07-16 16:07:08 +00:00
"os"
2019-07-12 10:44:01 +00:00
"github.com/filecoin-project/go-lotus/api"
2019-07-15 14:14:54 +00:00
"github.com/ipfs/go-filestore"
2019-07-12 09:59:18 +00:00
"go.uber.org/fx"
"github.com/ipfs/go-cid"
chunker "github.com/ipfs/go-ipfs-chunker"
files "github.com/ipfs/go-ipfs-files"
ipld "github.com/ipfs/go-ipld-format"
"github.com/ipfs/go-unixfs/importer/balanced"
ihelper "github.com/ipfs/go-unixfs/importer/helpers"
)
type LocalStorage struct {
fx.In
2019-07-15 14:14:54 +00:00
LocalDAG ipld.DAGService
2019-07-16 16:02:51 +00:00
Filestore *filestore.Filestore `optional:"true"`
2019-07-12 09:59:18 +00:00
}
2019-07-12 10:44:01 +00:00
func (s *LocalStorage) ClientImport(ctx context.Context, path string) (cid.Cid, error) {
2019-07-12 09:59:18 +00:00
f, err := os.Open(path)
if err != nil {
return cid.Undef, err
}
stat, err := f.Stat()
if err != nil {
return cid.Undef, err
}
file, err := files.NewReaderPathFile(path, f, stat)
if err != nil {
return cid.Undef, err
}
bufferedDS := ipld.NewBufferedDAG(ctx, s.LocalDAG)
params := ihelper.DagBuilderParams{
Maxlinks: ihelper.DefaultLinksPerBlock,
RawLeaves: true,
CidBuilder: nil,
2019-07-12 10:17:44 +00:00
Dagserv: bufferedDS,
NoCopy: true,
2019-07-12 09:59:18 +00:00
}
db, err := params.New(chunker.DefaultSplitter(file))
if err != nil {
return cid.Undef, err
}
nd, err := balanced.Layout(db)
if err != nil {
return cid.Undef, err
}
return nd.Cid(), bufferedDS.Commit()
}
2019-07-12 10:44:01 +00:00
func (s *LocalStorage) ClientListImports(ctx context.Context) ([]api.Import, error) {
2019-07-16 16:02:51 +00:00
if s.Filestore == nil {
return nil, errors.New("listing imports is not supported with in-memory dag yet")
}
2019-07-12 10:44:01 +00:00
next, err := filestore.ListAll(s.Filestore, false)
if err != nil {
return nil, err
}
// TODO: make this less very bad by tracking root cids instead of using ListAll
out := make([]api.Import, 0)
for {
r := next()
if r == nil {
return out, nil
}
if r.Offset != 0 {
continue
}
out = append(out, api.Import{
Status: r.Status,
Key: r.Key,
FilePath: r.FilePath,
Size: r.Size,
})
}
2019-07-15 14:14:54 +00:00
}