Skip to content

Commit

Permalink
gateway add: add option for pin
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: rht <rhtbot@gmail.com>
  • Loading branch information
rht committed Nov 13, 2015
1 parent 02f9a5b commit 950b41a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
10 changes: 9 additions & 1 deletion core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions core/coreunix/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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())

Expand Down
23 changes: 23 additions & 0 deletions test/sharness/t0111-gateway-writeable.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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" &&
Expand Down

0 comments on commit 950b41a

Please sign in to comment.