github.com/zignig/go-ipfs@v0.0.0-20141111235910-c9e5fdf55a52/cmd/ipfs2/ipfsHandler.go (about)

     1  package main
     2  
     3  import (
     4  	"io"
     5  	"net/http"
     6  
     7  	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
     8  	mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
     9  
    10  	core "github.com/jbenet/go-ipfs/core"
    11  	"github.com/jbenet/go-ipfs/importer"
    12  	dag "github.com/jbenet/go-ipfs/merkledag"
    13  	"github.com/jbenet/go-ipfs/routing"
    14  	uio "github.com/jbenet/go-ipfs/unixfs/io"
    15  	u "github.com/jbenet/go-ipfs/util"
    16  )
    17  
    18  type ipfs interface {
    19  	ResolvePath(string) (*dag.Node, error)
    20  	NewDagFromReader(io.Reader) (*dag.Node, error)
    21  	AddNodeToDAG(nd *dag.Node) (u.Key, error)
    22  	NewDagReader(nd *dag.Node) (io.Reader, error)
    23  }
    24  
    25  type ipfsHandler struct {
    26  	node *core.IpfsNode
    27  }
    28  
    29  func (i *ipfsHandler) ResolvePath(path string) (*dag.Node, error) {
    30  	return i.node.Resolver.ResolvePath(path)
    31  }
    32  
    33  func (i *ipfsHandler) NewDagFromReader(r io.Reader) (*dag.Node, error) {
    34  	return importer.NewDagFromReader(r)
    35  }
    36  
    37  func (i *ipfsHandler) AddNodeToDAG(nd *dag.Node) (u.Key, error) {
    38  	return i.node.DAG.Add(nd)
    39  }
    40  
    41  func (i *ipfsHandler) NewDagReader(nd *dag.Node) (io.Reader, error) {
    42  	return uio.NewDagReader(nd, i.node.DAG)
    43  }
    44  
    45  func (i *ipfsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    46  	path := r.URL.Path[5:]
    47  
    48  	nd, err := i.ResolvePath(path)
    49  	if err != nil {
    50  		if err == routing.ErrNotFound {
    51  			w.WriteHeader(http.StatusNotFound)
    52  		} else if err == context.DeadlineExceeded {
    53  			w.WriteHeader(http.StatusRequestTimeout)
    54  		} else {
    55  			w.WriteHeader(http.StatusBadRequest)
    56  		}
    57  
    58  		log.Error(err)
    59  		w.Write([]byte(err.Error()))
    60  		return
    61  	}
    62  
    63  	dr, err := i.NewDagReader(nd)
    64  	if err != nil {
    65  		// TODO: return json object containing the tree data if it's a directory (err == ErrIsDir)
    66  		w.WriteHeader(http.StatusInternalServerError)
    67  		log.Error(err)
    68  		w.Write([]byte(err.Error()))
    69  		return
    70  	}
    71  
    72  	io.Copy(w, dr)
    73  }
    74  
    75  func (i *ipfsHandler) postHandler(w http.ResponseWriter, r *http.Request) {
    76  	nd, err := i.NewDagFromReader(r.Body)
    77  	if err != nil {
    78  		w.WriteHeader(http.StatusInternalServerError)
    79  		log.Error(err)
    80  		w.Write([]byte(err.Error()))
    81  		return
    82  	}
    83  
    84  	k, err := i.AddNodeToDAG(nd)
    85  	if err != nil {
    86  		w.WriteHeader(http.StatusInternalServerError)
    87  		log.Error(err)
    88  		w.Write([]byte(err.Error()))
    89  		return
    90  	}
    91  
    92  	//TODO: return json representation of list instead
    93  	w.WriteHeader(http.StatusCreated)
    94  	w.Write([]byte(mh.Multihash(k).B58String()))
    95  }