Skip to content

Commit

Permalink
Make gateway POST handle multipart file & pin flag
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 Jan 15, 2016
1 parent a28e02b commit 39a38ac
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
37 changes: 35 additions & 2 deletions core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (

humanize "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/dustin/go-humanize"
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
"github.com/ipfs/go-ipfs/commands/files"
"github.com/ipfs/go-ipfs/core/coreunix"

key "github.com/ipfs/go-ipfs/blocks/key"
core "github.com/ipfs/go-ipfs/core"
Expand Down Expand Up @@ -287,18 +289,49 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
}

func (i *gatewayHandler) postHandler(w http.ResponseWriter, r *http.Request) {
nd, err := i.newDagFromReader(r.Body)
fileAdder, err := coreunix.NewAdder(i.node.Context(), i.node, nil)
if err != nil {
internalWebError(w, err)
return
}

k, err := i.node.DAG.Add(nd)
contentType := r.Header.Get("Content-Type")
if contentType == "multipart/form-data" || contentType == "multipart/mixed" {
f := &files.MultipartFile{Mediatype: contentType}
f.Reader, err = r.MultipartReader()
if err != nil {
internalWebError(w, err)
return
}
err = fileAdder.AddFile(f)
} else {
err = fileAdder.AddFile(files.NewReaderFile("", "", r.Body, nil))
}
if err != nil {
internalWebError(w, err)
return
}

nd, err := fileAdder.Finalize()
if err != nil {
internalWebError(w, err)
return
}

k, err := nd.Key()
if err != nil {
internalWebError(w, err)
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
39 changes: 39 additions & 0 deletions test/sharness/t0111-gateway-writeable.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,45 @@ 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 flag pin=false 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 POST multiple files gives Hash" '
echo "hapax" >infile1 &&
echo "dis" >infile2 &&
URL="http://localhost:$port/ipfs" &&
curl --form file1=@infile1 --form file2=@infile2 "$URL" &&
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 files just created" '
URL="http://localhost:${port}${HASH}" &&
curl -O "$URL" &&
test_cmp infile outfile
'

test_expect_success "HTTP PUT empty directory" '
URL="http://localhost:$port/ipfs/$HASH_EMPTY_DIR/" &&
echo "PUT $URL" &&
Expand Down

0 comments on commit 39a38ac

Please sign in to comment.