Skip to content

Commit

Permalink
Merge pull request #602 from fairDataSociety/groups.1
Browse files Browse the repository at this point in the history
rest and wasm apis for groups
  • Loading branch information
asabya authored Feb 21, 2024
2 parents 5c1ca4b + 0615bc1 commit 2dfa6b0
Show file tree
Hide file tree
Showing 23 changed files with 997 additions and 262 deletions.
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
147 changes: 147 additions & 0 deletions cmd/dfs/cmd/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,153 @@ func TestApis(t *testing.T) {
if !assert.Equal(t, perm.Permission, acl.PermissionWrite) {
t.Fatal("permission should be write")
}

entries := []struct {
path string
isDir bool
size int64
content []byte
}{
{
path: "/dir1",
isDir: true,
},
{
path: "/dir2",
isDir: true,
},
{
path: "/dir3",
isDir: true,
},
{
path: "/file1",
size: 1024 * 1024,
},
{
path: "/dir1/file11",
size: 1024 * 512,
},
{
path: "/dir1/file12",
size: 1024 * 1024,
},
{
path: "/dir3/file31",
size: 1024 * 1024,
},
{
path: "/dir3/file32",
size: 1024 * 1024,
},
{
path: "/dir3/file33",
size: 1024,
},
{
path: "/dir2/dir4",
isDir: true,
},
{
path: "/dir2/dir4/dir5",
isDir: true,
},
{
path: "/dir2/dir4/file241",
size: 5 * 1024 * 1024,
},
{
path: "/dir2/dir4/dir5/file2451",
size: 10 * 1024 * 1024,
},
}

for _, v := range entries {
if v.isDir {
mkdirRqst := common.FileSystemRequest{
GroupName: groupRequest.GroupName,
DirectoryPath: v.path,
}
mkDirBytes, err := json.Marshal(mkdirRqst)
if err != nil {
t.Fatal(err)
}
mkDirHttpReq, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s%s", basev1, string(common.DirMkdir)), bytes.NewBuffer(mkDirBytes))
if err != nil {
t.Fatal(err)

}
mkDirHttpReq.Header.Set("Cookie", cookie[0])
mkDirHttpReq.Header.Add("Content-Type", "application/json")
mkDirHttpReq.Header.Add("Content-Length", strconv.Itoa(len(mkDirBytes)))
mkDirResp, err := c.Do(mkDirHttpReq)
if err != nil {
t.Fatal(err)
}
err = mkDirResp.Body.Close()
if err != nil {
t.Fatal(err)
}
if mkDirResp.StatusCode != 201 {
t.Fatal("mkdir failed")
}
} else {
body := new(bytes.Buffer)
writer := multipart.NewWriter(body)
contentLength := fmt.Sprintf("%d", v.size)

err = writer.WriteField("groupName", groupRequest.GroupName)
if err != nil {
t.Fatal(err)
}
err = writer.WriteField("contentLength", contentLength)
if err != nil {
t.Fatal(err)
}
err = writer.WriteField("dirPath", filepath.Dir(v.path))
if err != nil {
t.Fatal(err)
}
err = writer.WriteField("blockSize", "1Mb")
if err != nil {
t.Fatal(err)
}
part, err := writer.CreateFormFile("files", filepath.Base(v.path))
if err != nil {
t.Fatal(err)
}
reader := &io.LimitedReader{R: rand.Reader, N: v.size}
_, err = io.Copy(part, reader)
if err != nil {
t.Fatal(err)
}

err = writer.Close()
if err != nil {
t.Fatal(err)
}

uploadReq, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s%s", basev1, string(common.FileUpload)), body)
if err != nil {
t.Fatal(err)

}
uploadReq.Header.Set("Cookie", cookie[0])
contentType := fmt.Sprintf("multipart/form-data;boundary=%v", writer.Boundary())
uploadReq.Header.Add("Content-Type", contentType)
uploadResp, err := c.Do(uploadReq)
if err != nil {
t.Fatal(err)
}
err = uploadResp.Body.Close()
if err != nil {
t.Fatal(err)
}
if uploadResp.StatusCode != 200 {
t.Fatal("upload failed")
}
}
}
})

