Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(api): file list endpoint #395

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions api/docs/index.html

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions api/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1239,10 +1239,6 @@ paths:
name: id
required: true
type: string
- description: Type
in: query
name: type
type: string
- description: Page
in: query
name: page
Expand Down
25 changes: 3 additions & 22 deletions api/router/file_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,6 @@ func (r *FileRouter) ListByPath(c *fiber.Ctx) error {
// @Id files_list
// @Produce json
// @Param id path string true "ID"
// @Param type query string false "Type"
// @Param page query string false "Page"
// @Param size query string false "Size"
// @Param sort_by query string false "Sort By"
Expand All @@ -343,17 +342,9 @@ func (r *FileRouter) List(c *fiber.Ctx) error {
if err != nil {
return err
}
var res *service.FileList
if c.Query("query") != "" {
res, err = r.fileSvc.Search(id, *opts, userID)
if err != nil {
return err
}
} else {
res, err = r.fileSvc.List(id, *opts, userID)
if err != nil {
return err
}
res, err := r.fileSvc.List(id, *opts, userID)
if err != nil {
return err
}
return c.JSON(res)
}
Expand Down Expand Up @@ -421,10 +412,6 @@ func (r *FileRouter) parseListQueryParams(c *fiber.Ctx) (*service.FileListOption
if !IsValidSortOrder(sortOrder) {
return nil, errorpkg.NewInvalidQueryParamError("sort_order")
}
fileType := c.Query("type")
if fileType != model.FileTypeFile && fileType != model.FileTypeFolder && fileType != "" {
return nil, errorpkg.NewInvalidQueryParamError("type")
}
query, err := url.QueryUnescape(c.Query("query"))
if err != nil {
return nil, errorpkg.NewInvalidQueryParamError("query")
Expand All @@ -443,12 +430,6 @@ func (r *FileRouter) parseListQueryParams(c *fiber.Ctx) (*service.FileListOption
if err := json.Unmarshal(b, &opts.Query); err != nil {
return nil, errorpkg.NewInvalidQueryParamError("query")
}
} else {
if fileType != "" {
opts.Query = &service.FileQuery{
Type: &fileType,
}
}
}
return &opts, nil
}
Expand Down
67 changes: 13 additions & 54 deletions api/service/file_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ func (svc *FileService) ListByPath(path string, userID string) ([]*File, error)
}

type FileQuery struct {
Text string `json:"text" validate:"required"`
Text *string `json:"text" validate:"required"`
Type *string `json:"type,omitempty" validate:"omitempty,oneof=file folder"`
CreateTimeAfter *int64 `json:"createTimeAfter,omitempty"`
CreateTimeBefore *int64 `json:"createTimeBefore,omitempty"`
Expand All @@ -633,52 +633,6 @@ type FileListOptions struct {
Query *FileQuery
}

func (svc *FileService) List(id string, opts FileListOptions, userID string) (*FileList, error) {
file, err := svc.fileCache.Get(id)
if err != nil {
return nil, err
}
if err = svc.fileGuard.Authorize(userID, file, model.PermissionViewer); err != nil {
return nil, err
}
ids, err := svc.fileRepo.FindChildrenIDs(id)
if err != nil {
return nil, err
}
var data []model.File
for _, id := range ids {
var f model.File
f, err := svc.fileCache.Get(id)
if err != nil {
return nil, err
}
data = append(data, f)
}
var filtered []model.File
for _, f := range data {
if opts.Query == nil || *opts.Query.Type == "" || f.GetType() == *opts.Query.Type {
filtered = append(filtered, f)
}
}
authorized, err := svc.doAuthorization(filtered, userID)
if err != nil {
return nil, err
}
sorted := svc.doSorting(authorized, opts.SortBy, opts.SortOrder, userID)
paged, totalElements, totalPages := svc.doPagination(sorted, opts.Page, opts.Size)
mapped, err := svc.fileMapper.mapMany(paged, userID)
if err != nil {
return nil, err
}
return &FileList{
Data: mapped,
TotalElements: totalElements,
TotalPages: totalPages,
Page: opts.Page,
Size: opts.Size,
}, nil
}

type FileProbe struct {
TotalPages uint64 `json:"totalPages"`
TotalElements uint64 `json:"totalElements"`
Expand All @@ -702,7 +656,7 @@ func (svc *FileService) Probe(id string, opts FileListOptions, userID string) (*
}, nil
}

func (svc *FileService) Search(id string, opts FileListOptions, userID string) (*FileList, error) {
func (svc *FileService) List(id string, opts FileListOptions, userID string) (*FileList, error) {
parent, err := svc.fileCache.Get(id)
if err != nil {
return nil, err
Expand All @@ -715,12 +669,12 @@ func (svc *FileService) Search(id string, opts FileListOptions, userID string) (
return nil, err
}
var data []model.File
if opts.Query.Text != "" {
if opts.Query != nil && opts.Query.Text != nil {
count, err := svc.fileRepo.Count()
if err != nil {
return nil, err
}
data, err = svc.fileSearch.Query(opts.Query.Text, infra.QueryOptions{Limit: count})
data, err = svc.fileSearch.Query(*opts.Query.Text, infra.QueryOptions{Limit: count})
if err != nil {
return nil, err
}
Expand All @@ -738,16 +692,21 @@ func (svc *FileService) Search(id string, opts FileListOptions, userID string) (
data = append(data, f)
}
}
filteredData, err := svc.doQueryFiltering(data, *opts.Query, parent)
var filtered []model.File
if opts.Query != nil {
filtered, err = svc.doQueryFiltering(data, *opts.Query, parent)
} else {
filtered = data
}
if err != nil {
return nil, err
}
authorizedData, err := svc.doAuthorization(filteredData, userID)
authorized, err := svc.doAuthorization(filtered, userID)
if err != nil {
return nil, err
}
sortedData := svc.doSorting(authorizedData, opts.SortBy, opts.SortOrder, userID)
paged, totalElements, totalPages := svc.doPagination(sortedData, opts.Page, opts.Size)
sorted := svc.doSorting(authorized, opts.SortBy, opts.SortOrder, userID)
paged, totalElements, totalPages := svc.doPagination(sorted, opts.Page, opts.Size)
mappedData, err := svc.fileMapper.mapMany(paged, userID)
if err != nil {
return nil, err
Expand Down
6 changes: 1 addition & 5 deletions ui/src/client/api/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export type GroupPermission = {
}

export type Query = {
text: string
text?: string
type?: FileType
createTimeAfter?: number
createTimeBefore?: number
Expand All @@ -81,7 +81,6 @@ export type Query = {
export type ListOptions = {
size?: number
page?: number
type?: FileType
sortBy?: SortBy
sortOrder?: SortOrder
query?: Query
Expand Down Expand Up @@ -289,9 +288,6 @@ export default class FileAPI {
if (options?.sortOrder) {
params.sort_order = options.sortOrder.toString()
}
if (options?.type) {
params.type = options.type
}
if (options?.query) {
params.query = encodeQuery(JSON.stringify(options.query))
}
Expand Down
8 changes: 6 additions & 2 deletions ui/src/components/file/file-browse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ const FileBrowse = ({ onChange }: FileBrowseProps) => {
const timeoutId = setTimeout(() => setIsSpinnerVisible(true), 250)
const result = await FileAPI.list(fileId, {
page: 1,
type: FileType.Folder,
query: {
type: FileType.Folder,
},
})
clearTimeout(timeoutId)
setTotalPages(result.totalPages)
Expand All @@ -67,7 +69,9 @@ const FileBrowse = ({ onChange }: FileBrowseProps) => {
setIsLoading(true)
const result = await FileAPI.list(fileId, {
page,
type: FileType.Folder,
query: {
type: FileType.Folder,
},
})
setTotalPages(result.totalPages)
setFolders(result.data)
Expand Down