Nick Craig-Wood 2021-06-10 13:11:00 +01:00
parent bef0c23e00
commit 3349b055f5
3 changed files with 83 additions and 11 deletions

View File

@ -404,6 +404,34 @@ func (f *Fs) copyFile(ctx context.Context, url string, folderID int, rename stri
return response, nil return response, nil
} }
func (f *Fs) renameFile(ctx context.Context, url string, newName string) (response *RenameFileResponse, err error) {
request := &RenameFileRequest{
URLs: []RenameFileURL{
{
URL: url,
Filename: newName,
},
},
}
opts := rest.Opts{
Method: "POST",
Path: "/file/rename.cgi",
}
response = &RenameFileResponse{}
err = f.pacer.Call(func() (bool, error) {
resp, err := f.rest.CallJSON(ctx, &opts, request, response)
return shouldRetry(ctx, resp, err)
})
if err != nil {
return nil, errors.Wrap(err, "couldn't rename file")
}
return response, nil
}
func (f *Fs) getUploadNode(ctx context.Context) (response *GetUploadNodeResponse, err error) { func (f *Fs) getUploadNode(ctx context.Context) (response *GetUploadNodeResponse, err error) {
// fs.Debugf(f, "Requesting Upload node") // fs.Debugf(f, "Requesting Upload node")

View File

@ -437,25 +437,45 @@ func (f *Fs) Move(ctx context.Context, src fs.Object, remote string) (fs.Object,
return nil, fs.ErrorCantMove return nil, fs.ErrorCantMove
} }
// Find current directory ID
_, currentDirectoryID, err := f.dirCache.FindPath(ctx, remote, false)
if err != nil {
return nil, err
}
// Create temporary object // Create temporary object
dstObj, leaf, directoryID, err := f.createObject(ctx, remote) dstObj, leaf, directoryID, err := f.createObject(ctx, remote)
if err != nil { if err != nil {
return nil, err return nil, err
} }
folderID, err := strconv.Atoi(directoryID) // If it is in the correct directory, just rename it
if err != nil { var url string
return nil, err if currentDirectoryID == directoryID {
} resp, err := f.renameFile(ctx, srcObj.file.URL, leaf)
resp, err := f.moveFile(ctx, srcObj.file.URL, folderID, leaf) if err != nil {
if err != nil { return nil, errors.Wrap(err, "couldn't rename file")
return nil, errors.Wrap(err, "couldn't move file") }
} if resp.Status != "OK" {
if resp.Status != "OK" { return nil, errors.Errorf("couldn't rename file: %s", resp.Message)
return nil, errors.Errorf("couldn't move file: %s", resp.Message) }
url = resp.URLs[0].URL
} else {
folderID, err := strconv.Atoi(directoryID)
if err != nil {
return nil, err
}
resp, err := f.moveFile(ctx, srcObj.file.URL, folderID, leaf)
if err != nil {
return nil, errors.Wrap(err, "couldn't move file")
}
if resp.Status != "OK" {
return nil, errors.Errorf("couldn't move file: %s", resp.Message)
}
url = resp.URLs[0]
} }
file, err := f.readFileInfo(ctx, resp.URLs[0]) file, err := f.readFileInfo(ctx, url)
if err != nil { if err != nil {
return nil, errors.New("couldn't read file data") return nil, errors.New("couldn't read file data")
} }

View File

@ -90,6 +90,30 @@ type FileCopy struct {
ToURL string `json:"to_url"` ToURL string `json:"to_url"`
} }
// RenameFileURL is the data structure to rename a single file
type RenameFileURL struct {
URL string `json:"url"`
Filename string `json:"filename"`
}
// RenameFileRequest is the request structure of the corresponding request
type RenameFileRequest struct {
URLs []RenameFileURL `json:"urls"`
Pretty int `json:"pretty"`
}
// RenameFileResponse is the response structure of the corresponding request
type RenameFileResponse struct {
Status string `json:"status"`
Message string `json:"message"`
Renamed int `json:"renamed"`
URLs []struct {
URL string `json:"url"`
OldFilename string `json:"old_filename"`
NewFilename string `json:"new_filename"`
} `json:"urls"`
}
// GetUploadNodeResponse is the response structure of the corresponding request // GetUploadNodeResponse is the response structure of the corresponding request
type GetUploadNodeResponse struct { type GetUploadNodeResponse struct {
ID string `json:"id"` ID string `json:"id"`