t.Run("ws test", func(t *testing.T) {
Expand Down
22 changes: 11 additions & 11 deletions gomobile/dfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func PodReceiveInfo(podSharingReference string) (string, error) {
}

func DirPresent(podName, dirPath string) (string, error) {
present, err := api.IsDirPresent(podName, dirPath, sessionId)
present, err := api.IsDirPresent(podName, dirPath, sessionId, false)
if err != nil {
return "", err
}
Expand All @@ -216,23 +216,23 @@ func DirPresent(podName, dirPath string) (string, error) {
}

func DirMake(podName, dirPath string) (string, error) {
err := api.Mkdir(podName, dirPath, sessionId, 0)
err := api.Mkdir(podName, dirPath, sessionId, 0, false)
if err != nil {
return "", err
}
return string("directory created successfully"), nil
}

func DirRemove(podName, dirPath string) (string, error) {
err := api.RmDir(podName, dirPath, sessionId)
err := api.RmDir(podName, dirPath, sessionId, false)
if err != nil {
return "", err
}
return string("directory removed successfully"), nil
}

func DirList(podName, dirPath string) (string, error) {
dirs, files, err := api.ListDir(podName, dirPath, sessionId)
dirs, files, err := api.ListDir(podName, dirPath, sessionId, false)
if err != nil {
return "", err
}
Expand All @@ -252,7 +252,7 @@ func DirList(podName, dirPath string) (string, error) {
}

func DirStat(podName, dirPath string) (string, error) {
stat, err := api.DirectoryStat(podName, dirPath, sessionId)
stat, err := api.DirectoryStat(podName, dirPath, sessionId, false)
if err != nil {
return "", err
}
Expand All @@ -261,7 +261,7 @@ func DirStat(podName, dirPath string) (string, error) {
}

func FileShare(podName, dirPath, destinationUser string) (string, error) {
ref, err := api.ShareFile(podName, dirPath, destinationUser, sessionId)
ref, err := api.ShareFile(podName, dirPath, destinationUser, sessionId, false)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -292,11 +292,11 @@ func FileReceiveInfo(podName, fileSharingReference string) (string, error) {
}

func FileDelete(podName, filePath string) error {
return api.DeleteFile(podName, filePath, sessionId)
return api.DeleteFile(podName, filePath, sessionId, false)
}

func FileStat(podName, filePath string) (string, error) {
stat, err := api.FileStat(podName, filePath, sessionId)
stat, err := api.FileStat(podName, filePath, sessionId, false)
if err != nil {
return "", err
}
Expand All @@ -319,16 +319,16 @@ func FileUpload(podName, filePath, dirPath, compression, blockSize string, overw
if err != nil {
return err
}
return api.UploadFile(podName, fileInfo.Name(), sessionId, fileInfo.Size(), f, dirPath, compression, uint32(bs), 0, overwrite)
return api.UploadFile(podName, fileInfo.Name(), sessionId, fileInfo.Size(), f, dirPath, compression, uint32(bs), 0, overwrite, false)
}

func BlobUpload(data []byte, podName, fileName, dirPath, compression string, size, blockSize int64, overwrite bool) error {
r := bytes.NewReader(data)
return api.UploadFile(podName, fileName, sessionId, size, r, dirPath, compression, uint32(blockSize), 0, overwrite)
return api.UploadFile(podName, fileName, sessionId, size, r, dirPath, compression, uint32(blockSize), 0, overwrite, false)
}

func FileDownload(podName, filePath string) ([]byte, error) {
r, _, err := api.DownloadFile(podName, filePath, sessionId)
r, _, err := api.DownloadFile(podName, filePath, sessionId, false)
if err != nil {
return nil, err
}
Expand Down
23 changes: 14 additions & 9 deletions pkg/api/dir_chmod.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import (

// DirModeRequest is used for changing dir mode
type DirModeRequest struct {
PodName string `json:"podName,omitempty"`
DirPath string `json:"dirPath,omitempty"`
Mode string `json:"mode,omitempty"`
PodName string `json:"podName,omitempty"`
GroupName string `json:"groupName,omitempty"`
DirPath string `json:"dirPath,omitempty"`
Mode string `json:"mode,omitempty"`
}

// DirectoryModeHandler godoc
Expand Down Expand Up @@ -49,11 +50,15 @@ func (h *Handler) DirectoryModeHandler(w http.ResponseWriter, r *http.Request) {
return
}

podName := chmodReq.PodName
if podName == "" {
h.logger.Errorf("dir chmod: \"podName\" argument missing")
jsonhttp.BadRequest(w, &response{Message: "dir chmod: \"podName\" argument missing"})
return
driveName, isGroup := chmodReq.GroupName, true
if driveName == "" {
driveName = chmodReq.PodName
isGroup = false
if driveName == "" {
h.logger.Errorf("dir chmod: \"podName\" argument missing")
jsonhttp.BadRequest(w, &response{Message: "dir chmod: \"podName\" argument missing"})
return
}
}

dirPath := chmodReq.DirPath
Expand Down Expand Up @@ -90,7 +95,7 @@ func (h *Handler) DirectoryModeHandler(w http.ResponseWriter, r *http.Request) {
return
}

err = h.dfsAPI.ChmodDir(podName, dirPath, sessionId, uint32(mode))
err = h.dfsAPI.ChmodDir(driveName, dirPath, sessionId, uint32(mode), isGroup)
if err != nil {
h.logger.Errorf("dir chmod: %v", err)
jsonhttp.BadRequest(w, &response{Message: err.Error()})
Expand Down
33 changes: 23 additions & 10 deletions pkg/api/dir_ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package api

import (
"errors"
"net/http"

"github.com/fairdatasociety/fairOS-dfs/pkg/auth"
Expand Down Expand Up @@ -48,13 +49,25 @@ type ListFileResponse struct {
// @Failure 500 {object} response
// @Router /v1/dir/ls [get]
func (h *Handler) DirectoryLsHandler(w http.ResponseWriter, r *http.Request) {
keys, ok := r.URL.Query()["podName"]
if !ok || len(keys[0]) < 1 {
h.logger.Errorf("ls: \"podName\" argument missing")
jsonhttp.BadRequest(w, &response{Message: "ls: \"podName\" argument missing"})
return
driveName, isGroup := "", false
keys, ok := r.URL.Query()["groupName"]
if ok || (len(keys) == 1 && len(keys[0]) > 0) {
driveName = keys[0]
isGroup = true
} else {
keys, ok = r.URL.Query()["podName"]
if !ok || len(keys[0]) < 1 {
h.logger.Errorf("ls: \"podName\" argument missing")
jsonhttp.BadRequest(w, &response{Message: "ls: \"podName\" argument missing"})
return
}
driveName = keys[0]
if driveName == "" {
h.logger.Errorf("ls: \"podName\" argument missing")
jsonhttp.BadRequest(w, &response{Message: "ls: \"podName\" argument missing"})
return
}
}
podName := keys[0]

keys, ok = r.URL.Query()["dirPath"]
if !ok || len(keys[0]) < 1 {
Expand All @@ -78,15 +91,15 @@ func (h *Handler) DirectoryLsHandler(w http.ResponseWriter, r *http.Request) {
}

// list directory
dEntries, fEntries, err := h.dfsAPI.ListDir(podName, directory, sessionId)
dEntries, fEntries, err := h.dfsAPI.ListDir(driveName, directory, sessionId, isGroup)
if err != nil {
if err == dfs.ErrPodNotOpen || err == dfs.ErrUserNotLoggedIn ||
err == p.ErrPodNotOpened {
if errors.Is(err, dfs.ErrPodNotOpen) || errors.Is(err, dfs.ErrUserNotLoggedIn) ||
errors.Is(err, p.ErrPodNotOpened) {
h.logger.Errorf("ls: %v", err)
jsonhttp.BadRequest(w, &response{Message: "ls: " + err.Error()})
return
}
if err == dir.ErrDirectoryNotPresent {
if errors.Is(err, dir.ErrDirectoryNotPresent) {
h.logger.Errorf("ls: %v", err)
jsonhttp.NotFound(w, &response{Message: "ls: " + err.Error()})
return
Expand Down
Loading

0 comments on commit 2dfa6b0

Please sign in to comment.