Skip to content

Commit

Permalink
assets: seed gateway index assets on 'ipfs init'
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Henry <cryptix@riseup.net>
  • Loading branch information
cryptix committed Jul 3, 2015
1 parent 358d04c commit 5ff020b
Show file tree
Hide file tree
Showing 6 changed files with 496 additions and 7 deletions.
29 changes: 22 additions & 7 deletions assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,54 @@ var initDocPaths = []string{
"init-doc/quick-start",
}

// SeedInitDocs adds the list of embedded init documentation to the passed node, pins it and returns the root key
func SeedInitDocs(nd *core.IpfsNode) (*key.Key, error) {
return addAssetList(nd, initDocPaths)
}

var initGwAssets = []string{
"gw-assets/icons.css",
"gw-assets/bootstrap.min.css",
}

// SeedGatewayAssets adds the list of embedded gateway inidex assets to the passed node, pins it and returns the root key
func SeedGatewayAssets(nd *core.IpfsNode) (*key.Key, error) {
return addAssetList(nd, initGwAssets)
}

func addAssetList(nd *core.IpfsNode, l []string) (*key.Key, error) {
dirb := uio.NewDirectory(nd.DAG)

for _, p := range initDocPaths {
for _, p := range l {
d, err := Asset(p)
if err != nil {
return nil, fmt.Errorf("assets.AddDocuDir: could load Asset '%s': %s", p, err)
return nil, fmt.Errorf("assets: could load Asset '%s': %s", p, err)
}

s, err := coreunix.Add(nd, bytes.NewBuffer(d))
if err != nil {
return nil, fmt.Errorf("assets.AddDocuDir: could not Add '%s': %s", p, err)
return nil, fmt.Errorf("assets: could not Add '%s': %s", p, err)
}

fname := filepath.Base(p)
k := key.B58KeyDecode(s)
if err := dirb.AddChild(fname, k); err != nil {
return nil, fmt.Errorf("assets.AddDocuDir: could not add '%s' as a child: %s", fname, err)
return nil, fmt.Errorf("assets: could not add '%s' as a child: %s", fname, err)
}
}

dir := dirb.GetNode()
dkey, err := nd.DAG.Add(dir)
if err != nil {
return nil, fmt.Errorf("assets.AddDocuDir: DAG.Add(dir) failed: %s", err)
return nil, fmt.Errorf("assets: DAG.Add(dir) failed: %s", err)
}

if err := nd.Pinning.Pin(nd.Context(), dir, true); err != nil {
return nil, fmt.Errorf("assets.AddDocuDir: Pinning on init-docu failed: %s", err)
return nil, fmt.Errorf("assets: Pinning on init-docu failed: %s", err)
}

if err := nd.Pinning.Flush(); err != nil {
return nil, fmt.Errorf("assets.AddDocuDir: Pinnig flush failed: %s", err)
return nil, fmt.Errorf("assets: Pinnig flush failed: %s", err)
}

return &dkey, nil
Expand Down
31 changes: 31 additions & 0 deletions assets/assets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,34 @@ func TestEmbeddedDocs(t *testing.T) {
}
wg.Wait()
}

func TestGatewayAssets(t *testing.T) {
const wantCnt = 2
if len(initGwAssets) < wantCnt {
t.Fatalf("expected %d assets. got %d", wantCnt, len(initDocPaths))
}

for _, f := range initGwAssets {
// load data from filesystem (git)
vcsData, err := ioutil.ReadFile(f)
if err != nil {
t.Errorf("asset %s: could not read vcs file: %s", f, err)
return
}

// load data from emdedded source
embdData, err := Asset(f)
if err != nil {
t.Errorf("asset %s: could not read vcs file: %s", f, err)
return
}

if !bytes.Equal(vcsData, embdData) {
t.Errorf("asset %s: vcs and embedded data isnt equal", f)
return
}

t.Logf("checked %s", f)
}

}
48 changes: 48 additions & 0 deletions assets/bindata.go

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions assets/gw-assets/bootstrap.min.css

Large diffs are not rendered by default.

Loading

0 comments on commit 5ff020b

Please sign in to comment.