Reading file by hash

files
go-ipfs
#1

Hi all. I am trying to read file by hash but getting unknown format instead of file data.

So first ipfs node has created file using mfs and wrote there normal data

....
         nrd := dag.NodeWithData(ft.FilePBData(nil, 0))

	prfx := dag.V0CidPrefix()

	nrd.SetPrefix(&prfx)

	err := directory.AddChild(fname, nrd)

	if err != nil {
		return err
	}

	fNode, err := directory.Child(fname)

	if err != nil {
		return err
	}

	file, ok := fNode.(*mfs.File)

	if !ok {
		return errors.New("File not found or has not been created")
	}

	wfd, err := file.Open(mfs.OpenReadWrite, true)

	if err != nil {
		return err
	}

	defer wfd.Close()

	wfd.Seek(0, 0)

	wfd.Truncate(0)

	wfd.Write(data)

Than I am get cid and send to another ipfs node.

But when second node trying to read data , gets unknown format.

Here is code.

node, err := core.Node.IpfsNode.DAG.Get(ctx, cID)

	if err != nil {
		return &api.FileResponse{}, err
	}

	data := node.RawData()

	f, err := os.OpenFile(output, os.O_CREATE|os.O_RDWR, 0644)

	if err != nil {
		return &api.FileResponse{}, errors.New(fmt.Sprintf("Error  : %v", err))
	}

	dt, err := ft.FromBytes(data)

	if err != nil {
		return &api.FileResponse{}, errors.New(fmt.Sprintf("Error  : %v", err))
	}

	defer f.Close()

_, err = f.Write(dt.Data)

	if err != nil {
		return &api.FileResponse{}, errors.New(fmt.Sprintf("Error  : %v", err))
	}

	return &api.FileResponse{}, nil

Is there any way to read file . Or I need to unmarshal this data before reading ?

#2

I am not familiar with Go that much, but can you confirm that you are getting back some sort of data in the 2nd part of the code?