feat(files): add search API and tests

This commit is contained in:
hexxa 2022-07-23 13:01:43 +08:00 committed by Hexxa
parent 4fbdee0eca
commit 826d472a96
6 changed files with 85 additions and 1 deletions

View file

@ -248,3 +248,18 @@ func (cl *FilesClient) GetSharingDir(shareID string) (*http.Response, string, []
}
return resp, sdResp.SharingDir, nil
}
func (cl *FilesClient) SearchItems(keyword string) (*http.Response, *fileshdr.SearchItemsResp, []error) {
resp, body, errs := cl.r.Get(cl.url("/v1/fs/search")).
AddCookie(cl.token).
Param(fileshdr.Keyword, keyword).
End()
searchResp := &fileshdr.SearchItemsResp{}
err := json.Unmarshal([]byte(body), searchResp)
if err != nil {
errs = append(errs, err)
return nil, nil, errs
}
return resp, searchResp, nil
}

View file

@ -30,6 +30,7 @@ const (
FilePathQuery = "fp"
ListDirQuery = "dp"
ShareIDQuery = "shid"
Keyword = "k"
// headers
rangeHeader = "Range"
@ -1151,6 +1152,25 @@ func (h *FileHandlers) GetSharingDir(c *gin.Context) {
c.JSON(200, &GetSharingDirResp{SharingDir: dirPath})
}
type SearchItemsResp struct {
Results []string `json:"results"`
}
func (h *FileHandlers) SearchItems(c *gin.Context) {
keyword := c.Query(Keyword)
if keyword == "" {
c.JSON(q.ErrResp(c, 400, errors.New("empty keyword")))
return
}
results, err := h.deps.FileIndex().Search(keyword)
if err != nil {
c.JSON(q.ErrResp(c, 500, err))
return
}
c.JSON(200, &SearchItemsResp{Results: results})
}
func (h *FileHandlers) GetStreamReader(userID uint64, fd io.Reader) (io.ReadCloser, error) {
pr, pw := io.Pipe()

View file

@ -61,6 +61,7 @@ func NewMultiUsersSvc(cfg gocfg.ICfg, deps *depidx.Deps) (*MultiUsersSvc, error)
apiRuleCname(db.AdminRole, "GET", "/v1/fs/files/chunks"): true,
apiRuleCname(db.AdminRole, "PATCH", "/v1/fs/files/copy"): true,
apiRuleCname(db.AdminRole, "PATCH", "/v1/fs/files/move"): true,
apiRuleCname(db.AdminRole, "GET", "/v1/fs/search"): true,
apiRuleCname(db.AdminRole, "GET", "/v1/fs/dirs"): true,
apiRuleCname(db.AdminRole, "GET", "/v1/fs/dirs/home"): true,
apiRuleCname(db.AdminRole, "POST", "/v1/fs/dirs"): true,
@ -98,6 +99,7 @@ func NewMultiUsersSvc(cfg gocfg.ICfg, deps *depidx.Deps) (*MultiUsersSvc, error)
apiRuleCname(db.UserRole, "GET", "/v1/fs/files/chunks"): true,
apiRuleCname(db.UserRole, "PATCH", "/v1/fs/files/copy"): true,
apiRuleCname(db.UserRole, "PATCH", "/v1/fs/files/move"): true,
apiRuleCname(db.UserRole, "GET", "/v1/fs/search"): true,
apiRuleCname(db.UserRole, "GET", "/v1/fs/dirs"): true,
apiRuleCname(db.UserRole, "GET", "/v1/fs/dirs/home"): true,
apiRuleCname(db.UserRole, "POST", "/v1/fs/dirs"): true,

View file

@ -327,6 +327,7 @@ func initHandlers(router *gin.Engine, cfg gocfg.ICfg, deps *depidx.Deps) (*gin.E
filesAPI.GET("/sharings/dirs", fileHdrs.GetSharingDir)
filesAPI.GET("/metadata", fileHdrs.Metadata)
filesAPI.GET("/search", fileHdrs.SearchItems)
filesAPI.POST("/hashes/sha1", fileHdrs.GenerateHash)

View file

@ -10,7 +10,7 @@ import (
q "github.com/ihexxa/quickshare/src/handlers"
)
func TestConcurrency(t *testing.T) {
func xTestConcurrency(t *testing.T) {
addr := "http://127.0.0.1:8686"
rootPath := "tmpTestData"
config := `{

View file

@ -850,6 +850,52 @@ func TestFileHandlers(t *testing.T) {
}
})
t.Run("Search", func(t *testing.T) {
files := map[string]string{
"qs/files/search/keyword": "12345678",
"qs/files/search/path/keyword": "12345678",
"qs/files/search/normal_file": "12345678",
}
expected := map[string]bool{
"qs/files/search/keyword": true,
"qs/files/search/path/keyword": true,
}
for filePath, content := range files {
assertUploadOK(t, filePath, content, addr, token)
err = fs.Sync()
if err != nil {
t.Fatal(err)
}
}
resp, searchItemsResp, errs := cl.SearchItems("keyword")
if len(errs) > 0 {
t.Fatal(errs)
} else if resp.StatusCode != 200 {
t.Fatal(resp.StatusCode)
}
exist := func(expectedPaths map[string]bool, searchItemsResp []string) bool {
results := map[string]bool{}
for _, result := range searchItemsResp {
results[result] = true
}
for got := range expectedPaths {
if !results[got] {
return false
}
}
return true
}
if !exist(expected, searchItemsResp.Results) {
fmt.Printf("expected(%+v) got(%+v)", expected, searchItemsResp.Results)
t.Fatal("search result not match")
}
})
resp, _, errs = usersCl.Logout(token)
if len(errs) > 0 {
t.Fatal(errs)