2017-07-05 23:20:40 +02:00
|
|
|
|
// Package box provides an interface to the Box
|
|
|
|
|
// object storage system.
|
|
|
|
|
package box
|
|
|
|
|
|
|
|
|
|
// FIXME Box only supports file names of 255 characters or less. Names
|
|
|
|
|
// that will not be supported are those that contain non-printable
|
|
|
|
|
// ascii, / or \, names with trailing spaces, and the special names
|
|
|
|
|
// “.” and “..”.
|
|
|
|
|
|
|
|
|
|
// FIXME box can copy a directory
|
|
|
|
|
|
|
|
|
|
import (
|
2019-06-17 10:34:30 +02:00
|
|
|
|
"context"
|
2017-07-05 23:20:40 +02:00
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"log"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/url"
|
|
|
|
|
"path"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2019-07-28 19:47:38 +02:00
|
|
|
|
"github.com/rclone/rclone/backend/box/api"
|
|
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
|
"github.com/rclone/rclone/fs/config"
|
|
|
|
|
"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/hash"
|
|
|
|
|
"github.com/rclone/rclone/lib/dircache"
|
|
|
|
|
"github.com/rclone/rclone/lib/oauthutil"
|
|
|
|
|
"github.com/rclone/rclone/lib/pacer"
|
|
|
|
|
"github.com/rclone/rclone/lib/rest"
|
2017-07-05 23:20:40 +02:00
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
rcloneClientID = "d0374ba6pgmaguie02ge15sv1mllndho"
|
|
|
|
|
rcloneEncryptedClientSecret = "sYbJYm99WB8jzeaLPU0OPDMJKIkZvD2qOn3SyEMfiJr03RdtDt3xcZEIudRhbIDL"
|
|
|
|
|
minSleep = 10 * time.Millisecond
|
|
|
|
|
maxSleep = 2 * time.Second
|
|
|
|
|
decayConstant = 2 // bigger for slower decay, exponential
|
|
|
|
|
rootID = "0" // ID of root folder is always this
|
|
|
|
|
rootURL = "https://api.box.com/2.0"
|
|
|
|
|
uploadURL = "https://upload.box.com/api/2.0"
|
|
|
|
|
listChunks = 1000 // chunk size to read directory listings
|
|
|
|
|
minUploadCutoff = 50000000 // upload cutoff can be no lower than this
|
2018-05-14 19:06:57 +02:00
|
|
|
|
defaultUploadCutoff = 50 * 1024 * 1024
|
2017-07-05 23:20:40 +02:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Globals
|
|
|
|
|
var (
|
|
|
|
|
// Description of how to auth for this app
|
|
|
|
|
oauthConfig = &oauth2.Config{
|
|
|
|
|
Scopes: nil,
|
|
|
|
|
Endpoint: oauth2.Endpoint{
|
|
|
|
|
AuthURL: "https://app.box.com/api/oauth2/authorize",
|
|
|
|
|
TokenURL: "https://app.box.com/api/oauth2/token",
|
|
|
|
|
},
|
|
|
|
|
ClientID: rcloneClientID,
|
2018-01-18 21:19:55 +01:00
|
|
|
|
ClientSecret: obscure.MustReveal(rcloneEncryptedClientSecret),
|
2017-07-05 23:20:40 +02:00
|
|
|
|
RedirectURL: oauthutil.RedirectURL,
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Register with Fs
|
|
|
|
|
func init() {
|
|
|
|
|
fs.Register(&fs.RegInfo{
|
|
|
|
|
Name: "box",
|
|
|
|
|
Description: "Box",
|
|
|
|
|
NewFs: NewFs,
|
2018-05-14 19:06:57 +02:00
|
|
|
|
Config: func(name string, m configmap.Mapper) {
|
|
|
|
|
err := oauthutil.Config("box", name, m, oauthConfig)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalf("Failed to configure token: %v", err)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
Options: []fs.Option{{
|
2018-01-12 17:30:54 +01:00
|
|
|
|
Name: config.ConfigClientID,
|
2018-05-14 19:06:57 +02:00
|
|
|
|
Help: "Box App Client Id.\nLeave blank normally.",
|
2017-07-05 23:20:40 +02:00
|
|
|
|
}, {
|
2018-01-12 17:30:54 +01:00
|
|
|
|
Name: config.ConfigClientSecret,
|
2018-05-14 19:06:57 +02:00
|
|
|
|
Help: "Box App Client Secret\nLeave blank normally.",
|
|
|
|
|
}, {
|
|
|
|
|
Name: "upload_cutoff",
|
2018-10-01 19:36:15 +02:00
|
|
|
|
Help: "Cutoff for switching to multipart upload (>= 50MB).",
|
2018-05-14 19:06:57 +02:00
|
|
|
|
Default: fs.SizeSuffix(defaultUploadCutoff),
|
|
|
|
|
Advanced: true,
|
2018-07-09 18:12:18 +02:00
|
|
|
|
}, {
|
|
|
|
|
Name: "commit_retries",
|
|
|
|
|
Help: "Max number of times to try committing a multipart file.",
|
|
|
|
|
Default: 100,
|
|
|
|
|
Advanced: true,
|
2017-07-05 23:20:40 +02:00
|
|
|
|
}},
|
|
|
|
|
})
|
2018-05-14 19:06:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Options defines the configuration for this backend
|
|
|
|
|
type Options struct {
|
2018-07-09 18:12:18 +02:00
|
|
|
|
UploadCutoff fs.SizeSuffix `config:"upload_cutoff"`
|
|
|
|
|
CommitRetries int `config:"commit_retries"`
|
2017-07-05 23:20:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fs represents a remote box
|
|
|
|
|
type Fs struct {
|
2017-07-29 23:05:36 +02:00
|
|
|
|
name string // name of this remote
|
|
|
|
|
root string // the path we are working on
|
2018-05-14 19:06:57 +02:00
|
|
|
|
opt Options // parsed options
|
2017-07-29 23:05:36 +02:00
|
|
|
|
features *fs.Features // optional features
|
|
|
|
|
srv *rest.Client // the connection to the one drive server
|
|
|
|
|
dirCache *dircache.DirCache // Map of directory path to directory id
|
2019-02-09 21:52:15 +01:00
|
|
|
|
pacer *fs.Pacer // pacer for API calls
|
2017-07-29 23:05:36 +02:00
|
|
|
|
tokenRenewer *oauthutil.Renew // renew the token on expiry
|
|
|
|
|
uploadToken *pacer.TokenDispenser // control concurrency
|
2017-07-05 23:20:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Object describes a box object
|
|
|
|
|
//
|
|
|
|
|
// Will definitely have info but maybe not meta
|
|
|
|
|
type Object struct {
|
|
|
|
|
fs *Fs // what this object is part of
|
|
|
|
|
remote string // The remote path
|
|
|
|
|
hasMetaData bool // whether info below has been set
|
|
|
|
|
size int64 // size of the object
|
|
|
|
|
modTime time.Time // modification time of the object
|
|
|
|
|
id string // ID of the object
|
2018-08-19 15:13:33 +02:00
|
|
|
|
publicLink string // Public Link for the object
|
2017-07-05 23:20:40 +02:00
|
|
|
|
sha1 string // SHA-1 of the object content
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
|
return f.root
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// String converts this Fs to a string
|
|
|
|
|
func (f *Fs) String() string {
|
|
|
|
|
return fmt.Sprintf("box root '%s'", f.root)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Features returns the optional features of this Fs
|
|
|
|
|
func (f *Fs) Features() *fs.Features {
|
|
|
|
|
return f.features
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// parsePath parses an box 'url'
|
|
|
|
|
func parsePath(path string) (root string) {
|
|
|
|
|
root = strings.Trim(path, "/")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// retryErrorCodes is a slice of error codes that we will retry
|
|
|
|
|
var retryErrorCodes = []int{
|
|
|
|
|
429, // Too Many Requests.
|
|
|
|
|
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
|
|
|
|
|
func shouldRetry(resp *http.Response, err error) (bool, error) {
|
2019-02-07 18:41:17 +01:00
|
|
|
|
authRetry := false
|
2017-07-05 23:20:40 +02:00
|
|
|
|
|
|
|
|
|
if resp != nil && resp.StatusCode == 401 && len(resp.Header["Www-Authenticate"]) == 1 && strings.Index(resp.Header["Www-Authenticate"][0], "expired_token") >= 0 {
|
2019-02-07 18:41:17 +01:00
|
|
|
|
authRetry = true
|
2017-07-05 23:20:40 +02:00
|
|
|
|
fs.Debugf(nil, "Should retry: %v", err)
|
|
|
|
|
}
|
2019-02-07 18:41:17 +01:00
|
|
|
|
return authRetry || fserrors.ShouldRetry(err) || fserrors.ShouldRetryHTTP(resp, retryErrorCodes), err
|
2017-07-05 23:20:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// substitute reserved characters for box
|
|
|
|
|
func replaceReservedChars(x string) string {
|
|
|
|
|
// Backslash for FULLWIDTH REVERSE SOLIDUS
|
|
|
|
|
return strings.Replace(x, "\\", "\", -1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// restore reserved characters for box
|
|
|
|
|
func restoreReservedChars(x string) string {
|
|
|
|
|
// FULLWIDTH REVERSE SOLIDUS for Backslash
|
|
|
|
|
return strings.Replace(x, "\", "\\", -1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// readMetaDataForPath reads the metadata from the path
|
2019-06-17 10:34:30 +02:00
|
|
|
|
func (f *Fs) readMetaDataForPath(ctx context.Context, path string) (info *api.Item, err error) {
|
2017-07-05 23:20:40 +02:00
|
|
|
|
// defer fs.Trace(f, "path=%q", path)("info=%+v, err=%v", &info, &err)
|
2019-06-17 10:34:30 +02:00
|
|
|
|
leaf, directoryID, err := f.dirCache.FindRootAndPath(ctx, path, false)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
if err == fs.ErrorDirNotFound {
|
|
|
|
|
return nil, fs.ErrorObjectNotFound
|
|
|
|
|
}
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
found, err := f.listAll(directoryID, false, true, func(item *api.Item) bool {
|
|
|
|
|
if item.Name == leaf {
|
|
|
|
|
info = item
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if !found {
|
|
|
|
|
return nil, fs.ErrorObjectNotFound
|
|
|
|
|
}
|
|
|
|
|
return info, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// errorHandler parses a non 2xx error response into an error
|
|
|
|
|
func errorHandler(resp *http.Response) error {
|
|
|
|
|
// Decode error response
|
|
|
|
|
errResponse := new(api.Error)
|
|
|
|
|
err := rest.DecodeJSON(resp, &errResponse)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fs.Debugf(nil, "Couldn't decode error response: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if errResponse.Code == "" {
|
|
|
|
|
errResponse.Code = resp.Status
|
|
|
|
|
}
|
|
|
|
|
if errResponse.Status == 0 {
|
|
|
|
|
errResponse.Status = resp.StatusCode
|
|
|
|
|
}
|
|
|
|
|
return errResponse
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewFs constructs an Fs from the path, container:path
|
2018-05-14 19:06:57 +02:00
|
|
|
|
func NewFs(name, root string, m configmap.Mapper) (fs.Fs, error) {
|
2019-06-17 10:34:30 +02:00
|
|
|
|
ctx := context.Background()
|
2018-05-14 19:06:57 +02:00
|
|
|
|
// Parse config into Options struct
|
|
|
|
|
opt := new(Options)
|
|
|
|
|
err := configstruct.Set(m, opt)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if opt.UploadCutoff < minUploadCutoff {
|
|
|
|
|
return nil, errors.Errorf("box: upload cutoff (%v) must be greater than equal to %v", opt.UploadCutoff, fs.SizeSuffix(minUploadCutoff))
|
2017-07-05 23:20:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
root = parsePath(root)
|
2018-05-14 19:06:57 +02:00
|
|
|
|
oAuthClient, ts, err := oauthutil.NewClient(name, m, oauthConfig)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
if err != nil {
|
2018-11-11 11:40:13 +01:00
|
|
|
|
return nil, errors.Wrap(err, "failed to configure Box")
|
2017-07-05 23:20:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
f := &Fs{
|
2017-07-29 23:05:36 +02:00
|
|
|
|
name: name,
|
|
|
|
|
root: root,
|
2018-05-14 19:06:57 +02:00
|
|
|
|
opt: *opt,
|
2017-07-29 23:05:36 +02:00
|
|
|
|
srv: rest.NewClient(oAuthClient).SetRoot(rootURL),
|
2019-02-09 21:52:15 +01:00
|
|
|
|
pacer: fs.NewPacer(pacer.NewDefault(pacer.MinSleep(minSleep), pacer.MaxSleep(maxSleep), pacer.DecayConstant(decayConstant))),
|
2017-07-29 23:05:36 +02:00
|
|
|
|
uploadToken: pacer.NewTokenDispenser(fs.Config.Transfers),
|
2017-07-05 23:20:40 +02:00
|
|
|
|
}
|
2017-08-09 16:27:43 +02:00
|
|
|
|
f.features = (&fs.Features{
|
|
|
|
|
CaseInsensitive: true,
|
|
|
|
|
CanHaveEmptyDirectories: true,
|
|
|
|
|
}).Fill(f)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
f.srv.SetErrorHandler(errorHandler)
|
|
|
|
|
|
|
|
|
|
// Renew the token in the background
|
|
|
|
|
f.tokenRenewer = oauthutil.NewRenew(f.String(), ts, func() error {
|
2019-06-17 10:34:30 +02:00
|
|
|
|
_, err := f.readMetaDataForPath(ctx, "")
|
2017-07-05 23:20:40 +02:00
|
|
|
|
return err
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Get rootID
|
|
|
|
|
f.dirCache = dircache.New(root, rootID, f)
|
|
|
|
|
|
|
|
|
|
// Find the current root
|
2019-06-17 10:34:30 +02:00
|
|
|
|
err = f.dirCache.FindRoot(ctx, false)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
// Assume it is a file
|
|
|
|
|
newRoot, remote := dircache.SplitPath(root)
|
2018-10-14 15:41:26 +02:00
|
|
|
|
tempF := *f
|
|
|
|
|
tempF.dirCache = dircache.New(newRoot, rootID, &tempF)
|
|
|
|
|
tempF.root = newRoot
|
2017-07-05 23:20:40 +02:00
|
|
|
|
// Make new Fs which is the parent
|
2019-06-17 10:34:30 +02:00
|
|
|
|
err = tempF.dirCache.FindRoot(ctx, false)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
// No root so return old f
|
|
|
|
|
return f, nil
|
|
|
|
|
}
|
2019-06-17 10:34:30 +02:00
|
|
|
|
_, err := tempF.newObjectWithInfo(ctx, remote, nil)
|
2017-07-05 23:20:40 +02: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 15:41:26 +02:00
|
|
|
|
f.features.Fill(&tempF)
|
|
|
|
|
// XXX: update the old f here instead of returning tempF, since
|
|
|
|
|
// `features` were already filled with functions having *f as a receiver.
|
2019-07-28 19:47:38 +02:00
|
|
|
|
// See https://github.com/rclone/rclone/issues/2182
|
2018-10-14 15:41:26 +02:00
|
|
|
|
f.dirCache = tempF.dirCache
|
|
|
|
|
f.root = tempF.root
|
2017-07-05 23:20:40 +02:00
|
|
|
|
// return an error with an fs which points to the parent
|
2018-10-14 15:41:26 +02:00
|
|
|
|
return f, fs.ErrorIsFile
|
2017-07-05 23:20:40 +02:00
|
|
|
|
}
|
|
|
|
|
return f, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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 + "/"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Return an Object from a path
|
|
|
|
|
//
|
|
|
|
|
// If it can't be found it returns the error fs.ErrorObjectNotFound.
|
2019-06-17 10:34:30 +02:00
|
|
|
|
func (f *Fs) newObjectWithInfo(ctx context.Context, remote string, info *api.Item) (fs.Object, error) {
|
2017-07-05 23:20:40 +02:00
|
|
|
|
o := &Object{
|
|
|
|
|
fs: f,
|
|
|
|
|
remote: remote,
|
|
|
|
|
}
|
|
|
|
|
var err error
|
|
|
|
|
if info != nil {
|
|
|
|
|
// Set info
|
|
|
|
|
err = o.setMetaData(info)
|
|
|
|
|
} else {
|
2019-06-17 10:34:30 +02:00
|
|
|
|
err = o.readMetaData(ctx) // reads info and meta, returning an error
|
2017-07-05 23:20:40 +02: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 10:34:30 +02:00
|
|
|
|
func (f *Fs) NewObject(ctx context.Context, remote string) (fs.Object, error) {
|
|
|
|
|
return f.newObjectWithInfo(ctx, remote, nil)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FindLeaf finds a directory of name leaf in the folder with ID pathID
|
2019-06-17 10:34:30 +02:00
|
|
|
|
func (f *Fs) FindLeaf(ctx context.Context, pathID, leaf string) (pathIDOut string, found bool, err error) {
|
2017-07-05 23:20:40 +02:00
|
|
|
|
// Find the leaf in pathID
|
|
|
|
|
found, err = f.listAll(pathID, true, false, func(item *api.Item) bool {
|
|
|
|
|
if item.Name == leaf {
|
|
|
|
|
pathIDOut = item.ID
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
})
|
|
|
|
|
return pathIDOut, found, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// fieldsValue creates a url.Values with fields set to those in api.Item
|
|
|
|
|
func fieldsValue() url.Values {
|
|
|
|
|
values := url.Values{}
|
|
|
|
|
values.Set("fields", api.ItemFields)
|
|
|
|
|
return values
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CreateDir makes a directory with pathID as parent and name leaf
|
2019-06-17 10:34:30 +02:00
|
|
|
|
func (f *Fs) CreateDir(ctx context.Context, pathID, leaf string) (newID string, err error) {
|
2017-07-05 23:20:40 +02:00
|
|
|
|
// fs.Debugf(f, "CreateDir(%q, %q)\n", pathID, leaf)
|
|
|
|
|
var resp *http.Response
|
|
|
|
|
var info *api.Item
|
|
|
|
|
opts := rest.Opts{
|
|
|
|
|
Method: "POST",
|
|
|
|
|
Path: "/folders",
|
|
|
|
|
Parameters: fieldsValue(),
|
|
|
|
|
}
|
|
|
|
|
mkdir := api.CreateFolder{
|
|
|
|
|
Name: replaceReservedChars(leaf),
|
|
|
|
|
Parent: api.Parent{
|
|
|
|
|
ID: pathID,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
|
|
|
|
resp, err = f.srv.CallJSON(&opts, &mkdir, &info)
|
|
|
|
|
return shouldRetry(resp, err)
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
//fmt.Printf("...Error %v\n", err)
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
// fmt.Printf("...Id %q\n", *info.Id)
|
|
|
|
|
return info.ID, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// list the objects into the function supplied
|
|
|
|
|
//
|
|
|
|
|
// If directories is set it only sends directories
|
|
|
|
|
// User function to process a File item from listAll
|
|
|
|
|
//
|
|
|
|
|
// Should return true to finish processing
|
|
|
|
|
type listAllFn func(*api.Item) bool
|
|
|
|
|
|
|
|
|
|
// Lists the directory required calling the user function on each item found
|
|
|
|
|
//
|
|
|
|
|
// If the user fn ever returns true then it early exits with found = true
|
|
|
|
|
func (f *Fs) listAll(dirID string, directoriesOnly bool, filesOnly bool, fn listAllFn) (found bool, err error) {
|
|
|
|
|
opts := rest.Opts{
|
|
|
|
|
Method: "GET",
|
|
|
|
|
Path: "/folders/" + dirID + "/items",
|
|
|
|
|
Parameters: fieldsValue(),
|
|
|
|
|
}
|
|
|
|
|
opts.Parameters.Set("limit", strconv.Itoa(listChunks))
|
|
|
|
|
offset := 0
|
|
|
|
|
OUTER:
|
|
|
|
|
for {
|
|
|
|
|
opts.Parameters.Set("offset", strconv.Itoa(offset))
|
|
|
|
|
|
|
|
|
|
var result api.FolderItems
|
|
|
|
|
var resp *http.Response
|
|
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
|
|
|
|
resp, err = f.srv.CallJSON(&opts, nil, &result)
|
|
|
|
|
return shouldRetry(resp, err)
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return found, errors.Wrap(err, "couldn't list files")
|
|
|
|
|
}
|
|
|
|
|
for i := range result.Entries {
|
|
|
|
|
item := &result.Entries[i]
|
|
|
|
|
if item.Type == api.ItemTypeFolder {
|
|
|
|
|
if filesOnly {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
} else if item.Type == api.ItemTypeFile {
|
|
|
|
|
if directoriesOnly {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
fs.Debugf(f, "Ignoring %q - unknown type %q", item.Name, item.Type)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if item.ItemStatus != api.ItemStatusActive {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
item.Name = restoreReservedChars(item.Name)
|
|
|
|
|
if fn(item) {
|
|
|
|
|
found = true
|
|
|
|
|
break OUTER
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
offset += result.Limit
|
|
|
|
|
if offset >= result.TotalCount {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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 10:34:30 +02:00
|
|
|
|
func (f *Fs) List(ctx context.Context, dir string) (entries fs.DirEntries, err error) {
|
|
|
|
|
err = f.dirCache.FindRoot(ctx, false)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2019-06-17 10:34:30 +02:00
|
|
|
|
directoryID, err := f.dirCache.FindDir(ctx, dir, false)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
var iErr error
|
|
|
|
|
_, err = f.listAll(directoryID, false, false, func(info *api.Item) bool {
|
|
|
|
|
remote := path.Join(dir, info.Name)
|
|
|
|
|
if info.Type == api.ItemTypeFolder {
|
|
|
|
|
// cache the directory ID for later lookups
|
|
|
|
|
f.dirCache.Put(remote, info.ID)
|
2017-08-02 17:44:36 +02:00
|
|
|
|
d := fs.NewDir(remote, info.ModTime()).SetID(info.ID)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
// FIXME more info from dir?
|
|
|
|
|
entries = append(entries, d)
|
|
|
|
|
} else if info.Type == api.ItemTypeFile {
|
2019-06-17 10:34:30 +02:00
|
|
|
|
o, err := f.newObjectWithInfo(ctx, remote, info)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
iErr = err
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
entries = append(entries, o)
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if iErr != nil {
|
|
|
|
|
return nil, iErr
|
|
|
|
|
}
|
|
|
|
|
return entries, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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 10:34:30 +02:00
|
|
|
|
func (f *Fs) createObject(ctx context.Context, remote string, modTime time.Time, size int64) (o *Object, leaf string, directoryID string, err error) {
|
2017-07-05 23:20:40 +02:00
|
|
|
|
// Create the directory for the object if it doesn't exist
|
2019-06-17 10:34:30 +02:00
|
|
|
|
leaf, directoryID, err = f.dirCache.FindRootAndPath(ctx, remote, true)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// Temporary Object under construction
|
|
|
|
|
o = &Object{
|
|
|
|
|
fs: f,
|
|
|
|
|
remote: remote,
|
|
|
|
|
}
|
|
|
|
|
return o, leaf, directoryID, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Put the object
|
|
|
|
|
//
|
|
|
|
|
// 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 10:34:30 +02:00
|
|
|
|
func (f *Fs) Put(ctx context.Context, in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) (fs.Object, error) {
|
|
|
|
|
existingObj, err := f.newObjectWithInfo(ctx, src.Remote(), nil)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
switch err {
|
|
|
|
|
case nil:
|
2019-06-17 10:34:30 +02:00
|
|
|
|
return existingObj, existingObj.Update(ctx, in, src, options...)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
case fs.ErrorObjectNotFound:
|
|
|
|
|
// Not found so create it
|
2019-06-17 10:34:30 +02:00
|
|
|
|
return f.PutUnchecked(ctx, in, src)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
default:
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-19 12:32:56 +02:00
|
|
|
|
// PutStream uploads to the remote path with the modTime given of indeterminate size
|
2019-06-17 10:34:30 +02:00
|
|
|
|
func (f *Fs) PutStream(ctx context.Context, in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) (fs.Object, error) {
|
|
|
|
|
return f.Put(ctx, in, src, options...)
|
2017-08-19 12:32:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-05 23:20:40 +02:00
|
|
|
|
// PutUnchecked the object into the container
|
|
|
|
|
//
|
|
|
|
|
// This will produce an error if the object already exists
|
|
|
|
|
//
|
|
|
|
|
// 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 10:34:30 +02:00
|
|
|
|
func (f *Fs) PutUnchecked(ctx context.Context, in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) (fs.Object, error) {
|
2017-07-05 23:20:40 +02:00
|
|
|
|
remote := src.Remote()
|
|
|
|
|
size := src.Size()
|
2019-06-17 10:34:30 +02:00
|
|
|
|
modTime := src.ModTime(ctx)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
|
2019-06-17 10:34:30 +02:00
|
|
|
|
o, _, _, err := f.createObject(ctx, remote, modTime, size)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2019-06-17 10:34:30 +02:00
|
|
|
|
return o, o.Update(ctx, in, src, options...)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Mkdir creates the container if it doesn't exist
|
2019-06-17 10:34:30 +02:00
|
|
|
|
func (f *Fs) Mkdir(ctx context.Context, dir string) error {
|
|
|
|
|
err := f.dirCache.FindRoot(ctx, true)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if dir != "" {
|
2019-06-17 10:34:30 +02:00
|
|
|
|
_, err = f.dirCache.FindDir(ctx, dir, true)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// deleteObject removes an object by ID
|
|
|
|
|
func (f *Fs) deleteObject(id string) error {
|
|
|
|
|
opts := rest.Opts{
|
|
|
|
|
Method: "DELETE",
|
|
|
|
|
Path: "/files/" + id,
|
|
|
|
|
NoResponse: true,
|
|
|
|
|
}
|
|
|
|
|
return f.pacer.Call(func() (bool, error) {
|
|
|
|
|
resp, err := f.srv.Call(&opts)
|
|
|
|
|
return shouldRetry(resp, err)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// purgeCheck removes the root directory, if check is set then it
|
|
|
|
|
// refuses to do so if it has anything in
|
2019-06-17 10:34:30 +02:00
|
|
|
|
func (f *Fs) purgeCheck(ctx context.Context, dir string, check bool) error {
|
2017-07-05 23:20:40 +02:00
|
|
|
|
root := path.Join(f.root, dir)
|
|
|
|
|
if root == "" {
|
|
|
|
|
return errors.New("can't purge root directory")
|
|
|
|
|
}
|
|
|
|
|
dc := f.dirCache
|
2019-06-17 10:34:30 +02:00
|
|
|
|
err := dc.FindRoot(ctx, false)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2019-06-17 10:34:30 +02:00
|
|
|
|
rootID, err := dc.FindDir(ctx, dir, false)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
opts := rest.Opts{
|
|
|
|
|
Method: "DELETE",
|
|
|
|
|
Path: "/folders/" + rootID,
|
|
|
|
|
Parameters: url.Values{},
|
|
|
|
|
NoResponse: true,
|
|
|
|
|
}
|
|
|
|
|
opts.Parameters.Set("recursive", strconv.FormatBool(!check))
|
|
|
|
|
var resp *http.Response
|
|
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
|
|
|
|
resp, err = f.srv.Call(&opts)
|
|
|
|
|
return shouldRetry(resp, err)
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrap(err, "rmdir failed")
|
|
|
|
|
}
|
|
|
|
|
f.dirCache.FlushDir(dir)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Rmdir deletes the root folder
|
|
|
|
|
//
|
|
|
|
|
// Returns an error if it isn't empty
|
2019-06-17 10:34:30 +02:00
|
|
|
|
func (f *Fs) Rmdir(ctx context.Context, dir string) error {
|
|
|
|
|
return f.purgeCheck(ctx, dir, true)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Precision return the precision of this Fs
|
|
|
|
|
func (f *Fs) Precision() time.Duration {
|
|
|
|
|
return time.Second
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Copy src to this remote using server side copy operations.
|
|
|
|
|
//
|
|
|
|
|
// 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 10:34:30 +02:00
|
|
|
|
func (f *Fs) Copy(ctx context.Context, src fs.Object, remote string) (fs.Object, error) {
|
2017-07-05 23:20:40 +02:00
|
|
|
|
srcObj, ok := src.(*Object)
|
|
|
|
|
if !ok {
|
|
|
|
|
fs.Debugf(src, "Can't copy - not same remote type")
|
|
|
|
|
return nil, fs.ErrorCantCopy
|
|
|
|
|
}
|
2019-06-17 10:34:30 +02:00
|
|
|
|
err := srcObj.readMetaData(ctx)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
srcPath := srcObj.fs.rootSlash() + srcObj.remote
|
|
|
|
|
dstPath := f.rootSlash() + remote
|
|
|
|
|
if strings.ToLower(srcPath) == strings.ToLower(dstPath) {
|
|
|
|
|
return nil, errors.Errorf("can't copy %q -> %q as are same name when lowercase", srcPath, dstPath)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create temporary object
|
2019-06-17 10:34:30 +02:00
|
|
|
|
dstObj, leaf, directoryID, err := f.createObject(ctx, remote, srcObj.modTime, srcObj.size)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Copy the object
|
|
|
|
|
opts := rest.Opts{
|
|
|
|
|
Method: "POST",
|
|
|
|
|
Path: "/files/" + srcObj.id + "/copy",
|
|
|
|
|
Parameters: fieldsValue(),
|
|
|
|
|
}
|
|
|
|
|
replacedLeaf := replaceReservedChars(leaf)
|
2018-08-04 12:16:43 +02:00
|
|
|
|
copyFile := api.CopyFile{
|
2017-07-05 23:20:40 +02:00
|
|
|
|
Name: replacedLeaf,
|
|
|
|
|
Parent: api.Parent{
|
|
|
|
|
ID: directoryID,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
var resp *http.Response
|
|
|
|
|
var info *api.Item
|
|
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
2018-08-04 12:16:43 +02:00
|
|
|
|
resp, err = f.srv.CallJSON(&opts, ©File, &info)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
return shouldRetry(resp, err)
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
err = dstObj.setMetaData(info)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return dstObj, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Purge deletes all the files and the container
|
|
|
|
|
//
|
|
|
|
|
// 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()
|
2019-06-17 10:34:30 +02:00
|
|
|
|
func (f *Fs) Purge(ctx context.Context) error {
|
|
|
|
|
return f.purgeCheck(ctx, "", false)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// move a file or folder
|
|
|
|
|
func (f *Fs) move(endpoint, id, leaf, directoryID string) (info *api.Item, err error) {
|
|
|
|
|
// Move the object
|
|
|
|
|
opts := rest.Opts{
|
|
|
|
|
Method: "PUT",
|
|
|
|
|
Path: endpoint + id,
|
|
|
|
|
Parameters: fieldsValue(),
|
|
|
|
|
}
|
|
|
|
|
move := api.UpdateFileMove{
|
|
|
|
|
Name: replaceReservedChars(leaf),
|
|
|
|
|
Parent: api.Parent{
|
|
|
|
|
ID: directoryID,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
var resp *http.Response
|
|
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
|
|
|
|
resp, err = f.srv.CallJSON(&opts, &move, &info)
|
|
|
|
|
return shouldRetry(resp, err)
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return info, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Move src to this remote using server side move operations.
|
|
|
|
|
//
|
|
|
|
|
// 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 10:34:30 +02:00
|
|
|
|
func (f *Fs) Move(ctx context.Context, src fs.Object, remote string) (fs.Object, error) {
|
2017-07-05 23:20:40 +02:00
|
|
|
|
srcObj, ok := src.(*Object)
|
|
|
|
|
if !ok {
|
|
|
|
|
fs.Debugf(src, "Can't move - not same remote type")
|
|
|
|
|
return nil, fs.ErrorCantMove
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create temporary object
|
2019-06-17 10:34:30 +02:00
|
|
|
|
dstObj, leaf, directoryID, err := f.createObject(ctx, remote, srcObj.modTime, srcObj.size)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Do the move
|
|
|
|
|
info, err := f.move("/files/", srcObj.id, leaf, directoryID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = dstObj.setMetaData(info)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return dstObj, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DirMove moves src, srcRemote to this remote at dstRemote
|
|
|
|
|
// using server side move operations.
|
|
|
|
|
//
|
|
|
|
|
// 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 10:34:30 +02:00
|
|
|
|
func (f *Fs) DirMove(ctx context.Context, src fs.Fs, srcRemote, dstRemote string) error {
|
2017-07-05 23:20:40 +02:00
|
|
|
|
srcFs, ok := src.(*Fs)
|
|
|
|
|
if !ok {
|
|
|
|
|
fs.Debugf(srcFs, "Can't move directory - not same remote type")
|
|
|
|
|
return fs.ErrorCantDirMove
|
|
|
|
|
}
|
|
|
|
|
srcPath := path.Join(srcFs.root, srcRemote)
|
|
|
|
|
dstPath := path.Join(f.root, dstRemote)
|
|
|
|
|
|
|
|
|
|
// Refuse to move to or from the root
|
|
|
|
|
if srcPath == "" || dstPath == "" {
|
|
|
|
|
fs.Debugf(src, "DirMove error: Can't move root")
|
|
|
|
|
return errors.New("can't move root directory")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// find the root src directory
|
2019-06-17 10:34:30 +02:00
|
|
|
|
err := srcFs.dirCache.FindRoot(ctx, false)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// find the root dst directory
|
|
|
|
|
if dstRemote != "" {
|
2019-06-17 10:34:30 +02:00
|
|
|
|
err = f.dirCache.FindRoot(ctx, true)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if f.dirCache.FoundRoot() {
|
|
|
|
|
return fs.ErrorDirExists
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find ID of dst parent, creating subdirs if necessary
|
|
|
|
|
var leaf, directoryID string
|
|
|
|
|
findPath := dstRemote
|
|
|
|
|
if dstRemote == "" {
|
|
|
|
|
findPath = f.root
|
|
|
|
|
}
|
2019-06-17 10:34:30 +02:00
|
|
|
|
leaf, directoryID, err = f.dirCache.FindPath(ctx, findPath, true)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check destination does not exist
|
|
|
|
|
if dstRemote != "" {
|
2019-06-17 10:34:30 +02:00
|
|
|
|
_, err = f.dirCache.FindDir(ctx, dstRemote, false)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
if err == fs.ErrorDirNotFound {
|
|
|
|
|
// OK
|
|
|
|
|
} else if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
} else {
|
|
|
|
|
return fs.ErrorDirExists
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find ID of src
|
2019-06-17 10:34:30 +02:00
|
|
|
|
srcID, err := srcFs.dirCache.FindDir(ctx, srcRemote, false)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Do the move
|
|
|
|
|
_, err = f.move("/folders/", srcID, leaf, directoryID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
srcFs.dirCache.FlushDir(srcRemote)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-19 15:13:33 +02:00
|
|
|
|
// PublicLink adds a "readable by anyone with link" permission on the given file or folder.
|
2019-06-17 10:34:30 +02:00
|
|
|
|
func (f *Fs) PublicLink(ctx context.Context, remote string) (string, error) {
|
|
|
|
|
id, err := f.dirCache.FindDir(ctx, remote, false)
|
2018-08-19 15:13:33 +02:00
|
|
|
|
var opts rest.Opts
|
|
|
|
|
if err == nil {
|
|
|
|
|
fs.Debugf(f, "attempting to share directory '%s'", remote)
|
|
|
|
|
|
|
|
|
|
opts = rest.Opts{
|
|
|
|
|
Method: "PUT",
|
|
|
|
|
Path: "/folders/" + id,
|
|
|
|
|
Parameters: fieldsValue(),
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
fs.Debugf(f, "attempting to share single file '%s'", remote)
|
2019-06-17 10:34:30 +02:00
|
|
|
|
o, err := f.NewObject(ctx, remote)
|
2018-08-19 15:13:33 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if o.(*Object).publicLink != "" {
|
|
|
|
|
return o.(*Object).publicLink, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
opts = rest.Opts{
|
|
|
|
|
Method: "PUT",
|
|
|
|
|
Path: "/files/" + o.(*Object).id,
|
|
|
|
|
Parameters: fieldsValue(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
shareLink := api.CreateSharedLink{}
|
|
|
|
|
var info api.Item
|
|
|
|
|
var resp *http.Response
|
|
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
|
|
|
|
resp, err = f.srv.CallJSON(&opts, &shareLink, &info)
|
|
|
|
|
return shouldRetry(resp, err)
|
|
|
|
|
})
|
|
|
|
|
return info.SharedLink.URL, err
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-05 23:20:40 +02:00
|
|
|
|
// DirCacheFlush resets the directory cache - used in testing as an
|
|
|
|
|
// optional interface
|
|
|
|
|
func (f *Fs) DirCacheFlush() {
|
|
|
|
|
f.dirCache.ResetRoot()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Hashes returns the supported hash sets.
|
2018-01-12 17:30:54 +01:00
|
|
|
|
func (f *Fs) Hashes() hash.Set {
|
2018-01-18 21:27:52 +01:00
|
|
|
|
return hash.Set(hash.SHA1)
|
2017-07-05 23:20:40 +02: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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// srvPath returns a path for use in server
|
|
|
|
|
func (o *Object) srvPath() string {
|
|
|
|
|
return replaceReservedChars(o.fs.rootSlash() + o.remote)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Hash returns the SHA-1 of an object returning a lowercase hex string
|
2019-06-17 10:34:30 +02:00
|
|
|
|
func (o *Object) Hash(ctx context.Context, t hash.Type) (string, error) {
|
2018-01-18 21:27:52 +01:00
|
|
|
|
if t != hash.SHA1 {
|
|
|
|
|
return "", hash.ErrUnsupported
|
2017-07-05 23:20:40 +02:00
|
|
|
|
}
|
|
|
|
|
return o.sha1, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Size returns the size of an object in bytes
|
|
|
|
|
func (o *Object) Size() int64 {
|
2019-06-17 10:34:30 +02:00
|
|
|
|
err := o.readMetaData(context.TODO())
|
2017-07-05 23:20:40 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
fs.Logf(o, "Failed to read metadata: %v", err)
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
return o.size
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// setMetaData sets the metadata from info
|
|
|
|
|
func (o *Object) setMetaData(info *api.Item) (err error) {
|
|
|
|
|
if info.Type != api.ItemTypeFile {
|
|
|
|
|
return errors.Wrapf(fs.ErrorNotAFile, "%q is %q", o.remote, info.Type)
|
|
|
|
|
}
|
|
|
|
|
o.hasMetaData = true
|
2018-04-19 10:18:14 +02:00
|
|
|
|
o.size = int64(info.Size)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
o.sha1 = info.SHA1
|
|
|
|
|
o.modTime = info.ModTime()
|
|
|
|
|
o.id = info.ID
|
2018-08-19 15:13:33 +02:00
|
|
|
|
o.publicLink = info.SharedLink.URL
|
2017-07-05 23:20:40 +02:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// readMetaData gets the metadata if it hasn't already been fetched
|
|
|
|
|
//
|
|
|
|
|
// it also sets the info
|
2019-06-17 10:34:30 +02:00
|
|
|
|
func (o *Object) readMetaData(ctx context.Context) (err error) {
|
2017-07-05 23:20:40 +02:00
|
|
|
|
if o.hasMetaData {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2019-06-17 10:34:30 +02:00
|
|
|
|
info, err := o.fs.readMetaDataForPath(ctx, o.remote)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
if apiErr, ok := err.(*api.Error); ok {
|
|
|
|
|
if apiErr.Code == "not_found" || apiErr.Code == "trashed" {
|
|
|
|
|
return fs.ErrorObjectNotFound
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return o.setMetaData(info)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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 10:34:30 +02:00
|
|
|
|
func (o *Object) ModTime(ctx context.Context) time.Time {
|
|
|
|
|
err := o.readMetaData(ctx)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
fs.Logf(o, "Failed to read metadata: %v", err)
|
|
|
|
|
return time.Now()
|
|
|
|
|
}
|
|
|
|
|
return o.modTime
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// setModTime sets the modification time of the local fs object
|
2019-06-17 10:34:30 +02:00
|
|
|
|
func (o *Object) setModTime(ctx context.Context, modTime time.Time) (*api.Item, error) {
|
2017-07-05 23:20:40 +02:00
|
|
|
|
opts := rest.Opts{
|
|
|
|
|
Method: "PUT",
|
|
|
|
|
Path: "/files/" + o.id,
|
|
|
|
|
Parameters: fieldsValue(),
|
|
|
|
|
}
|
|
|
|
|
update := api.UpdateFileModTime{
|
|
|
|
|
ContentModifiedAt: api.Time(modTime),
|
|
|
|
|
}
|
|
|
|
|
var info *api.Item
|
|
|
|
|
err := o.fs.pacer.Call(func() (bool, error) {
|
|
|
|
|
resp, err := o.fs.srv.CallJSON(&opts, &update, &info)
|
|
|
|
|
return shouldRetry(resp, err)
|
|
|
|
|
})
|
|
|
|
|
return info, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetModTime sets the modification time of the local fs object
|
2019-06-17 10:34:30 +02:00
|
|
|
|
func (o *Object) SetModTime(ctx context.Context, modTime time.Time) error {
|
|
|
|
|
info, err := o.setModTime(ctx, modTime)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return o.setMetaData(info)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Storable returns a boolean showing whether this object storable
|
|
|
|
|
func (o *Object) Storable() bool {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Open an object for read
|
2019-06-17 10:34:30 +02:00
|
|
|
|
func (o *Object) Open(ctx context.Context, options ...fs.OpenOption) (in io.ReadCloser, err error) {
|
2017-07-05 23:20:40 +02:00
|
|
|
|
if o.id == "" {
|
|
|
|
|
return nil, errors.New("can't download - no id")
|
|
|
|
|
}
|
2018-01-22 18:05:47 +01:00
|
|
|
|
fs.FixRangeOption(options, o.size)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
var resp *http.Response
|
|
|
|
|
opts := rest.Opts{
|
|
|
|
|
Method: "GET",
|
|
|
|
|
Path: "/files/" + o.id + "/content",
|
|
|
|
|
Options: options,
|
|
|
|
|
}
|
|
|
|
|
err = o.fs.pacer.Call(func() (bool, error) {
|
|
|
|
|
resp, err = o.fs.srv.Call(&opts)
|
|
|
|
|
return shouldRetry(resp, err)
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return resp.Body, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// upload does a single non-multipart upload
|
|
|
|
|
//
|
|
|
|
|
// This is recommended for less than 50 MB of content
|
2017-08-19 12:32:56 +02:00
|
|
|
|
func (o *Object) upload(in io.Reader, leaf, directoryID string, modTime time.Time) (err error) {
|
2017-07-05 23:20:40 +02:00
|
|
|
|
upload := api.UploadFile{
|
|
|
|
|
Name: replaceReservedChars(leaf),
|
|
|
|
|
ContentModifiedAt: api.Time(modTime),
|
|
|
|
|
ContentCreatedAt: api.Time(modTime),
|
|
|
|
|
Parent: api.Parent{
|
|
|
|
|
ID: directoryID,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var resp *http.Response
|
|
|
|
|
var result api.FolderItems
|
|
|
|
|
opts := rest.Opts{
|
2018-08-18 13:33:33 +02:00
|
|
|
|
Method: "POST",
|
|
|
|
|
Body: in,
|
2017-07-05 23:20:40 +02:00
|
|
|
|
MultipartMetadataName: "attributes",
|
|
|
|
|
MultipartContentName: "contents",
|
|
|
|
|
MultipartFileName: upload.Name,
|
|
|
|
|
RootURL: uploadURL,
|
|
|
|
|
}
|
|
|
|
|
// If object has an ID then it is existing so create a new version
|
|
|
|
|
if o.id != "" {
|
|
|
|
|
opts.Path = "/files/" + o.id + "/content"
|
|
|
|
|
} else {
|
|
|
|
|
opts.Path = "/files/content"
|
|
|
|
|
}
|
|
|
|
|
err = o.fs.pacer.CallNoRetry(func() (bool, error) {
|
|
|
|
|
resp, err = o.fs.srv.CallJSON(&opts, &upload, &result)
|
|
|
|
|
return shouldRetry(resp, err)
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if result.TotalCount != 1 || len(result.Entries) != 1 {
|
|
|
|
|
return errors.Errorf("failed to upload %v - not sure why", o)
|
|
|
|
|
}
|
|
|
|
|
return o.setMetaData(&result.Entries[0])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update the object with the contents of the io.Reader, modTime and size
|
|
|
|
|
//
|
|
|
|
|
// If existing is set then it updates the object rather than creating a new one
|
|
|
|
|
//
|
|
|
|
|
// The new object may have been created if an error is returned
|
2019-06-17 10:34:30 +02:00
|
|
|
|
func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) (err error) {
|
2017-07-05 23:20:40 +02:00
|
|
|
|
o.fs.tokenRenewer.Start()
|
|
|
|
|
defer o.fs.tokenRenewer.Stop()
|
|
|
|
|
|
|
|
|
|
size := src.Size()
|
2019-06-17 10:34:30 +02:00
|
|
|
|
modTime := src.ModTime(ctx)
|
2017-09-14 18:21:11 +02:00
|
|
|
|
remote := o.Remote()
|
2017-07-05 23:20:40 +02:00
|
|
|
|
|
|
|
|
|
// Create the directory for the object if it doesn't exist
|
2019-06-17 10:34:30 +02:00
|
|
|
|
leaf, directoryID, err := o.fs.dirCache.FindRootAndPath(ctx, remote, true)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Upload with simple or multipart
|
2018-05-14 19:06:57 +02:00
|
|
|
|
if size <= int64(o.fs.opt.UploadCutoff) {
|
2017-08-19 12:32:56 +02:00
|
|
|
|
err = o.upload(in, leaf, directoryID, modTime)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
} else {
|
|
|
|
|
err = o.uploadMultipart(in, leaf, directoryID, size, modTime)
|
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove an object
|
2019-06-17 10:34:30 +02:00
|
|
|
|
func (o *Object) Remove(ctx context.Context) error {
|
2017-07-05 23:20:40 +02:00
|
|
|
|
return o.fs.deleteObject(o.id)
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-13 10:16:56 +02:00
|
|
|
|
// ID returns the ID of the Object if known, or "" if not
|
|
|
|
|
func (o *Object) ID() string {
|
|
|
|
|
return o.id
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-05 23:20:40 +02:00
|
|
|
|
// Check the interfaces are satisfied
|
|
|
|
|
var (
|
|
|
|
|
_ fs.Fs = (*Fs)(nil)
|
|
|
|
|
_ fs.Purger = (*Fs)(nil)
|
2017-08-19 12:32:56 +02:00
|
|
|
|
_ fs.PutStreamer = (*Fs)(nil)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
_ fs.Copier = (*Fs)(nil)
|
|
|
|
|
_ fs.Mover = (*Fs)(nil)
|
|
|
|
|
_ fs.DirMover = (*Fs)(nil)
|
|
|
|
|
_ fs.DirCacheFlusher = (*Fs)(nil)
|
2018-08-19 15:13:33 +02:00
|
|
|
|
_ fs.PublicLinker = (*Fs)(nil)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
_ fs.Object = (*Object)(nil)
|
2018-05-13 10:16:56 +02:00
|
|
|
|
_ fs.IDer = (*Object)(nil)
|
2017-07-05 23:20:40 +02:00
|
|
|
|
)
|