80 lines
2.2 KiB
Go
80 lines
2.2 KiB
Go
package drive
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"strings"
|
|
|
|
"github.com/ultisuite/ulti-backend/internal/api/paginate"
|
|
"github.com/ultisuite/ulti-backend/internal/api/query"
|
|
"github.com/ultisuite/ulti-backend/internal/nextcloud"
|
|
)
|
|
|
|
type Service struct {
|
|
nc *nextcloud.Client
|
|
}
|
|
|
|
func NewService(nc *nextcloud.Client) *Service {
|
|
return &Service{nc: nc}
|
|
}
|
|
|
|
type FilesList struct {
|
|
Files []nextcloud.FileInfo `json:"files"`
|
|
Pagination query.PaginationMeta `json:"pagination,omitempty"`
|
|
}
|
|
|
|
func (s *Service) ListFiles(ctx context.Context, userID, path string, params query.ListParams) (FilesList, error) {
|
|
if path == "" {
|
|
path = "/"
|
|
}
|
|
files, err := s.nc.ListFiles(ctx, userID, path)
|
|
if err != nil {
|
|
return FilesList{}, err
|
|
}
|
|
filtered := filterFiles(files, params.Q)
|
|
page, total := paginate.Slice(filtered, params.Offset(), params.Limit())
|
|
return FilesList{
|
|
Files: page,
|
|
Pagination: params.Meta(&total),
|
|
}, nil
|
|
}
|
|
|
|
func (s *Service) Upload(ctx context.Context, userID, path string, body io.Reader, contentType string) error {
|
|
return s.nc.Upload(ctx, userID, path, body, contentType)
|
|
}
|
|
|
|
func (s *Service) Download(ctx context.Context, userID, path string) (io.ReadCloser, string, error) {
|
|
return s.nc.Download(ctx, userID, path)
|
|
}
|
|
|
|
func (s *Service) Delete(ctx context.Context, userID, path string) error {
|
|
return s.nc.Delete(ctx, userID, path)
|
|
}
|
|
|
|
func (s *Service) CreateFolder(ctx context.Context, userID, path string) error {
|
|
return s.nc.CreateFolder(ctx, userID, path)
|
|
}
|
|
|
|
func (s *Service) Move(ctx context.Context, userID, source, destination string) error {
|
|
return s.nc.Move(ctx, userID, source, destination)
|
|
}
|
|
|
|
func (s *Service) CreateShare(ctx context.Context, userID, path string, shareType, permissions int) (*nextcloud.ShareInfo, error) {
|
|
return s.nc.CreateShare(ctx, userID, path, shareType, permissions)
|
|
}
|
|
|
|
func filterFiles(files []nextcloud.FileInfo, q string) []nextcloud.FileInfo {
|
|
q = strings.ToLower(strings.TrimSpace(q))
|
|
if q == "" {
|
|
return files
|
|
}
|
|
out := make([]nextcloud.FileInfo, 0, len(files))
|
|
for _, f := range files {
|
|
if strings.Contains(strings.ToLower(f.Name), q) ||
|
|
strings.Contains(strings.ToLower(f.Path), q) {
|
|
out = append(out, f)
|
|
}
|
|
}
|
|
return out
|
|
}
|