Fix #1362: Add "size" to response when adding CARs with a unixfs file root.

This sets the "size" value to the FileSize() value when the CAR file's root
is a unixfs file. (Folders are not files, size will still be 0).
This commit is contained in:
Hector Sanjuan 2021-06-28 22:37:32 +02:00
parent d37df4fd95
commit b6238df481

View File

@ -10,6 +10,7 @@ import (
"mime/multipart" "mime/multipart"
"strings" "strings"
"github.com/ipfs/go-unixfs"
"github.com/ipfs/ipfs-cluster/adder/ipfsadd" "github.com/ipfs/ipfs-cluster/adder/ipfsadd"
"github.com/ipfs/ipfs-cluster/api" "github.com/ipfs/ipfs-cluster/api"
"github.com/ipld/go-car" "github.com/ipld/go-car"
@ -282,6 +283,7 @@ func (ca *carAdder) Add(name string, fn files.Node) (cid.Cid, error) {
root := carReader.Header.Roots[0] root := carReader.Header.Roots[0]
bytes := uint64(0) bytes := uint64(0)
size := uint64(0)
for { for {
block, err := carReader.Next() block, err := carReader.Next()
@ -298,6 +300,15 @@ func (ca *carAdder) Add(name string, fn files.Node) (cid.Cid, error) {
return cid.Undef, err return cid.Undef, err
} }
// If the root is in the CAR and the root is a UnixFS
// node, then set the size in the output object.
if nd.Cid().Equals(root) {
ufs, err := unixfs.ExtractFSNode(nd)
if err == nil {
size = ufs.FileSize()
}
}
err = ca.dgs.Add(ca.ctx, nd) err = ca.dgs.Add(ca.ctx, nd)
if err != nil { if err != nil {
return cid.Undef, err return cid.Undef, err
@ -308,7 +319,7 @@ func (ca *carAdder) Add(name string, fn files.Node) (cid.Cid, error) {
Name: name, Name: name,
Cid: root, Cid: root,
Bytes: bytes, Bytes: bytes,
Size: 0, Size: size,
} }
return root, nil return root, nil