feat(be/files): enable shareID for sharing APIs

This commit is contained in:
hexxa 2022-01-15 14:33:52 +08:00 committed by Hexxa
parent 772ec7992c
commit a378296980
5 changed files with 217 additions and 85 deletions

View file

@ -190,6 +190,7 @@ func (cl *FilesClient) IsSharing(dirpath string) (*http.Response, string, []erro
End() End()
} }
// Deprecated: use ListSharingIDs intead
func (cl *FilesClient) ListSharings() (*http.Response, *fileshdr.SharingResp, []error) { func (cl *FilesClient) ListSharings() (*http.Response, *fileshdr.SharingResp, []error) {
resp, body, errs := cl.r.Get(cl.url("/v1/fs/sharings")). resp, body, errs := cl.r.Get(cl.url("/v1/fs/sharings")).
AddCookie(cl.token). AddCookie(cl.token).
@ -206,6 +207,22 @@ func (cl *FilesClient) ListSharings() (*http.Response, *fileshdr.SharingResp, []
return resp, shResp, nil return resp, shResp, nil
} }
func (cl *FilesClient) ListSharingIDs() (*http.Response, *fileshdr.SharingIDsResp, []error) {
resp, body, errs := cl.r.Get(cl.url("/v1/fs/sharings/ids")).
AddCookie(cl.token).
End()
if len(errs) > 0 {
return nil, nil, errs
}
shResp := &fileshdr.SharingIDsResp{}
err := json.Unmarshal([]byte(body), shResp)
if err != nil {
return nil, nil, append(errs, err)
}
return resp, shResp, nil
}
func (cl *FilesClient) GenerateHash(filepath string) (*http.Response, string, []error) { func (cl *FilesClient) GenerateHash(filepath string) (*http.Response, string, []error) {
return cl.r.Post(cl.url("/v1/fs/hashes/sha1")). return cl.r.Post(cl.url("/v1/fs/hashes/sha1")).
AddCookie(cl.token). AddCookie(cl.token).
@ -214,3 +231,20 @@ func (cl *FilesClient) GenerateHash(filepath string) (*http.Response, string, []
}). }).
End() End()
} }
func (cl *FilesClient) GetSharingDir(shareID string) (*http.Response, string, []error) {
resp, body, errs := cl.r.Get(cl.url("/v1/fs/sharings/dirs")).
AddCookie(cl.token).
Param(fileshdr.ShareIDQuery, shareID).
End()
if len(errs) > 0 {
return nil, "", errs
}
sdResp := &fileshdr.GetSharingDirResp{}
err := json.Unmarshal([]byte(body), sdResp)
if err != nil {
return nil, "", append(errs, err)
}
return resp, sdResp.SharingDir, nil
}

View file

