Skip to content

Commit

Permalink
refactor: improve deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
divyam234 committed Jun 11, 2024
1 parent 06afce6 commit a5a33fa
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 54 deletions.
18 changes: 2 additions & 16 deletions backend/teldrive/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,20 +124,6 @@ type CreateFileRequest struct {
UpdatedAt string `json:"updatedAt,omitempty"`
}

// DeleteFolderRequest is used for deleting a folder
type DeleteFolderRequest struct {
Token string `json:"token"`
FolderID string `json:"fld_id"`
}

// CopyMoveFileRequest is used for moving/copying a file
type CopyMoveFileRequest struct {
Token string `json:"token"`
FileCodes string `json:"file_codes"`
DestinationFolderID string `json:"destination_fld_id"`
Action string `json:"action"`
}

// MoveFolderRequest is used for moving a folder
type MoveFileRequest struct {
Files []string `json:"files"`
Expand All @@ -159,9 +145,9 @@ type UpdateFileInformation struct {

// RemoveFileRequest is used for deleting a file
type RemoveFileRequest struct {
Files []string `json:"files"`
Source string `json:"source,omitempty"`
Files []string `json:"files,omitempty"`
}

type CopyFile struct {
ID string `json:"id"`
Name string `json:"name"`
Expand Down
43 changes: 5 additions & 38 deletions backend/teldrive/teldrive.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,30 +321,6 @@ func (f *Fs) readMetaDataForPath(ctx context.Context, path string, options *api.
return &info, nil
}

func (f *Fs) getPathInfo(ctx context.Context, path string) (*api.ReadMetadataResponse, error) {

opts := rest.Opts{
Method: "GET",
Path: "/api/files",
Parameters: url.Values{
"path": []string{path},
"op": []string{"find"},
},
}
var err error
var info api.ReadMetadataResponse
var resp *http.Response
err = f.pacer.Call(func() (bool, error) {
resp, err = f.srv.CallJSON(ctx, &opts, nil, &info)
return shouldRetry(ctx, resp, err)
})
if err != nil {
return nil, err
}

return &info, nil
}

func (f *Fs) findObject(ctx context.Context, path string, name string) (*api.ReadMetadataResponse, error) {

opts := rest.Opts{
Expand Down Expand Up @@ -695,15 +671,17 @@ func (f *Fs) Mkdir(ctx context.Context, dir string) (err error) {
return f.CreateDir(ctx, f.root, dir)
}

// may or may not delete folders with contents?
func (f *Fs) purge(ctx context.Context, folderID string) (err error) {
// Rmdir removes the directory (container, bucket) if empty
//
// Return an error if it doesn't exist or isn't empty
func (f *Fs) Rmdir(ctx context.Context, dir string) (err error) {
var resp *http.Response
opts := rest.Opts{
Method: "POST",
Path: "/api/files/delete",
}
rm := api.RemoveFileRequest{
Files: []string{folderID},
Source: f.dirPath(dir),
}
err = f.pacer.Call(func() (bool, error) {
resp, err = f.srv.CallJSON(ctx, &opts, &rm, nil)
Expand All @@ -715,17 +693,6 @@ func (f *Fs) purge(ctx context.Context, folderID string) (err error) {
return nil
}

// Rmdir removes the directory (container, bucket) if empty
//
// Return an error if it doesn't exist or isn't empty
func (f *Fs) Rmdir(ctx context.Context, dir string) error {
info, err := f.getPathInfo(ctx, f.dirPath(dir))
if err != nil {
return err
}
return f.purge(ctx, info.Files[0].Id)
}

// Move src to this remote using server-side move operations.
//
// This is stored with the remote path given.
Expand Down

0 comments on commit a5a33fa

Please sign in to comment.