2017-06-02 14:51:38 +08:00
|
|
|
|
package opendrive
|
|
|
|
|
|
|
|
|
|
import (
|
2019-06-17 16:34:30 +08:00
|
|
|
|
"context"
|
2021-11-04 18:12:57 +08:00
|
|
|
|
"errors"
|
2018-04-27 05:02:31 +08:00
|
|
|
|
"fmt"
|
2017-06-02 14:51:38 +08:00
|
|
|
|
"io"
|
|
|
|
|
"net/http"
|
2018-10-17 04:55:19 +08:00
|
|
|
|
"net/url"
|
2017-07-17 13:36:45 +08:00
|
|
|
|
"path"
|
2017-06-02 14:51:38 +08:00
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
|
2019-07-29 01:47:38 +08:00
|
|
|
|
"github.com/rclone/rclone/fs"
|
2020-01-15 01:33:35 +08:00
|
|
|
|
"github.com/rclone/rclone/fs/config"
|
2019-07-29 01:47:38 +08:00
|
|
|
|
"github.com/rclone/rclone/fs/config/configmap"
|
|
|
|
|
"github.com/rclone/rclone/fs/config/configstruct"
|
|
|
|
|
"github.com/rclone/rclone/fs/config/obscure"
|
|
|
|
|
"github.com/rclone/rclone/fs/fserrors"
|
|
|
|
|
"github.com/rclone/rclone/fs/fshttp"
|
|
|
|
|
"github.com/rclone/rclone/fs/hash"
|
|
|
|
|
"github.com/rclone/rclone/lib/dircache"
|
2020-01-15 01:33:35 +08:00
|
|
|
|
"github.com/rclone/rclone/lib/encoder"
|
2019-07-29 01:47:38 +08:00
|
|
|
|
"github.com/rclone/rclone/lib/pacer"
|
|
|
|
|
"github.com/rclone/rclone/lib/readers"
|
|
|
|
|
"github.com/rclone/rclone/lib/rest"
|
2017-06-02 14:51:38 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
defaultEndpoint = "https://dev.opendrive.com/api/v1"
|
|
|
|
|
minSleep = 10 * time.Millisecond
|
|
|
|
|
maxSleep = 5 * time.Minute
|
|
|
|
|
decayConstant = 1 // bigger for slower decay, exponential
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Register with Fs
|
|
|
|
|
func init() {
|
|
|
|
|
fs.Register(&fs.RegInfo{
|
|
|
|
|
Name: "opendrive",
|
2017-07-17 13:36:45 +08:00
|
|
|
|
Description: "OpenDrive",
|
2017-06-02 14:51:38 +08:00
|
|
|
|
NewFs: NewFs,
|
|
|
|
|
Options: []fs.Option{{
|
2018-05-15 01:06:57 +08:00
|
|
|
|
Name: "username",
|
2021-08-16 17:30:01 +08:00
|
|
|
|
Help: "Username.",
|
2018-05-15 01:06:57 +08:00
|
|
|
|
Required: true,
|
2017-06-02 14:51:38 +08:00
|
|
|
|
}, {
|
|
|
|
|
Name: "password",
|
|
|
|
|
Help: "Password.",
|
|
|
|
|
IsPassword: true,
|
2018-05-15 01:06:57 +08:00
|
|
|
|
Required: true,
|
2020-01-15 01:33:35 +08:00
|
|
|
|
}, {
|
|
|
|
|
Name: config.ConfigEncoding,
|
|
|
|
|
Help: config.ConfigEncodingHelp,
|
|
|
|
|
Advanced: true,
|
2020-01-15 05:51:49 +08:00
|
|
|
|
// List of replaced characters:
|
|
|
|
|
// < (less than) -> '<' // FULLWIDTH LESS-THAN SIGN
|
|
|
|
|
// > (greater than) -> '>' // FULLWIDTH GREATER-THAN SIGN
|
|
|
|
|
// : (colon) -> ':' // FULLWIDTH COLON
|
|
|
|
|
// " (double quote) -> '"' // FULLWIDTH QUOTATION MARK
|
|
|
|
|
// \ (backslash) -> '\' // FULLWIDTH REVERSE SOLIDUS
|
|
|
|
|
// | (vertical line) -> '|' // FULLWIDTH VERTICAL LINE
|
|
|
|
|
// ? (question mark) -> '?' // FULLWIDTH QUESTION MARK
|
|
|
|
|
// * (asterisk) -> '*' // FULLWIDTH ASTERISK
|
|
|
|
|
//
|
2020-05-25 14:05:53 +08:00
|
|
|
|
// Additionally names can't begin or end with an ASCII whitespace.
|
2020-01-15 05:51:49 +08:00
|
|
|
|
// List of replaced characters:
|
|
|
|
|
// (space) -> '␠' // SYMBOL FOR SPACE
|
|
|
|
|
// (horizontal tab) -> '␉' // SYMBOL FOR HORIZONTAL TABULATION
|
|
|
|
|
// (line feed) -> '␊' // SYMBOL FOR LINE FEED
|
|
|
|
|
// (vertical tab) -> '␋' // SYMBOL FOR VERTICAL TABULATION
|
|
|
|
|
// (carriage return) -> '␍' // SYMBOL FOR CARRIAGE RETURN
|
|
|
|
|
//
|
|
|
|
|
// Also encode invalid UTF-8 bytes as json doesn't handle them properly.
|
|
|
|
|
//
|
|
|
|
|
// https://www.opendrive.com/wp-content/uploads/guides/OpenDrive_API_guide.pdf
|
|
|
|
|
Default: (encoder.Base |
|
|
|
|
|
encoder.EncodeWin |
|
|
|
|
|
encoder.EncodeLeftCrLfHtVt |
|
|
|
|
|
encoder.EncodeRightCrLfHtVt |
|
|
|
|
|
encoder.EncodeBackSlash |
|
|
|
|
|
encoder.EncodeLeftSpace |
|
|
|
|
|
encoder.EncodeRightSpace |
|
|
|
|
|
encoder.EncodeInvalidUtf8),
|
2019-11-15 19:22:13 +08:00
|
|
|
|
}, {
|
|
|
|
|
Name: "chunk_size",
|
|
|
|
|
Help: `Files will be uploaded in chunks this size.
|
|
|
|
|
|
|
|
|
|
Note that these chunks are buffered in memory so increasing them will
|
|
|
|
|
increase memory use.`,
|
2021-03-03 03:11:57 +08:00
|
|
|
|
Default: 10 * fs.Mebi,
|
2019-11-15 19:22:13 +08:00
|
|
|
|
Advanced: true,
|
2017-06-02 14:51:38 +08:00
|
|
|
|
}},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-15 01:06:57 +08:00
|
|
|
|
// Options defines the configuration for this backend
|
|
|
|
|
type Options struct {
|
2019-11-15 19:22:13 +08:00
|
|
|
|
UserName string `config:"username"`
|
|
|
|
|
Password string `config:"password"`
|
|
|
|
|
Enc encoder.MultiEncoder `config:"encoding"`
|
|
|
|
|
ChunkSize fs.SizeSuffix `config:"chunk_size"`
|
2018-05-15 01:06:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-27 05:02:31 +08:00
|
|
|
|
// Fs represents a remote server
|
2017-06-02 14:51:38 +08:00
|
|
|
|
type Fs struct {
|
|
|
|
|
name string // name of this remote
|
2017-07-17 13:36:45 +08:00
|
|
|
|
root string // the path we are working on
|
2018-05-15 01:06:57 +08:00
|
|
|
|
opt Options // parsed options
|
2017-06-02 14:51:38 +08:00
|
|
|
|
features *fs.Features // optional features
|
2018-04-27 05:02:31 +08:00
|
|
|
|
srv *rest.Client // the connection to the server
|
2019-02-10 04:52:15 +08:00
|
|
|
|
pacer *fs.Pacer // To pace and retry the API calls
|
2017-06-02 14:51:38 +08:00
|
|
|
|
session UserSessionInfo // contains the session data
|
|
|
|
|
dirCache *dircache.DirCache // Map of directory path to directory id
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-27 05:02:31 +08:00
|
|
|
|
// Object describes an object
|
2017-06-02 14:51:38 +08:00
|
|
|
|
type Object struct {
|
|
|
|
|
fs *Fs // what this object is part of
|
|
|
|
|
remote string // The remote path
|
2018-04-27 05:02:31 +08:00
|
|
|
|
id string // ID of the file
|
2021-03-12 01:40:29 +08:00
|
|
|
|
parent string // ID of the parent directory
|
2017-06-02 14:51:38 +08:00
|
|
|
|
modTime time.Time // The modified time of the object if known
|
|
|
|
|
md5 string // MD5 hash if known
|
|
|
|
|
size int64 // Size of the object
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-27 05:02:31 +08:00
|
|
|
|
// parsePath parses an incoming 'url'
|
2017-06-02 14:51:38 +08:00
|
|
|
|
func parsePath(path string) (root string) {
|
|
|
|
|
root = strings.Trim(path, "/")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
// Name of the remote (as passed into NewFs)
|
|
|
|
|
func (f *Fs) Name() string {
|
|
|
|
|
return f.name
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Root of the remote (as passed into NewFs)
|
|
|
|
|
func (f *Fs) Root() string {
|
2017-07-17 13:36:45 +08:00
|
|
|
|
return f.root
|
2017-06-02 14:51:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// String converts this Fs to a string
|
|
|
|
|
func (f *Fs) String() string {
|
2017-07-17 13:36:45 +08:00
|
|
|
|
return fmt.Sprintf("OpenDrive root '%s'", f.root)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Features returns the optional features of this Fs
|
|
|
|
|
func (f *Fs) Features() *fs.Features {
|
|
|
|
|
return f.features
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Hashes returns the supported hash sets.
|
2017-07-17 13:36:45 +08:00
|
|
|
|
func (f *Fs) Hashes() hash.Set {
|
|
|
|
|
return hash.Set(hash.MD5)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-27 05:02:31 +08:00
|
|
|
|
// DirCacheFlush resets the directory cache - used in testing as an
|
|
|
|
|
// optional interface
|
|
|
|
|
func (f *Fs) DirCacheFlush() {
|
|
|
|
|
f.dirCache.ResetRoot()
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-08 01:41:17 +08:00
|
|
|
|
// NewFs constructs an Fs from the path, bucket:path
|
2020-11-05 23:18:51 +08:00
|
|
|
|
func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, error) {
|
2018-05-15 01:06:57 +08:00
|
|
|
|
// Parse config into Options struct
|
|
|
|
|
opt := new(Options)
|
|
|
|
|
err := configstruct.Set(m, opt)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2017-06-02 14:51:38 +08:00
|
|
|
|
root = parsePath(root)
|
2018-05-15 01:06:57 +08:00
|
|
|
|
if opt.UserName == "" {
|
2017-06-02 14:51:38 +08:00
|
|
|
|
return nil, errors.New("username not found")
|
|
|
|
|
}
|
2018-05-15 01:06:57 +08:00
|
|
|
|
opt.Password, err = obscure.Reveal(opt.Password)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
if err != nil {
|
2018-05-15 01:06:57 +08:00
|
|
|
|
return nil, errors.New("password could not revealed")
|
2017-06-02 14:51:38 +08:00
|
|
|
|
}
|
2018-05-15 01:06:57 +08:00
|
|
|
|
if opt.Password == "" {
|
2017-06-02 14:51:38 +08:00
|
|
|
|
return nil, errors.New("password not found")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
f := &Fs{
|
2018-05-15 01:06:57 +08:00
|
|
|
|
name: name,
|
|
|
|
|
root: root,
|
|
|
|
|
opt: *opt,
|
2020-11-13 23:24:43 +08:00
|
|
|
|
srv: rest.NewClient(fshttp.NewClient(ctx)).SetErrorHandler(errorHandler),
|
2020-11-05 19:33:32 +08:00
|
|
|
|
pacer: fs.NewPacer(ctx, pacer.NewDefault(pacer.MinSleep(minSleep), pacer.MaxSleep(maxSleep), pacer.DecayConstant(decayConstant))),
|
2017-06-02 14:51:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
f.dirCache = dircache.New(root, "0", f)
|
|
|
|
|
|
|
|
|
|
// set the rootURL for the REST client
|
|
|
|
|
f.srv.SetRoot(defaultEndpoint)
|
|
|
|
|
|
|
|
|
|
// get sessionID
|
|
|
|
|
var resp *http.Response
|
|
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
2018-05-15 01:06:57 +08:00
|
|
|
|
account := Account{Username: opt.UserName, Password: opt.Password}
|
2017-06-02 14:51:38 +08:00
|
|
|
|
|
|
|
|
|
opts := rest.Opts{
|
|
|
|
|
Method: "POST",
|
|
|
|
|
Path: "/session/login.json",
|
|
|
|
|
}
|
2019-09-05 03:00:37 +08:00
|
|
|
|
resp, err = f.srv.CallJSON(ctx, &opts, &account, &f.session)
|
2021-03-11 22:44:01 +08:00
|
|
|
|
return f.shouldRetry(ctx, resp, err)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
|
return nil, fmt.Errorf("failed to create session: %w", err)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
}
|
2017-07-17 13:36:45 +08:00
|
|
|
|
fs.Debugf(nil, "Starting OpenDrive session with ID: %s", f.session.SessionID)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
|
2018-04-27 05:02:31 +08:00
|
|
|
|
f.features = (&fs.Features{
|
|
|
|
|
CaseInsensitive: true,
|
|
|
|
|
CanHaveEmptyDirectories: true,
|
2020-11-06 00:00:40 +08:00
|
|
|
|
}).Fill(ctx, f)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
|
|
|
|
|
// Find the current root
|
2019-06-17 16:34:30 +08:00
|
|
|
|
err = f.dirCache.FindRoot(ctx, false)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
// Assume it is a file
|
|
|
|
|
newRoot, remote := dircache.SplitPath(root)
|
2018-10-14 21:41:26 +08:00
|
|
|
|
tempF := *f
|
|
|
|
|
tempF.dirCache = dircache.New(newRoot, "0", &tempF)
|
|
|
|
|
tempF.root = newRoot
|
2017-06-02 14:51:38 +08:00
|
|
|
|
|
|
|
|
|
// Make new Fs which is the parent
|
2019-06-17 16:34:30 +08:00
|
|
|
|
err = tempF.dirCache.FindRoot(ctx, false)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
// No root so return old f
|
|
|
|
|
return f, nil
|
|
|
|
|
}
|
2021-03-12 01:40:29 +08:00
|
|
|
|
_, err := tempF.newObjectWithInfo(ctx, remote, nil, "")
|
2017-06-02 14:51:38 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
if err == fs.ErrorObjectNotFound {
|
|
|
|
|
// File doesn't exist so return old f
|
|
|
|
|
return f, nil
|
|
|
|
|
}
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2018-10-14 21:41:26 +08:00
|
|
|
|
// XXX: update the old f here instead of returning tempF, since
|
|
|
|
|
// `features` were already filled with functions having *f as a receiver.
|
2019-07-29 01:47:38 +08:00
|
|
|
|
// See https://github.com/rclone/rclone/issues/2182
|
2018-10-14 21:41:26 +08:00
|
|
|
|
f.dirCache = tempF.dirCache
|
|
|
|
|
f.root = tempF.root
|
2017-06-02 14:51:38 +08:00
|
|
|
|
// return an error with an fs which points to the parent
|
2018-10-14 21:41:26 +08:00
|
|
|
|
return f, fs.ErrorIsFile
|
2017-06-02 14:51:38 +08:00
|
|
|
|
}
|
|
|
|
|
return f, nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-17 13:36:45 +08:00
|
|
|
|
// rootSlash returns root with a slash on if it is empty, otherwise empty string
|
|
|
|
|
func (f *Fs) rootSlash() string {
|
|
|
|
|
if f.root == "" {
|
|
|
|
|
return f.root
|
|
|
|
|
}
|
|
|
|
|
return f.root + "/"
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-02 14:51:38 +08:00
|
|
|
|
// errorHandler parses a non 2xx error response into an error
|
|
|
|
|
func errorHandler(resp *http.Response) error {
|
2018-04-27 05:02:31 +08:00
|
|
|
|
errResponse := new(Error)
|
|
|
|
|
err := rest.DecodeJSON(resp, &errResponse)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fs.Debugf(nil, "Couldn't decode error response: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if errResponse.Info.Code == 0 {
|
|
|
|
|
errResponse.Info.Code = resp.StatusCode
|
|
|
|
|
}
|
|
|
|
|
if errResponse.Info.Message == "" {
|
|
|
|
|
errResponse.Info.Message = "Unknown " + resp.Status
|
|
|
|
|
}
|
|
|
|
|
return errResponse
|
2017-06-02 14:51:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-17 13:36:45 +08:00
|
|
|
|
// Mkdir creates the folder if it doesn't exist
|
2019-06-17 16:34:30 +08:00
|
|
|
|
func (f *Fs) Mkdir(ctx context.Context, dir string) error {
|
2018-04-27 05:02:31 +08:00
|
|
|
|
// fs.Debugf(nil, "Mkdir(\"%s\")", dir)
|
2020-05-12 00:24:37 +08:00
|
|
|
|
_, err := f.dirCache.FindDir(ctx, dir, true)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-17 13:36:45 +08:00
|
|
|
|
// deleteObject removes an object by ID
|
2019-09-05 03:00:37 +08:00
|
|
|
|
func (f *Fs) deleteObject(ctx context.Context, id string) error {
|
2017-07-17 13:36:45 +08:00
|
|
|
|
return f.pacer.Call(func() (bool, error) {
|
|
|
|
|
removeDirData := removeFolder{SessionID: f.session.SessionID, FolderID: id}
|
|
|
|
|
opts := rest.Opts{
|
|
|
|
|
Method: "POST",
|
|
|
|
|
NoResponse: true,
|
|
|
|
|
Path: "/folder/remove.json",
|
|
|
|
|
}
|
2019-09-05 03:00:37 +08:00
|
|
|
|
resp, err := f.srv.CallJSON(ctx, &opts, &removeDirData, nil)
|
2021-03-11 22:44:01 +08:00
|
|
|
|
return f.shouldRetry(ctx, resp, err)
|
2017-07-17 13:36:45 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// purgeCheck remotes the root directory, if check is set then it
|
|
|
|
|
// refuses to do so if it has anything in
|
2019-06-17 16:34:30 +08:00
|
|
|
|
func (f *Fs) purgeCheck(ctx context.Context, dir string, check bool) error {
|
2017-07-17 13:36:45 +08:00
|
|
|
|
root := path.Join(f.root, dir)
|
|
|
|
|
if root == "" {
|
|
|
|
|
return errors.New("can't purge root directory")
|
|
|
|
|
}
|
|
|
|
|
dc := f.dirCache
|
2019-06-17 16:34:30 +08:00
|
|
|
|
rootID, err := dc.FindDir(ctx, dir, false)
|
2017-07-17 13:36:45 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2019-09-05 03:00:37 +08:00
|
|
|
|
item, err := f.readMetaDataForFolderID(ctx, rootID)
|
2017-07-17 13:36:45 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if check && len(item.Files) != 0 {
|
|
|
|
|
return errors.New("folder not empty")
|
|
|
|
|
}
|
2019-09-05 03:00:37 +08:00
|
|
|
|
err = f.deleteObject(ctx, rootID)
|
2017-07-17 13:36:45 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
f.dirCache.FlushDir(dir)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Rmdir deletes the root folder
|
2017-06-02 14:51:38 +08:00
|
|
|
|
//
|
|
|
|
|
// Returns an error if it isn't empty
|
2019-06-17 16:34:30 +08:00
|
|
|
|
func (f *Fs) Rmdir(ctx context.Context, dir string) error {
|
2018-04-27 05:02:31 +08:00
|
|
|
|
// fs.Debugf(nil, "Rmdir(\"%s\")", path.Join(f.root, dir))
|
2019-06-17 16:34:30 +08:00
|
|
|
|
return f.purgeCheck(ctx, dir, true)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Precision of the remote
|
|
|
|
|
func (f *Fs) Precision() time.Duration {
|
2017-07-17 13:36:45 +08:00
|
|
|
|
return time.Second
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-14 05:43:40 +08:00
|
|
|
|
// Copy src to this remote using server-side copy operations.
|
2017-07-17 13:36:45 +08:00
|
|
|
|
//
|
|
|
|
|
// This is stored with the remote path given
|
|
|
|
|
//
|
|
|
|
|
// It returns the destination Object and a possible error
|
|
|
|
|
//
|
|
|
|
|
// Will only be called if src.Fs().Name() == f.Name()
|
|
|
|
|
//
|
|
|
|
|
// If it isn't possible then return fs.ErrorCantCopy
|
2019-06-17 16:34:30 +08:00
|
|
|
|
func (f *Fs) Copy(ctx context.Context, src fs.Object, remote string) (fs.Object, error) {
|
2018-04-27 05:02:31 +08:00
|
|
|
|
// fs.Debugf(nil, "Copy(%v)", remote)
|
2017-07-17 13:36:45 +08:00
|
|
|
|
srcObj, ok := src.(*Object)
|
|
|
|
|
if !ok {
|
|
|
|
|
fs.Debugf(src, "Can't copy - not same remote type")
|
|
|
|
|
return nil, fs.ErrorCantCopy
|
|
|
|
|
}
|
2019-06-17 16:34:30 +08:00
|
|
|
|
err := srcObj.readMetaData(ctx)
|
2017-07-17 13:36:45 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
srcPath := srcObj.fs.rootSlash() + srcObj.remote
|
|
|
|
|
dstPath := f.rootSlash() + remote
|
|
|
|
|
if strings.ToLower(srcPath) == strings.ToLower(dstPath) {
|
2021-11-04 18:12:57 +08:00
|
|
|
|
return nil, fmt.Errorf("Can't copy %q -> %q as are same name when lowercase", srcPath, dstPath)
|
2017-07-17 13:36:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create temporary object
|
2019-06-17 16:34:30 +08:00
|
|
|
|
dstObj, leaf, directoryID, err := f.createObject(ctx, remote, srcObj.modTime, srcObj.size)
|
2017-07-17 13:36:45 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2018-04-27 05:02:31 +08:00
|
|
|
|
// fs.Debugf(nil, "...%#v\n...%#v", remote, directoryID)
|
2017-07-17 13:36:45 +08:00
|
|
|
|
|
|
|
|
|
// Copy the object
|
|
|
|
|
var resp *http.Response
|
2018-04-27 05:02:31 +08:00
|
|
|
|
response := moveCopyFileResponse{}
|
2017-07-17 13:36:45 +08:00
|
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
2018-04-27 05:02:31 +08:00
|
|
|
|
copyFileData := moveCopyFile{
|
2017-07-17 13:36:45 +08:00
|
|
|
|
SessionID: f.session.SessionID,
|
|
|
|
|
SrcFileID: srcObj.id,
|
|
|
|
|
DstFolderID: directoryID,
|
|
|
|
|
Move: "false",
|
|
|
|
|
OverwriteIfExists: "true",
|
2018-04-27 05:02:31 +08:00
|
|
|
|
NewFileName: leaf,
|
2017-07-17 13:36:45 +08:00
|
|
|
|
}
|
|
|
|
|
opts := rest.Opts{
|
|
|
|
|
Method: "POST",
|
|
|
|
|
Path: "/file/move_copy.json",
|
|
|
|
|
}
|
2019-09-05 03:00:37 +08:00
|
|
|
|
resp, err = f.srv.CallJSON(ctx, &opts, ©FileData, &response)
|
2021-03-11 22:44:01 +08:00
|
|
|
|
return f.shouldRetry(ctx, resp, err)
|
2017-07-17 13:36:45 +08:00
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size, _ := strconv.ParseInt(response.Size, 10, 64)
|
|
|
|
|
dstObj.id = response.FileID
|
|
|
|
|
dstObj.size = size
|
|
|
|
|
|
|
|
|
|
return dstObj, nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-14 05:43:40 +08:00
|
|
|
|
// Move src to this remote using server-side move operations.
|
2017-07-17 13:36:45 +08:00
|
|
|
|
//
|
|
|
|
|
// This is stored with the remote path given
|
|
|
|
|
//
|
|
|
|
|
// It returns the destination Object and a possible error
|
|
|
|
|
//
|
|
|
|
|
// Will only be called if src.Fs().Name() == f.Name()
|
|
|
|
|
//
|
|
|
|
|
// If it isn't possible then return fs.ErrorCantMove
|
2019-06-17 16:34:30 +08:00
|
|
|
|
func (f *Fs) Move(ctx context.Context, src fs.Object, remote string) (fs.Object, error) {
|
2018-04-27 05:02:31 +08:00
|
|
|
|
// fs.Debugf(nil, "Move(%v)", remote)
|
2017-07-17 13:36:45 +08:00
|
|
|
|
srcObj, ok := src.(*Object)
|
|
|
|
|
if !ok {
|
|
|
|
|
fs.Debugf(src, "Can't move - not same remote type")
|
|
|
|
|
return nil, fs.ErrorCantCopy
|
|
|
|
|
}
|
2019-06-17 16:34:30 +08:00
|
|
|
|
err := srcObj.readMetaData(ctx)
|
2017-07-17 13:36:45 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create temporary object
|
2019-06-17 16:34:30 +08:00
|
|
|
|
dstObj, leaf, directoryID, err := f.createObject(ctx, remote, srcObj.modTime, srcObj.size)
|
2017-07-17 13:36:45 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Copy the object
|
|
|
|
|
var resp *http.Response
|
2018-04-27 05:02:31 +08:00
|
|
|
|
response := moveCopyFileResponse{}
|
2017-07-17 13:36:45 +08:00
|
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
2018-04-27 05:02:31 +08:00
|
|
|
|
copyFileData := moveCopyFile{
|
2017-07-17 13:36:45 +08:00
|
|
|
|
SessionID: f.session.SessionID,
|
|
|
|
|
SrcFileID: srcObj.id,
|
|
|
|
|
DstFolderID: directoryID,
|
|
|
|
|
Move: "true",
|
|
|
|
|
OverwriteIfExists: "true",
|
2018-04-27 05:02:31 +08:00
|
|
|
|
NewFileName: leaf,
|
2017-07-17 13:36:45 +08:00
|
|
|
|
}
|
|
|
|
|
opts := rest.Opts{
|
|
|
|
|
Method: "POST",
|
|
|
|
|
Path: "/file/move_copy.json",
|
|
|
|
|
}
|
2019-09-05 03:00:37 +08:00
|
|
|
|
resp, err = f.srv.CallJSON(ctx, &opts, ©FileData, &response)
|
2021-03-11 22:44:01 +08:00
|
|
|
|
return f.shouldRetry(ctx, resp, err)
|
2017-07-17 13:36:45 +08:00
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size, _ := strconv.ParseInt(response.Size, 10, 64)
|
|
|
|
|
dstObj.id = response.FileID
|
|
|
|
|
dstObj.size = size
|
|
|
|
|
|
|
|
|
|
return dstObj, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DirMove moves src, srcRemote to this remote at dstRemote
|
2020-10-14 05:43:40 +08:00
|
|
|
|
// using server-side move operations.
|
2017-07-17 13:36:45 +08:00
|
|
|
|
//
|
|
|
|
|
// Will only be called if src.Fs().Name() == f.Name()
|
|
|
|
|
//
|
|
|
|
|
// If it isn't possible then return fs.ErrorCantDirMove
|
|
|
|
|
//
|
|
|
|
|
// If destination exists then return fs.ErrorDirExists
|
2019-06-17 16:34:30 +08:00
|
|
|
|
func (f *Fs) DirMove(ctx context.Context, src fs.Fs, srcRemote, dstRemote string) (err error) {
|
2017-07-17 13:36:45 +08:00
|
|
|
|
srcFs, ok := src.(*Fs)
|
|
|
|
|
if !ok {
|
2018-04-27 05:02:31 +08:00
|
|
|
|
fs.Debugf(srcFs, "Can't move directory - not same remote type")
|
2017-07-17 13:36:45 +08:00
|
|
|
|
return fs.ErrorCantDirMove
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-12 18:39:30 +08:00
|
|
|
|
srcID, _, _, dstDirectoryID, dstLeaf, err := f.dirCache.DirMove(ctx, srcFs.dirCache, srcFs.root, srcRemote, f.root, dstRemote)
|
2018-04-27 05:02:31 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Do the move
|
2017-07-17 13:36:45 +08:00
|
|
|
|
var resp *http.Response
|
2018-04-27 05:02:31 +08:00
|
|
|
|
response := moveCopyFolderResponse{}
|
2017-07-17 13:36:45 +08:00
|
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
2018-04-27 05:02:31 +08:00
|
|
|
|
moveFolderData := moveCopyFolder{
|
|
|
|
|
SessionID: f.session.SessionID,
|
|
|
|
|
FolderID: srcID,
|
2020-05-12 18:39:30 +08:00
|
|
|
|
DstFolderID: dstDirectoryID,
|
2018-04-27 05:02:31 +08:00
|
|
|
|
Move: "true",
|
2020-05-12 18:39:30 +08:00
|
|
|
|
NewFolderName: dstLeaf,
|
2017-07-17 13:36:45 +08:00
|
|
|
|
}
|
|
|
|
|
opts := rest.Opts{
|
|
|
|
|
Method: "POST",
|
|
|
|
|
Path: "/folder/move_copy.json",
|
|
|
|
|
}
|
2019-09-05 03:00:37 +08:00
|
|
|
|
resp, err = f.srv.CallJSON(ctx, &opts, &moveFolderData, &response)
|
2021-03-11 22:44:01 +08:00
|
|
|
|
return f.shouldRetry(ctx, resp, err)
|
2017-07-17 13:36:45 +08:00
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
fs.Debugf(src, "DirMove error %v", err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
srcFs.dirCache.FlushDir(srcRemote)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-05 05:25:14 +08:00
|
|
|
|
// Purge deletes all the files in the directory
|
2017-07-17 13:36:45 +08:00
|
|
|
|
//
|
|
|
|
|
// Optional interface: Only implement this if you have a way of
|
|
|
|
|
// deleting all the files quicker than just running Remove() on the
|
|
|
|
|
// result of List()
|
2020-06-05 05:25:14 +08:00
|
|
|
|
func (f *Fs) Purge(ctx context.Context, dir string) error {
|
|
|
|
|
return f.purgeCheck(ctx, dir, false)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Return an Object from a path
|
|
|
|
|
//
|
|
|
|
|
// If it can't be found it returns the error fs.ErrorObjectNotFound.
|
2021-03-12 01:40:29 +08:00
|
|
|
|
func (f *Fs) newObjectWithInfo(ctx context.Context, remote string, file *File, parent string) (fs.Object, error) {
|
2018-04-27 05:02:31 +08:00
|
|
|
|
// fs.Debugf(nil, "newObjectWithInfo(%s, %v)", remote, file)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
|
|
|
|
|
var o *Object
|
|
|
|
|
if nil != file {
|
|
|
|
|
o = &Object{
|
|
|
|
|
fs: f,
|
|
|
|
|
remote: remote,
|
|
|
|
|
id: file.FileID,
|
2021-03-12 01:40:29 +08:00
|
|
|
|
parent: parent,
|
2017-06-02 14:51:38 +08:00
|
|
|
|
modTime: time.Unix(file.DateModified, 0),
|
|
|
|
|
size: file.Size,
|
2017-07-17 13:36:45 +08:00
|
|
|
|
md5: file.FileHash,
|
2017-06-02 14:51:38 +08:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
o = &Object{
|
|
|
|
|
fs: f,
|
|
|
|
|
remote: remote,
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-17 16:34:30 +08:00
|
|
|
|
err := o.readMetaData(ctx)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return o, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewObject finds the Object at remote. If it can't be found
|
|
|
|
|
// it returns the error fs.ErrorObjectNotFound.
|
2019-06-17 16:34:30 +08:00
|
|
|
|
func (f *Fs) NewObject(ctx context.Context, remote string) (fs.Object, error) {
|
2018-04-27 05:02:31 +08:00
|
|
|
|
// fs.Debugf(nil, "NewObject(\"%s\")", remote)
|
2021-03-12 01:40:29 +08:00
|
|
|
|
return f.newObjectWithInfo(ctx, remote, nil, "")
|
2017-06-02 14:51:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Creates from the parameters passed in a half finished Object which
|
|
|
|
|
// must have setMetaData called on it
|
|
|
|
|
//
|
|
|
|
|
// Returns the object, leaf, directoryID and error
|
|
|
|
|
//
|
|
|
|
|
// Used to create new objects
|
2019-06-17 16:34:30 +08:00
|
|
|
|
func (f *Fs) createObject(ctx context.Context, remote string, modTime time.Time, size int64) (o *Object, leaf string, directoryID string, err error) {
|
2017-06-02 14:51:38 +08:00
|
|
|
|
// Create the directory for the object if it doesn't exist
|
2020-05-12 00:24:37 +08:00
|
|
|
|
leaf, directoryID, err = f.dirCache.FindPath(ctx, remote, true)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, leaf, directoryID, err
|
|
|
|
|
}
|
2018-04-27 05:02:31 +08:00
|
|
|
|
// fs.Debugf(nil, "\n...leaf %#v\n...id %#v", leaf, directoryID)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
// Temporary Object under construction
|
|
|
|
|
o = &Object{
|
|
|
|
|
fs: f,
|
|
|
|
|
remote: remote,
|
|
|
|
|
}
|
2020-01-15 01:33:35 +08:00
|
|
|
|
return o, f.opt.Enc.FromStandardName(leaf), directoryID, nil
|
2017-06-02 14:51:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-17 13:36:45 +08:00
|
|
|
|
// readMetaDataForPath reads the metadata from the path
|
2019-09-05 03:00:37 +08:00
|
|
|
|
func (f *Fs) readMetaDataForFolderID(ctx context.Context, id string) (info *FolderList, err error) {
|
2017-07-17 13:36:45 +08:00
|
|
|
|
var resp *http.Response
|
|
|
|
|
opts := rest.Opts{
|
|
|
|
|
Method: "GET",
|
|
|
|
|
Path: "/folder/list.json/" + f.session.SessionID + "/" + id,
|
|
|
|
|
}
|
|
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
2019-09-05 03:00:37 +08:00
|
|
|
|
resp, err = f.srv.CallJSON(ctx, &opts, nil, &info)
|
2021-03-11 22:44:01 +08:00
|
|
|
|
return f.shouldRetry(ctx, resp, err)
|
2017-07-17 13:36:45 +08:00
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if resp != nil {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return info, err
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-02 14:51:38 +08:00
|
|
|
|
// Put the object into the bucket
|
|
|
|
|
//
|
|
|
|
|
// Copy the reader in to the new object which is returned
|
|
|
|
|
//
|
|
|
|
|
// The new object may have been created if an error is returned
|
2019-06-17 16:34:30 +08:00
|
|
|
|
func (f *Fs) Put(ctx context.Context, in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) (fs.Object, error) {
|
2017-06-02 14:51:38 +08:00
|
|
|
|
remote := src.Remote()
|
|
|
|
|
size := src.Size()
|
2019-06-17 16:34:30 +08:00
|
|
|
|
modTime := src.ModTime(ctx)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
|
2018-04-27 05:02:31 +08:00
|
|
|
|
// fs.Debugf(nil, "Put(%s)", remote)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
|
2019-06-17 16:34:30 +08:00
|
|
|
|
o, leaf, directoryID, err := f.createObject(ctx, remote, modTime, size)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2017-07-17 13:36:45 +08:00
|
|
|
|
|
|
|
|
|
if "" == o.id {
|
2018-04-27 05:02:31 +08:00
|
|
|
|
// Attempt to read ID, ignore error
|
|
|
|
|
// FIXME is this correct?
|
2019-06-17 16:34:30 +08:00
|
|
|
|
_ = o.readMetaData(ctx)
|
2017-07-17 13:36:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if "" == o.id {
|
2020-05-20 18:39:20 +08:00
|
|
|
|
// We need to create an ID for this file
|
2017-07-17 13:36:45 +08:00
|
|
|
|
var resp *http.Response
|
|
|
|
|
response := createFileResponse{}
|
|
|
|
|
err := o.fs.pacer.Call(func() (bool, error) {
|
2018-11-02 20:14:30 +08:00
|
|
|
|
createFileData := createFile{
|
|
|
|
|
SessionID: o.fs.session.SessionID,
|
|
|
|
|
FolderID: directoryID,
|
|
|
|
|
Name: leaf,
|
|
|
|
|
}
|
2017-07-17 13:36:45 +08:00
|
|
|
|
opts := rest.Opts{
|
2020-03-22 05:50:17 +08:00
|
|
|
|
Method: "POST",
|
|
|
|
|
Options: options,
|
|
|
|
|
Path: "/upload/create_file.json",
|
2017-07-17 13:36:45 +08:00
|
|
|
|
}
|
2019-09-05 03:00:37 +08:00
|
|
|
|
resp, err = o.fs.srv.CallJSON(ctx, &opts, &createFileData, &response)
|
2021-03-11 22:44:01 +08:00
|
|
|
|
return o.fs.shouldRetry(ctx, resp, err)
|
2017-07-17 13:36:45 +08:00
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
|
return nil, fmt.Errorf("failed to create file: %w", err)
|
2017-07-17 13:36:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
o.id = response.FileID
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-17 16:34:30 +08:00
|
|
|
|
return o, o.Update(ctx, in, src, options...)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// retryErrorCodes is a slice of error codes that we will retry
|
|
|
|
|
var retryErrorCodes = []int{
|
|
|
|
|
401, // Unauthorized (seen in "Token has expired")
|
|
|
|
|
408, // Request Timeout
|
2018-04-27 05:02:31 +08:00
|
|
|
|
423, // Locked - get this on folders sometimes
|
2017-06-02 14:51:38 +08:00
|
|
|
|
429, // Rate exceeded.
|
|
|
|
|
500, // Get occasional 500 Internal Server Error
|
|
|
|
|
502, // Bad Gateway when doing big listings
|
|
|
|
|
503, // Service Unavailable
|
|
|
|
|
504, // Gateway Time-out
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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 22:44:01 +08:00
|
|
|
|
func (f *Fs) shouldRetry(ctx context.Context, resp *http.Response, err error) (bool, error) {
|
|
|
|
|
if fserrors.ContextError(ctx, &err) {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
2017-07-17 13:36:45 +08:00
|
|
|
|
return fserrors.ShouldRetry(err) || fserrors.ShouldRetryHTTP(resp, retryErrorCodes), err
|
2017-06-02 14:51:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-17 13:36:45 +08:00
|
|
|
|
// DirCacher methods
|
2017-06-02 14:51:38 +08:00
|
|
|
|
|
|
|
|
|
// CreateDir makes a directory with pathID as parent and name leaf
|
2019-06-17 16:34:30 +08:00
|
|
|
|
func (f *Fs) CreateDir(ctx context.Context, pathID, leaf string) (newID string, err error) {
|
2018-04-27 05:02:31 +08:00
|
|
|
|
// fs.Debugf(f, "CreateDir(%q, %q)\n", pathID, replaceReservedChars(leaf))
|
2017-07-17 13:36:45 +08:00
|
|
|
|
var resp *http.Response
|
|
|
|
|
response := createFolderResponse{}
|
|
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
|
|
|
|
createDirData := createFolder{
|
|
|
|
|
SessionID: f.session.SessionID,
|
2020-01-15 01:33:35 +08:00
|
|
|
|
FolderName: f.opt.Enc.FromStandardName(leaf),
|
2017-07-17 13:36:45 +08:00
|
|
|
|
FolderSubParent: pathID,
|
|
|
|
|
FolderIsPublic: 0,
|
|
|
|
|
FolderPublicUpl: 0,
|
|
|
|
|
FolderPublicDisplay: 0,
|
|
|
|
|
FolderPublicDnl: 0,
|
|
|
|
|
}
|
|
|
|
|
opts := rest.Opts{
|
|
|
|
|
Method: "POST",
|
|
|
|
|
Path: "/folder.json",
|
|
|
|
|
}
|
2019-09-05 03:00:37 +08:00
|
|
|
|
resp, err = f.srv.CallJSON(ctx, &opts, &createDirData, &response)
|
2021-03-11 22:44:01 +08:00
|
|
|
|
return f.shouldRetry(ctx, resp, err)
|
2017-07-17 13:36:45 +08:00
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response.FolderID, nil
|
2017-06-02 14:51:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FindLeaf finds a directory of name leaf in the folder with ID pathID
|
2019-06-17 16:34:30 +08:00
|
|
|
|
func (f *Fs) FindLeaf(ctx context.Context, pathID, leaf string) (pathIDOut string, found bool, err error) {
|
2018-04-27 05:02:31 +08:00
|
|
|
|
// fs.Debugf(nil, "FindLeaf(\"%s\", \"%s\")", pathID, leaf)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
|
|
|
|
|
if pathID == "0" && leaf == "" {
|
2018-04-27 05:02:31 +08:00
|
|
|
|
// fs.Debugf(nil, "Found OpenDrive root")
|
2017-06-02 14:51:38 +08:00
|
|
|
|
// that's the root directory
|
|
|
|
|
return pathID, true, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get the folderIDs
|
|
|
|
|
var resp *http.Response
|
|
|
|
|
folderList := FolderList{}
|
|
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
|
|
|
|
opts := rest.Opts{
|
|
|
|
|
Method: "GET",
|
|
|
|
|
Path: "/folder/list.json/" + f.session.SessionID + "/" + pathID,
|
|
|
|
|
}
|
2019-09-05 03:00:37 +08:00
|
|
|
|
resp, err = f.srv.CallJSON(ctx, &opts, nil, &folderList)
|
2021-03-11 22:44:01 +08:00
|
|
|
|
return f.shouldRetry(ctx, resp, err)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
|
return "", false, fmt.Errorf("failed to get folder list: %w", err)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-15 01:33:35 +08:00
|
|
|
|
leaf = f.opt.Enc.FromStandardName(leaf)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
for _, folder := range folderList.Folders {
|
2018-04-27 05:02:31 +08:00
|
|
|
|
// fs.Debugf(nil, "Folder: %s (%s)", folder.Name, folder.FolderID)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
|
2021-01-27 21:44:19 +08:00
|
|
|
|
if strings.EqualFold(leaf, folder.Name) {
|
2017-06-02 14:51:38 +08:00
|
|
|
|
// found
|
|
|
|
|
return folder.FolderID, true, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "", false, nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-17 13:36:45 +08:00
|
|
|
|
// List the objects and directories in dir into entries. The
|
|
|
|
|
// entries can be returned in any order but should be for a
|
|
|
|
|
// complete directory.
|
|
|
|
|
//
|
|
|
|
|
// dir should be "" to list the root, and should not have
|
|
|
|
|
// trailing slashes.
|
|
|
|
|
//
|
|
|
|
|
// This should return ErrDirNotFound if the directory isn't
|
|
|
|
|
// found.
|
2019-06-17 16:34:30 +08:00
|
|
|
|
func (f *Fs) List(ctx context.Context, dir string) (entries fs.DirEntries, err error) {
|
2018-04-27 05:02:31 +08:00
|
|
|
|
// fs.Debugf(nil, "List(%v)", dir)
|
2019-06-17 16:34:30 +08:00
|
|
|
|
directoryID, err := f.dirCache.FindDir(ctx, dir, false)
|
2017-07-17 13:36:45 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-02 14:51:38 +08:00
|
|
|
|
var resp *http.Response
|
2017-07-17 13:36:45 +08:00
|
|
|
|
opts := rest.Opts{
|
|
|
|
|
Method: "GET",
|
|
|
|
|
Path: "/folder/list.json/" + f.session.SessionID + "/" + directoryID,
|
|
|
|
|
}
|
2017-06-02 14:51:38 +08:00
|
|
|
|
folderList := FolderList{}
|
|
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
2019-09-05 03:00:37 +08:00
|
|
|
|
resp, err = f.srv.CallJSON(ctx, &opts, nil, &folderList)
|
2021-03-11 22:44:01 +08:00
|
|
|
|
return f.shouldRetry(ctx, resp, err)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
|
return nil, fmt.Errorf("failed to get folder list: %w", err)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, folder := range folderList.Folders {
|
2020-01-15 01:33:35 +08:00
|
|
|
|
folder.Name = f.opt.Enc.ToStandardName(folder.Name)
|
2018-04-27 05:02:31 +08:00
|
|
|
|
// fs.Debugf(nil, "Folder: %s (%s)", folder.Name, folder.FolderID)
|
2017-07-17 13:36:45 +08:00
|
|
|
|
remote := path.Join(dir, folder.Name)
|
|
|
|
|
// cache the directory ID for later lookups
|
|
|
|
|
f.dirCache.Put(remote, folder.FolderID)
|
2019-01-12 01:17:46 +08:00
|
|
|
|
d := fs.NewDir(remote, time.Unix(folder.DateModified, 0)).SetID(folder.FolderID)
|
2017-07-17 13:36:45 +08:00
|
|
|
|
d.SetItems(int64(folder.ChildFolders))
|
2021-03-12 01:40:29 +08:00
|
|
|
|
d.SetParentID(directoryID)
|
2017-07-17 13:36:45 +08:00
|
|
|
|
entries = append(entries, d)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, file := range folderList.Files {
|
2020-01-15 01:33:35 +08:00
|
|
|
|
file.Name = f.opt.Enc.ToStandardName(file.Name)
|
2018-04-27 05:02:31 +08:00
|
|
|
|
// fs.Debugf(nil, "File: %s (%s)", file.Name, file.FileID)
|
2017-07-17 13:36:45 +08:00
|
|
|
|
remote := path.Join(dir, file.Name)
|
2021-03-12 01:40:29 +08:00
|
|
|
|
o, err := f.newObjectWithInfo(ctx, remote, &file, directoryID)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
if err != nil {
|
2017-07-17 13:36:45 +08:00
|
|
|
|
return nil, err
|
2017-06-02 14:51:38 +08:00
|
|
|
|
}
|
2017-07-17 13:36:45 +08:00
|
|
|
|
entries = append(entries, o)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-17 13:36:45 +08:00
|
|
|
|
return entries, nil
|
2017-06-02 14:51:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
// Fs returns the parent Fs
|
|
|
|
|
func (o *Object) Fs() fs.Info {
|
|
|
|
|
return o.fs
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Return a string version
|
|
|
|
|
func (o *Object) String() string {
|
|
|
|
|
if o == nil {
|
|
|
|
|
return "<nil>"
|
|
|
|
|
}
|
|
|
|
|
return o.remote
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remote returns the remote path
|
|
|
|
|
func (o *Object) Remote() string {
|
|
|
|
|
return o.remote
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Hash returns the Md5sum of an object returning a lowercase hex string
|
2019-06-17 16:34:30 +08:00
|
|
|
|
func (o *Object) Hash(ctx context.Context, t hash.Type) (string, error) {
|
2017-07-17 13:36:45 +08:00
|
|
|
|
if t != hash.MD5 {
|
|
|
|
|
return "", hash.ErrUnsupported
|
2017-06-02 14:51:38 +08:00
|
|
|
|
}
|
|
|
|
|
return o.md5, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Size returns the size of an object in bytes
|
|
|
|
|
func (o *Object) Size() int64 {
|
|
|
|
|
return o.size // Object is likely PENDING
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ModTime returns the modification time of the object
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// It attempts to read the objects mtime and if that isn't present the
|
|
|
|
|
// LastModified returned in the http headers
|
2019-06-17 16:34:30 +08:00
|
|
|
|
func (o *Object) ModTime(ctx context.Context) time.Time {
|
2017-06-02 14:51:38 +08:00
|
|
|
|
return o.modTime
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetModTime sets the modification time of the local fs object
|
2019-06-17 16:34:30 +08:00
|
|
|
|
func (o *Object) SetModTime(ctx context.Context, modTime time.Time) error {
|
2018-04-27 05:02:31 +08:00
|
|
|
|
// fs.Debugf(nil, "SetModTime(%v)", modTime.String())
|
2017-07-17 13:36:45 +08:00
|
|
|
|
opts := rest.Opts{
|
|
|
|
|
Method: "PUT",
|
|
|
|
|
NoResponse: true,
|
|
|
|
|
Path: "/file/filesettings.json",
|
|
|
|
|
}
|
2018-11-02 20:14:30 +08:00
|
|
|
|
update := modTimeFile{
|
|
|
|
|
SessionID: o.fs.session.SessionID,
|
|
|
|
|
FileID: o.id,
|
|
|
|
|
FileModificationTime: strconv.FormatInt(modTime.Unix(), 10),
|
|
|
|
|
}
|
2017-07-17 13:36:45 +08:00
|
|
|
|
err := o.fs.pacer.Call(func() (bool, error) {
|
2019-09-05 03:00:37 +08:00
|
|
|
|
resp, err := o.fs.srv.CallJSON(ctx, &opts, &update, nil)
|
2021-03-11 22:44:01 +08:00
|
|
|
|
return o.fs.shouldRetry(ctx, resp, err)
|
2017-07-17 13:36:45 +08:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
o.modTime = modTime
|
|
|
|
|
|
|
|
|
|
return err
|
2017-06-02 14:51:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Open an object for read
|
2019-06-17 16:34:30 +08:00
|
|
|
|
func (o *Object) Open(ctx context.Context, options ...fs.OpenOption) (in io.ReadCloser, err error) {
|
2018-04-27 05:02:31 +08:00
|
|
|
|
// fs.Debugf(nil, "Open(\"%v\")", o.remote)
|
|
|
|
|
fs.FixRangeOption(options, o.size)
|
|
|
|
|
opts := rest.Opts{
|
|
|
|
|
Method: "GET",
|
|
|
|
|
Path: "/download/file.json/" + o.id + "?session_id=" + o.fs.session.SessionID,
|
|
|
|
|
Options: options,
|
2017-07-17 13:36:45 +08:00
|
|
|
|
}
|
2017-06-02 14:51:38 +08:00
|
|
|
|
var resp *http.Response
|
|
|
|
|
err = o.fs.pacer.Call(func() (bool, error) {
|
2019-09-05 03:00:37 +08:00
|
|
|
|
resp, err = o.fs.srv.Call(ctx, &opts)
|
2021-03-11 22:44:01 +08:00
|
|
|
|
return o.fs.shouldRetry(ctx, resp, err)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
|
return nil, fmt.Errorf("failed to open file): %w", err)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resp.Body, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove an object
|
2019-06-17 16:34:30 +08:00
|
|
|
|
func (o *Object) Remove(ctx context.Context) error {
|
2018-04-27 05:02:31 +08:00
|
|
|
|
// fs.Debugf(nil, "Remove(\"%s\")", o.id)
|
2017-07-17 13:36:45 +08:00
|
|
|
|
return o.fs.pacer.Call(func() (bool, error) {
|
|
|
|
|
opts := rest.Opts{
|
|
|
|
|
Method: "DELETE",
|
|
|
|
|
NoResponse: true,
|
|
|
|
|
Path: "/file.json/" + o.fs.session.SessionID + "/" + o.id,
|
|
|
|
|
}
|
2019-09-05 03:00:37 +08:00
|
|
|
|
resp, err := o.fs.srv.Call(ctx, &opts)
|
2021-03-11 22:44:01 +08:00
|
|
|
|
return o.fs.shouldRetry(ctx, resp, err)
|
2017-07-17 13:36:45 +08:00
|
|
|
|
})
|
2017-06-02 14:51:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Storable returns a boolean showing whether this object storable
|
|
|
|
|
func (o *Object) Storable() bool {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update the object with the contents of the io.Reader, modTime and size
|
|
|
|
|
//
|
|
|
|
|
// The new object may have been created if an error is returned
|
2019-06-17 16:34:30 +08:00
|
|
|
|
func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) error {
|
2017-06-02 14:51:38 +08:00
|
|
|
|
size := src.Size()
|
2019-06-17 16:34:30 +08:00
|
|
|
|
modTime := src.ModTime(ctx)
|
2018-04-27 05:02:31 +08:00
|
|
|
|
// fs.Debugf(nil, "Update(\"%s\", \"%s\")", o.id, o.remote)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
|
|
|
|
|
// Open file for upload
|
|
|
|
|
var resp *http.Response
|
|
|
|
|
openResponse := openUploadResponse{}
|
2017-07-17 13:36:45 +08:00
|
|
|
|
err := o.fs.pacer.Call(func() (bool, error) {
|
2017-06-02 14:51:38 +08:00
|
|
|
|
openUploadData := openUpload{SessionID: o.fs.session.SessionID, FileID: o.id, Size: size}
|
2018-04-27 05:02:31 +08:00
|
|
|
|
// fs.Debugf(nil, "PreOpen: %#v", openUploadData)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
opts := rest.Opts{
|
2020-03-22 05:50:17 +08:00
|
|
|
|
Method: "POST",
|
|
|
|
|
Options: options,
|
|
|
|
|
Path: "/upload/open_file_upload.json",
|
2017-06-02 14:51:38 +08:00
|
|
|
|
}
|
2019-09-05 03:00:37 +08:00
|
|
|
|
resp, err := o.fs.srv.CallJSON(ctx, &opts, &openUploadData, &openResponse)
|
2021-03-11 22:44:01 +08:00
|
|
|
|
return o.fs.shouldRetry(ctx, resp, err)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
|
return fmt.Errorf("failed to create file: %w", err)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
}
|
2017-07-17 13:36:45 +08:00
|
|
|
|
// resp.Body.Close()
|
2018-04-27 05:02:31 +08:00
|
|
|
|
// fs.Debugf(nil, "PostOpen: %#v", openResponse)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
|
2019-11-15 19:22:13 +08:00
|
|
|
|
buf := make([]byte, o.fs.opt.ChunkSize)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
chunkOffset := int64(0)
|
|
|
|
|
remainingBytes := size
|
|
|
|
|
chunkCounter := 0
|
|
|
|
|
|
|
|
|
|
for remainingBytes > 0 {
|
2019-11-15 19:22:13 +08:00
|
|
|
|
currentChunkSize := int64(o.fs.opt.ChunkSize)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
if currentChunkSize > remainingBytes {
|
|
|
|
|
currentChunkSize = remainingBytes
|
|
|
|
|
}
|
|
|
|
|
remainingBytes -= currentChunkSize
|
2018-04-27 05:02:31 +08:00
|
|
|
|
fs.Debugf(o, "Uploading chunk %d, size=%d, remain=%d", chunkCounter, currentChunkSize, remainingBytes)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
|
2018-10-13 20:09:38 +08:00
|
|
|
|
chunk := readers.NewRepeatableLimitReaderBuffer(in, buf, currentChunkSize)
|
2019-07-23 21:20:51 +08:00
|
|
|
|
var reply uploadFileChunkReply
|
2017-06-02 14:51:38 +08:00
|
|
|
|
err = o.fs.pacer.Call(func() (bool, error) {
|
2018-10-13 20:09:38 +08:00
|
|
|
|
// seek to the start in case this is a retry
|
|
|
|
|
if _, err = chunk.Seek(0, io.SeekStart); err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
2017-06-02 14:51:38 +08:00
|
|
|
|
opts := rest.Opts{
|
2019-07-23 21:20:51 +08:00
|
|
|
|
Method: "POST",
|
|
|
|
|
Path: "/upload/upload_file_chunk.json",
|
|
|
|
|
Body: chunk,
|
|
|
|
|
MultipartParams: url.Values{
|
|
|
|
|
"session_id": []string{o.fs.session.SessionID},
|
|
|
|
|
"file_id": []string{o.id},
|
|
|
|
|
"temp_location": []string{openResponse.TempLocation},
|
|
|
|
|
"chunk_offset": []string{strconv.FormatInt(chunkOffset, 10)},
|
|
|
|
|
"chunk_size": []string{strconv.FormatInt(currentChunkSize, 10)},
|
|
|
|
|
},
|
|
|
|
|
MultipartContentName: "file_data", // ..name of the parameter which is the attached file
|
|
|
|
|
MultipartFileName: o.remote, // ..name of the file for the attached file
|
|
|
|
|
|
2017-06-02 14:51:38 +08:00
|
|
|
|
}
|
2019-09-05 03:00:37 +08:00
|
|
|
|
resp, err = o.fs.srv.CallJSON(ctx, &opts, nil, &reply)
|
2021-03-11 22:44:01 +08:00
|
|
|
|
return o.fs.shouldRetry(ctx, resp, err)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
|
return fmt.Errorf("failed to create file: %w", err)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
}
|
2019-07-23 21:20:51 +08:00
|
|
|
|
if reply.TotalWritten != currentChunkSize {
|
2021-11-04 18:12:57 +08:00
|
|
|
|
return fmt.Errorf("failed to create file: incomplete write of %d/%d bytes", reply.TotalWritten, currentChunkSize)
|
2018-04-27 05:02:31 +08:00
|
|
|
|
}
|
2017-06-02 14:51:38 +08:00
|
|
|
|
|
|
|
|
|
chunkCounter++
|
|
|
|
|
chunkOffset += currentChunkSize
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-17 13:36:45 +08:00
|
|
|
|
// Close file for upload
|
2017-06-02 14:51:38 +08:00
|
|
|
|
closeResponse := closeUploadResponse{}
|
|
|
|
|
err = o.fs.pacer.Call(func() (bool, error) {
|
|
|
|
|
closeUploadData := closeUpload{SessionID: o.fs.session.SessionID, FileID: o.id, Size: size, TempLocation: openResponse.TempLocation}
|
2018-04-27 05:02:31 +08:00
|
|
|
|
// fs.Debugf(nil, "PreClose: %#v", closeUploadData)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
opts := rest.Opts{
|
|
|
|
|
Method: "POST",
|
|
|
|
|
Path: "/upload/close_file_upload.json",
|
|
|
|
|
}
|
2019-09-05 03:00:37 +08:00
|
|
|
|
resp, err = o.fs.srv.CallJSON(ctx, &opts, &closeUploadData, &closeResponse)
|
2021-03-11 22:44:01 +08:00
|
|
|
|
return o.fs.shouldRetry(ctx, resp, err)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
|
return fmt.Errorf("failed to create file: %w", err)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
}
|
2018-04-27 05:02:31 +08:00
|
|
|
|
// fs.Debugf(nil, "PostClose: %#v", closeResponse)
|
2017-07-17 13:36:45 +08:00
|
|
|
|
|
|
|
|
|
o.id = closeResponse.FileID
|
|
|
|
|
o.size = closeResponse.Size
|
|
|
|
|
|
2018-06-15 21:50:17 +08:00
|
|
|
|
// Set the mod time now
|
2019-06-17 16:34:30 +08:00
|
|
|
|
err = o.SetModTime(ctx, modTime)
|
2017-07-17 13:36:45 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set permissions
|
|
|
|
|
err = o.fs.pacer.Call(func() (bool, error) {
|
|
|
|
|
update := permissions{SessionID: o.fs.session.SessionID, FileID: o.id, FileIsPublic: 0}
|
2018-04-27 05:02:31 +08:00
|
|
|
|
// fs.Debugf(nil, "Permissions : %#v", update)
|
2017-07-17 13:36:45 +08:00
|
|
|
|
opts := rest.Opts{
|
|
|
|
|
Method: "POST",
|
|
|
|
|
NoResponse: true,
|
|
|
|
|
Path: "/file/access.json",
|
|
|
|
|
}
|
2019-09-05 03:00:37 +08:00
|
|
|
|
resp, err = o.fs.srv.CallJSON(ctx, &opts, &update, nil)
|
2021-03-11 22:44:01 +08:00
|
|
|
|
return o.fs.shouldRetry(ctx, resp, err)
|
2017-07-17 13:36:45 +08:00
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2017-06-02 14:51:38 +08:00
|
|
|
|
|
2019-06-17 16:34:30 +08:00
|
|
|
|
return o.readMetaData(ctx)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-17 16:34:30 +08:00
|
|
|
|
func (o *Object) readMetaData(ctx context.Context) (err error) {
|
2020-05-12 00:24:37 +08:00
|
|
|
|
leaf, directoryID, err := o.fs.dirCache.FindPath(ctx, o.remote, false)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
if err == fs.ErrorDirNotFound {
|
|
|
|
|
return fs.ErrorObjectNotFound
|
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
var resp *http.Response
|
|
|
|
|
folderList := FolderList{}
|
|
|
|
|
err = o.fs.pacer.Call(func() (bool, error) {
|
|
|
|
|
opts := rest.Opts{
|
|
|
|
|
Method: "GET",
|
2018-11-02 20:14:30 +08:00
|
|
|
|
Path: fmt.Sprintf("/folder/itembyname.json/%s/%s?name=%s",
|
2020-01-15 01:33:35 +08:00
|
|
|
|
o.fs.session.SessionID, directoryID, url.QueryEscape(o.fs.opt.Enc.FromStandardName(leaf))),
|
2017-06-02 14:51:38 +08:00
|
|
|
|
}
|
2019-09-05 03:00:37 +08:00
|
|
|
|
resp, err = o.fs.srv.CallJSON(ctx, &opts, nil, &folderList)
|
2021-03-11 22:44:01 +08:00
|
|
|
|
return o.fs.shouldRetry(ctx, resp, err)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
|
return fmt.Errorf("failed to get folder list: %w", err)
|
2017-06-02 14:51:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(folderList.Files) == 0 {
|
|
|
|
|
return fs.ErrorObjectNotFound
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
leafFile := folderList.Files[0]
|
|
|
|
|
o.id = leafFile.FileID
|
|
|
|
|
o.modTime = time.Unix(leafFile.DateModified, 0)
|
2017-07-17 13:36:45 +08:00
|
|
|
|
o.md5 = leafFile.FileHash
|
2017-06-02 14:51:38 +08:00
|
|
|
|
o.size = leafFile.Size
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2018-04-27 05:02:31 +08:00
|
|
|
|
|
2018-05-13 16:16:56 +08:00
|
|
|
|
// ID returns the ID of the Object if known, or "" if not
|
|
|
|
|
func (o *Object) ID() string {
|
|
|
|
|
return o.id
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-12 01:40:29 +08:00
|
|
|
|
// ParentID returns the ID of the Object parent directory if known, or "" if not
|
|
|
|
|
func (o *Object) ParentID() string {
|
|
|
|
|
return o.parent
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-27 05:02:31 +08:00
|
|
|
|
// Check the interfaces are satisfied
|
|
|
|
|
var (
|
|
|
|
|
_ fs.Fs = (*Fs)(nil)
|
|
|
|
|
_ fs.Purger = (*Fs)(nil)
|
|
|
|
|
_ fs.Copier = (*Fs)(nil)
|
|
|
|
|
_ fs.Mover = (*Fs)(nil)
|
|
|
|
|
_ fs.DirMover = (*Fs)(nil)
|
|
|
|
|
_ fs.DirCacheFlusher = (*Fs)(nil)
|
|
|
|
|
_ fs.Object = (*Object)(nil)
|
2018-05-13 16:16:56 +08:00
|
|
|
|
_ fs.IDer = (*Object)(nil)
|
2021-03-12 01:40:29 +08:00
|
|
|
|
_ fs.ParentIDer = (*Object)(nil)
|
2018-04-27 05:02:31 +08:00
|
|
|
|
)
|