2015-09-22 19:47:16 +02:00
|
|
|
// Package dropbox provides an interface to Dropbox object storage
|
2014-07-08 22:59:30 +02:00
|
|
|
package dropbox
|
|
|
|
|
2017-05-28 18:55:18 +02:00
|
|
|
// FIXME buffer chunks for retries in upload
|
2017-05-21 22:35:33 +02:00
|
|
|
// FIXME dropbox for business would be quite easy to add
|
|
|
|
|
2014-07-08 22:59:30 +02:00
|
|
|
/*
|
2017-06-11 23:43:31 +02:00
|
|
|
The Case folding of PathDisplay problem
|
2017-05-21 22:35:33 +02:00
|
|
|
|
2017-06-11 23:43:31 +02:00
|
|
|
From the docs:
|
2014-07-08 22:59:30 +02:00
|
|
|
|
2017-05-21 22:35:33 +02:00
|
|
|
path_display String. The cased path to be used for display purposes
|
|
|
|
only. In rare instances the casing will not correctly match the user's
|
|
|
|
filesystem, but this behavior will match the path provided in the Core
|
|
|
|
API v1, and at least the last path component will have the correct
|
|
|
|
casing. Changes to only the casing of paths won't be returned by
|
|
|
|
list_folder/continue. This field will be null if the file or folder is
|
|
|
|
not mounted. This field is optional.
|
2017-06-05 17:14:24 +02:00
|
|
|
|
2017-06-11 23:43:31 +02:00
|
|
|
We solve this by not implementing the ListR interface. The dropbox remote will recurse directory by directory and all will be well.
|
2014-07-08 22:59:30 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
2014-07-14 12:24:04 +02:00
|
|
|
"path"
|
2015-08-20 19:36:06 +02:00
|
|
|
"regexp"
|
2014-07-08 22:59:30 +02:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2017-05-30 16:49:29 +02:00
|
|
|
"github.com/ncw/dropbox-sdk-go-unofficial/dropbox"
|
|
|
|
"github.com/ncw/dropbox-sdk-go-unofficial/dropbox/files"
|
2014-07-08 22:59:30 +02:00
|
|
|
"github.com/ncw/rclone/fs"
|
2015-08-29 18:45:10 +02:00
|
|
|
"github.com/ncw/rclone/oauthutil"
|
2017-05-28 18:55:18 +02:00
|
|
|
"github.com/ncw/rclone/pacer"
|
2016-06-12 16:06:02 +02:00
|
|
|
"github.com/pkg/errors"
|
2017-05-28 18:55:18 +02:00
|
|
|
"golang.org/x/oauth2"
|
2014-07-08 22:59:30 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Constants
|
|
|
|
const (
|
2017-05-21 22:35:33 +02:00
|
|
|
rcloneClientID = "5jcck7diasz0rqy"
|
|
|
|
rcloneEncryptedClientSecret = "fRS5vVLr2v6FbyXYnIgjwBuUAt0osq_QZTXAEcmZ7g"
|
2017-05-28 18:55:18 +02:00
|
|
|
minSleep = 10 * time.Millisecond
|
|
|
|
maxSleep = 2 * time.Second
|
|
|
|
decayConstant = 2 // bigger for slower decay, exponential
|
2014-07-08 22:59:30 +02:00
|
|
|
)
|
|
|
|
|
2015-08-25 20:01:37 +02:00
|
|
|
var (
|
2017-05-21 22:35:33 +02:00
|
|
|
// Description of how to auth for this app
|
|
|
|
dropboxConfig = &oauth2.Config{
|
|
|
|
Scopes: []string{},
|
2017-05-30 16:49:29 +02:00
|
|
|
// Endpoint: oauth2.Endpoint{
|
|
|
|
// AuthURL: "https://www.dropbox.com/1/oauth2/authorize",
|
|
|
|
// TokenURL: "https://api.dropboxapi.com/1/oauth2/token",
|
|
|
|
// },
|
|
|
|
Endpoint: dropbox.OAuthEndpoint(""),
|
2017-05-21 22:35:33 +02:00
|
|
|
ClientID: rcloneClientID,
|
|
|
|
ClientSecret: fs.MustReveal(rcloneEncryptedClientSecret),
|
|
|
|
RedirectURL: oauthutil.RedirectLocalhostURL,
|
|
|
|
}
|
2015-08-25 20:01:37 +02:00
|
|
|
// A regexp matching path names for files Dropbox ignores
|
|
|
|
// See https://www.dropbox.com/en/help/145 - Ignored files
|
|
|
|
ignoredFiles = regexp.MustCompile(`(?i)(^|/)(desktop\.ini|thumbs\.db|\.ds_store|icon\r|\.dropbox|\.dropbox.attr)$`)
|
|
|
|
// Upload chunk size - setting too small makes uploads slow.
|
|
|
|
// Chunks aren't buffered into memory though so can set large.
|
|
|
|
uploadChunkSize = fs.SizeSuffix(128 * 1024 * 1024)
|
|
|
|
maxUploadChunkSize = fs.SizeSuffix(150 * 1024 * 1024)
|
|
|
|
)
|
2015-08-20 19:36:06 +02:00
|
|
|
|
2014-07-08 22:59:30 +02:00
|
|
|
// Register with Fs
|
|
|
|
func init() {
|
2016-02-18 12:35:25 +01:00
|
|
|
fs.Register(&fs.RegInfo{
|
2016-02-15 19:11:53 +01:00
|
|
|
Name: "dropbox",
|
|
|
|
Description: "Dropbox",
|
|
|
|
NewFs: NewFs,
|
2017-05-21 22:35:33 +02:00
|
|
|
Config: func(name string) {
|
2017-06-14 17:46:46 +02:00
|
|
|
err := oauthutil.ConfigNoOffline("dropbox", name, dropboxConfig)
|
2017-05-21 22:35:33 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to configure token: %v", err)
|
|
|
|
}
|
|
|
|
},
|
2014-07-08 22:59:30 +02:00
|
|
|
Options: []fs.Option{{
|
|
|
|
Name: "app_key",
|
2015-10-03 15:23:12 +02:00
|
|
|
Help: "Dropbox App Key - leave blank normally.",
|
2014-07-08 22:59:30 +02:00
|
|
|
}, {
|
|
|
|
Name: "app_secret",
|
2015-10-03 15:23:12 +02:00
|
|
|
Help: "Dropbox App Secret - leave blank normally.",
|
2014-07-08 22:59:30 +02:00
|
|
|
}},
|
|
|
|
})
|
2016-12-20 16:50:46 +01:00
|
|
|
fs.VarP(&uploadChunkSize, "dropbox-chunk-size", "", fmt.Sprintf("Upload chunk size. Max %v.", maxUploadChunkSize))
|
2014-07-08 22:59:30 +02:00
|
|
|
}
|
|
|
|
|
2015-11-07 12:14:46 +01:00
|
|
|
// Fs represents a remote dropbox server
|
|
|
|
type Fs struct {
|
2017-05-21 22:35:33 +02:00
|
|
|
name string // name of this remote
|
|
|
|
root string // the path we are working on
|
|
|
|
features *fs.Features // optional features
|
|
|
|
srv files.Client // the connection to the dropbox server
|
|
|
|
slashRoot string // root with "/" prefix, lowercase
|
|
|
|
slashRootSlash string // root with "/" prefix and postfix, lowercase
|
2017-05-28 18:55:18 +02:00
|
|
|
pacer *pacer.Pacer // To pace the API calls
|
2014-07-08 22:59:30 +02:00
|
|
|
}
|
|
|
|
|
2015-11-07 12:14:46 +01:00
|
|
|
// Object describes a dropbox object
|
2017-05-26 16:09:31 +02:00
|
|
|
//
|
|
|
|
// Dropbox Objects always have full metadata
|
2015-11-07 12:14:46 +01:00
|
|
|
type Object struct {
|
2017-05-26 16:09:31 +02:00
|
|
|
fs *Fs // what this object is part of
|
|
|
|
remote string // The remote path
|
|
|
|
bytes int64 // size of the object
|
|
|
|
modTime time.Time // time it was last modified
|
|
|
|
hash string // content_hash of the object
|
2014-07-08 22:59:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// Name of the remote (as passed into NewFs)
|
2015-11-07 12:14:46 +01:00
|
|
|
func (f *Fs) Name() string {
|
2015-08-22 17:53:11 +02:00
|
|
|
return f.name
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// Root of the remote (as passed into NewFs)
|
2015-11-07 12:14:46 +01:00
|
|
|
func (f *Fs) Root() string {
|
2015-09-01 21:45:27 +02:00
|
|
|
return f.root
|
|
|
|
}
|
|
|
|
|
2015-11-07 12:14:46 +01:00
|
|
|
// String converts this Fs to a string
|
|
|
|
func (f *Fs) String() string {
|
2014-07-08 22:59:30 +02:00
|
|
|
return fmt.Sprintf("Dropbox root '%s'", f.root)
|
|
|
|
}
|
|
|
|
|
2017-01-13 18:21:47 +01:00
|
|
|
// Features returns the optional features of this Fs
|
|
|
|
func (f *Fs) Features() *fs.Features {
|
|
|
|
return f.features
|
|
|
|
}
|
|
|
|
|
2017-05-28 18:55:18 +02:00
|
|
|
// shouldRetry returns a boolean as to whether this err deserves to be
|
|
|
|
// retried. It returns the err as a convenience
|
|
|
|
func shouldRetry(err error) (bool, error) {
|
|
|
|
if err == nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
baseErrString := errors.Cause(err).Error()
|
|
|
|
// FIXME there is probably a better way of doing this!
|
|
|
|
if strings.Contains(baseErrString, "too_many_write_operations") || strings.Contains(baseErrString, "too_many_requests") {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
return fs.ShouldRetry(err), err
|
|
|
|
}
|
|
|
|
|
2015-11-07 12:14:46 +01:00
|
|
|
// NewFs contstructs an Fs from the path, container:path
|
2014-07-14 12:24:04 +02:00
|
|
|
func NewFs(name, root string) (fs.Fs, error) {
|
2015-08-25 20:01:37 +02:00
|
|
|
if uploadChunkSize > maxUploadChunkSize {
|
2016-06-12 16:06:02 +02:00
|
|
|
return nil, errors.Errorf("chunk size too big, must be < %v", maxUploadChunkSize)
|
2015-08-25 20:01:37 +02:00
|
|
|
}
|
2017-05-21 22:35:33 +02:00
|
|
|
|
|
|
|
// Convert the old token if it exists. The old token was just
|
|
|
|
// just a string, the new one is a JSON blob
|
|
|
|
oldToken := strings.TrimSpace(fs.ConfigFileGet(name, fs.ConfigToken))
|
|
|
|
if oldToken != "" && oldToken[0] != '{' {
|
|
|
|
fs.Infof(name, "Converting token to new format")
|
|
|
|
newToken := fmt.Sprintf(`{"access_token":"%s","token_type":"bearer","expiry":"0001-01-01T00:00:00Z"}`, oldToken)
|
|
|
|
err := fs.ConfigSetValueAndSave(name, fs.ConfigToken, newToken)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "NewFS convert token")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
oAuthClient, _, err := oauthutil.NewClient(name, dropboxConfig)
|
2015-09-22 08:31:12 +02:00
|
|
|
if err != nil {
|
2017-05-21 22:35:33 +02:00
|
|
|
log.Fatalf("Failed to configure dropbox: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config := dropbox.Config{
|
|
|
|
Verbose: false, // enables verbose logging in the SDK
|
|
|
|
Client: oAuthClient, // maybe???
|
2015-09-22 08:31:12 +02:00
|
|
|
}
|
2017-05-21 22:35:33 +02:00
|
|
|
srv := files.New(config)
|
|
|
|
|
2015-11-07 12:14:46 +01:00
|
|
|
f := &Fs{
|
2017-05-28 18:55:18 +02:00
|
|
|
name: name,
|
|
|
|
srv: srv,
|
|
|
|
pacer: pacer.New().SetMinSleep(minSleep).SetMaxSleep(maxSleep).SetDecayConstant(decayConstant),
|
2014-07-08 22:59:30 +02:00
|
|
|
}
|
2017-01-13 18:21:47 +01:00
|
|
|
f.features = (&fs.Features{CaseInsensitive: true, ReadMimeType: true}).Fill(f)
|
2014-07-14 12:24:04 +02:00
|
|
|
f.setRoot(root)
|
2014-07-08 22:59:30 +02:00
|
|
|
|
2014-07-14 12:24:04 +02:00
|
|
|
// See if the root is actually an object
|
2017-05-21 22:35:33 +02:00
|
|
|
_, err = f.getFileMetadata(f.slashRoot)
|
|
|
|
if err == nil {
|
2014-07-14 12:24:04 +02:00
|
|
|
newRoot := path.Dir(f.root)
|
|
|
|
if newRoot == "." {
|
|
|
|
newRoot = ""
|
2014-07-12 13:38:30 +02:00
|
|
|
}
|
2014-07-14 12:24:04 +02:00
|
|
|
f.setRoot(newRoot)
|
2016-06-21 19:01:53 +02:00
|
|
|
// return an error with an fs which points to the parent
|
|
|
|
return f, fs.ErrorIsFile
|
2014-07-14 12:24:04 +02:00
|
|
|
}
|
2014-07-08 22:59:30 +02:00
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
2014-07-14 12:24:04 +02:00
|
|
|
// Sets root in f
|
2015-11-07 12:14:46 +01:00
|
|
|
func (f *Fs) setRoot(root string) {
|
2014-07-14 12:24:04 +02:00
|
|
|
f.root = strings.Trim(root, "/")
|
2015-05-23 20:56:48 +02:00
|
|
|
lowerCaseRoot := strings.ToLower(f.root)
|
|
|
|
|
|
|
|
f.slashRoot = "/" + lowerCaseRoot
|
2014-07-14 12:24:04 +02:00
|
|
|
f.slashRootSlash = f.slashRoot
|
2015-05-23 20:56:48 +02:00
|
|
|
if lowerCaseRoot != "" {
|
2014-07-14 12:24:04 +02:00
|
|
|
f.slashRootSlash += "/"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-21 22:35:33 +02:00
|
|
|
// getMetadata gets the metadata for a file or directory
|
|
|
|
func (f *Fs) getMetadata(objPath string) (entry files.IsMetadata, notFound bool, err error) {
|
2017-05-28 18:55:18 +02:00
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
|
|
|
entry, err = f.srv.GetMetadata(&files.GetMetadataArg{Path: objPath})
|
|
|
|
return shouldRetry(err)
|
|
|
|
})
|
2017-05-21 22:35:33 +02:00
|
|
|
if err != nil {
|
|
|
|
switch e := err.(type) {
|
|
|
|
case files.GetMetadataAPIError:
|
|
|
|
switch e.EndpointError.Path.Tag {
|
|
|
|
case files.LookupErrorNotFound:
|
|
|
|
notFound = true
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// getFileMetadata gets the metadata for a file
|
|
|
|
func (f *Fs) getFileMetadata(filePath string) (fileInfo *files.FileMetadata, err error) {
|
|
|
|
entry, notFound, err := f.getMetadata(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if notFound {
|
|
|
|
return nil, fs.ErrorObjectNotFound
|
|
|
|
}
|
|
|
|
fileInfo, ok := entry.(*files.FileMetadata)
|
|
|
|
if !ok {
|
|
|
|
return nil, fs.ErrorNotAFile
|
|
|
|
}
|
|
|
|
return fileInfo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getDirMetadata gets the metadata for a directory
|
|
|
|
func (f *Fs) getDirMetadata(dirPath string) (dirInfo *files.FolderMetadata, err error) {
|
|
|
|
entry, notFound, err := f.getMetadata(dirPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if notFound {
|
|
|
|
return nil, fs.ErrorDirNotFound
|
|
|
|
}
|
|
|
|
dirInfo, ok := entry.(*files.FolderMetadata)
|
|
|
|
if !ok {
|
|
|
|
return nil, fs.ErrorIsFile
|
|
|
|
}
|
|
|
|
return dirInfo, nil
|
|
|
|
}
|
|
|
|
|
2016-06-25 22:58:34 +02:00
|
|
|
// Return an Object from a path
|
2014-07-29 18:50:07 +02:00
|
|
|
//
|
2016-06-25 22:23:20 +02:00
|
|
|
// If it can't be found it returns the error fs.ErrorObjectNotFound.
|
2017-05-21 22:35:33 +02:00
|
|
|
func (f *Fs) newObjectWithInfo(remote string, info *files.FileMetadata) (fs.Object, error) {
|
2017-02-25 16:23:27 +01:00
|
|
|
o := &Object{
|
2015-11-07 12:14:46 +01:00
|
|
|
fs: f,
|
|
|
|
remote: remote,
|
2014-07-08 22:59:30 +02:00
|
|
|
}
|
2017-02-25 16:23:27 +01:00
|
|
|
var err error
|
2014-07-12 12:46:45 +02:00
|
|
|
if info != nil {
|
2017-02-25 12:09:57 +01:00
|
|
|
err = o.setMetadataFromEntry(info)
|
2014-07-08 22:59:30 +02:00
|
|
|
} else {
|
2017-02-25 12:09:57 +01:00
|
|
|
err = o.readEntryAndSetMetadata()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-07-08 22:59:30 +02:00
|
|
|
}
|
2016-06-25 22:23:20 +02:00
|
|
|
return o, nil
|
2014-07-08 22:59:30 +02:00
|
|
|
}
|
|
|
|
|
2016-06-25 22:23:20 +02:00
|
|
|
// NewObject finds the Object at remote. If it can't be found
|
|
|
|
// it returns the error fs.ErrorObjectNotFound.
|
|
|
|
func (f *Fs) NewObject(remote string) (fs.Object, error) {
|
2016-06-25 22:58:34 +02:00
|
|
|
return f.newObjectWithInfo(remote, nil)
|
2014-07-08 22:59:30 +02:00
|
|
|
}
|
|
|
|
|
2015-05-23 20:56:48 +02:00
|
|
|
// Strips the root off path and returns it
|
2016-05-07 15:50:35 +02:00
|
|
|
func strip(path, root string) (string, error) {
|
|
|
|
if len(root) > 0 {
|
|
|
|
if root[0] != '/' {
|
|
|
|
root = "/" + root
|
|
|
|
}
|
|
|
|
if root[len(root)-1] != '/' {
|
|
|
|
root += "/"
|
|
|
|
}
|
2016-05-16 18:54:59 +02:00
|
|
|
} else if len(root) == 0 {
|
|
|
|
root = "/"
|
2016-05-07 15:50:35 +02:00
|
|
|
}
|
2017-02-22 13:48:16 +01:00
|
|
|
if !strings.HasPrefix(strings.ToLower(path), strings.ToLower(root)) {
|
2016-06-12 16:06:02 +02:00
|
|
|
return "", errors.Errorf("path %q is not under root %q", path, root)
|
2014-07-08 22:59:30 +02:00
|
|
|
}
|
2016-05-07 15:50:35 +02:00
|
|
|
return path[len(root):], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strips the root off path and returns it
|
|
|
|
func (f *Fs) stripRoot(path string) (string, error) {
|
|
|
|
return strip(path, f.slashRootSlash)
|
2014-07-08 22:59:30 +02:00
|
|
|
}
|
|
|
|
|
2017-06-11 23:43:31 +02: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.
|
|
|
|
func (f *Fs) List(dir string) (entries fs.DirEntries, err error) {
|
2016-04-23 22:46:52 +02:00
|
|
|
root := f.slashRoot
|
|
|
|
if dir != "" {
|
|
|
|
root += "/" + dir
|
|
|
|
}
|
2017-05-21 22:35:33 +02:00
|
|
|
|
|
|
|
started := false
|
|
|
|
var res *files.ListFolderResult
|
2014-07-12 12:46:45 +02:00
|
|
|
for {
|
2017-05-21 22:35:33 +02:00
|
|
|
if !started {
|
|
|
|
arg := files.ListFolderArg{
|
|
|
|
Path: root,
|
2017-06-11 23:43:31 +02:00
|
|
|
Recursive: false,
|
2017-05-21 22:35:33 +02:00
|
|
|
}
|
|
|
|
if root == "/" {
|
|
|
|
arg.Path = "" // Specify root folder as empty string
|
|
|
|
}
|
2017-05-28 18:55:18 +02:00
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
|
|
|
res, err = f.srv.ListFolder(&arg)
|
|
|
|
return shouldRetry(err)
|
|
|
|
})
|
2017-05-21 22:35:33 +02:00
|
|
|
if err != nil {
|
|
|
|
switch e := err.(type) {
|
|
|
|
case files.ListFolderAPIError:
|
|
|
|
switch e.EndpointError.Path.Tag {
|
|
|
|
case files.LookupErrorNotFound:
|
|
|
|
err = fs.ErrorDirNotFound
|
|
|
|
}
|
2016-05-07 15:50:35 +02:00
|
|
|
}
|
2017-06-11 23:43:31 +02:00
|
|
|
return nil, err
|
2017-05-21 22:35:33 +02:00
|
|
|
}
|
|
|
|
started = false
|
|
|
|
} else {
|
|
|
|
arg := files.ListFolderContinueArg{
|
|
|
|
Cursor: res.Cursor,
|
|
|
|
}
|
2017-05-28 18:55:18 +02:00
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
|
|
|
res, err = f.srv.ListFolderContinue(&arg)
|
|
|
|
return shouldRetry(err)
|
|
|
|
})
|
2017-05-21 22:35:33 +02:00
|
|
|
if err != nil {
|
2017-06-11 23:43:31 +02:00
|
|
|
return nil, errors.Wrap(err, "list continue")
|
2017-05-21 22:35:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, entry := range res.Entries {
|
|
|
|
var fileInfo *files.FileMetadata
|
|
|
|
var folderInfo *files.FolderMetadata
|
|
|
|
var metadata *files.Metadata
|
|
|
|
switch info := entry.(type) {
|
|
|
|
case *files.FolderMetadata:
|
|
|
|
folderInfo = info
|
|
|
|
metadata = &info.Metadata
|
|
|
|
case *files.FileMetadata:
|
|
|
|
fileInfo = info
|
|
|
|
metadata = &info.Metadata
|
|
|
|
default:
|
|
|
|
fs.Errorf(f, "Unknown type %T", entry)
|
|
|
|
continue
|
|
|
|
}
|
2015-05-23 20:56:48 +02:00
|
|
|
|
2017-05-21 22:35:33 +02:00
|
|
|
entryPath := metadata.PathDisplay // FIXME PathLower
|
2015-05-23 20:56:48 +02:00
|
|
|
|
2017-05-21 22:35:33 +02:00
|
|
|
if folderInfo != nil {
|
|
|
|
name, err := f.stripRoot(entryPath + "/")
|
|
|
|
if err != nil {
|
2017-06-11 23:43:31 +02:00
|
|
|
return nil, err
|
2016-05-07 15:50:35 +02:00
|
|
|
}
|
2017-05-21 22:35:33 +02:00
|
|
|
name = strings.Trim(name, "/")
|
|
|
|
if name != "" && name != dir {
|
2017-06-30 14:37:29 +02:00
|
|
|
d := fs.NewDir(name, time.Now())
|
2017-06-11 23:43:31 +02:00
|
|
|
entries = append(entries, d)
|
2014-07-12 12:46:45 +02:00
|
|
|
}
|
2017-05-21 22:35:33 +02:00
|
|
|
} else if fileInfo != nil {
|
|
|
|
path, err := f.stripRoot(entryPath)
|
|
|
|
if err != nil {
|
2017-06-11 23:43:31 +02:00
|
|
|
return nil, err
|
2017-05-21 22:35:33 +02:00
|
|
|
}
|
|
|
|
o, err := f.newObjectWithInfo(path, fileInfo)
|
|
|
|
if err != nil {
|
2017-06-11 23:43:31 +02:00
|
|
|
return nil, err
|
2017-05-21 22:35:33 +02:00
|
|
|
}
|
2017-06-11 23:43:31 +02:00
|
|
|
entries = append(entries, o)
|
2014-07-12 12:46:45 +02:00
|
|
|
}
|
2014-07-08 22:59:30 +02:00
|
|
|
}
|
2017-05-21 22:35:33 +02:00
|
|
|
if !res.HasMore {
|
2016-05-07 15:50:35 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2017-06-11 23:43:31 +02:00
|
|
|
return entries, nil
|
2014-07-08 22:59:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// A read closer which doesn't close the input
|
|
|
|
type readCloser struct {
|
|
|
|
in io.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read bytes from the object - see io.Reader
|
|
|
|
func (rc *readCloser) Read(p []byte) (n int, err error) {
|
|
|
|
return rc.in.Read(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dummy close function
|
|
|
|
func (rc *readCloser) Close() error {
|
|
|
|
return 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
|
2017-05-28 13:44:22 +02:00
|
|
|
func (f *Fs) Put(in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) (fs.Object, error) {
|
2015-11-07 12:14:46 +01:00
|
|
|
// Temporary Object under construction
|
|
|
|
o := &Object{
|
|
|
|
fs: f,
|
2016-02-18 12:35:25 +01:00
|
|
|
remote: src.Remote(),
|
2015-11-07 12:14:46 +01:00
|
|
|
}
|
2017-05-28 13:44:22 +02:00
|
|
|
return o, o.Update(in, src, options...)
|
2014-07-08 22:59:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mkdir creates the container if it doesn't exist
|
2016-11-25 22:52:43 +01:00
|
|
|
func (f *Fs) Mkdir(dir string) error {
|
|
|
|
root := path.Join(f.slashRoot, dir)
|
2017-05-21 22:35:33 +02:00
|
|
|
|
|
|
|
// can't create or run metadata on root
|
|
|
|
if root == "/" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// check directory doesn't exist
|
|
|
|
_, err := f.getDirMetadata(root)
|
2014-07-13 11:51:47 +02:00
|
|
|
if err == nil {
|
2017-05-21 22:35:33 +02:00
|
|
|
return nil // directory exists already
|
|
|
|
} else if err != fs.ErrorDirNotFound {
|
|
|
|
return err // some other error
|
|
|
|
}
|
|
|
|
|
|
|
|
// create it
|
|
|
|
arg2 := files.CreateFolderArg{
|
|
|
|
Path: root,
|
2014-07-13 11:51:47 +02:00
|
|
|
}
|
2017-05-28 18:55:18 +02:00
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
|
|
|
_, err = f.srv.CreateFolder(&arg2)
|
|
|
|
return shouldRetry(err)
|
|
|
|
})
|
2014-07-08 22:59:30 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rmdir deletes the container
|
|
|
|
//
|
|
|
|
// Returns an error if it isn't empty
|
2016-11-25 22:52:43 +01:00
|
|
|
func (f *Fs) Rmdir(dir string) error {
|
|
|
|
root := path.Join(f.slashRoot, dir)
|
2017-05-21 22:35:33 +02:00
|
|
|
|
|
|
|
// can't remove root
|
|
|
|
if root == "/" {
|
|
|
|
return errors.New("can't remove root directory")
|
|
|
|
}
|
|
|
|
|
|
|
|
// check directory exists
|
|
|
|
_, err := f.getDirMetadata(root)
|
2014-07-08 22:59:30 +02:00
|
|
|
if err != nil {
|
2017-05-21 22:35:33 +02:00
|
|
|
return errors.Wrap(err, "Rmdir")
|
|
|
|
}
|
|
|
|
|
|
|
|
// check directory empty
|
|
|
|
arg := files.ListFolderArg{
|
|
|
|
Path: root,
|
|
|
|
Recursive: false,
|
|
|
|
}
|
|
|
|
if root == "/" {
|
|
|
|
arg.Path = "" // Specify root folder as empty string
|
|
|
|
}
|
2017-05-28 18:55:18 +02:00
|
|
|
var res *files.ListFolderResult
|
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
|
|
|
res, err = f.srv.ListFolder(&arg)
|
|
|
|
return shouldRetry(err)
|
|
|
|
})
|
2017-05-21 22:35:33 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Rmdir")
|
2014-07-08 22:59:30 +02:00
|
|
|
}
|
2017-05-21 22:35:33 +02:00
|
|
|
if len(res.Entries) != 0 {
|
2016-06-12 16:06:02 +02:00
|
|
|
return errors.New("directory not empty")
|
2014-07-08 22:59:30 +02:00
|
|
|
}
|
2017-05-21 22:35:33 +02:00
|
|
|
|
|
|
|
// remove it
|
2017-05-28 18:55:18 +02:00
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
|
|
|
_, err = f.srv.Delete(&files.DeleteArg{Path: root})
|
|
|
|
return shouldRetry(err)
|
|
|
|
})
|
2016-11-25 22:52:43 +01:00
|
|
|
return err
|
2014-07-08 22:59:30 +02:00
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// Precision returns the precision
|
2015-11-07 12:14:46 +01:00
|
|
|
func (f *Fs) Precision() time.Duration {
|
2017-05-21 22:35:33 +02:00
|
|
|
return time.Second
|
2014-07-08 22:59:30 +02:00
|
|
|
}
|
|
|
|
|
2015-02-14 19:48:08 +01:00
|
|
|
// 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
|
2015-11-07 12:14:46 +01:00
|
|
|
func (f *Fs) Copy(src fs.Object, remote string) (fs.Object, error) {
|
|
|
|
srcObj, ok := src.(*Object)
|
2015-02-14 19:48:08 +01:00
|
|
|
if !ok {
|
2017-02-09 12:01:20 +01:00
|
|
|
fs.Debugf(src, "Can't copy - not same remote type")
|
2015-02-14 19:48:08 +01:00
|
|
|
return nil, fs.ErrorCantCopy
|
|
|
|
}
|
|
|
|
|
2015-11-07 12:14:46 +01:00
|
|
|
// Temporary Object under construction
|
|
|
|
dstObj := &Object{
|
|
|
|
fs: f,
|
|
|
|
remote: remote,
|
|
|
|
}
|
2015-02-14 19:48:08 +01:00
|
|
|
|
2017-05-21 22:35:33 +02:00
|
|
|
// Copy
|
|
|
|
arg := files.RelocationArg{}
|
|
|
|
arg.FromPath = srcObj.remotePath()
|
|
|
|
arg.ToPath = dstObj.remotePath()
|
2017-05-28 18:55:18 +02:00
|
|
|
var err error
|
|
|
|
var entry files.IsMetadata
|
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
|
|
|
entry, err = f.srv.Copy(&arg)
|
|
|
|
return shouldRetry(err)
|
|
|
|
})
|
2015-02-14 19:48:08 +01:00
|
|
|
if err != nil {
|
2016-06-12 16:06:02 +02:00
|
|
|
return nil, errors.Wrap(err, "copy failed")
|
2015-02-14 19:48:08 +01:00
|
|
|
}
|
2017-05-21 22:35:33 +02:00
|
|
|
|
|
|
|
// Set the metadata
|
|
|
|
fileInfo, ok := entry.(*files.FileMetadata)
|
|
|
|
if !ok {
|
|
|
|
return nil, fs.ErrorNotAFile
|
|
|
|
}
|
|
|
|
err = dstObj.setMetadataFromEntry(fileInfo)
|
2017-02-25 12:09:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "copy failed")
|
|
|
|
}
|
2017-05-21 22:35:33 +02:00
|
|
|
|
2015-02-14 19:48:08 +01:00
|
|
|
return dstObj, nil
|
|
|
|
}
|
|
|
|
|
2014-07-08 22:59:30 +02:00
|
|
|
// Purge deletes all the files and the container
|
|
|
|
//
|
2014-07-13 11:53:53 +02: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()
|
2017-05-28 18:55:18 +02:00
|
|
|
func (f *Fs) Purge() (err error) {
|
2014-07-13 11:53:53 +02:00
|
|
|
// Let dropbox delete the filesystem tree
|
2017-05-28 18:55:18 +02:00
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
|
|
|
_, err = f.srv.Delete(&files.DeleteArg{Path: f.slashRoot})
|
|
|
|
return shouldRetry(err)
|
|
|
|
})
|
2014-07-08 22:59:30 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-31 22:05:51 +02:00
|
|
|
// 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
|
2015-11-07 12:14:46 +01:00
|
|
|
func (f *Fs) Move(src fs.Object, remote string) (fs.Object, error) {
|
|
|
|
srcObj, ok := src.(*Object)
|
2015-08-31 22:05:51 +02:00
|
|
|
if !ok {
|
2017-02-09 12:01:20 +01:00
|
|
|
fs.Debugf(src, "Can't move - not same remote type")
|
2015-08-31 22:05:51 +02:00
|
|
|
return nil, fs.ErrorCantMove
|
|
|
|
}
|
|
|
|
|
2015-11-07 12:14:46 +01:00
|
|
|
// Temporary Object under construction
|
|
|
|
dstObj := &Object{
|
|
|
|
fs: f,
|
|
|
|
remote: remote,
|
|
|
|
}
|
2015-08-31 22:05:51 +02:00
|
|
|
|
2017-05-21 22:35:33 +02:00
|
|
|
// Do the move
|
|
|
|
arg := files.RelocationArg{}
|
|
|
|
arg.FromPath = srcObj.remotePath()
|
|
|
|
arg.ToPath = dstObj.remotePath()
|
2017-05-28 18:55:18 +02:00
|
|
|
var err error
|
|
|
|
var entry files.IsMetadata
|
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
|
|
|
entry, err = f.srv.Move(&arg)
|
|
|
|
return shouldRetry(err)
|
|
|
|
})
|
2015-08-31 22:05:51 +02:00
|
|
|
if err != nil {
|
2016-06-12 16:06:02 +02:00
|
|
|
return nil, errors.Wrap(err, "move failed")
|
2015-08-31 22:05:51 +02:00
|
|
|
}
|
2017-05-21 22:35:33 +02:00
|
|
|
|
|
|
|
// Set the metadata
|
|
|
|
fileInfo, ok := entry.(*files.FileMetadata)
|
|
|
|
if !ok {
|
|
|
|
return nil, fs.ErrorNotAFile
|
|
|
|
}
|
|
|
|
err = dstObj.setMetadataFromEntry(fileInfo)
|
2017-02-25 12:09:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "move failed")
|
|
|
|
}
|
2015-08-31 22:05:51 +02:00
|
|
|
return dstObj, nil
|
|
|
|
}
|
|
|
|
|
2017-02-05 22:20:56 +01:00
|
|
|
// DirMove moves src, srcRemote to this remote at dstRemote
|
|
|
|
// using server side move operations.
|
2015-08-31 22:05:51 +02: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
|
2017-02-05 22:20:56 +01:00
|
|
|
func (f *Fs) DirMove(src fs.Fs, srcRemote, dstRemote string) error {
|
2015-11-07 12:14:46 +01:00
|
|
|
srcFs, ok := src.(*Fs)
|
2015-08-31 22:05:51 +02:00
|
|
|
if !ok {
|
2017-02-09 12:01:20 +01:00
|
|
|
fs.Debugf(srcFs, "Can't move directory - not same remote type")
|
2015-08-31 22:05:51 +02:00
|
|
|
return fs.ErrorCantDirMove
|
|
|
|
}
|
2017-02-05 22:20:56 +01:00
|
|
|
srcPath := path.Join(srcFs.slashRoot, srcRemote)
|
|
|
|
dstPath := path.Join(f.slashRoot, dstRemote)
|
2015-08-31 22:05:51 +02:00
|
|
|
|
|
|
|
// Check if destination exists
|
2017-05-21 22:35:33 +02:00
|
|
|
_, err := f.getDirMetadata(f.slashRoot)
|
|
|
|
if err == nil {
|
2015-08-31 22:05:51 +02:00
|
|
|
return fs.ErrorDirExists
|
2017-05-21 22:35:33 +02:00
|
|
|
} else if err != fs.ErrorDirNotFound {
|
|
|
|
return err
|
2015-08-31 22:05:51 +02:00
|
|
|
}
|
|
|
|
|
2017-02-05 22:20:56 +01:00
|
|
|
// Make sure the parent directory exists
|
|
|
|
// ...apparently not necessary
|
|
|
|
|
2015-08-31 22:05:51 +02:00
|
|
|
// Do the move
|
2017-05-21 22:35:33 +02:00
|
|
|
arg := files.RelocationArg{}
|
|
|
|
arg.FromPath = srcPath
|
|
|
|
arg.ToPath = dstPath
|
2017-05-28 18:55:18 +02:00
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
|
|
|
_, err = f.srv.Move(&arg)
|
|
|
|
return shouldRetry(err)
|
|
|
|
})
|
2015-08-31 22:05:51 +02:00
|
|
|
if err != nil {
|
2016-06-12 16:06:02 +02:00
|
|
|
return errors.Wrap(err, "MoveDir failed")
|
2015-08-31 22:05:51 +02:00
|
|
|
}
|
2017-05-21 22:35:33 +02:00
|
|
|
|
2015-08-31 22:05:51 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-11 13:39:33 +01:00
|
|
|
// Hashes returns the supported hash sets.
|
|
|
|
func (f *Fs) Hashes() fs.HashSet {
|
2017-05-26 16:09:31 +02:00
|
|
|
return fs.HashSet(fs.HashDropbox)
|
2016-01-11 13:39:33 +01:00
|
|
|
}
|
|
|
|
|
2014-07-08 22:59:30 +02:00
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// Fs returns the parent Fs
|
2016-02-18 12:35:25 +01:00
|
|
|
func (o *Object) Fs() fs.Info {
|
2015-11-07 12:14:46 +01:00
|
|
|
return o.fs
|
2014-07-08 22:59:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return a string version
|
2015-11-07 12:14:46 +01:00
|
|
|
func (o *Object) String() string {
|
2014-07-08 22:59:30 +02:00
|
|
|
if o == nil {
|
|
|
|
return "<nil>"
|
|
|
|
}
|
|
|
|
return o.remote
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// Remote returns the remote path
|
2015-11-07 12:14:46 +01:00
|
|
|
func (o *Object) Remote() string {
|
2014-07-08 22:59:30 +02:00
|
|
|
return o.remote
|
|
|
|
}
|
|
|
|
|
2017-05-26 16:09:31 +02:00
|
|
|
// Hash returns the dropbox special hash
|
2016-01-11 13:39:33 +01:00
|
|
|
func (o *Object) Hash(t fs.HashType) (string, error) {
|
2017-05-26 16:09:31 +02:00
|
|
|
if t != fs.HashDropbox {
|
|
|
|
return "", fs.ErrHashUnsupported
|
|
|
|
}
|
|
|
|
err := o.readMetaData()
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "failed to read hash from metadata")
|
|
|
|
}
|
|
|
|
return o.hash, nil
|
2014-07-08 22:59:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns the size of an object in bytes
|
2015-11-07 12:14:46 +01:00
|
|
|
func (o *Object) Size() int64 {
|
2014-07-08 22:59:30 +02:00
|
|
|
return o.bytes
|
|
|
|
}
|
|
|
|
|
2017-05-21 22:35:33 +02:00
|
|
|
// setMetadataFromEntry sets the fs data from a files.FileMetadata
|
2014-07-10 01:17:40 +02:00
|
|
|
//
|
|
|
|
// This isn't a complete set of metadata and has an inacurate date
|
2017-05-21 22:35:33 +02:00
|
|
|
func (o *Object) setMetadataFromEntry(info *files.FileMetadata) error {
|
|
|
|
o.bytes = int64(info.Size)
|
|
|
|
o.modTime = info.ClientModified
|
2017-05-26 16:09:31 +02:00
|
|
|
o.hash = info.ContentHash
|
2017-02-25 12:09:57 +01:00
|
|
|
return nil
|
2014-07-08 22:59:30 +02:00
|
|
|
}
|
|
|
|
|
2017-05-21 22:35:33 +02:00
|
|
|
// Reads the entry for a file from dropbox
|
|
|
|
func (o *Object) readEntry() (*files.FileMetadata, error) {
|
|
|
|
return o.fs.getFileMetadata(o.remotePath())
|
2014-07-10 01:17:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read entry if not set and set metadata from it
|
2015-11-07 12:14:46 +01:00
|
|
|
func (o *Object) readEntryAndSetMetadata() error {
|
2014-07-10 01:17:40 +02:00
|
|
|
// Last resort set time from client
|
|
|
|
if !o.modTime.IsZero() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
entry, err := o.readEntry()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-25 12:09:57 +01:00
|
|
|
return o.setMetadataFromEntry(entry)
|
2014-07-10 01:17:40 +02:00
|
|
|
}
|
|
|
|
|
2014-07-08 22:59:30 +02:00
|
|
|
// Returns the remote path for the object
|
2015-11-07 12:14:46 +01:00
|
|
|
func (o *Object) remotePath() string {
|
|
|
|
return o.fs.slashRootSlash + o.remote
|
2014-07-08 22:59:30 +02:00
|
|
|
}
|
|
|
|
|
2014-07-13 11:53:53 +02:00
|
|
|
// Returns the key for the metadata database for a given path
|
|
|
|
func metadataKey(path string) string {
|
|
|
|
// NB File system is case insensitive
|
|
|
|
path = strings.ToLower(path)
|
2014-07-19 16:48:40 +02:00
|
|
|
hash := md5.New()
|
2014-07-25 19:19:49 +02:00
|
|
|
_, _ = hash.Write([]byte(path))
|
2014-07-19 16:48:40 +02:00
|
|
|
return fmt.Sprintf("%x", hash.Sum(nil))
|
2014-07-13 11:53:53 +02:00
|
|
|
}
|
|
|
|
|
2014-07-10 01:17:40 +02:00
|
|
|
// Returns the key for the metadata database
|
2015-11-07 12:14:46 +01:00
|
|
|
func (o *Object) metadataKey() string {
|
2014-07-13 11:53:53 +02:00
|
|
|
return metadataKey(o.remotePath())
|
2014-07-10 01:17:40 +02:00
|
|
|
}
|
|
|
|
|
2014-07-08 22:59:30 +02:00
|
|
|
// readMetaData gets the info if it hasn't already been fetched
|
2015-11-07 12:14:46 +01:00
|
|
|
func (o *Object) readMetaData() (err error) {
|
2017-05-26 16:09:31 +02:00
|
|
|
if !o.modTime.IsZero() {
|
2014-07-08 22:59:30 +02:00
|
|
|
return nil
|
|
|
|
}
|
2014-07-10 01:17:40 +02:00
|
|
|
// Last resort
|
2014-07-25 19:19:49 +02:00
|
|
|
return o.readEntryAndSetMetadata()
|
2014-07-08 22:59:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2015-11-07 12:14:46 +01:00
|
|
|
func (o *Object) ModTime() time.Time {
|
2014-07-08 22:59:30 +02:00
|
|
|
err := o.readMetaData()
|
|
|
|
if err != nil {
|
2017-02-09 18:08:51 +01:00
|
|
|
fs.Debugf(o, "Failed to read metadata: %v", err)
|
2014-07-08 22:59:30 +02:00
|
|
|
return time.Now()
|
|
|
|
}
|
|
|
|
return o.modTime
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// SetModTime sets the modification time of the local fs object
|
2014-07-10 01:17:40 +02:00
|
|
|
//
|
|
|
|
// Commits the datastore
|
2016-03-22 16:07:10 +01:00
|
|
|
func (o *Object) SetModTime(modTime time.Time) error {
|
2017-05-21 22:35:33 +02:00
|
|
|
// Dropbox doesn't have a way of doing this so returning this
|
2017-06-13 14:58:39 +02:00
|
|
|
// error will cause the file to be deleted first then
|
|
|
|
// re-uploaded to set the time.
|
|
|
|
return fs.ErrorCantSetModTimeWithoutDelete
|
2014-07-08 22:59:30 +02:00
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// Storable returns whether this object is storable
|
2015-11-07 12:14:46 +01:00
|
|
|
func (o *Object) Storable() bool {
|
2014-07-08 22:59:30 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open an object for read
|
2016-09-10 12:29:57 +02:00
|
|
|
func (o *Object) Open(options ...fs.OpenOption) (in io.ReadCloser, err error) {
|
2017-05-21 22:35:33 +02:00
|
|
|
headers := fs.OpenOptionHeaders(options)
|
|
|
|
arg := files.DownloadArg{Path: o.remotePath(), ExtraHeaders: headers}
|
2017-05-28 18:55:18 +02:00
|
|
|
err = o.fs.pacer.Call(func() (bool, error) {
|
|
|
|
_, in, err = o.fs.srv.Download(&arg)
|
|
|
|
return shouldRetry(err)
|
|
|
|
})
|
2017-05-21 22:35:33 +02:00
|
|
|
|
|
|
|
switch e := err.(type) {
|
|
|
|
case files.DownloadAPIError:
|
|
|
|
// Don't attempt to retry copyright violation errors
|
|
|
|
if e.EndpointError.Path.Tag == files.LookupErrorRestrictedContent {
|
|
|
|
return nil, fs.NoRetryError(err)
|
2016-09-10 12:29:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-21 22:35:33 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// uploadChunked uploads the object in parts
|
|
|
|
//
|
|
|
|
// Call only if size is >= uploadChunkSize
|
|
|
|
//
|
2017-05-28 18:55:18 +02:00
|
|
|
// FIXME buffer chunks to improve upload retries
|
2017-05-21 22:35:33 +02:00
|
|
|
func (o *Object) uploadChunked(in io.Reader, commitInfo *files.CommitInfo, size int64) (entry *files.FileMetadata, err error) {
|
|
|
|
chunkSize := int64(uploadChunkSize)
|
|
|
|
chunks := int(size/chunkSize) + 1
|
|
|
|
|
|
|
|
// write the first whole chunk
|
|
|
|
fs.Debugf(o, "Uploading chunk 1/%d", chunks)
|
2017-05-28 18:55:18 +02:00
|
|
|
var res *files.UploadSessionStartResult
|
|
|
|
err = o.fs.pacer.CallNoRetry(func() (bool, error) {
|
|
|
|
res, err = o.fs.srv.UploadSessionStart(&files.UploadSessionStartArg{}, &io.LimitedReader{R: in, N: chunkSize})
|
|
|
|
return shouldRetry(err)
|
|
|
|
})
|
2017-05-21 22:35:33 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor := files.UploadSessionCursor{
|
|
|
|
SessionId: res.SessionId,
|
|
|
|
Offset: uint64(chunkSize),
|
|
|
|
}
|
|
|
|
appendArg := files.UploadSessionAppendArg{
|
|
|
|
Cursor: &cursor,
|
|
|
|
Close: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
// write more whole chunks (if any)
|
|
|
|
for i := 2; i < chunks; i++ {
|
|
|
|
fs.Debugf(o, "Uploading chunk %d/%d", i, chunks)
|
2017-05-28 18:55:18 +02:00
|
|
|
err = o.fs.pacer.CallNoRetry(func() (bool, error) {
|
|
|
|
err = o.fs.srv.UploadSessionAppendV2(&appendArg, &io.LimitedReader{R: in, N: chunkSize})
|
|
|
|
return shouldRetry(err)
|
|
|
|
})
|
2017-05-21 22:35:33 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-07-04 14:45:10 +02:00
|
|
|
}
|
2017-05-21 22:35:33 +02:00
|
|
|
cursor.Offset += uint64(chunkSize)
|
2016-07-04 14:45:10 +02:00
|
|
|
}
|
2017-05-21 22:35:33 +02:00
|
|
|
|
|
|
|
// write the remains
|
|
|
|
args := &files.UploadSessionFinishArg{
|
|
|
|
Cursor: &cursor,
|
|
|
|
Commit: commitInfo,
|
|
|
|
}
|
|
|
|
fs.Debugf(o, "Uploading chunk %d/%d", chunks, chunks)
|
2017-05-28 18:55:18 +02:00
|
|
|
err = o.fs.pacer.CallNoRetry(func() (bool, error) {
|
|
|
|
entry, err = o.fs.srv.UploadSessionFinish(args, in)
|
|
|
|
return shouldRetry(err)
|
|
|
|
})
|
2017-05-21 22:35:33 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return entry, nil
|
2014-07-08 22:59:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the already existing object
|
|
|
|
//
|
|
|
|
// Copy the reader into the object updating modTime and size
|
|
|
|
//
|
|
|
|
// The new object may have been created if an error is returned
|
2017-05-28 13:44:22 +02:00
|
|
|
func (o *Object) Update(in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) error {
|
2015-08-20 19:36:06 +02:00
|
|
|
remote := o.remotePath()
|
|
|
|
if ignoredFiles.MatchString(remote) {
|
2017-02-09 12:01:20 +01:00
|
|
|
fs.Logf(o, "File name disallowed - not uploading")
|
2015-08-20 19:36:06 +02:00
|
|
|
return nil
|
|
|
|
}
|
2017-05-21 22:35:33 +02:00
|
|
|
commitInfo := files.NewCommitInfo(o.remotePath())
|
|
|
|
commitInfo.Mode.Tag = "overwrite"
|
|
|
|
// The Dropbox API only accepts timestamps in UTC with second precision.
|
|
|
|
commitInfo.ClientModified = src.ModTime().UTC().Round(time.Second)
|
|
|
|
|
|
|
|
size := src.Size()
|
|
|
|
var err error
|
|
|
|
var entry *files.FileMetadata
|
|
|
|
if size > int64(uploadChunkSize) {
|
|
|
|
entry, err = o.uploadChunked(in, commitInfo, size)
|
|
|
|
} else {
|
2017-05-28 18:55:18 +02:00
|
|
|
err = o.fs.pacer.CallNoRetry(func() (bool, error) {
|
|
|
|
entry, err = o.fs.srv.Upload(commitInfo, in)
|
|
|
|
return shouldRetry(err)
|
|
|
|
})
|
2017-05-21 22:35:33 +02:00
|
|
|
}
|
2014-07-08 22:59:30 +02:00
|
|
|
if err != nil {
|
2016-06-12 16:06:02 +02:00
|
|
|
return errors.Wrap(err, "upload failed")
|
2014-07-08 22:59:30 +02:00
|
|
|
}
|
2017-02-25 12:09:57 +01:00
|
|
|
return o.setMetadataFromEntry(entry)
|
2014-07-08 22:59:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove an object
|
2017-05-28 18:55:18 +02:00
|
|
|
func (o *Object) Remove() (err error) {
|
|
|
|
err = o.fs.pacer.CallNoRetry(func() (bool, error) {
|
|
|
|
_, err = o.fs.srv.Delete(&files.DeleteArg{Path: o.remotePath()})
|
|
|
|
return shouldRetry(err)
|
|
|
|
})
|
2014-07-08 22:59:30 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the interfaces are satisfied
|
2015-08-31 22:05:51 +02:00
|
|
|
var (
|
2017-05-21 22:35:33 +02:00
|
|
|
_ fs.Fs = (*Fs)(nil)
|
|
|
|
_ fs.Copier = (*Fs)(nil)
|
|
|
|
_ fs.Purger = (*Fs)(nil)
|
|
|
|
_ fs.Mover = (*Fs)(nil)
|
|
|
|
_ fs.DirMover = (*Fs)(nil)
|
|
|
|
_ fs.Object = (*Object)(nil)
|
2015-08-31 22:05:51 +02:00
|
|
|
)
|