Skip to content
Permalink
Browse files

Fix user login and cp (#15)

* fix files/cp bug.

Signed-off-by: Yi Wang <wangyi@storswift.com>

* add uid/login API.

Signed-off-by: Yi Wang <wangyi@storswift.com>
  • Loading branch information...
yeewang authored and coder-lb committed Mar 20, 2019
1 parent 390b912 commit a7cfc19921e33f1d6529b523c8bf5ba310de4a83
Showing with 79 additions and 1 deletion.
  1. +43 −0 api/ipfsproxy/ipfsproxy.go
  2. +2 −0 ipfscluster.go
  3. +28 −1 ipfsconn/ipfshttp/ipfshttp.go
  4. +6 −0 rpc_api.go
@@ -230,6 +230,10 @@ func New(cfg *Config) (*Server, error) {
Path("/uid/info").
HandlerFunc(proxy.uidInfoHandler).
Name("UidInfo")
hijackSubrouter.
Path("/uid/login").
HandlerFunc(proxy.uidLoginHandler).
Name("UidLogin")

hijackSubrouter.
Path("/file/add").
@@ -766,6 +770,45 @@ func (proxy *Server) uidInfoHandler(w http.ResponseWriter, r *http.Request) {
return
}

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

q := r.URL.Query()

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

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

err := proxy.uidSpawn(uid)
if err != nil {
ipfsErrorResponder(w, err.Error())
return
}

err = proxy.rpcClient.Call(
"",
"Cluster",
"UidLogin",
[]string{uid, hash},
&struct{}{},
)
if err != nil {
ipfsErrorResponder(w, err.Error())
return
}

w.WriteHeader(http.StatusOK)
return
}

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

@@ -93,6 +93,8 @@ type IPFSConnector interface {
UidRenew([]string) (api.UIDRenew, error)
// UidInfo get uid Name and Id
UidInfo(uid string) (api.UIDSecret, error)
// UidLogin login server and create home directory
UidLogin([]string) error
// FileGet downloads file from ipfs service
FileGet(fg []string) ([]byte, error)
// FilesCp is used to copy file
@@ -783,6 +783,33 @@ func (ipfs *Connector) UidInfo(uid string) (api.UIDSecret, error) {
return secret, nil
}

// log in Hive cluster to recreate user home
func (ipfs *Connector) UidLogin(params []string) error {
ctx, cancel := context.WithTimeout(ipfs.ctx, ipfs.config.IPFSRequestTimeout)
defer cancel()

uid := params[0]
hash := params[1]
if !strings.HasPrefix(hash, "/ipfs/") {
hash = "/ipfs/" + hash
}

url := "files/rm?arg=/nodes/" + uid + "&recursive=true&force=true"
_, err := ipfs.postCtx(ctx, url, "", nil)
if err != nil {
logger.Error(err)
}

url = "files/cp?arg=" + hash + "&arg=" + "/nodes/" + uid
_, err = ipfs.postCtx(ctx, url, "", nil)
if err != nil {
logger.Error(err)
return hiveError(err, uid)
}

return nil
}

// get file from IPFS service
func (ipfs *Connector) FileGet(fg []string) ([]byte, error) {
ctx, cancel := context.WithTimeout(ipfs.ctx, ipfs.config.IPFSRequestTimeout)
@@ -816,7 +843,7 @@ func (ipfs *Connector) FileGet(fg []string) ([]byte, error) {
func (ipfs *Connector) FilesCp(l []string) error {
ctx, cancel := context.WithTimeout(ipfs.ctx, ipfs.config.IPFSRequestTimeout)
defer cancel()
url := "files/cp?arg=" + l[1] + "&arg=" + filepath.Join("/nodes/", l[0], l[1])
url := "files/cp?arg=" + l[1] + "&arg=" + filepath.Join("/nodes/", l[0], l[2])
_, err := ipfs.postCtx(ctx, url, "", nil)
if err != nil {
logger.Error(err)
@@ -394,6 +394,12 @@ func (rpcapi *RPCAPI) UidInfo(ctx context.Context, in string, out *api.UIDSecret
return err
}

// UidLogin runs IPFSConnector.UidLogin().
func (rpcapi *RPCAPI) UidLogin(ctx context.Context, in []string, out *struct{}) error {
err := rpcapi.c.ipfs.UidLogin(in)
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)

0 comments on commit a7cfc19

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