2019-06-26 20:39:01 +02:00
|
|
|
package fichier
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-11-04 11:12:57 +01:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-06-26 20:39:01 +02:00
|
|
|
"io"
|
|
|
|
"net/http"
|
2021-05-16 23:28:18 +02:00
|
|
|
"net/url"
|
2019-06-26 20:39:01 +02:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2020-08-23 18:59:44 +02:00
|
|
|
"strings"
|
2019-06-26 20:39:01 +02:00
|
|
|
"time"
|
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/fserrors"
|
|
|
|
"github.com/rclone/rclone/lib/rest"
|
2019-06-26 20:39:01 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// retryErrorCodes is a slice of error codes that we will retry
|
|
|
|
var retryErrorCodes = []int{
|
|
|
|
429, // Too Many Requests.
|
2020-04-24 03:15:52 +02:00
|
|
|
403, // Forbidden (may happen when request limit is exceeded)
|
2019-06-26 20:39:01 +02:00
|
|
|
500, // Internal Server Error
|
|
|
|
502, // Bad Gateway
|
|
|
|
503, // Service Unavailable
|
|
|
|
504, // Gateway Timeout
|
|
|
|
509, // Bandwidth Limit Exceeded
|
|
|
|
}
|
|
|
|
|
|
|
|
// shouldRetry returns a boolean as to whether this resp and err
|
|
|
|
// deserve to be retried. It returns the err as a convenience
|
2021-03-11 15:44:01 +01:00
|
|
|
func shouldRetry(ctx context.Context, resp *http.Response, err error) (bool, error) {
|
|
|
|
if fserrors.ContextError(ctx, &err) {
|
|
|
|
return false, err
|
|
|
|
}
|
2020-08-23 18:59:44 +02:00
|
|
|
// Detect this error which the integration tests provoke
|
|
|
|
// error HTTP error 403 (403 Forbidden) returned body: "{\"message\":\"Flood detected: IP Locked #374\",\"status\":\"KO\"}"
|
|
|
|
//
|
|
|
|
// https://1fichier.com/api.html
|
|
|
|
//
|
|
|
|
// file/ls.cgi is limited :
|
|
|
|
//
|
|
|
|
// Warning (can be changed in case of abuses) :
|
|
|
|
// List all files of the account is limited to 1 request per hour.
|
|
|
|
// List folders is limited to 5 000 results and 1 request per folder per 30s.
|
|
|
|
if err != nil && strings.Contains(err.Error(), "Flood detected") {
|
|
|
|
fs.Debugf(nil, "Sleeping for 30 seconds due to: %v", err)
|
|
|
|
time.Sleep(30 * time.Second)
|
|
|
|
}
|
2019-06-26 20:39:01 +02:00
|
|
|
return fserrors.ShouldRetry(err) || fserrors.ShouldRetryHTTP(resp, retryErrorCodes), err
|
|
|
|
}
|
|
|
|
|
|
|
|
var isAlphaNumeric = regexp.MustCompile(`^[a-zA-Z0-9]+$`).MatchString
|
|
|
|
|
2021-02-22 00:37:38 +01:00
|
|
|
func (f *Fs) createObject(ctx context.Context, remote string) (o *Object, leaf string, directoryID string, err error) {
|
|
|
|
// Create the directory for the object if it doesn't exist
|
|
|
|
leaf, directoryID, err = f.dirCache.FindPath(ctx, remote, true)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Temporary Object under construction
|
|
|
|
o = &Object{
|
|
|
|
fs: f,
|
|
|
|
remote: remote,
|
|
|
|
}
|
|
|
|
return o, leaf, directoryID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Fs) readFileInfo(ctx context.Context, url string) (*File, error) {
|
|
|
|
request := FileInfoRequest{
|
|
|
|
URL: url,
|
|
|
|
}
|
|
|
|
opts := rest.Opts{
|
|
|
|
Method: "POST",
|
|
|
|
Path: "/file/info.cgi",
|
|
|
|
}
|
|
|
|
|
|
|
|
var file File
|
|
|
|
err := f.pacer.Call(func() (bool, error) {
|
|
|
|
resp, err := f.rest.CallJSON(ctx, &opts, &request, &file)
|
2021-03-11 15:44:01 +01:00
|
|
|
return shouldRetry(ctx, resp, err)
|
2021-02-22 00:37:38 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
return nil, fmt.Errorf("couldn't read file info: %w", err)
|
2021-02-22 00:37:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return &file, err
|
|
|
|
}
|
|
|
|
|
2021-07-05 20:55:08 +02:00
|
|
|
// maybe do some actual validation later if necessary
|
|
|
|
func validToken(token *GetTokenResponse) bool {
|
|
|
|
return token.Status == "OK"
|
|
|
|
}
|
|
|
|
|
2019-09-04 21:00:37 +02:00
|
|
|
func (f *Fs) getDownloadToken(ctx context.Context, url string) (*GetTokenResponse, error) {
|
2019-06-26 20:39:01 +02:00
|
|
|
request := DownloadRequest{
|
|
|
|
URL: url,
|
|
|
|
Single: 1,
|
2021-05-16 23:28:18 +02:00
|
|
|
Pass: f.opt.FilePassword,
|
2019-06-26 20:39:01 +02:00
|
|
|
}
|
|
|
|
opts := rest.Opts{
|
|
|
|
Method: "POST",
|
|
|
|
Path: "/download/get_token.cgi",
|
|
|
|
}
|
|
|
|
|
|
|
|
var token GetTokenResponse
|
|
|
|
err := f.pacer.Call(func() (bool, error) {
|
2019-09-04 21:00:37 +02:00
|
|
|
resp, err := f.rest.CallJSON(ctx, &opts, &request, &token)
|
2021-07-05 20:55:08 +02:00
|
|
|
doretry, err := shouldRetry(ctx, resp, err)
|
|
|
|
return doretry || !validToken(&token), err
|
2019-06-26 20:39:01 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
return nil, fmt.Errorf("couldn't list files: %w", err)
|
2019-06-26 20:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return &token, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func fileFromSharedFile(file *SharedFile) File {
|
|
|
|
return File{
|
|
|
|
URL: file.Link,
|
|
|
|
Filename: file.Filename,
|
|
|
|
Size: file.Size,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Fs) listSharedFiles(ctx context.Context, id string) (entries fs.DirEntries, err error) {
|
|
|
|
opts := rest.Opts{
|
2021-05-16 23:28:18 +02:00
|
|
|
Method: "GET",
|
|
|
|
RootURL: "https://1fichier.com/dir/",
|
|
|
|
Path: id,
|
|
|
|
Parameters: map[string][]string{"json": {"1"}},
|
|
|
|
ContentType: "application/x-www-form-urlencoded",
|
|
|
|
}
|
|
|
|
if f.opt.FolderPassword != "" {
|
|
|
|
opts.Method = "POST"
|
|
|
|
opts.Parameters = nil
|
|
|
|
opts.Body = strings.NewReader("json=1&pass=" + url.QueryEscape(f.opt.FolderPassword))
|
2019-06-26 20:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var sharedFiles SharedFolderResponse
|
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
2019-09-04 21:00:37 +02:00
|
|
|
resp, err := f.rest.CallJSON(ctx, &opts, nil, &sharedFiles)
|
2021-03-11 15:44:01 +01:00
|
|
|
return shouldRetry(ctx, resp, err)
|
2019-06-26 20:39:01 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
return nil, fmt.Errorf("couldn't list files: %w", err)
|
2019-06-26 20:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
entries = make([]fs.DirEntry, len(sharedFiles))
|
|
|
|
|
|
|
|
for i, sharedFile := range sharedFiles {
|
|
|
|
entries[i] = f.newObjectFromFile(ctx, "", fileFromSharedFile(&sharedFile))
|
|
|
|
}
|
|
|
|
|
|
|
|
return entries, nil
|
|
|
|
}
|
|
|
|
|
2019-09-04 21:00:37 +02:00
|
|
|
func (f *Fs) listFiles(ctx context.Context, directoryID int) (filesList *FilesList, err error) {
|
2019-06-26 20:39:01 +02:00
|
|
|
// fs.Debugf(f, "Requesting files for dir `%s`", directoryID)
|
|
|
|
request := ListFilesRequest{
|
|
|
|
FolderID: directoryID,
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := rest.Opts{
|
|
|
|
Method: "POST",
|
|
|
|
Path: "/file/ls.cgi",
|
|
|
|
}
|
|
|
|
|
|
|
|
filesList = &FilesList{}
|
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
2019-09-04 21:00:37 +02:00
|
|
|
resp, err := f.rest.CallJSON(ctx, &opts, &request, filesList)
|
2021-03-11 15:44:01 +01:00
|
|
|
return shouldRetry(ctx, resp, err)
|
2019-06-26 20:39:01 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
return nil, fmt.Errorf("couldn't list files: %w", err)
|
2019-06-26 20:39:01 +02:00
|
|
|
}
|
2019-09-23 13:55:49 +02:00
|
|
|
for i := range filesList.Items {
|
|
|
|
item := &filesList.Items[i]
|
2020-01-14 18:33:35 +01:00
|
|
|
item.Filename = f.opt.Enc.ToStandardName(item.Filename)
|
2019-09-23 13:55:49 +02:00
|
|
|
}
|
2019-06-26 20:39:01 +02:00
|
|
|
|
|
|
|
return filesList, nil
|
|
|
|
}
|
|
|
|
|
2019-09-04 21:00:37 +02:00
|
|
|
func (f *Fs) listFolders(ctx context.Context, directoryID int) (foldersList *FoldersList, err error) {
|
2019-06-26 20:39:01 +02:00
|
|
|
// fs.Debugf(f, "Requesting folders for id `%s`", directoryID)
|
|
|
|
|
|
|
|
request := ListFolderRequest{
|
|
|
|
FolderID: directoryID,
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := rest.Opts{
|
|
|
|
Method: "POST",
|
|
|
|
Path: "/folder/ls.cgi",
|
|
|
|
}
|
|
|
|
|
|
|
|
foldersList = &FoldersList{}
|
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
2019-09-04 21:00:37 +02:00
|
|
|
resp, err := f.rest.CallJSON(ctx, &opts, &request, foldersList)
|
2021-03-11 15:44:01 +01:00
|
|
|
return shouldRetry(ctx, resp, err)
|
2019-06-26 20:39:01 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
return nil, fmt.Errorf("couldn't list folders: %w", err)
|
2019-06-26 20:39:01 +02:00
|
|
|
}
|
2020-01-14 18:33:35 +01:00
|
|
|
foldersList.Name = f.opt.Enc.ToStandardName(foldersList.Name)
|
2019-09-23 13:55:49 +02:00
|
|
|
for i := range foldersList.SubFolders {
|
|
|
|
folder := &foldersList.SubFolders[i]
|
2020-01-14 18:33:35 +01:00
|
|
|
folder.Name = f.opt.Enc.ToStandardName(folder.Name)
|
2019-09-23 13:55:49 +02:00
|
|
|
}
|
2019-06-26 20:39:01 +02:00
|
|
|
|
|
|
|
// fs.Debugf(f, "Got FoldersList for id `%s`", directoryID)
|
|
|
|
|
|
|
|
return foldersList, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Fs) listDir(ctx context.Context, dir string) (entries fs.DirEntries, err error) {
|
|
|
|
directoryID, err := f.dirCache.FindDir(ctx, dir, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-07-20 01:50:57 +02:00
|
|
|
folderID, err := strconv.Atoi(directoryID)
|
2019-06-26 20:39:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-09-04 21:00:37 +02:00
|
|
|
files, err := f.listFiles(ctx, folderID)
|
2019-07-20 01:50:57 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-09-04 21:00:37 +02:00
|
|
|
folders, err := f.listFolders(ctx, folderID)
|
2019-06-26 20:39:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
entries = make([]fs.DirEntry, len(files.Items)+len(folders.SubFolders))
|
|
|
|
|
|
|
|
for i, item := range files.Items {
|
|
|
|
entries[i] = f.newObjectFromFile(ctx, dir, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, folder := range folders.SubFolders {
|
|
|
|
createDate, err := time.Parse("2006-01-02 15:04:05", folder.CreateDate)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fullPath := getRemote(dir, folder.Name)
|
|
|
|
folderID := strconv.Itoa(folder.ID)
|
|
|
|
|
|
|
|
entries[len(files.Items)+i] = fs.NewDir(fullPath, createDate).SetID(folderID)
|
|
|
|
|
|
|
|
// fs.Debugf(f, "Put Path `%s` for id `%d` into dircache", fullPath, folder.ID)
|
|
|
|
f.dirCache.Put(fullPath, folderID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return entries, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Fs) newObjectFromFile(ctx context.Context, dir string, item File) *Object {
|
|
|
|
return &Object{
|
|
|
|
fs: f,
|
|
|
|
remote: getRemote(dir, item.Filename),
|
|
|
|
file: item,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRemote(dir, fileName string) string {
|
|
|
|
if dir == "" {
|
|
|
|
return fileName
|
|
|
|
}
|
|
|
|
|
|
|
|
return dir + "/" + fileName
|
|
|
|
}
|
|
|
|
|
2019-09-04 21:00:37 +02:00
|
|
|
func (f *Fs) makeFolder(ctx context.Context, leaf string, folderID int) (response *MakeFolderResponse, err error) {
|
2020-01-14 18:33:35 +01:00
|
|
|
name := f.opt.Enc.FromStandardName(leaf)
|
2019-06-26 20:39:01 +02:00
|
|
|
// fs.Debugf(f, "Creating folder `%s` in id `%s`", name, directoryID)
|
|
|
|
|
|
|
|
request := MakeFolderRequest{
|
2019-07-20 01:50:57 +02:00
|
|
|
FolderID: folderID,
|
2019-06-26 20:39:01 +02:00
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := rest.Opts{
|
|
|
|
Method: "POST",
|
|
|
|
Path: "/folder/mkdir.cgi",
|
|
|
|
}
|
|
|
|
|
|
|
|
response = &MakeFolderResponse{}
|
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
2019-09-04 21:00:37 +02:00
|
|
|
resp, err := f.rest.CallJSON(ctx, &opts, &request, response)
|
2021-03-11 15:44:01 +01:00
|
|
|
return shouldRetry(ctx, resp, err)
|
2019-06-26 20:39:01 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
return nil, fmt.Errorf("couldn't create folder: %w", err)
|
2019-06-26 20:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// fs.Debugf(f, "Created Folder `%s` in id `%s`", name, directoryID)
|
|
|
|
|
|
|
|
return response, err
|
|
|
|
}
|
|
|
|
|
2019-09-04 21:00:37 +02:00
|
|
|
func (f *Fs) removeFolder(ctx context.Context, name string, folderID int) (response *GenericOKResponse, err error) {
|
2019-06-26 20:39:01 +02:00
|
|
|
// fs.Debugf(f, "Removing folder with id `%s`", directoryID)
|
|
|
|
|
|
|
|
request := &RemoveFolderRequest{
|
2019-07-20 01:50:57 +02:00
|
|
|
FolderID: folderID,
|
2019-06-26 20:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
opts := rest.Opts{
|
|
|
|
Method: "POST",
|
|
|
|
Path: "/folder/rm.cgi",
|
|
|
|
}
|
|
|
|
|
|
|
|
response = &GenericOKResponse{}
|
|
|
|
var resp *http.Response
|
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
2019-09-04 21:00:37 +02:00
|
|
|
resp, err = f.rest.CallJSON(ctx, &opts, request, response)
|
2021-03-11 15:44:01 +01:00
|
|
|
return shouldRetry(ctx, resp, err)
|
2019-06-26 20:39:01 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
return nil, fmt.Errorf("couldn't remove folder: %w", err)
|
2019-06-26 20:39:01 +02:00
|
|
|
}
|
|
|
|
if response.Status != "OK" {
|
2021-11-04 11:12:57 +01:00
|
|
|
return nil, fmt.Errorf("can't remove folder: %s", response.Message)
|
2019-06-26 20:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// fs.Debugf(f, "Removed Folder with id `%s`", directoryID)
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
2019-09-04 21:00:37 +02:00
|
|
|
func (f *Fs) deleteFile(ctx context.Context, url string) (response *GenericOKResponse, err error) {
|
2019-06-26 20:39:01 +02:00
|
|
|
request := &RemoveFileRequest{
|
|
|
|
Files: []RmFile{
|
|
|
|
{url},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := rest.Opts{
|
|
|
|
Method: "POST",
|
|
|
|
Path: "/file/rm.cgi",
|
|
|
|
}
|
|
|
|
|
|
|
|
response = &GenericOKResponse{}
|
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
2019-09-04 21:00:37 +02:00
|
|
|
resp, err := f.rest.CallJSON(ctx, &opts, request, response)
|
2021-03-11 15:44:01 +01:00
|
|
|
return shouldRetry(ctx, resp, err)
|
2019-06-26 20:39:01 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
return nil, fmt.Errorf("couldn't remove file: %w", err)
|
2019-06-26 20:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// fs.Debugf(f, "Removed file with url `%s`", url)
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
2021-02-22 00:37:38 +01:00
|
|
|
func (f *Fs) moveFile(ctx context.Context, url string, folderID int, rename string) (response *MoveFileResponse, err error) {
|
|
|
|
request := &MoveFileRequest{
|
|
|
|
URLs: []string{url},
|
|
|
|
FolderID: folderID,
|
|
|
|
Rename: rename,
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := rest.Opts{
|
|
|
|
Method: "POST",
|
|
|
|
Path: "/file/mv.cgi",
|
|
|
|
}
|
|
|
|
|
|
|
|
response = &MoveFileResponse{}
|
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
|
|
|
resp, err := f.rest.CallJSON(ctx, &opts, request, response)
|
2021-03-11 15:44:01 +01:00
|
|
|
return shouldRetry(ctx, resp, err)
|
2021-02-22 00:37:38 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
return nil, fmt.Errorf("couldn't copy file: %w", err)
|
2021-02-22 00:37:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Fs) copyFile(ctx context.Context, url string, folderID int, rename string) (response *CopyFileResponse, err error) {
|
|
|
|
request := &CopyFileRequest{
|
|
|
|
URLs: []string{url},
|
|
|
|
FolderID: folderID,
|
|
|
|
Rename: rename,
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := rest.Opts{
|
|
|
|
Method: "POST",
|
|
|
|
Path: "/file/cp.cgi",
|
|
|
|
}
|
|
|
|
|
|
|
|
response = &CopyFileResponse{}
|
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
|
|
|
resp, err := f.rest.CallJSON(ctx, &opts, request, response)
|
2021-03-11 15:44:01 +01:00
|
|
|
return shouldRetry(ctx, resp, err)
|
2021-02-22 00:37:38 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
return nil, fmt.Errorf("couldn't copy file: %w", err)
|
2021-02-22 00:37:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
2021-06-10 14:11:00 +02:00
|
|
|
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 {
|
2021-11-04 11:12:57 +01:00
|
|
|
return nil, fmt.Errorf("couldn't rename file: %w", err)
|
2021-06-10 14:11:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
2019-09-04 21:00:37 +02:00
|
|
|
func (f *Fs) getUploadNode(ctx context.Context) (response *GetUploadNodeResponse, err error) {
|
2019-06-26 20:39:01 +02:00
|
|
|
// fs.Debugf(f, "Requesting Upload node")
|
|
|
|
|
|
|
|
opts := rest.Opts{
|
|
|
|
Method: "GET",
|
|
|
|
ContentType: "application/json", // 1Fichier API is bad
|
|
|
|
Path: "/upload/get_upload_server.cgi",
|
|
|
|
}
|
|
|
|
|
|
|
|
response = &GetUploadNodeResponse{}
|
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
2019-09-04 21:00:37 +02:00
|
|
|
resp, err := f.rest.CallJSON(ctx, &opts, nil, response)
|
2021-03-11 15:44:01 +01:00
|
|
|
return shouldRetry(ctx, resp, err)
|
2019-06-26 20:39:01 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
return nil, fmt.Errorf("didnt got an upload node: %w", err)
|
2019-06-26 20:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// fs.Debugf(f, "Got Upload node")
|
|
|
|
|
|
|
|
return response, err
|
|
|
|
}
|
|
|
|
|
2020-03-21 23:11:02 +01:00
|
|
|
func (f *Fs) uploadFile(ctx context.Context, in io.Reader, size int64, fileName, folderID, uploadID, node string, options ...fs.OpenOption) (response *http.Response, err error) {
|
2019-06-26 20:39:01 +02:00
|
|
|
// fs.Debugf(f, "Uploading File `%s`", fileName)
|
|
|
|
|
2020-01-14 18:33:35 +01:00
|
|
|
fileName = f.opt.Enc.FromStandardName(fileName)
|
2019-08-12 16:09:24 +02:00
|
|
|
|
2019-06-26 20:39:01 +02:00
|
|
|
if len(uploadID) > 10 || !isAlphaNumeric(uploadID) {
|
|
|
|
return nil, errors.New("Invalid UploadID")
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := rest.Opts{
|
|
|
|
Method: "POST",
|
|
|
|
Path: "/upload.cgi",
|
|
|
|
Parameters: map[string][]string{
|
|
|
|
"id": {uploadID},
|
|
|
|
},
|
|
|
|
NoResponse: true,
|
|
|
|
Body: in,
|
|
|
|
ContentLength: &size,
|
2020-03-21 23:11:02 +01:00
|
|
|
Options: options,
|
2019-06-26 20:39:01 +02:00
|
|
|
MultipartContentName: "file[]",
|
|
|
|
MultipartFileName: fileName,
|
|
|
|
MultipartParams: map[string][]string{
|
|
|
|
"did": {folderID},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if node != "" {
|
|
|
|
opts.RootURL = "https://" + node
|
|
|
|
}
|
|
|
|
|
|
|
|
err = f.pacer.CallNoRetry(func() (bool, error) {
|
2019-09-04 21:00:37 +02:00
|
|
|
resp, err := f.rest.CallJSON(ctx, &opts, nil, nil)
|
2021-03-11 15:44:01 +01:00
|
|
|
return shouldRetry(ctx, resp, err)
|
2019-06-26 20:39:01 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
return nil, fmt.Errorf("couldn't upload file: %w", err)
|
2019-06-26 20:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// fs.Debugf(f, "Uploaded File `%s`", fileName)
|
|
|
|
|
|
|
|
return response, err
|
|
|
|
}
|
|
|
|
|
2019-09-04 21:00:37 +02:00
|
|
|
func (f *Fs) endUpload(ctx context.Context, uploadID string, nodeurl string) (response *EndFileUploadResponse, err error) {
|
2019-06-26 20:39:01 +02:00
|
|
|
// fs.Debugf(f, "Ending File Upload `%s`", uploadID)
|
|
|
|
|
|
|
|
if len(uploadID) > 10 || !isAlphaNumeric(uploadID) {
|
|
|
|
return nil, errors.New("Invalid UploadID")
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := rest.Opts{
|
|
|
|
Method: "GET",
|
|
|
|
Path: "/end.pl",
|
|
|
|
RootURL: "https://" + nodeurl,
|
|
|
|
Parameters: map[string][]string{
|
|
|
|
"xid": {uploadID},
|
|
|
|
},
|
|
|
|
ExtraHeaders: map[string]string{
|
|
|
|
"JSON": "1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
response = &EndFileUploadResponse{}
|
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
2019-09-04 21:00:37 +02:00
|
|
|
resp, err := f.rest.CallJSON(ctx, &opts, nil, response)
|
2021-03-11 15:44:01 +01:00
|
|
|
return shouldRetry(ctx, resp, err)
|
2019-06-26 20:39:01 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
return nil, fmt.Errorf("couldn't finish file upload: %w", err)
|
2019-06-26 20:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return response, err
|
|
|
|
}
|