feat(files/sharing): refactor sharing apis with tests
This commit is contained in:
parent
5ef94afd7a
commit
80d54f42a1
5 changed files with 67 additions and 16 deletions
|
@ -179,7 +179,14 @@ func (cl *FilesClient) AddSharing(dirpath string) (*http.Response, string, []err
|
||||||
func (cl *FilesClient) DelSharing(dirpath string) (*http.Response, string, []error) {
|
func (cl *FilesClient) DelSharing(dirpath string) (*http.Response, string, []error) {
|
||||||
return cl.r.Delete(cl.url("/v1/fs/sharings")).
|
return cl.r.Delete(cl.url("/v1/fs/sharings")).
|
||||||
AddCookie(cl.token).
|
AddCookie(cl.token).
|
||||||
Send(fileshdr.SharingReq{SharingPath: dirpath}).
|
Param(fileshdr.FilePathQuery, dirpath).
|
||||||
|
End()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cl *FilesClient) IsSharing(dirpath string) (*http.Response, string, []error) {
|
||||||
|
return cl.r.Get(cl.url("/v1/fs/sharings/exist")).
|
||||||
|
AddCookie(cl.token).
|
||||||
|
Param(fileshdr.FilePathQuery, dirpath).
|
||||||
End()
|
End()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,13 +14,12 @@ const listDirQuery = "dp";
|
||||||
function translateResp(resp: Response<any>): Response<any> {
|
function translateResp(resp: Response<any>): Response<any> {
|
||||||
if (resp.status === 500) {
|
if (resp.status === 500) {
|
||||||
if (
|
if (
|
||||||
(resp.data == null || resp.data === "") ||
|
resp.data == null ||
|
||||||
(
|
resp.data === "" ||
|
||||||
resp.data.error != null &&
|
(resp.data.error != null &&
|
||||||
!resp.data.error.includes("fail to lock the file") &&
|
!resp.data.error.includes("fail to lock the file") &&
|
||||||
!resp.data.error.includes("offset != uploaded") &&
|
!resp.data.error.includes("offset != uploaded") &&
|
||||||
!resp.data.error.includes("i/o timeout")
|
!resp.data.error.includes("i/o timeout"))
|
||||||
)
|
|
||||||
) {
|
) {
|
||||||
return FatalErrResp(resp.statusText);
|
return FatalErrResp(resp.statusText);
|
||||||
}
|
}
|
||||||
|
@ -160,4 +159,34 @@ export class FilesClient extends BaseClient {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
addSharing = (dirPath: string): Promise<Response> => {
|
||||||
|
return this.do({
|
||||||
|
method: "post",
|
||||||
|
url: `${this.url}/v1/fs/sharings`,
|
||||||
|
data: {
|
||||||
|
SharingPath: dirPath,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
deleteSharing = (dirPath: string): Promise<Response> => {
|
||||||
|
return this.do({
|
||||||
|
method: "delete",
|
||||||
|
url: `${this.url}/v1/fs/sharings`,
|
||||||
|
params: {
|
||||||
|
[filePathQuery]: dirPath,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
isSharing = (dirPath: string): Promise<Response> => {
|
||||||
|
return this.do({
|
||||||
|
method: "get",
|
||||||
|
url: `${this.url}/v1/fs/sharings/exist`,
|
||||||
|
params: {
|
||||||
|
[filePathQuery]: dirPath,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -763,21 +763,21 @@ func (h *FileHandlers) AddSharing(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *FileHandlers) DelSharing(c *gin.Context) {
|
func (h *FileHandlers) DelSharing(c *gin.Context) {
|
||||||
req := &SharingReq{}
|
dirPath := c.Query(FilePathQuery)
|
||||||
if err := c.ShouldBindJSON(&req); err != nil {
|
if dirPath == "" {
|
||||||
c.JSON(q.ErrResp(c, 400, err))
|
c.JSON(q.ErrResp(c, 400, errors.New("invalid file path")))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: move canAccess to authedFS
|
// TODO: move canAccess to authedFS
|
||||||
userName := c.MustGet(q.UserParam).(string)
|
userName := c.MustGet(q.UserParam).(string)
|
||||||
role := c.MustGet(q.RoleParam).(string)
|
role := c.MustGet(q.RoleParam).(string)
|
||||||
if !h.canAccess(userName, role, "", req.SharingPath) {
|
if !h.canAccess(userName, role, "", dirPath) {
|
||||||
c.JSON(q.ErrResp(c, 403, errors.New("forbidden")))
|
c.JSON(q.ErrResp(c, 403, errors.New("forbidden")))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err := h.deps.FileInfos().DelSharing(req.SharingPath)
|
err := h.deps.FileInfos().DelSharing(dirPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(q.ErrResp(c, 500, err))
|
c.JSON(q.ErrResp(c, 500, err))
|
||||||
return
|
return
|
||||||
|
@ -786,13 +786,13 @@ func (h *FileHandlers) DelSharing(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *FileHandlers) IsSharing(c *gin.Context) {
|
func (h *FileHandlers) IsSharing(c *gin.Context) {
|
||||||
req := &SharingReq{}
|
dirPath := c.Query(FilePathQuery)
|
||||||
if err := c.ShouldBindJSON(&req); err != nil {
|
if dirPath == "" {
|
||||||
c.JSON(q.ErrResp(c, 400, err))
|
c.JSON(q.ErrResp(c, 400, errors.New("invalid file path")))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
_, ok := h.deps.FileInfos().GetSharing(req.SharingPath)
|
_, ok := h.deps.FileInfos().GetSharing(dirPath)
|
||||||
if ok {
|
if ok {
|
||||||
c.JSON(q.Resp(200))
|
c.JSON(q.Resp(200))
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -148,7 +148,6 @@ func TestConcurrency(t *testing.T) {
|
||||||
if selfResp.UsedSpace != int64((filesCount-1)*len(content)) {
|
if selfResp.UsedSpace != int64((filesCount-1)*len(content)) {
|
||||||
t.Fatalf("usedSpace(%d) doesn't match (%d)", selfResp.UsedSpace, int64((filesCount-1)*len(content)))
|
t.Fatalf("usedSpace(%d) doesn't match (%d)", selfResp.UsedSpace, int64((filesCount-1)*len(content)))
|
||||||
}
|
}
|
||||||
fmt.Println("\n\n\n", selfResp.UsedSpace, int64((filesCount-1)*len(content)))
|
|
||||||
|
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}
|
}
|
||||||
|
|
|
@ -387,6 +387,22 @@ func TestFileHandlers(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for dirPath := range sharedPaths {
|
||||||
|
res, _, errs := cl.IsSharing(dirPath)
|
||||||
|
if len(errs) > 0 {
|
||||||
|
t.Fatal(errs)
|
||||||
|
} else if res.StatusCode != 200 {
|
||||||
|
t.Fatal(res.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
res, _, errs = cl.IsSharing(fmt.Sprintf("%s/", dirPath))
|
||||||
|
if len(errs) > 0 {
|
||||||
|
t.Fatal(errs)
|
||||||
|
} else if res.StatusCode != 404 {
|
||||||
|
t.Fatal(res.StatusCode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for _, dirPath := range shRes.SharingDirs {
|
for _, dirPath := range shRes.SharingDirs {
|
||||||
res, lsResp, errs := cl.List(dirPath)
|
res, lsResp, errs := cl.List(dirPath)
|
||||||
if len(errs) > 0 {
|
if len(errs) > 0 {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue