Skip to content

Commit

Permalink
Merge pull request #588 from fairDataSociety/staging
Browse files Browse the repository at this point in the history
Staging
  • Loading branch information
asabya authored Feb 26, 2024
2 parents 627a1a5 + 2dfa6b0 commit 656a7ab
Show file tree
Hide file tree
Showing 64 changed files with 6,423 additions and 512 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ The user can share files in his pod with any other user just like in other centr

Pod creation is cheap. A user can create multiple pods and use it to organise his data. for ex: Personal-Pod, Applications-Pod etc.

## (NEW) What is a group?
A group is a shared drive created by a user. It is basically a pod, but on steroids. Group Owner can add members and update permissions. Members with "write" permission can create and store any number of files or directories in a group.

## How to run FairOS-dfs?
Run the following command to download the latest release

Expand Down
8 changes: 5 additions & 3 deletions cmd/common/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type PodReceiveRequest struct {
// FileSystemRequest is the request body for file system operations
type FileSystemRequest struct {
PodName string `json:"podName,omitempty"`
GroupName string `json:"groupName,omitempty"`
DirectoryPath string `json:"dirPath,omitempty"`
DirectoryName string `json:"dirName,omitempty"`
FilePath string `json:"filePath,omitempty"`
Expand All @@ -62,9 +63,10 @@ type FileSystemRequest struct {

// RenameRequest is the request body for file rename
type RenameRequest struct {
PodName string `json:"podName,omitempty"`
OldPath string `json:"oldPath,omitempty"`
NewPath string `json:"newPath,omitempty"`
PodName string `json:"podName,omitempty"`
GroupName string `json:"groupName,omitempty"`
OldPath string `json:"oldPath,omitempty"`
NewPath string `json:"newPath,omitempty"`
}

// FileReceiveRequest is the request body for file receiving
Expand Down
38 changes: 19 additions & 19 deletions cmd/dfs-cli/cmd/fdfs-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ const (

// fdfsClient is the http client for dfs
type fdfsClient struct {
url string
client *http.Client
cookie *http.Cookie
url string
client *http.Client
accessToken string
}

func newFdfsClient(fdfsServer string) (*fdfsClient, error) {
Expand Down Expand Up @@ -97,6 +97,14 @@ func (s *fdfsClient) CheckConnection() bool {
return err == nil
}

func (s *fdfsClient) setAccessToken(token string) {
s.accessToken = token
}

func (s *fdfsClient) getAccessToken() string {
return s.accessToken
}

func (s *fdfsClient) postReq(method, urlPath string, jsonBytes []byte) ([]byte, error) {
// prepare the request
fullUrl := fmt.Sprintf(s.url + urlPath)
Expand All @@ -118,8 +126,8 @@ func (s *fdfsClient) postReq(method, urlPath string, jsonBytes []byte) ([]byte,
}
}

if s.cookie != nil {
req.AddCookie(s.cookie)
if s.getAccessToken() != "" {
req.Header.Add("Authorization", "Bearer "+s.getAccessToken())
}
// execute the request
response, err := s.client.Do(req)
Expand Down Expand Up @@ -150,10 +158,6 @@ func (s *fdfsClient) postReq(method, urlPath string, jsonBytes []byte) ([]byte,
return nil, errors.New(resp.Message)
}

if len(response.Cookies()) > 0 {
s.cookie = response.Cookies()[0]
}

data, err := io.ReadAll(response.Body)
if err != nil {
return nil, errors.New("error downloading data")
Expand Down Expand Up @@ -190,8 +194,8 @@ func (s *fdfsClient) getReq(urlPath, argsString string) ([]byte, error) {
}
}

if s.cookie != nil {
req.AddCookie(s.cookie)
if s.getAccessToken() != "" {
req.Header.Add("Authorization", "Bearer "+s.getAccessToken())
}

// execute the request
Expand Down Expand Up @@ -219,10 +223,6 @@ func (s *fdfsClient) getReq(urlPath, argsString string) ([]byte, error) {
return nil, errors.New(resp.Message)
}

if len(response.Cookies()) > 0 {
s.cookie = response.Cookies()[0]
}

data, err := io.ReadAll(response.Body)
if err != nil {
return nil, errors.New("error downloading data")
Expand Down Expand Up @@ -283,8 +283,8 @@ func (s *fdfsClient) uploadMultipartFile(urlPath, fileName string, fileSize int6
req.Header.Set(api.CompressionHeader, compValue)
}

if s.cookie != nil {
req.AddCookie(s.cookie)
if s.getAccessToken() != "" {
req.Header.Add("Authorization", "Bearer "+s.getAccessToken())
}

// execute the request
Expand Down Expand Up @@ -338,8 +338,8 @@ func (s *fdfsClient) downloadMultipartFile(method, urlPath string, arguments map
req.Header.Add("Content-Type", contentType)
req.Header.Add("Content-Length", strconv.Itoa(len(body.Bytes())))

if s.cookie != nil {
req.AddCookie(s.cookie)
if s.getAccessToken() != "" {
req.Header.Add("Authorization", "Bearer "+s.getAccessToken())
}

// execute the request
Expand Down
10 changes: 9 additions & 1 deletion cmd/dfs-cli/cmd/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,19 @@ func initPrompt() {
prompt.OptionPrefix(currentPrompt),
prompt.OptionLivePrefix(changeLivePrefix),
prompt.OptionTitle("dfs"),
prompt.OptionSetExitCheckerOnInput(exitChecker),
)
p.Run()
}

func exitChecker(in string, breakline bool) bool {
if breakline && strings.TrimSpace(in) == "exit" {
fmt.Println("exiting dfs-cli")
return true
}
return false
}

func changeLivePrefix() (string, bool) {
return currentPrompt, true
}
Expand Down Expand Up @@ -261,7 +270,6 @@ func executor(in string) {
case "help":
help()
case "exit":
os.Exit(0)
case "user":
if len(blocks) < 2 {
log.Println("invalid command.")
Expand Down
12 changes: 12 additions & 0 deletions cmd/dfs-cli/cmd/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ func userNew(userName, mnemonic string) {
fmt.Println("Please store the 12 words mnemonic safely")
fmt.Println("if you loose that, you cannot recover the data in-case of an emergency.")
fmt.Println("you can also use that mnemonic to access the data in-case this device is lost")

fdfsAPI.setAccessToken(resp.AccessToken)

currentUser = userName
}

Expand All @@ -85,8 +88,17 @@ func userLogin(userName, apiEndpoint string) {
fmt.Println("login user: ", err)
return
}
var resp api.UserSignupResponse
err = json.Unmarshal(data, &resp)
if err != nil {
fmt.Println("create user: ", err)
return
}

currentUser = userName
message := strings.ReplaceAll(string(data), "\n", "")
fdfsAPI.setAccessToken(resp.AccessToken)

fmt.Println(message)
}

Expand Down
15 changes: 15 additions & 0 deletions cmd/dfs/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,21 @@ func startHttpService(logger logging.Logger) *http.Server {
podRouter.HandleFunc("/fork", handler.PodForkHandler).Methods("POST")
podRouter.HandleFunc("/fork-from-reference", handler.PodForkFromReferenceHandler).Methods("POST")

groupRouter := baseRouter.PathPrefix("/group/").Subrouter()
groupRouter.Use(handler.LoginMiddleware)
groupRouter.HandleFunc("/new", handler.GroupCreateHandler).Methods("POST")
groupRouter.HandleFunc("/ls", handler.GroupListHandler).Methods("GET")
groupRouter.HandleFunc("/delete", handler.GroupDeleteHandler).Methods("DELETE")
groupRouter.HandleFunc("/delete-shared", handler.GroupDeleteSharedHandler).Methods("DELETE")
groupRouter.HandleFunc("/accept", handler.GroupAcceptInviteHandler).Methods("POST")
groupRouter.HandleFunc("/invite", handler.GroupAddMemberHandler).Methods("POST")
groupRouter.HandleFunc("/remove", handler.GroupRemoveMemberHandler).Methods("POST")
groupRouter.HandleFunc("/update-permission", handler.GroupUpdatePermissionHandler).Methods("POST")
groupRouter.HandleFunc("/close", handler.GroupCloseHandler).Methods("POST")
groupRouter.HandleFunc("/members", handler.GroupGetMembers).Methods("GET")
groupRouter.HandleFunc("/permission", handler.GroupGetPermission).Methods("GET")
groupRouter.HandleFunc("/open", handler.GroupOpenHandler).Methods("POST")

// directory related handlers
dirRouter := baseRouter.PathPrefix("/dir/").Subrouter()
dirRouter.Use(handler.LoginMiddleware)
Expand Down
Loading

0 comments on commit 656a7ab

Please sign in to comment.