Skip to content

Commit

Permalink
move file with different name
Browse files Browse the repository at this point in the history
  • Loading branch information
cenkalti committed Aug 15, 2020
1 parent b419379 commit 2197b13
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions job_move.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import (
"context"
"errors"
"fmt"
"net/url"
"os"
"path"
"path/filepath"
"strconv"
"strings"
)

type MoveLocalFile struct {
Expand Down Expand Up @@ -62,14 +65,30 @@ func (j *MoveRemoteFile) String() string {
}

func (j *MoveRemoteFile) Run(ctx context.Context) error {
dir := path.Dir(j.toRelpath)
dir, name := path.Split(j.toRelpath)
parentID, err := dirCache.Mkdirp(ctx, dir)
if err != nil {
return err
}
err = client.Files.Move(ctx, parentID, j.remoteFile.putioFile.ID)
err = moveRemoteFile(ctx, parentID, j.remoteFile.putioFile.ID, name)
if err != nil {
return err
}
return j.state.Move(j.toRelpath)
}

func moveRemoteFile(ctx context.Context, parentID, fileID int64, name string) error {
params := url.Values{}
params.Set("file_id", strconv.FormatInt(fileID, 10))
params.Set("parent_id", strconv.FormatInt(parentID, 10))
params.Set("name", name)

req, err := client.NewRequest(ctx, "POST", "/v2/files/move", strings.NewReader(params.Encode()))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

_, err = client.Do(req, nil)
return err
}

0 comments on commit 2197b13

Please sign in to comment.