From 950b41a6f09b241ea3cab504d6bedf1a16788b0c Mon Sep 17 00:00:00 2001 From: rht Date: Thu, 12 Nov 2015 21:12:02 +0700 Subject: [PATCH] gateway add: add option for pin License: MIT Signed-off-by: rht --- core/corehttp/gateway_handler.go | 10 +++++++++- core/coreunix/add.go | 6 +++--- test/sharness/t0111-gateway-writeable.sh | 23 +++++++++++++++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index 33b25c763f34..8ba5f7e50067 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -270,7 +270,7 @@ func (i *gatewayHandler) postHandler(w http.ResponseWriter, r *http.Request) { internalWebError(w, err) return } - nd, err = fileAdder.AddDir(f) + nd, err = fileAdder.AddFile(f) } else { nd, err = fileAdder.AddFile(files.NewReaderFile("", "", r.Body, nil)) } @@ -285,6 +285,14 @@ func (i *gatewayHandler) postHandler(w http.ResponseWriter, r *http.Request) { return } + doPin := r.FormValue("pin") + if doPin == "" || doPin == "true" { + if err := fileAdder.PinRoot(); err != nil { + internalWebError(w, err) + return + } + } + i.addUserHeaders(w) // ok, _now_ write user's headers. w.Header().Set("IPFS-Hash", k.String()) http.Redirect(w, r, ipfsPathPrefix+k.String(), http.StatusCreated) diff --git a/core/coreunix/add.go b/core/coreunix/add.go index 165da1b38e63..dfc7b522f48e 100644 --- a/core/coreunix/add.go +++ b/core/coreunix/add.go @@ -216,7 +216,7 @@ func AddWrapped(n *core.IpfsNode, r io.Reader, filename string) (string, *merkle unlock := n.Blockstore.PinLock() defer unlock() - dagnode, err := fileAdder.AddDir(dir) + dagnode, err := fileAdder.addDir(dir) if err != nil { return "", nil, err } @@ -252,7 +252,7 @@ func (params *Adder) AddFile(file files.File) (*merkledag.Node, error) { log.Debugf("%s is hidden, skipping", file.FileName()) return nil, &hiddenFileError{file.FileName()} case file.IsDirectory(): - return params.AddDir(file) + return params.addDir(file) } // case for symlink @@ -291,7 +291,7 @@ func (params *Adder) AddFile(file files.File) (*merkledag.Node, error) { return dagnode, err } -func (params *Adder) AddDir(dir files.File) (*merkledag.Node, error) { +func (params *Adder) addDir(dir files.File) (*merkledag.Node, error) { tree := newDirNode() log.Infof("adding directory: %s", dir.FileName()) diff --git a/test/sharness/t0111-gateway-writeable.sh b/test/sharness/t0111-gateway-writeable.sh index a171fbcb52fa..32862ea302ce 100755 --- a/test/sharness/t0111-gateway-writeable.sh +++ b/test/sharness/t0111-gateway-writeable.sh @@ -39,6 +39,29 @@ test_expect_success "We can HTTP GET file just created" ' test_cmp infile outfile ' +test_expect_success "File is pinned (unpin file should work)" ' + ipfs pin rm -r $HASH +' + +test_expect_success "HTTP POST file with unpin flag gives Hash" ' + echo "$RANDOM" >infile && + URL="http://localhost:$port/ipfs/?pin=false" && + curl -svX POST --data-binary @infile "$URL" 2>curl_post.out && + grep "HTTP/1.1 201 Created" curl_post.out && + LOCATION=$(grep Location curl_post.out) && + HASH=$(echo $LOCATION | cut -d":" -f2- |tr -d " \n\r") +' + +test_expect_success "We can HTTP GET file just created" ' + URL="http://localhost:${port}${HASH}" && + curl -so outfile "$URL" && + test_cmp infile outfile +' + +test_expect_success "File is not pinned (unpin file should fail)" ' + test_must_fail ipfs pin rm -r $HASH +' + test_expect_success "HTTP PUT empty directory" ' URL="http://localhost:$port/ipfs/$HASH_EMPTY_DIR/" && echo "PUT $URL" &&