Skip to content

Commit

Permalink
Fix user login and cp (#15)
Browse files Browse the repository at this point in the history
* 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 a7cfc19
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
43 changes: 43 additions & 0 deletions api/ipfsproxy/ipfsproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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").
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions ipfscluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 28 additions & 1 deletion ipfsconn/ipfshttp/ipfshttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions rpc_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a7cfc19

Please sign in to comment.