Skip to content
Permalink
Browse files

update file/get API; (#8)

add file/cat API.
Signed-off-by: Yi Wang <wangyi@storswift.com>
  • Loading branch information...
yeewang authored and stiartsly committed Jan 21, 2019
1 parent 9506f54 commit 0581a90b0ece7858198b6fc82a369cd61b65e407
Showing with 87 additions and 2 deletions.
  1. +1 −1 .gx/lastpubver
  2. +72 −0 api/ipfsproxy/ipfsproxy.go
  3. +7 −1 package.json
  4. +7 −0 rpc_api.go
@@ -1 +1 @@
0.8.0: QmfDMnXBd6ChZziataTVWszPFKo1Yr4AwMYWPHXKBfCUAe
0.1.1-hive-alpha: QmS9RCPUNGAVXYobwPJQwT8YQe2QDijr4xZBNHwLpmRtYK
@@ -6,11 +6,13 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net"
"net/http"
"net/http/httputil"
"net/url"
"os"
"strconv"
"strings"
"sync"
@@ -28,6 +30,7 @@ import (
madns "github.com/multiformats/go-multiaddr-dns"
manet "github.com/multiformats/go-multiaddr-net"
uuid "github.com/satori/go.uuid"
tar "github.com/whyrusleeping/tar-utils"
)

// DNSTimeout is used when resolving DNS multiaddresses in this module
@@ -236,6 +239,10 @@ func New(cfg *Config) (*Server, error) {
Path("/file/get").
HandlerFunc(proxy.fileGetHandler).
Name("FileGet")
hijackSubrouter.
Path("/file/cat").
HandlerFunc(proxy.fileCatHandler).
Name("FileCat")

hijackSubrouter.
Path("/files/cp").
@@ -768,6 +775,71 @@ func (proxy *Server) fileGetHandler(w http.ResponseWriter, r *http.Request) {
return
}

func (proxy *Server) fileCatHandler(w http.ResponseWriter, r *http.Request) {
proxy.setHeaders(w.Header(), r)

var FileGet []byte

q := r.URL.Query()

arg := q.Get("arg")
if arg == "" {
ipfsErrorResponder(w, "error reading request: "+r.URL.String())
return
}

output := q.Get("output")
archive := q.Get("archive")
compress := q.Get("compress")
compressionLevel := q.Get("compression-level")

err := proxy.rpcClient.Call(
"",
"Cluster",
"IPFSFileGet",
[]string{arg, output, archive, compress, compressionLevel},
&FileGet,
)
if err != nil {
ipfsErrorResponder(w, err.Error())
return
}

// create io.Reader
rspbuf := bytes.NewReader(FileGet)

// create path
fpath, err := ioutil.TempDir("", "ipfsget")
if err != nil {
ipfsErrorResponder(w, err.Error())
return
}
defer os.RemoveAll(fpath)

// extract
extractor := &tar.Extractor{Path: fpath}
extractor.Extract(rspbuf)

// read files from the path
files, err := ioutil.ReadDir(fpath)
if err != nil {
ipfsErrorResponder(w, err.Error())
return
}

w.WriteHeader(http.StatusOK)
for _, file := range files {
buf, err := ioutil.ReadFile(fpath + "/" + file.Name())
if err != nil {
ipfsErrorResponder(w, err.Error())
return
}
w.Write(buf)
}

return
}

func (proxy *Server) filesCpHandler(w http.ResponseWriter, r *http.Request) {
proxy.setHeaders(w.Header(), r)

@@ -161,13 +161,19 @@
"hash": "QmNNk4iczWp8Q4R1mXQ2mrrjQvWisYqMqbW1an8qGbJZsM",
"name": "cors",
"version": "1.6.0"
},
{
"author": "whyrusleeping",
"hash": "QmQine7gvHncNevKtG9QXxf3nXcwSj6aDDmMm52mHofEEp",
"name": "tar-utils",
"version": "0.0.3"
}
],
"gxVersion": "0.11.0",
"language": "go",
"license": "MIT",
"name": "ipfs-cluster",
"releaseCmd": "git commit -S -a -m \"gx publish $VERSION\"",
"version": "0.8.0"
"version": "0.1.1-hive-alpha"
}

@@ -374,6 +374,13 @@ func (rpcapi *RPCAPI) UidInfo(ctx context.Context, in string, out *api.UIDSecret
return err
}

// IPFSFileGet runs IPFSConnector.IPFSFileGet().
func (rpcapi *RPCAPI) IPFSFileGet(ctx context.Context, in []string, out *[]byte) error {
res, err := rpcapi.c.ipfs.FileGet(in)
*out = res
return err
}

// FilesCp runs IPFSConnector.FilesCp().
func (rpcapi *RPCAPI) IPFSFilesCp(ctx context.Context, in []string, out *struct{}) error {
err := rpcapi.c.ipfs.FilesCp(in)

0 comments on commit 0581a90

Please sign in to comment.
You can’t perform that action at this time.