@ -28,6 +28,7 @@ var (
// queries // queries
FilePathQuery = "fp" FilePathQuery = "fp"
ListDirQuery = "dp" ListDirQuery = "dp"
ShareIDQuery = "sh"
// headers // headers
rangeHeader = "Range" rangeHeader = "Range"
@ -113,8 +114,8 @@ func (h *FileHandlers) canAccess(userName, role, op, sharedPath string) bool {
return false return false
} }
_, ok := h.deps.FileInfos().GetSharing(sharedPath) isSharing, ok := h.deps.FileInfos().GetSharing(sharedPath)
return ok return isSharing && ok
} }
type CreateReq struct { type CreateReq struct {
@ -535,7 +536,8 @@ func (h *FileHandlers) Download(c *gin.Context) {
} }
role := c.MustGet(q.RoleParam).(string) role := c.MustGet(q.RoleParam).(string)
userName := c.MustGet(q.UserParam).(string) userName := c.MustGet(q.UserParam).(string)
if !h.canAccess(userName, role, "download", filePath) { dirPath := filepath.Dir(filePath)
if !h.canAccess(userName, role, "download", dirPath) {
c.JSON(q.ErrResp(c, 403, q.ErrAccessDenied)) c.JSON(q.ErrResp(c, 403, q.ErrAccessDenied))
return return
} }
@ -895,6 +897,7 @@ type SharingResp struct {
SharingDirs []string `json:"sharingDirs"` SharingDirs []string `json:"sharingDirs"`
} }
// Deprecated: use ListSharingIDs instead
func (h *FileHandlers) ListSharings(c *gin.Context) { func (h *FileHandlers) ListSharings(c *gin.Context) {
// TODO: move canAccess to authedFS // TODO: move canAccess to authedFS
userName := c.MustGet(q.UserParam).(string) userName := c.MustGet(q.UserParam).(string)
@ -912,6 +915,22 @@ func (h *FileHandlers) ListSharings(c *gin.Context) {
c.JSON(200, &SharingResp{SharingDirs: dirs}) c.JSON(200, &SharingResp{SharingDirs: dirs})
} }
type SharingIDsResp struct {
IDs map[string]string `json:"IDs"`
}
func (h *FileHandlers) ListSharingIDs(c *gin.Context) {
// TODO: move canAccess to authedFS
userName := c.MustGet(q.UserParam).(string)
dirToID, err := h.deps.FileInfos().ListSharings(q.FsRootPath(userName, "/"))
if err != nil {
c.JSON(q.ErrResp(c, 500, err))
return
}
c.JSON(200, &SharingIDsResp{IDs: dirToID})
}
type GenerateHashReq struct { type GenerateHashReq struct {
FilePath string `json:"filePath"` FilePath string `json:"filePath"`
} }
@ -952,6 +971,25 @@ func (h *FileHandlers) GenerateHash(c *gin.Context) {
c.JSON(q.Resp(200)) c.JSON(q.Resp(200))
} }
type GetSharingDirResp struct {
SharingDir string `json:"sharingDir"`
}
func (h *FileHandlers) GetSharingDir(c *gin.Context) {
shareID := c.Query(ShareIDQuery)
if shareID == "" {
c.JSON(q.ErrResp(c, 400, errors.New("invalid share ID")))
return
}
dirPath, err := h.deps.FileInfos().GetSharingDir(shareID)
if err != nil {
c.JSON(q.ErrResp(c, 500, err))
return
}
c.JSON(200, &GetSharingDirResp{SharingDir: dirPath})
}
func (h *FileHandlers) GetStreamReader(userID uint64, fd io.Reader) (io.ReadCloser, error) { func (h *FileHandlers) GetStreamReader(userID uint64, fd io.Reader) (io.ReadCloser, error) {
pr, pw := io.Pipe() pr, pw := io.Pipe()
chunkSize := 100 * 1024 // notice: it can not be greater than limiter's token count chunkSize := 100 * 1024 // notice: it can not be greater than limiter's token count

View file

@ -73,6 +73,8 @@ func NewMultiUsersSvc(cfg gocfg.ICfg, deps *depidx.Deps) (*MultiUsersSvc, error)
apiRuleCname(userstore.AdminRole, "DELETE", "/v1/fs/sharings"): true, apiRuleCname(userstore.AdminRole, "DELETE", "/v1/fs/sharings"): true,
apiRuleCname(userstore.AdminRole, "GET", "/v1/fs/sharings"): true, apiRuleCname(userstore.AdminRole, "GET", "/v1/fs/sharings"): true,
apiRuleCname(userstore.AdminRole, "GET", "/v1/fs/sharings/exist"): true, apiRuleCname(userstore.AdminRole, "GET", "/v1/fs/sharings/exist"): true,
apiRuleCname(userstore.AdminRole, "GET", "/v1/fs/sharings/dirs"): true,
apiRuleCname(userstore.AdminRole, "GET", "/v1/fs/sharings/ids"): true,
apiRuleCname(userstore.AdminRole, "POST", "/v1/fs/hashes/sha1"): true, apiRuleCname(userstore.AdminRole, "POST", "/v1/fs/hashes/sha1"): true,
// user rules // user rules
apiRuleCname(userstore.UserRole, "GET", "/"): true, apiRuleCname(userstore.UserRole, "GET", "/"): true,
@ -105,7 +107,9 @@ func NewMultiUsersSvc(cfg gocfg.ICfg, deps *depidx.Deps) (*MultiUsersSvc, error)
apiRuleCname(userstore.UserRole, "DELETE", "/v1/fs/sharings"): true, apiRuleCname(userstore.UserRole, "DELETE", "/v1/fs/sharings"): true,
apiRuleCname(userstore.UserRole, "GET", "/v1/fs/sharings"): true, apiRuleCname(userstore.UserRole, "GET", "/v1/fs/sharings"): true,
apiRuleCname(userstore.UserRole, "GET", "/v1/fs/sharings/exist"): true, apiRuleCname(userstore.UserRole, "GET", "/v1/fs/sharings/exist"): true,
apiRuleCname(userstore.AdminRole, "POST", "/v1/fs/hashes/sha1"): true, apiRuleCname(userstore.UserRole, "GET", "/v1/fs/sharings/dirs"): true,
apiRuleCname(userstore.UserRole, "GET", "/v1/fs/sharings/ids"): true,
apiRuleCname(userstore.UserRole, "POST", "/v1/fs/hashes/sha1"): true,
// visitor rules // visitor rules
apiRuleCname(userstore.VisitorRole, "GET", "/"): true, apiRuleCname(userstore.VisitorRole, "GET", "/"): true,
apiRuleCname(userstore.VisitorRole, "GET", publicPath): true, apiRuleCname(userstore.VisitorRole, "GET", publicPath): true,
@ -120,6 +124,7 @@ func NewMultiUsersSvc(cfg gocfg.ICfg, deps *depidx.Deps) (*MultiUsersSvc, error)
apiRuleCname(userstore.VisitorRole, "GET", "/v1/captchas/"): true, apiRuleCname(userstore.VisitorRole, "GET", "/v1/captchas/"): true,
apiRuleCname(userstore.VisitorRole, "GET", "/v1/captchas/imgs"): true, apiRuleCname(userstore.VisitorRole, "GET", "/v1/captchas/imgs"): true,
apiRuleCname(userstore.VisitorRole, "GET", "/v1/fs/sharings/exist"): true, apiRuleCname(userstore.VisitorRole, "GET", "/v1/fs/sharings/exist"): true,
apiRuleCname(userstore.VisitorRole, "GET", "/v1/fs/sharings/dirs"): true,
} }
return &MultiUsersSvc{ return &MultiUsersSvc{

View file

@ -293,7 +293,9 @@ func initHandlers(router *gin.Engine, cfg gocfg.ICfg, deps *depidx.Deps) (*gin.E
filesAPI.POST("/sharings", fileHdrs.AddSharing) filesAPI.POST("/sharings", fileHdrs.AddSharing)
filesAPI.DELETE("/sharings", fileHdrs.DelSharing) filesAPI.DELETE("/sharings", fileHdrs.DelSharing)
filesAPI.GET("/sharings", fileHdrs.ListSharings) filesAPI.GET("/sharings", fileHdrs.ListSharings)
filesAPI.GET("/sharings/ids", fileHdrs.ListSharingIDs)
filesAPI.GET("/sharings/exist", fileHdrs.IsSharing) filesAPI.GET("/sharings/exist", fileHdrs.IsSharing)
filesAPI.GET("/sharings/dirs", fileHdrs.GetSharingDir)
filesAPI.GET("/metadata", fileHdrs.Metadata) filesAPI.GET("/metadata", fileHdrs.Metadata)

View file

@ -28,7 +28,14 @@ func TestFileHandlers(t *testing.T) {
"downloadSpeedLimit": 409600, "downloadSpeedLimit": 409600,
"spaceLimit": 1000, "spaceLimit": 1000,
"limiterCapacity": 1000, "limiterCapacity": 1000,
"limiterCyc": 1000 "limiterCyc": 1000,
"predefinedUsers": [
{
"name": "demo",
"pwd": "Quicksh@re",
"role": "user"
}
]
}, },
"server": { "server": {
"debug": true, "debug": true,
@ -380,10 +387,10 @@ func TestFileHandlers(t *testing.T) {
} }
}) })
t.Run("test sharing APIs: Upload-AddSharing-ListSharings-List-Download-DelSharing-ListSharings", func(t *testing.T) { t.Run("test sharing APIs: Upload-AddSharing-ListSharings-IsSharing-List-Download-DelSharing-ListSharings", func(t *testing.T) {
files := map[string]string{ files := map[string]string{
"qs/files/sharing/path1/f1": "123456", "qs/files/sharing/path1/f1": "123456",
"qs/files/sharing/path2/path2": "12345678", "qs/files/sharing/path2/f2": "12345678",
} }
for filePath, content := range files { for filePath, content := range files {
@ -394,98 +401,144 @@ func TestFileHandlers(t *testing.T) {
} }
} }
// add sharings userUsersCl := client.NewSingleUserClient(addr)
sharedPaths := map[string]bool{} resp, _, errs := userUsersCl.Login("demo", "Quicksh@re")
for filePath := range files {
dirPath := filepath.Dir(filePath)
sharedPaths[dirPath] = true
res, _, errs := cl.AddSharing(dirPath)
if len(errs) > 0 {
t.Fatal(errs)
} else if res.StatusCode != 200 {
t.Fatal(res.StatusCode)
}
}
// check listSharings
res, shRes, errs := cl.ListSharings()
if len(errs) > 0 { if len(errs) > 0 {
t.Fatal(errs) t.Fatal(errs)
} else if res.StatusCode != 200 { } else if resp.StatusCode != 200 {
t.Fatal(res.StatusCode) t.Fatal(resp.StatusCode)
}
for _, dirPath := range shRes.SharingDirs {
if !sharedPaths[dirPath] {
t.Fatalf("sharing %s not found", dirPath)
}
} }
userUsersToken := client.GetCookie(resp.Cookies(), q.TokenCookie)
userFilesCl := client.NewFilesClient(addr, userUsersToken)
for dirPath := range sharedPaths { for i := 0; i < 2; i++ {
res, _, errs := cl.IsSharing(dirPath) // add sharings
sharedPaths := map[string]bool{}
for filePath := range files {
dirPath := filepath.Dir(filePath)
sharedPaths[dirPath] = true
res, _, errs := cl.AddSharing(dirPath)
if len(errs) > 0 {
t.Fatal(errs)
} else if res.StatusCode != 200 {
t.Fatal(res.StatusCode)
}
}
// check listSharings
res, shRes, errs := cl.ListSharingIDs()
if len(errs) > 0 {
t.Fatal(errs)
} else if res.StatusCode != 200 {
t.Fatal(res.StatusCode)
}
for dirPath, shareID := range shRes.IDs {
if !sharedPaths[dirPath] {
t.Fatalf("sharing %s not found", dirPath)
}
// check getShareDir
res, sharingDir, errs := userFilesCl.GetSharingDir(shareID)
if len(errs) > 0 {
t.Fatal(errs)
} else if res.StatusCode != 200 {
t.Fatal(res.StatusCode)
} else if sharingDir != dirPath {
t.Fatalf("sharing path not equal: got(%s) (%s)", sharingDir, dirPath)
}
}
// check isSharing
for dirPath := range sharedPaths {
res, _, errs := userFilesCl.IsSharing(dirPath)
if len(errs) > 0 {
t.Fatal(errs)
} else if res.StatusCode != 200 {
t.Fatal(res.StatusCode)
}
res, _, errs = userFilesCl.IsSharing(fmt.Sprintf("%s/", dirPath))
if len(errs) > 0 {
t.Fatal(errs)
} else if res.StatusCode != 404 {
t.Fatal(res.StatusCode)
}
}
// check listing
for dirPath := range shRes.IDs {
res, lsResp, errs := userFilesCl.List(dirPath)
if len(errs) > 0 {
t.Fatal(errs)
} else if res.StatusCode != 200 {
t.Fatal(res.StatusCode)
}
if lsResp.Cwd != dirPath {
t.Fatalf("list sharing folder: incorrect cwd (%s)", lsResp.Cwd)
}
if len(lsResp.Metadatas) != 1 {
t.Fatalf("list sharing folder: incorrect metadata size (%d)", len(lsResp.Metadatas))
}
}
// check downloading
for filePath, content := range files {
assertDownloadOK(t, filePath, content, addr, userUsersToken)
}
// delete sharing
for filePath := range files {
dirPath := filepath.Dir(filePath)
res, _, errs := cl.DelSharing(dirPath)
if len(errs) > 0 {
t.Fatal(errs)
} else if res.StatusCode != 200 {
t.Fatal(res.StatusCode)
}
}
// check listSharings
res, shRes, errs = cl.ListSharingIDs()
if len(errs) > 0 { if len(errs) > 0 {
t.Fatal(errs) t.Fatal(errs)
} else if res.StatusCode != 200 { } else if res.StatusCode != 200 {
t.Fatal(res.StatusCode) t.Fatal(res.StatusCode)
} }
res, _, errs = cl.IsSharing(fmt.Sprintf("%s/", dirPath)) if len(shRes.IDs) > 0 {
if len(errs) > 0 { t.Fatalf("sharings should be deleted len(%d)", len(shRes.IDs))
t.Fatal(errs)
} else if res.StatusCode != 404 {
t.Fatal(res.StatusCode)
} }
}
for _, dirPath := range shRes.SharingDirs { // check isSharing
res, lsResp, errs := cl.List(dirPath) for dirPath := range sharedPaths {
if len(errs) > 0 { res, _, errs := userFilesCl.IsSharing(dirPath)
t.Fatal(errs) if len(errs) > 0 {
} else if res.StatusCode != 200 { t.Fatal(errs)
t.Fatal(res.StatusCode) } else if res.StatusCode != 404 {
t.Fatal(res.StatusCode)
}
} }
if lsResp.Cwd != dirPath {
t.Fatalf("list sharing folder: incorrect cwd (%s)", lsResp.Cwd) // check rejecting listing
for dirPath, _ := range shRes.IDs {
res, _, errs := userFilesCl.List(dirPath)
if len(errs) > 0 {
t.Fatal(errs)
} else if res.StatusCode != 403 {
t.Fatal(res.StatusCode)
}
} }
if len(lsResp.Metadatas) != 1 {
t.Fatalf("list sharing folder: incorrect metadata size (%d)", len(lsResp.Metadatas))
}
}
for filePath, content := range files { // check rejecting downloading
assertDownloadOK(t, filePath, content, addr, token) for filePath, _ := range files {
} res, _, errs = userFilesCl.Download(filePath, map[string]string{})
if len(errs) > 0 {
for filePath := range files { t.Fatal(errs)
dirPath := filepath.Dir(filePath) } else if res.StatusCode != 403 {
t.Fatal(res.StatusCode)
res, _, errs := cl.DelSharing(dirPath) }
if len(errs) > 0 {
t.Fatal(errs)
} else if res.StatusCode != 200 {
t.Fatal(res.StatusCode)
}
}
// check listSharings
res, shRes, errs = cl.ListSharings()
if len(errs) > 0 {
t.Fatal(errs)
} else if res.StatusCode != 200 {
t.Fatal(res.StatusCode)
}
for _, dirPath := range shRes.SharingDirs {
if sharedPaths[dirPath] {
t.Fatalf("sharing %s should be deleted", dirPath)
}
}
for dirPath := range sharedPaths {
res, _, errs := cl.IsSharing(dirPath)
if len(errs) > 0 {
t.Fatal(errs)
} else if res.StatusCode != 404 {
t.Fatal(res.StatusCode)
} }
} }
}) })