dirbuilder.go 708 Bytes
Newer Older
1 2 3
package io

import (
4 5 6
	mdag "github.com/ipfs/go-ipfs/merkledag"
	format "github.com/ipfs/go-ipfs/unixfs"
	u "github.com/ipfs/go-ipfs/util"
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
)

type directoryBuilder struct {
	dserv   mdag.DAGService
	dirnode *mdag.Node
}

func NewDirectory(dserv mdag.DAGService) *directoryBuilder {
	db := new(directoryBuilder)
	db.dserv = dserv
	db.dirnode = new(mdag.Node)
	db.dirnode.Data = format.FolderPBData()
	return db
}

func (d *directoryBuilder) AddChild(name string, k u.Key) error {
	cnode, err := d.dserv.Get(k)
	if err != nil {
		return err
	}

	err = d.dirnode.AddNodeLinkClean(name, cnode)
	if err != nil {
		return err
	}

	return nil
}

func (d *directoryBuilder) GetNode() *mdag.Node {
	return d.dirnode
}