Skip to content

Commit

Permalink
Drop some coreunix code
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <[email protected]>
  • Loading branch information
magik6k committed Jan 22, 2019
1 parent 74bf836 commit b2e76ab
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 231 deletions.
48 changes: 21 additions & 27 deletions assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
package assets

import (
"bytes"
"fmt"
"os"
"path/filepath"

"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/coreunix"
uio "gx/ipfs/QmQXze9tG878pa4Euya4rrDpyTNX3kQe4dhCaBzBozGgpe/go-unixfs/io"
"github.com/ipfs/go-ipfs/core/coreapi"
"github.com/ipfs/go-ipfs/core/coreapi/interface"
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"

cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
files "gx/ipfs/QmXWZCd8jfaHmt4UDSnjKmGcrQMw95bDGWqEeVLVJjoANX/go-ipfs-files"

// this import keeps gx from thinking the dep isn't used
_ "gx/ipfs/QmT1jwrqzSMjSjLG5oBd9w4P9vXPKQksWuf5ghsE3Q88ZV/dir-index-html"
Expand Down Expand Up @@ -45,48 +47,40 @@ func SeedInitDirIndex(nd *core.IpfsNode) (cid.Cid, error) {
}

func addAssetList(nd *core.IpfsNode, l []string) (cid.Cid, error) {
dirb := uio.NewDirectory(nd.DAG)
api, err := coreapi.NewCoreAPI(nd)
if err != nil {
return cid.Cid{}, err
}

dirb, err := api.Object().New(nd.Context(), options.Object.Type("unixfs-dir"))
if err != nil {
return cid.Cid{}, err
}

basePath := iface.IpfsPath(dirb.Cid())

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

s, err := coreunix.Add(nd, bytes.NewBuffer(d))
fp, err := api.Unixfs().Add(nd.Context(), files.NewBytesFile(d))
if err != nil {
return cid.Cid{}, fmt.Errorf("assets: could not Add '%s': %s", p, err)
return cid.Cid{}, err
}

fname := filepath.Base(p)

c, err := cid.Decode(s)
basePath, err = api.Object().AddLink(nd.Context(), basePath, fname, fp)
if err != nil {
return cid.Cid{}, err
}

node, err := nd.DAG.Get(nd.Context(), c)
if err != nil {
return cid.Cid{}, err
}

if err := dirb.AddChild(nd.Context(), fname, node); err != nil {
return cid.Cid{}, fmt.Errorf("assets: could not add '%s' as a child: %s", fname, err)
}
}

dir, err := dirb.GetNode()
if err != nil {
if err := api.Pin().Add(nd.Context(), basePath); err != nil {
return cid.Cid{}, err
}

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

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

return dir.Cid(), nil
return basePath.Cid(), nil
}
23 changes: 21 additions & 2 deletions cmd/ipfswatch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
commands "github.com/ipfs/go-ipfs/commands"
core "github.com/ipfs/go-ipfs/core"
corehttp "github.com/ipfs/go-ipfs/core/corehttp"
coreunix "github.com/ipfs/go-ipfs/core/coreunix"
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"

process "gx/ipfs/QmSF8fPo3jgVBAy8fpdjjYqgG87dkJgUprRBHRd2tmfgpP/goprocess"
files "gx/ipfs/QmXWZCd8jfaHmt4UDSnjKmGcrQMw95bDGWqEeVLVJjoANX/go-ipfs-files"
config "gx/ipfs/QmcRKBUqc2p3L1ZraoJjbXfs9E6xzvEuyK9iypb5RGwfsr/go-ipfs-config"
homedir "gx/ipfs/QmdcULN1WCzgoQmcCaUAmEhwcxHYsDrbZ2LvRJKCL8dMrK/go-homedir"
fsnotify "gx/ipfs/QmfNjggF4Pt6erqg3NDafD3MdvDHk1qqCVr8pL5hnPucS8/fsnotify"
Expand Down Expand Up @@ -83,6 +83,11 @@ func run(ipfsPath, watchPath string) error {
}
defer node.Close()

api, err := coreapi.NewCoreAPI(node)
if err != nil {
return err
}

if *http {
addr := "/ip4/127.0.0.1/tcp/5001"
var opts = []corehttp.ServeOption{
Expand Down Expand Up @@ -130,9 +135,23 @@ func run(ipfsPath, watchPath string) error {
file, err := os.Open(e.Name)
if err != nil {
log.Println(err)
return
}
defer file.Close()
k, err := coreunix.Add(node, file)

st, err := file.Stat()
if err != nil {
log.Println(err)
return
}

f, err := files.NewReaderPathFile(e.Name, file, st)
if err != nil {
log.Println(err)
return
}

k, err := api.Unixfs().Add(node.Context(), f)
if err != nil {
log.Println(err)
}
Expand Down
Loading

0 comments on commit b2e76ab

Please sign in to comment.