2017-03-08 11:21:57 +00:00
|
|
|
// Package onedrive provides an interface to the Microsoft OneDrive
|
2015-10-04 21:08:31 +00:00
|
|
|
// object storage system.
|
|
|
|
package onedrive
|
|
|
|
|
|
|
|
import (
|
2018-04-20 11:55:49 +00:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/hex"
|
2017-03-12 12:00:10 +00:00
|
|
|
"encoding/json"
|
2015-10-04 21:08:31 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2016-08-22 09:58:49 +00:00
|
|
|
"net/url"
|
2016-11-25 21:52:43 +00:00
|
|
|
"path"
|
2015-10-04 21:08:31 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2018-01-11 16:05:41 +00:00
|
|
|
"github.com/ncw/rclone/backend/onedrive/api"
|
2015-10-04 21:08:31 +00:00
|
|
|
"github.com/ncw/rclone/fs"
|
2018-01-12 16:30:54 +00:00
|
|
|
"github.com/ncw/rclone/fs/config"
|
|
|
|
"github.com/ncw/rclone/fs/config/flags"
|
2018-01-18 20:19:55 +00:00
|
|
|
"github.com/ncw/rclone/fs/config/obscure"
|
2018-01-12 16:30:54 +00:00
|
|
|
"github.com/ncw/rclone/fs/fserrors"
|
|
|
|
"github.com/ncw/rclone/fs/hash"
|
2018-01-11 16:29:20 +00:00
|
|
|
"github.com/ncw/rclone/lib/dircache"
|
|
|
|
"github.com/ncw/rclone/lib/oauthutil"
|
|
|
|
"github.com/ncw/rclone/lib/pacer"
|
2018-01-12 16:30:54 +00:00
|
|
|
"github.com/ncw/rclone/lib/readers"
|
2018-01-11 16:29:20 +00:00
|
|
|
"github.com/ncw/rclone/lib/rest"
|
2016-05-30 18:49:21 +00:00
|
|
|
"github.com/pkg/errors"
|
2015-10-04 21:08:31 +00:00
|
|
|
"golang.org/x/oauth2"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-08-03 19:57:42 +00:00
|
|
|
rclonePersonalClientID = "0000000044165769"
|
|
|
|
rclonePersonalEncryptedClientSecret = "ugVWLNhKkVT1-cbTRO-6z1MlzwdW6aMwpKgNaFG-qXjEn_WfDnG9TVyRA5yuoliU"
|
|
|
|
rcloneBusinessClientID = "52857fec-4bc2-483f-9f1b-5fe28e97532c"
|
|
|
|
rcloneBusinessEncryptedClientSecret = "6t4pC8l6L66SFYVIi8PgECDyjXy_ABo1nsTaE-Lr9LpzC6yT4vNOwHsakwwdEui0O6B0kX8_xbBLj91J"
|
|
|
|
minSleep = 10 * time.Millisecond
|
|
|
|
maxSleep = 2 * time.Second
|
|
|
|
decayConstant = 2 // bigger for slower decay, exponential
|
|
|
|
rootURLPersonal = "https://api.onedrive.com/v1.0/drive" // root URL for requests
|
|
|
|
discoveryServiceURL = "https://api.office.com/discovery/"
|
|
|
|
configResourceURL = "resource_url"
|
2015-10-04 21:08:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Globals
|
|
|
|
var (
|
2017-08-03 19:57:42 +00:00
|
|
|
// Description of how to auth for this app for a personal account
|
|
|
|
oauthPersonalConfig = &oauth2.Config{
|
2015-10-04 21:08:31 +00:00
|
|
|
Scopes: []string{
|
|
|
|
"wl.signin", // Allow single sign-on capabilities
|
|
|
|
"wl.offline_access", // Allow receiving a refresh token
|
|
|
|
"onedrive.readwrite", // r/w perms to all of a user's OneDrive files
|
|
|
|
},
|
|
|
|
Endpoint: oauth2.Endpoint{
|
|
|
|
AuthURL: "https://login.live.com/oauth20_authorize.srf",
|
|
|
|
TokenURL: "https://login.live.com/oauth20_token.srf",
|
|
|
|
},
|
2017-08-03 19:57:42 +00:00
|
|
|
ClientID: rclonePersonalClientID,
|
2018-01-18 20:19:55 +00:00
|
|
|
ClientSecret: obscure.MustReveal(rclonePersonalEncryptedClientSecret),
|
2017-03-02 20:21:35 +00:00
|
|
|
RedirectURL: oauthutil.RedirectLocalhostURL,
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
2017-08-03 19:57:42 +00:00
|
|
|
|
|
|
|
// Description of how to auth for this app for a business account
|
|
|
|
oauthBusinessConfig = &oauth2.Config{
|
|
|
|
Endpoint: oauth2.Endpoint{
|
|
|
|
AuthURL: "https://login.microsoftonline.com/common/oauth2/authorize",
|
|
|
|
TokenURL: "https://login.microsoftonline.com/common/oauth2/token",
|
|
|
|
},
|
|
|
|
ClientID: rcloneBusinessClientID,
|
2018-01-18 20:19:55 +00:00
|
|
|
ClientSecret: obscure.MustReveal(rcloneBusinessEncryptedClientSecret),
|
2017-08-03 19:57:42 +00:00
|
|
|
RedirectURL: oauthutil.RedirectLocalhostURL,
|
|
|
|
}
|
|
|
|
oauthBusinessResource = oauth2.SetAuthURLParam("resource", discoveryServiceURL)
|
|
|
|
|
2018-03-15 07:06:17 +00:00
|
|
|
chunkSize = fs.SizeSuffix(10 * 1024 * 1024)
|
2015-10-04 21:08:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Register with Fs
|
|
|
|
func init() {
|
2016-02-18 11:35:25 +00:00
|
|
|
fs.Register(&fs.RegInfo{
|
2016-02-15 18:11:53 +00:00
|
|
|
Name: "onedrive",
|
|
|
|
Description: "Microsoft OneDrive",
|
|
|
|
NewFs: NewFs,
|
2015-10-04 21:08:31 +00:00
|
|
|
Config: func(name string) {
|
2017-08-03 19:57:42 +00:00
|
|
|
// choose account type
|
|
|
|
fmt.Printf("Choose OneDrive account type?\n")
|
|
|
|
fmt.Printf(" * Say b for a OneDrive business account\n")
|
|
|
|
fmt.Printf(" * Say p for a personal OneDrive account\n")
|
2018-01-12 16:30:54 +00:00
|
|
|
isPersonal := config.Command([]string{"bBusiness", "pPersonal"}) == 'p'
|
2017-08-03 19:57:42 +00:00
|
|
|
|
|
|
|
if isPersonal {
|
|
|
|
// for personal accounts we don't safe a field about the account
|
|
|
|
err := oauthutil.Config("onedrive", name, oauthPersonalConfig)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to configure token: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
2017-09-06 15:19:52 +00:00
|
|
|
err := oauthutil.Config("onedrive", name, oauthBusinessConfig, oauthBusinessResource)
|
2017-08-03 19:57:42 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to configure token: %v", err)
|
2017-09-06 15:19:52 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Are we running headless?
|
2018-01-12 16:30:54 +00:00
|
|
|
if config.FileGet(name, config.ConfigAutomatic) != "" {
|
2017-09-06 15:19:52 +00:00
|
|
|
// Yes, okay we are done
|
|
|
|
return
|
2017-08-03 19:57:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type serviceResource struct {
|
|
|
|
ServiceAPIVersion string `json:"serviceApiVersion"`
|
|
|
|
ServiceEndpointURI string `json:"serviceEndpointUri"`
|
|
|
|
ServiceResourceID string `json:"serviceResourceId"`
|
|
|
|
}
|
|
|
|
type serviceResponse struct {
|
|
|
|
Services []serviceResource `json:"value"`
|
|
|
|
}
|
|
|
|
|
|
|
|
oAuthClient, _, err := oauthutil.NewClient(name, oauthBusinessConfig)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to configure OneDrive: %v", err)
|
2017-09-06 15:19:52 +00:00
|
|
|
return
|
2017-08-03 19:57:42 +00:00
|
|
|
}
|
2017-09-06 15:19:52 +00:00
|
|
|
srv := rest.NewClient(oAuthClient)
|
2017-08-03 19:57:42 +00:00
|
|
|
|
|
|
|
opts := rest.Opts{
|
2017-09-06 15:19:52 +00:00
|
|
|
Method: "GET",
|
|
|
|
RootURL: discoveryServiceURL,
|
|
|
|
Path: "/v2.0/me/services",
|
2017-08-03 19:57:42 +00:00
|
|
|
}
|
|
|
|
services := serviceResponse{}
|
|
|
|
resp, err := srv.CallJSON(&opts, nil, &services)
|
|
|
|
if err != nil {
|
2017-09-06 15:19:52 +00:00
|
|
|
fs.Errorf(nil, "Failed to query available services: %v", err)
|
|
|
|
return
|
2017-08-03 19:57:42 +00:00
|
|
|
}
|
|
|
|
if resp.StatusCode != 200 {
|
2017-09-06 15:19:52 +00:00
|
|
|
fs.Errorf(nil, "Failed to query available services: Got HTTP error code %d", resp.StatusCode)
|
|
|
|
return
|
2017-08-03 19:57:42 +00:00
|
|
|
}
|
|
|
|
|
2017-10-28 16:48:39 +00:00
|
|
|
var resourcesURL []string
|
|
|
|
var resourcesID []string
|
|
|
|
|
2017-08-03 19:57:42 +00:00
|
|
|
for _, service := range services.Services {
|
|
|
|
if service.ServiceAPIVersion == "v2.0" {
|
2017-10-28 16:48:39 +00:00
|
|
|
resourcesID = append(resourcesID, service.ServiceResourceID)
|
|
|
|
resourcesURL = append(resourcesURL, service.ServiceEndpointURI)
|
2017-08-03 19:57:42 +00:00
|
|
|
}
|
|
|
|
// we only support 2.0 API
|
2017-09-06 15:19:52 +00:00
|
|
|
fs.Infof(nil, "Skipping API %s endpoint %s", service.ServiceAPIVersion, service.ServiceEndpointURI)
|
2017-08-03 19:57:42 +00:00
|
|
|
}
|
|
|
|
|
2017-10-28 16:48:39 +00:00
|
|
|
var foundService string
|
|
|
|
if len(resourcesID) == 0 {
|
2017-09-06 15:19:52 +00:00
|
|
|
fs.Errorf(nil, "No Service found")
|
|
|
|
return
|
2017-10-28 16:48:39 +00:00
|
|
|
} else if len(resourcesID) == 1 {
|
|
|
|
foundService = resourcesID[0]
|
|
|
|
} else {
|
2018-01-12 16:30:54 +00:00
|
|
|
foundService = config.Choose("Choose resource URL", resourcesID, resourcesURL, false)
|
2017-08-03 19:57:42 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 16:30:54 +00:00
|
|
|
config.FileSet(name, configResourceURL, foundService)
|
2017-10-28 16:48:39 +00:00
|
|
|
oauthBusinessResource = oauth2.SetAuthURLParam("resource", foundService)
|
|
|
|
|
2017-09-06 15:19:52 +00:00
|
|
|
// get the token from the inital config
|
|
|
|
// we need to update the token with a resource
|
|
|
|
// specific token we will query now
|
|
|
|
token, err := oauthutil.GetToken(name)
|
2017-08-03 19:57:42 +00:00
|
|
|
if err != nil {
|
2017-09-06 15:19:52 +00:00
|
|
|
fs.Errorf(nil, "Error while getting token: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// values for the token query
|
|
|
|
values := url.Values{}
|
|
|
|
values.Set("refresh_token", token.RefreshToken)
|
|
|
|
values.Set("grant_type", "refresh_token")
|
|
|
|
values.Set("resource", foundService)
|
|
|
|
values.Set("client_id", oauthBusinessConfig.ClientID)
|
|
|
|
values.Set("client_secret", oauthBusinessConfig.ClientSecret)
|
|
|
|
opts = rest.Opts{
|
|
|
|
Method: "POST",
|
|
|
|
RootURL: oauthBusinessConfig.Endpoint.TokenURL,
|
|
|
|
ContentType: "application/x-www-form-urlencoded",
|
|
|
|
Body: strings.NewReader(values.Encode()),
|
|
|
|
}
|
|
|
|
|
|
|
|
// tokenJSON is the struct representing the HTTP response from OAuth2
|
|
|
|
// providers returning a token in JSON form.
|
|
|
|
// we are only interested in the new tokens, all other fields we don't care
|
|
|
|
type tokenJSON struct {
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
RefreshToken string `json:"refresh_token"`
|
|
|
|
}
|
|
|
|
jsonToken := tokenJSON{}
|
|
|
|
resp, err = srv.CallJSON(&opts, nil, &jsonToken)
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(nil, "Failed to get resource token: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
fs.Errorf(nil, "Failed to get resource token: Got HTTP error code %d", resp.StatusCode)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// update the tokens
|
|
|
|
token.AccessToken = jsonToken.AccessToken
|
|
|
|
token.RefreshToken = jsonToken.RefreshToken
|
|
|
|
|
|
|
|
// finally save them in the config
|
|
|
|
err = oauthutil.PutToken(name, token, true)
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(nil, "Error while setting token: %s", err)
|
2017-08-03 19:57:42 +00:00
|
|
|
}
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
Options: []fs.Option{{
|
2018-01-12 16:30:54 +00:00
|
|
|
Name: config.ConfigClientID,
|
2015-10-04 21:08:31 +00:00
|
|
|
Help: "Microsoft App Client Id - leave blank normally.",
|
|
|
|
}, {
|
2018-01-12 16:30:54 +00:00
|
|
|
Name: config.ConfigClientSecret,
|
2015-10-04 21:08:31 +00:00
|
|
|
Help: "Microsoft App Client Secret - leave blank normally.",
|
|
|
|
}},
|
|
|
|
})
|
2017-08-03 19:57:42 +00:00
|
|
|
|
2018-01-12 16:30:54 +00:00
|
|
|
flags.VarP(&chunkSize, "onedrive-chunk-size", "", "Above this size files will be chunked - must be multiple of 320k.")
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fs represents a remote one drive
|
|
|
|
type Fs struct {
|
2017-01-29 20:42:43 +00:00
|
|
|
name string // name of this remote
|
|
|
|
root string // the path we are working on
|
|
|
|
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
|
|
|
|
pacer *pacer.Pacer // pacer for API calls
|
|
|
|
tokenRenewer *oauthutil.Renew // renew the token on expiry
|
2017-08-03 19:57:42 +00:00
|
|
|
isBusiness bool // true if this is an OneDrive Business account
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Object describes a one drive object
|
|
|
|
//
|
|
|
|
// Will definitely have info but maybe not meta
|
|
|
|
type Object struct {
|
2018-04-20 11:55:49 +00:00
|
|
|
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
|
|
|
|
sha1 string // SHA-1 of the object content
|
|
|
|
quickxorhash string // QuickXorHash of the object content
|
|
|
|
mimeType string // Content-Type of object from server (may not be as uploaded)
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
|
|
|
// 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("One drive root '%s'", f.root)
|
|
|
|
}
|
|
|
|
|
2017-01-13 17:21:47 +00:00
|
|
|
// Features returns the optional features of this Fs
|
|
|
|
func (f *Fs) Features() *fs.Features {
|
|
|
|
return f.features
|
|
|
|
}
|
|
|
|
|
2015-10-04 21:08:31 +00:00
|
|
|
// Pattern to match a one drive path
|
|
|
|
var matcher = regexp.MustCompile(`^([^/]*)(.*)$`)
|
|
|
|
|
|
|
|
// parsePath parses an one drive '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) {
|
2017-03-23 13:10:43 +00:00
|
|
|
authRety := false
|
|
|
|
|
|
|
|
if resp != nil && resp.StatusCode == 401 && len(resp.Header["Www-Authenticate"]) == 1 && strings.Index(resp.Header["Www-Authenticate"][0], "expired_token") >= 0 {
|
|
|
|
authRety = true
|
|
|
|
fs.Debugf(nil, "Should retry: %v", err)
|
|
|
|
}
|
2018-01-12 16:30:54 +00:00
|
|
|
return authRety || fserrors.ShouldRetry(err) || fserrors.ShouldRetryHTTP(resp, retryErrorCodes), err
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// readMetaDataForPath reads the metadata from the path
|
|
|
|
func (f *Fs) readMetaDataForPath(path string) (info *api.Item, resp *http.Response, err error) {
|
2015-11-27 12:46:13 +00:00
|
|
|
opts := rest.Opts{
|
2015-10-04 21:08:31 +00:00
|
|
|
Method: "GET",
|
2018-01-05 15:55:43 +00:00
|
|
|
Path: "/root:/" + rest.URLPathEscape(replaceReservedChars(path)),
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
|
|
|
resp, err = f.srv.CallJSON(&opts, nil, &info)
|
|
|
|
return shouldRetry(resp, err)
|
|
|
|
})
|
|
|
|
return info, resp, err
|
|
|
|
}
|
|
|
|
|
2015-11-27 12:46:13 +00:00
|
|
|
// 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 {
|
2017-02-09 11:01:20 +00:00
|
|
|
fs.Debugf(nil, "Couldn't decode error response: %v", err)
|
2015-11-27 12:46:13 +00:00
|
|
|
}
|
|
|
|
if errResponse.ErrorInfo.Code == "" {
|
|
|
|
errResponse.ErrorInfo.Code = resp.Status
|
|
|
|
}
|
|
|
|
return errResponse
|
|
|
|
}
|
|
|
|
|
2015-10-04 21:08:31 +00:00
|
|
|
// NewFs constructs an Fs from the path, container:path
|
|
|
|
func NewFs(name, root string) (fs.Fs, error) {
|
2017-08-03 19:57:42 +00:00
|
|
|
// get the resource URL from the config file0
|
2018-01-12 16:30:54 +00:00
|
|
|
resourceURL := config.FileGet(name, configResourceURL, "")
|
2017-08-03 19:57:42 +00:00
|
|
|
// if we have a resource URL it's a business account otherwise a personal one
|
|
|
|
var rootURL string
|
|
|
|
var oauthConfig *oauth2.Config
|
|
|
|
if resourceURL == "" {
|
|
|
|
// personal account setup
|
|
|
|
oauthConfig = oauthPersonalConfig
|
|
|
|
rootURL = rootURLPersonal
|
|
|
|
} else {
|
|
|
|
// business account setup
|
|
|
|
oauthConfig = oauthBusinessConfig
|
|
|
|
rootURL = resourceURL + "_api/v2.0/drives/me"
|
|
|
|
|
|
|
|
// update the URL in the AuthOptions
|
|
|
|
oauthBusinessResource = oauth2.SetAuthURLParam("resource", resourceURL)
|
|
|
|
}
|
2015-10-04 21:08:31 +00:00
|
|
|
root = parsePath(root)
|
2017-01-29 20:42:43 +00:00
|
|
|
oAuthClient, ts, err := oauthutil.NewClient(name, oauthConfig)
|
2015-10-04 21:08:31 +00:00
|
|
|
if err != nil {
|
2017-03-08 11:21:57 +00:00
|
|
|
log.Fatalf("Failed to configure OneDrive: %v", err)
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
f := &Fs{
|
2017-08-03 19:57:42 +00:00
|
|
|
name: name,
|
|
|
|
root: root,
|
|
|
|
srv: rest.NewClient(oAuthClient).SetRoot(rootURL),
|
|
|
|
pacer: pacer.New().SetMinSleep(minSleep).SetMaxSleep(maxSleep).SetDecayConstant(decayConstant),
|
|
|
|
isBusiness: resourceURL != "",
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
2017-08-09 14:27:43 +00:00
|
|
|
f.features = (&fs.Features{
|
2017-08-03 19:57:42 +00:00
|
|
|
CaseInsensitive: true,
|
|
|
|
// OneDrive for business doesn't support mime types properly
|
|
|
|
// so we disable it until resolved
|
|
|
|
// https://github.com/OneDrive/onedrive-api-docs/issues/643
|
|
|
|
ReadMimeType: !f.isBusiness,
|
2017-08-09 14:27:43 +00:00
|
|
|
CanHaveEmptyDirectories: true,
|
|
|
|
}).Fill(f)
|
2015-11-27 12:46:13 +00:00
|
|
|
f.srv.SetErrorHandler(errorHandler)
|
2015-10-04 21:08:31 +00:00
|
|
|
|
2017-01-29 20:42:43 +00:00
|
|
|
// Renew the token in the background
|
|
|
|
f.tokenRenewer = oauthutil.NewRenew(f.String(), ts, func() error {
|
|
|
|
_, _, err := f.readMetaDataForPath("")
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
2017-06-27 10:34:32 +00:00
|
|
|
// Get rootID
|
|
|
|
rootInfo, _, err := f.readMetaDataForPath("")
|
|
|
|
if err != nil || rootInfo.ID == "" {
|
|
|
|
return nil, errors.Wrap(err, "failed to get root")
|
|
|
|
}
|
|
|
|
|
2015-10-04 21:08:31 +00:00
|
|
|
f.dirCache = dircache.New(root, rootInfo.ID, f)
|
|
|
|
|
|
|
|
// Find the current root
|
|
|
|
err = f.dirCache.FindRoot(false)
|
|
|
|
if err != nil {
|
|
|
|
// Assume it is a file
|
|
|
|
newRoot, remote := dircache.SplitPath(root)
|
|
|
|
newF := *f
|
|
|
|
newF.dirCache = dircache.New(newRoot, rootInfo.ID, &newF)
|
|
|
|
newF.root = newRoot
|
|
|
|
// Make new Fs which is the parent
|
|
|
|
err = newF.dirCache.FindRoot(false)
|
|
|
|
if err != nil {
|
|
|
|
// No root so return old f
|
|
|
|
return f, nil
|
|
|
|
}
|
2016-06-25 20:23:20 +00:00
|
|
|
_, err := newF.newObjectWithInfo(remote, nil)
|
|
|
|
if err != nil {
|
|
|
|
if err == fs.ErrorObjectNotFound {
|
|
|
|
// File doesn't exist so return old f
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
2016-06-21 17:01:53 +00:00
|
|
|
// return an error with an fs which points to the parent
|
|
|
|
return &newF, fs.ErrorIsFile
|
2015-10-04 21:08:31 +00: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
|
|
|
|
//
|
2016-06-25 20:23:20 +00:00
|
|
|
// If it can't be found it returns the error fs.ErrorObjectNotFound.
|
|
|
|
func (f *Fs) newObjectWithInfo(remote string, info *api.Item) (fs.Object, error) {
|
2015-10-04 21:08:31 +00:00
|
|
|
o := &Object{
|
|
|
|
fs: f,
|
|
|
|
remote: remote,
|
|
|
|
}
|
2017-03-06 20:11:54 +00:00
|
|
|
var err error
|
2015-10-04 21:08:31 +00:00
|
|
|
if info != nil {
|
|
|
|
// Set info
|
2017-03-06 20:11:54 +00:00
|
|
|
err = o.setMetaData(info)
|
2015-10-04 21:08:31 +00:00
|
|
|
} else {
|
2017-03-06 20:11:54 +00:00
|
|
|
err = o.readMetaData() // reads info and meta, returning an error
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
2016-06-25 20:23:20 +00:00
|
|
|
return o, nil
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
|
|
|
|
2016-06-25 20:23:20 +00: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) {
|
2015-10-04 21:08:31 +00:00
|
|
|
return f.newObjectWithInfo(remote, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindLeaf finds a directory of name leaf in the folder with ID pathID
|
|
|
|
func (f *Fs) FindLeaf(pathID, leaf string) (pathIDOut string, found bool, err error) {
|
2017-02-09 11:01:20 +00:00
|
|
|
// fs.Debugf(f, "FindLeaf(%q, %q)", pathID, leaf)
|
2015-10-04 21:08:31 +00:00
|
|
|
parent, ok := f.dirCache.GetInv(pathID)
|
|
|
|
if !ok {
|
2016-06-12 14:06:02 +00:00
|
|
|
return "", false, errors.New("couldn't find parent ID")
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
|
|
|
path := leaf
|
|
|
|
if parent != "" {
|
|
|
|
path = parent + "/" + path
|
|
|
|
}
|
|
|
|
if f.dirCache.FoundRoot() {
|
|
|
|
path = f.rootSlash() + path
|
|
|
|
}
|
|
|
|
info, resp, err := f.readMetaDataForPath(path)
|
|
|
|
if err != nil {
|
|
|
|
if resp != nil && resp.StatusCode == http.StatusNotFound {
|
|
|
|
return "", false, nil
|
|
|
|
}
|
|
|
|
return "", false, err
|
|
|
|
}
|
|
|
|
if info.Folder == nil {
|
2016-06-12 14:06:02 +00:00
|
|
|
return "", false, errors.New("found file when looking for folder")
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
|
|
|
return info.ID, true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateDir makes a directory with pathID as parent and name leaf
|
|
|
|
func (f *Fs) CreateDir(pathID, leaf string) (newID string, err error) {
|
2017-02-09 11:01:20 +00:00
|
|
|
// fs.Debugf(f, "CreateDir(%q, %q)\n", pathID, leaf)
|
2015-10-04 21:08:31 +00:00
|
|
|
var resp *http.Response
|
|
|
|
var info *api.Item
|
2015-11-27 12:46:13 +00:00
|
|
|
opts := rest.Opts{
|
2015-10-04 21:08:31 +00:00
|
|
|
Method: "POST",
|
2017-08-03 19:57:42 +00:00
|
|
|
Path: "/items/" + pathID + "/children",
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
|
|
|
mkdir := api.CreateItemRequest{
|
2015-10-30 08:40:14 +00:00
|
|
|
Name: replaceReservedChars(leaf),
|
2015-10-04 21:08:31 +00:00
|
|
|
ConflictBehavior: "fail",
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
// Top parameter asks for bigger pages of data
|
|
|
|
// https://dev.onedrive.com/odata/optional-query-parameters.htm
|
2015-11-27 12:46:13 +00:00
|
|
|
opts := rest.Opts{
|
2015-10-04 21:08:31 +00:00
|
|
|
Method: "GET",
|
2017-08-03 19:57:42 +00:00
|
|
|
Path: "/items/" + dirID + "/children?top=1000",
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
|
|
|
OUTER:
|
|
|
|
for {
|
|
|
|
var result api.ListChildrenResponse
|
|
|
|
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 {
|
2016-05-30 18:49:21 +00:00
|
|
|
return found, errors.Wrap(err, "couldn't list files")
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
|
|
|
if len(result.Value) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
for i := range result.Value {
|
|
|
|
item := &result.Value[i]
|
|
|
|
isFolder := item.Folder != nil
|
|
|
|
if isFolder {
|
|
|
|
if filesOnly {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if directoriesOnly {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if item.Deleted != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
item.Name = restoreReservedChars(item.Name)
|
|
|
|
if fn(item) {
|
|
|
|
found = true
|
|
|
|
break OUTER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if result.NextLink == "" {
|
|
|
|
break
|
|
|
|
}
|
2017-07-07 07:18:13 +00:00
|
|
|
opts.Path = ""
|
|
|
|
opts.RootURL = result.NextLink
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-06-11 21:43:31 +00: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) {
|
|
|
|
err = f.dirCache.FindRoot(false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
directoryID, err := f.dirCache.FindDir(dir, false)
|
|
|
|
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)
|
2015-10-04 21:08:31 +00:00
|
|
|
if info.Folder != nil {
|
2017-06-11 21:43:31 +00:00
|
|
|
// cache the directory ID for later lookups
|
|
|
|
f.dirCache.Put(remote, info.ID)
|
2017-08-02 15:44:36 +00:00
|
|
|
d := fs.NewDir(remote, time.Time(info.LastModifiedDateTime)).SetID(info.ID)
|
2017-06-11 21:43:31 +00:00
|
|
|
if info.Folder != nil {
|
2017-06-30 12:37:29 +00:00
|
|
|
d.SetItems(info.Folder.ChildCount)
|
2016-04-21 19:06:21 +00:00
|
|
|
}
|
2017-06-11 21:43:31 +00:00
|
|
|
entries = append(entries, d)
|
2015-10-04 21:08:31 +00:00
|
|
|
} else {
|
2016-06-25 20:23:20 +00:00
|
|
|
o, err := f.newObjectWithInfo(remote, info)
|
|
|
|
if err != nil {
|
2017-06-11 21:43:31 +00:00
|
|
|
iErr = err
|
2016-06-25 20:23:20 +00:00
|
|
|
return true
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
2017-06-11 21:43:31 +00:00
|
|
|
entries = append(entries, o)
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
2017-06-11 21:43:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if iErr != nil {
|
|
|
|
return nil, iErr
|
|
|
|
}
|
|
|
|
return entries, nil
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
|
|
|
|
2015-10-30 08:40:14 +00:00
|
|
|
// Creates from the parameters passed in a half finished Object which
|
|
|
|
// must have setMetaData called on it
|
2015-10-04 21:08:31 +00:00
|
|
|
//
|
2015-10-30 08:40:14 +00:00
|
|
|
// Returns the object, leaf, directoryID and error
|
2015-10-04 21:08:31 +00:00
|
|
|
//
|
2015-10-30 08:40:14 +00:00
|
|
|
// Used to create new objects
|
|
|
|
func (f *Fs) createObject(remote string, modTime time.Time, size int64) (o *Object, leaf string, directoryID string, err error) {
|
2015-10-04 21:08:31 +00:00
|
|
|
// Create the directory for the object if it doesn't exist
|
2017-03-15 20:55:05 +00:00
|
|
|
leaf, directoryID, err = f.dirCache.FindRootAndPath(remote, true)
|
2015-10-04 21:08:31 +00:00
|
|
|
if err != nil {
|
2015-10-30 08:40:14 +00:00
|
|
|
return nil, leaf, directoryID, err
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
|
|
|
// Temporary Object under construction
|
2015-10-30 08:40:14 +00:00
|
|
|
o = &Object{
|
2015-10-04 21:08:31 +00:00
|
|
|
fs: f,
|
|
|
|
remote: remote,
|
|
|
|
}
|
2015-10-30 08:40:14 +00:00
|
|
|
return o, leaf, directoryID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put the object into the container
|
|
|
|
//
|
|
|
|
// 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 11:44:22 +00:00
|
|
|
func (f *Fs) Put(in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) (fs.Object, error) {
|
2016-02-18 11:35:25 +00:00
|
|
|
remote := src.Remote()
|
|
|
|
size := src.Size()
|
|
|
|
modTime := src.ModTime()
|
|
|
|
|
2015-10-30 08:40:14 +00:00
|
|
|
o, _, _, err := f.createObject(remote, modTime, size)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-28 11:44:22 +00:00
|
|
|
return o, o.Update(in, src, options...)
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mkdir creates the container if it doesn't exist
|
2016-11-25 21:52:43 +00:00
|
|
|
func (f *Fs) Mkdir(dir string) error {
|
|
|
|
err := f.dirCache.FindRoot(true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if dir != "" {
|
|
|
|
_, err = f.dirCache.FindDir(dir, true)
|
|
|
|
}
|
|
|
|
return err
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// deleteObject removes an object by ID
|
|
|
|
func (f *Fs) deleteObject(id string) error {
|
2015-11-27 12:46:13 +00:00
|
|
|
opts := rest.Opts{
|
2015-10-04 21:08:31 +00:00
|
|
|
Method: "DELETE",
|
2017-08-03 19:57:42 +00:00
|
|
|
Path: "/items/" + id,
|
2015-10-04 21:08:31 +00:00
|
|
|
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
|
2016-11-25 21:52:43 +00:00
|
|
|
func (f *Fs) purgeCheck(dir string, check bool) error {
|
|
|
|
root := path.Join(f.root, dir)
|
|
|
|
if root == "" {
|
2016-06-12 14:06:02 +00:00
|
|
|
return errors.New("can't purge root directory")
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
|
|
|
dc := f.dirCache
|
2017-01-15 12:18:07 +00:00
|
|
|
err := dc.FindRoot(false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-25 21:52:43 +00:00
|
|
|
rootID, err := dc.FindDir(dir, false)
|
2015-10-04 21:08:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-25 21:52:43 +00:00
|
|
|
item, _, err := f.readMetaDataForPath(root)
|
2015-10-04 21:08:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if item.Folder == nil {
|
2016-06-12 14:06:02 +00:00
|
|
|
return errors.New("not a folder")
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
|
|
|
if check && item.Folder.ChildCount != 0 {
|
2016-06-12 14:06:02 +00:00
|
|
|
return errors.New("folder not empty")
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
|
|
|
err = f.deleteObject(rootID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-25 21:52:43 +00:00
|
|
|
f.dirCache.FlushDir(dir)
|
2015-10-04 21:08:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rmdir deletes the root folder
|
|
|
|
//
|
|
|
|
// Returns an error if it isn't empty
|
2016-11-25 21:52:43 +00:00
|
|
|
func (f *Fs) Rmdir(dir string) error {
|
|
|
|
return f.purgeCheck(dir, true)
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Precision return the precision of this Fs
|
|
|
|
func (f *Fs) Precision() time.Duration {
|
|
|
|
return time.Second
|
|
|
|
}
|
|
|
|
|
2015-10-30 08:40:14 +00:00
|
|
|
// waitForJob waits for the job with status in url to complete
|
|
|
|
func (f *Fs) waitForJob(location string, o *Object) error {
|
|
|
|
deadline := time.Now().Add(fs.Config.Timeout)
|
|
|
|
for time.Now().Before(deadline) {
|
2015-11-27 12:46:13 +00:00
|
|
|
opts := rest.Opts{
|
2017-03-12 12:00:10 +00:00
|
|
|
Method: "GET",
|
2017-07-07 07:18:13 +00:00
|
|
|
RootURL: location,
|
2017-03-12 12:00:10 +00:00
|
|
|
IgnoreStatus: true, // Ignore the http status response since it seems to return valid info on 500 errors
|
2015-10-30 08:40:14 +00:00
|
|
|
}
|
|
|
|
var resp *http.Response
|
|
|
|
var err error
|
2017-03-12 12:00:10 +00:00
|
|
|
var body []byte
|
2015-10-30 08:40:14 +00:00
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
|
|
|
resp, err = f.srv.Call(&opts)
|
2017-03-12 12:00:10 +00:00
|
|
|
if err != nil {
|
2018-01-12 16:30:54 +00:00
|
|
|
return fserrors.ShouldRetry(err), err
|
2017-03-12 12:00:10 +00:00
|
|
|
}
|
|
|
|
body, err = rest.ReadBody(resp)
|
2018-01-12 16:30:54 +00:00
|
|
|
return fserrors.ShouldRetry(err), err
|
2015-10-30 08:40:14 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-12 12:00:10 +00:00
|
|
|
// Try to decode the body first as an api.AsyncOperationStatus
|
|
|
|
var status api.AsyncOperationStatus
|
|
|
|
err = json.Unmarshal(body, &status)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "async status result not JSON: %q", body)
|
|
|
|
}
|
|
|
|
// See if we decoded anything...
|
|
|
|
if !(status.Operation == "" && status.PercentageComplete == 0 && status.Status == "") {
|
2015-10-30 08:40:14 +00:00
|
|
|
if status.Status == "failed" || status.Status == "deleteFailed" {
|
2017-03-12 12:00:10 +00:00
|
|
|
return errors.Errorf("%s: async operation %q returned %q", o.remote, status.Operation, status.Status)
|
2015-10-30 08:40:14 +00:00
|
|
|
}
|
2017-03-12 12:00:10 +00:00
|
|
|
} else if resp.StatusCode == 200 {
|
2015-10-30 08:40:14 +00:00
|
|
|
var info api.Item
|
2017-03-12 12:00:10 +00:00
|
|
|
err = json.Unmarshal(body, &info)
|
2015-10-30 08:40:14 +00:00
|
|
|
if err != nil {
|
2017-03-12 12:00:10 +00:00
|
|
|
return errors.Wrapf(err, "async item result not JSON: %q", body)
|
2015-10-30 08:40:14 +00:00
|
|
|
}
|
2017-03-06 20:11:54 +00:00
|
|
|
return o.setMetaData(&info)
|
2015-10-30 08:40:14 +00:00
|
|
|
}
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
2016-06-12 14:06:02 +00:00
|
|
|
return errors.Errorf("async operation didn't complete after %v", fs.Config.Timeout)
|
2015-10-30 08:40:14 +00:00
|
|
|
}
|
|
|
|
|
2015-10-04 21:08:31 +00: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-22 11:04:16 +00:00
|
|
|
func (f *Fs) Copy(src fs.Object, remote string) (fs.Object, error) {
|
2015-10-30 08:40:14 +00:00
|
|
|
srcObj, ok := src.(*Object)
|
|
|
|
if !ok {
|
2017-02-09 11:01:20 +00:00
|
|
|
fs.Debugf(src, "Can't copy - not same remote type")
|
2015-10-30 08:40:14 +00:00
|
|
|
return nil, fs.ErrorCantCopy
|
|
|
|
}
|
|
|
|
err := srcObj.readMetaData()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-03-11 22:22:13 +00:00
|
|
|
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)
|
2017-02-22 19:28:22 +00:00
|
|
|
}
|
|
|
|
|
2015-10-30 08:40:14 +00:00
|
|
|
// Create temporary object
|
|
|
|
dstObj, leaf, directoryID, err := f.createObject(remote, srcObj.modTime, srcObj.size)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the object
|
2015-11-27 12:46:13 +00:00
|
|
|
opts := rest.Opts{
|
2015-10-30 08:40:14 +00:00
|
|
|
Method: "POST",
|
2017-08-03 19:57:42 +00:00
|
|
|
Path: "/items/" + srcObj.id + "/action.copy",
|
2015-10-30 08:40:14 +00:00
|
|
|
ExtraHeaders: map[string]string{"Prefer": "respond-async"},
|
|
|
|
NoResponse: true,
|
|
|
|
}
|
|
|
|
replacedLeaf := replaceReservedChars(leaf)
|
|
|
|
copy := api.CopyItemRequest{
|
|
|
|
Name: &replacedLeaf,
|
|
|
|
ParentReference: api.ItemReference{
|
|
|
|
ID: directoryID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var resp *http.Response
|
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
|
|
|
resp, err = f.srv.CallJSON(&opts, ©, nil)
|
|
|
|
return shouldRetry(resp, err)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// read location header
|
|
|
|
location := resp.Header.Get("Location")
|
|
|
|
if location == "" {
|
2016-06-12 14:06:02 +00:00
|
|
|
return nil, errors.New("didn't receive location header in copy response")
|
2015-10-30 08:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for job to finish
|
|
|
|
err = f.waitForJob(location, dstObj)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-03-15 07:06:17 +00:00
|
|
|
|
|
|
|
// Copy does NOT copy the modTime from the source and there seems to
|
|
|
|
// be no way to set date before
|
|
|
|
// This will create TWO versions on OneDrive
|
|
|
|
err = dstObj.SetModTime(srcObj.ModTime())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-10-30 08:40:14 +00:00
|
|
|
return dstObj, nil
|
|
|
|
}
|
2015-10-04 21:08:31 +00:00
|
|
|
|
|
|
|
// 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()
|
|
|
|
func (f *Fs) Purge() error {
|
2016-11-25 21:52:43 +00:00
|
|
|
return f.purgeCheck("", false)
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
|
|
|
|
2017-03-14 15:35:10 +00: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
|
|
|
|
func (f *Fs) Move(src fs.Object, remote string) (fs.Object, error) {
|
|
|
|
srcObj, ok := src.(*Object)
|
|
|
|
if !ok {
|
|
|
|
fs.Debugf(src, "Can't move - not same remote type")
|
|
|
|
return nil, fs.ErrorCantMove
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create temporary object
|
|
|
|
dstObj, leaf, directoryID, err := f.createObject(remote, srcObj.modTime, srcObj.size)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move the object
|
|
|
|
opts := rest.Opts{
|
|
|
|
Method: "PATCH",
|
2017-08-03 19:57:42 +00:00
|
|
|
Path: "/items/" + srcObj.id,
|
2017-03-14 15:35:10 +00:00
|
|
|
}
|
|
|
|
move := api.MoveItemRequest{
|
|
|
|
Name: replaceReservedChars(leaf),
|
|
|
|
ParentReference: &api.ItemReference{
|
|
|
|
ID: directoryID,
|
|
|
|
},
|
|
|
|
// We set the mod time too as it gets reset otherwise
|
|
|
|
FileSystemInfo: &api.FileSystemInfoFacet{
|
|
|
|
CreatedDateTime: api.Timestamp(srcObj.modTime),
|
|
|
|
LastModifiedDateTime: api.Timestamp(srcObj.modTime),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var resp *http.Response
|
|
|
|
var info api.Item
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
err = dstObj.setMetaData(&info)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return dstObj, nil
|
|
|
|
}
|
|
|
|
|
2016-12-09 15:39:29 +00:00
|
|
|
// DirCacheFlush resets the directory cache - used in testing as an
|
|
|
|
// optional interface
|
|
|
|
func (f *Fs) DirCacheFlush() {
|
|
|
|
f.dirCache.ResetRoot()
|
|
|
|
}
|
|
|
|
|
2018-04-16 21:19:25 +00:00
|
|
|
// About gets quota information
|
|
|
|
func (f *Fs) About() (usage *fs.Usage, err error) {
|
|
|
|
var drive api.Drive
|
|
|
|
opts := rest.Opts{
|
|
|
|
Method: "GET",
|
|
|
|
Path: "",
|
|
|
|
}
|
|
|
|
var resp *http.Response
|
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
|
|
|
resp, err = f.srv.CallJSON(&opts, nil, &drive)
|
|
|
|
return shouldRetry(resp, err)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "about failed")
|
|
|
|
}
|
|
|
|
q := drive.Quota
|
|
|
|
usage = &fs.Usage{
|
|
|
|
Total: fs.NewUsageValue(q.Total), // quota of bytes that can be used
|
|
|
|
Used: fs.NewUsageValue(q.Used), // bytes in use
|
|
|
|
Trashed: fs.NewUsageValue(q.Deleted), // bytes in trash
|
|
|
|
Free: fs.NewUsageValue(q.Remaining), // bytes which can be uploaded before reaching the quota
|
|
|
|
}
|
|
|
|
return usage, nil
|
|
|
|
}
|
|
|
|
|
2016-01-11 12:39:33 +00:00
|
|
|
// Hashes returns the supported hash sets.
|
2018-01-12 16:30:54 +00:00
|
|
|
func (f *Fs) Hashes() hash.Set {
|
2018-04-20 11:55:49 +00:00
|
|
|
if f.isBusiness {
|
|
|
|
return hash.Set(hash.QuickXorHash)
|
|
|
|
}
|
2018-01-18 20:27:52 +00:00
|
|
|
return hash.Set(hash.SHA1)
|
2016-01-11 12:39:33 +00:00
|
|
|
}
|
|
|
|
|
2015-10-04 21:08:31 +00:00
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
|
|
|
// Fs returns the parent Fs
|
2016-02-18 11:35:25 +00:00
|
|
|
func (o *Object) Fs() fs.Info {
|
2015-10-04 21:08:31 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-01-11 12:39:33 +00:00
|
|
|
// Hash returns the SHA-1 of an object returning a lowercase hex string
|
2018-01-12 16:30:54 +00:00
|
|
|
func (o *Object) Hash(t hash.Type) (string, error) {
|
2018-04-20 11:55:49 +00:00
|
|
|
if o.fs.isBusiness {
|
|
|
|
if t != hash.QuickXorHash {
|
|
|
|
return "", hash.ErrUnsupported
|
|
|
|
}
|
|
|
|
return o.quickxorhash, nil
|
|
|
|
}
|
2018-01-18 20:27:52 +00:00
|
|
|
if t != hash.SHA1 {
|
|
|
|
return "", hash.ErrUnsupported
|
2016-01-11 12:39:33 +00:00
|
|
|
}
|
|
|
|
return o.sha1, nil
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns the size of an object in bytes
|
|
|
|
func (o *Object) Size() int64 {
|
|
|
|
err := o.readMetaData()
|
|
|
|
if err != nil {
|
2017-02-09 11:01:20 +00:00
|
|
|
fs.Logf(o, "Failed to read metadata: %v", err)
|
2015-10-04 21:08:31 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return o.size
|
|
|
|
}
|
|
|
|
|
|
|
|
// setMetaData sets the metadata from info
|
2017-03-06 20:11:54 +00:00
|
|
|
func (o *Object) setMetaData(info *api.Item) (err error) {
|
|
|
|
if info.Folder != nil {
|
|
|
|
return errors.Wrapf(fs.ErrorNotAFile, "%q", o.remote)
|
|
|
|
}
|
2015-10-04 21:08:31 +00:00
|
|
|
o.hasMetaData = true
|
|
|
|
o.size = info.Size
|
2016-01-11 12:39:33 +00:00
|
|
|
|
2018-04-20 11:55:49 +00:00
|
|
|
// Docs: https://docs.microsoft.com/en-us/onedrive/developer/rest-api/resources/hashes
|
2016-01-17 10:45:17 +00:00
|
|
|
//
|
2018-04-20 11:55:49 +00:00
|
|
|
// We use SHA1 for onedrive personal and QuickXorHash for onedrive for business
|
2016-09-21 21:13:24 +00:00
|
|
|
if info.File != nil {
|
|
|
|
o.mimeType = info.File.MimeType
|
|
|
|
if info.File.Hashes.Sha1Hash != "" {
|
|
|
|
o.sha1 = strings.ToLower(info.File.Hashes.Sha1Hash)
|
|
|
|
}
|
2018-04-20 11:55:49 +00:00
|
|
|
if info.File.Hashes.QuickXorHash != "" {
|
|
|
|
h, err := base64.StdEncoding.DecodeString(info.File.Hashes.QuickXorHash)
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(o, "Failed to decode QuickXorHash %q: %v", info.File.Hashes.QuickXorHash, err)
|
|
|
|
} else {
|
|
|
|
o.quickxorhash = hex.EncodeToString(h)
|
|
|
|
}
|
|
|
|
}
|
2016-01-11 12:39:33 +00:00
|
|
|
}
|
2015-10-04 21:08:31 +00:00
|
|
|
if info.FileSystemInfo != nil {
|
|
|
|
o.modTime = time.Time(info.FileSystemInfo.LastModifiedDateTime)
|
|
|
|
} else {
|
|
|
|
o.modTime = time.Time(info.LastModifiedDateTime)
|
|
|
|
}
|
|
|
|
o.id = info.ID
|
2017-03-06 20:11:54 +00:00
|
|
|
return nil
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// readMetaData gets the metadata if it hasn't already been fetched
|
|
|
|
//
|
|
|
|
// it also sets the info
|
|
|
|
func (o *Object) readMetaData() (err error) {
|
|
|
|
if o.hasMetaData {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
info, _, err := o.fs.readMetaDataForPath(o.srvPath())
|
|
|
|
if err != nil {
|
2016-06-25 20:23:20 +00:00
|
|
|
if apiErr, ok := err.(*api.Error); ok {
|
|
|
|
if apiErr.ErrorInfo.Code == "itemNotFound" {
|
|
|
|
return fs.ErrorObjectNotFound
|
|
|
|
}
|
|
|
|
}
|
2015-10-04 21:08:31 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-03-06 20:11:54 +00:00
|
|
|
return o.setMetaData(info)
|
2015-10-04 21:08:31 +00: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
|
|
|
|
func (o *Object) ModTime() time.Time {
|
|
|
|
err := o.readMetaData()
|
|
|
|
if err != nil {
|
2017-02-09 11:01:20 +00:00
|
|
|
fs.Logf(o, "Failed to read metadata: %v", err)
|
2015-10-04 21:08:31 +00:00
|
|
|
return time.Now()
|
|
|
|
}
|
|
|
|
return o.modTime
|
|
|
|
}
|
|
|
|
|
|
|
|
// setModTime sets the modification time of the local fs object
|
|
|
|
func (o *Object) setModTime(modTime time.Time) (*api.Item, error) {
|
2015-11-27 12:46:13 +00:00
|
|
|
opts := rest.Opts{
|
2015-10-04 21:08:31 +00:00
|
|
|
Method: "PATCH",
|
2018-01-05 15:55:43 +00:00
|
|
|
Path: "/root:/" + rest.URLPathEscape(o.srvPath()),
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
|
|
|
update := api.SetFileSystemInfo{
|
|
|
|
FileSystemInfo: api.FileSystemInfoFacet{
|
|
|
|
CreatedDateTime: api.Timestamp(modTime),
|
|
|
|
LastModifiedDateTime: api.Timestamp(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
|
2016-03-22 15:07:10 +00:00
|
|
|
func (o *Object) SetModTime(modTime time.Time) error {
|
2015-10-04 21:08:31 +00:00
|
|
|
info, err := o.setModTime(modTime)
|
|
|
|
if err != nil {
|
2016-03-22 15:07:10 +00:00
|
|
|
return err
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
2017-03-06 20:11:54 +00:00
|
|
|
return o.setMetaData(info)
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Storable returns a boolean showing whether this object storable
|
|
|
|
func (o *Object) Storable() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open an object for read
|
2016-09-10 10:29:57 +00:00
|
|
|
func (o *Object) Open(options ...fs.OpenOption) (in io.ReadCloser, err error) {
|
2015-10-04 21:08:31 +00:00
|
|
|
if o.id == "" {
|
2016-06-12 14:06:02 +00:00
|
|
|
return nil, errors.New("can't download - no id")
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
2018-01-22 17:05:00 +00:00
|
|
|
fs.FixRangeOption(options, o.size)
|
2015-10-04 21:08:31 +00:00
|
|
|
var resp *http.Response
|
2015-11-27 12:46:13 +00:00
|
|
|
opts := rest.Opts{
|
2016-09-10 10:29:57 +00:00
|
|
|
Method: "GET",
|
2017-08-03 19:57:42 +00:00
|
|
|
Path: "/items/" + o.id + "/content",
|
2016-09-10 10:29:57 +00:00
|
|
|
Options: options,
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
2018-02-16 13:21:26 +00:00
|
|
|
|
|
|
|
if resp.StatusCode == http.StatusOK && resp.ContentLength > 0 && resp.Header.Get("Content-Range") == "" {
|
|
|
|
//Overwrite size with actual size since size readings from Onedrive is unreliable.
|
|
|
|
o.size = resp.ContentLength
|
|
|
|
}
|
2015-10-04 21:08:31 +00:00
|
|
|
return resp.Body, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// createUploadSession creates an upload session for the object
|
2018-03-15 07:06:17 +00:00
|
|
|
func (o *Object) createUploadSession(modTime time.Time) (response *api.CreateUploadResponse, err error) {
|
2015-11-27 12:46:13 +00:00
|
|
|
opts := rest.Opts{
|
2015-10-04 21:08:31 +00:00
|
|
|
Method: "POST",
|
2018-03-16 19:18:51 +00:00
|
|
|
Path: "/root:/" + rest.URLPathEscape(o.srvPath()) + ":/upload.createSession",
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
2018-03-15 07:06:17 +00:00
|
|
|
createRequest := api.CreateUploadRequest{}
|
2018-03-16 19:18:51 +00:00
|
|
|
createRequest.Item.FileSystemInfo.CreatedDateTime = api.Timestamp(modTime)
|
2018-03-15 07:06:17 +00:00
|
|
|
createRequest.Item.FileSystemInfo.LastModifiedDateTime = api.Timestamp(modTime)
|
2015-10-04 21:08:31 +00:00
|
|
|
var resp *http.Response
|
|
|
|
err = o.fs.pacer.Call(func() (bool, error) {
|
2018-03-15 07:06:17 +00:00
|
|
|
resp, err = o.fs.srv.CallJSON(&opts, &createRequest, &response)
|
2015-10-04 21:08:31 +00:00
|
|
|
return shouldRetry(resp, err)
|
|
|
|
})
|
2018-03-15 07:06:17 +00:00
|
|
|
return response, err
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// uploadFragment uploads a part
|
2018-03-17 09:46:06 +00:00
|
|
|
func (o *Object) uploadFragment(url string, start int64, totalSize int64, chunk io.ReadSeeker, chunkSize int64) (info *api.Item, err error) {
|
2015-11-27 12:46:13 +00:00
|
|
|
opts := rest.Opts{
|
2015-10-04 21:08:31 +00:00
|
|
|
Method: "PUT",
|
2017-07-07 07:18:13 +00:00
|
|
|
RootURL: url,
|
2017-04-05 16:19:26 +00:00
|
|
|
ContentLength: &chunkSize,
|
|
|
|
ContentRange: fmt.Sprintf("bytes %d-%d/%d", start, start+chunkSize-1, totalSize),
|
|
|
|
Body: chunk,
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
2018-03-15 07:06:17 +00:00
|
|
|
// var response api.UploadFragmentResponse
|
2015-10-04 21:08:31 +00:00
|
|
|
var resp *http.Response
|
|
|
|
err = o.fs.pacer.Call(func() (bool, error) {
|
2018-04-06 18:53:06 +00:00
|
|
|
_, _ = chunk.Seek(0, io.SeekStart)
|
2018-03-15 07:06:17 +00:00
|
|
|
resp, err = o.fs.srv.Call(&opts)
|
2018-03-26 16:17:56 +00:00
|
|
|
if resp != nil {
|
|
|
|
defer fs.CheckClose(resp.Body, &err)
|
|
|
|
}
|
2018-03-15 07:06:17 +00:00
|
|
|
retry, err := shouldRetry(resp, err)
|
|
|
|
if !retry && resp != nil {
|
|
|
|
if resp.StatusCode == 200 || resp.StatusCode == 201 {
|
|
|
|
// we are done :)
|
|
|
|
// read the item
|
2018-03-17 09:46:06 +00:00
|
|
|
info = &api.Item{}
|
2018-03-15 07:06:17 +00:00
|
|
|
return false, json.NewDecoder(resp.Body).Decode(info)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return retry, err
|
2015-10-04 21:08:31 +00:00
|
|
|
})
|
2018-03-17 09:46:06 +00:00
|
|
|
return info, err
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// cancelUploadSession cancels an upload session
|
|
|
|
func (o *Object) cancelUploadSession(url string) (err error) {
|
2015-11-27 12:46:13 +00:00
|
|
|
opts := rest.Opts{
|
2015-10-04 21:08:31 +00:00
|
|
|
Method: "DELETE",
|
2017-07-07 07:18:13 +00:00
|
|
|
RootURL: url,
|
2015-10-04 21:08:31 +00:00
|
|
|
NoResponse: true,
|
|
|
|
}
|
|
|
|
var resp *http.Response
|
|
|
|
err = o.fs.pacer.Call(func() (bool, error) {
|
|
|
|
resp, err = o.fs.srv.Call(&opts)
|
|
|
|
return shouldRetry(resp, err)
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// uploadMultipart uploads a file using multipart upload
|
2018-03-17 09:46:06 +00:00
|
|
|
func (o *Object) uploadMultipart(in io.Reader, size int64, modTime time.Time) (info *api.Item, err error) {
|
2015-10-04 21:08:31 +00:00
|
|
|
if chunkSize%(320*1024) != 0 {
|
2018-03-17 09:46:06 +00:00
|
|
|
return nil, errors.Errorf("chunk size %d is not a multiple of 320k", chunkSize)
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create upload session
|
2017-02-09 11:01:20 +00:00
|
|
|
fs.Debugf(o, "Starting multipart upload")
|
2018-03-15 07:06:17 +00:00
|
|
|
session, err := o.createUploadSession(modTime)
|
2015-10-04 21:08:31 +00:00
|
|
|
if err != nil {
|
2018-03-17 09:46:06 +00:00
|
|
|
return nil, err
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
|
|
|
uploadURL := session.UploadURL
|
|
|
|
|
|
|
|
// Cancel the session if something went wrong
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2017-03-23 13:10:43 +00:00
|
|
|
fs.Debugf(o, "Cancelling multipart upload: %v", err)
|
2015-10-04 21:08:31 +00:00
|
|
|
cancelErr := o.cancelUploadSession(uploadURL)
|
|
|
|
if cancelErr != nil {
|
2017-02-09 11:01:20 +00:00
|
|
|
fs.Logf(o, "Failed to cancel multipart upload: %v", err)
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Upload the chunks
|
|
|
|
remaining := size
|
|
|
|
position := int64(0)
|
|
|
|
for remaining > 0 {
|
|
|
|
n := int64(chunkSize)
|
|
|
|
if remaining < n {
|
|
|
|
n = remaining
|
|
|
|
}
|
2018-01-12 16:30:54 +00:00
|
|
|
seg := readers.NewRepeatableReader(io.LimitReader(in, n))
|
2017-02-09 11:01:20 +00:00
|
|
|
fs.Debugf(o, "Uploading segment %d/%d size %d", position, size, n)
|
2018-03-17 09:46:06 +00:00
|
|
|
info, err = o.uploadFragment(uploadURL, position, size, seg, n)
|
2015-10-04 21:08:31 +00:00
|
|
|
if err != nil {
|
2018-03-17 09:46:06 +00:00
|
|
|
return nil, err
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
|
|
|
remaining -= n
|
|
|
|
position += n
|
|
|
|
}
|
|
|
|
|
2018-03-17 09:46:06 +00:00
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// uploadSinglepart uploads a file as a single part
|
|
|
|
func (o *Object) uploadSinglepart(in io.Reader, size int64, modTime time.Time) (info *api.Item, err error) {
|
|
|
|
var resp *http.Response
|
|
|
|
opts := rest.Opts{
|
|
|
|
Method: "PUT",
|
|
|
|
Path: "/root:/" + rest.URLPathEscape(o.srvPath()) + ":/content",
|
|
|
|
ContentLength: &size,
|
|
|
|
Body: in,
|
|
|
|
}
|
|
|
|
// for go1.8 (see release notes) we must nil the Body if we want a
|
|
|
|
// "Content-Length: 0" header which onedrive requires for all files.
|
|
|
|
if size == 0 {
|
|
|
|
opts.Body = nil
|
|
|
|
}
|
|
|
|
err = o.fs.pacer.CallNoRetry(func() (bool, error) {
|
|
|
|
resp, err = o.fs.srv.CallJSON(&opts, nil, &info)
|
|
|
|
return shouldRetry(resp, err)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = o.setMetaData(info)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Set the mod time now and read metadata
|
|
|
|
return o.setModTime(modTime)
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2017-05-28 11:44:22 +00:00
|
|
|
func (o *Object) Update(in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) (err error) {
|
2017-01-29 20:42:43 +00:00
|
|
|
o.fs.tokenRenewer.Start()
|
|
|
|
defer o.fs.tokenRenewer.Stop()
|
|
|
|
|
2016-02-18 11:35:25 +00:00
|
|
|
size := src.Size()
|
|
|
|
modTime := src.ModTime()
|
|
|
|
|
2018-03-17 09:46:06 +00:00
|
|
|
var info *api.Item
|
|
|
|
if size <= 0 {
|
|
|
|
// This is for 0 length files, or files with an unknown size
|
|
|
|
info, err = o.uploadSinglepart(in, size, modTime)
|
|
|
|
} else {
|
|
|
|
info, err = o.uploadMultipart(in, size, modTime)
|
|
|
|
}
|
2015-10-04 21:08:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-06 20:11:54 +00:00
|
|
|
return o.setMetaData(info)
|
2015-10-04 21:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove an object
|
|
|
|
func (o *Object) Remove() error {
|
|
|
|
return o.fs.deleteObject(o.id)
|
|
|
|
}
|
|
|
|
|
2016-09-21 21:13:24 +00:00
|
|
|
// MimeType of an Object if known, "" otherwise
|
|
|
|
func (o *Object) MimeType() string {
|
|
|
|
return o.mimeType
|
|
|
|
}
|
|
|
|
|
2015-10-04 21:08:31 +00:00
|
|
|
// Check the interfaces are satisfied
|
|
|
|
var (
|
|
|
|
_ fs.Fs = (*Fs)(nil)
|
|
|
|
_ fs.Purger = (*Fs)(nil)
|
2015-11-22 11:04:16 +00:00
|
|
|
_ fs.Copier = (*Fs)(nil)
|
2017-03-14 15:35:10 +00:00
|
|
|
_ fs.Mover = (*Fs)(nil)
|
2015-10-04 21:08:31 +00:00
|
|
|
// _ fs.DirMover = (*Fs)(nil)
|
2016-12-09 15:39:29 +00:00
|
|
|
_ fs.DirCacheFlusher = (*Fs)(nil)
|
2018-04-16 21:19:25 +00:00
|
|
|
_ fs.Abouter = (*Fs)(nil)
|
2016-12-09 15:39:29 +00:00
|
|
|
_ fs.Object = (*Object)(nil)
|
|
|
|
_ fs.MimeTyper = &Object{}
|
2015-10-04 21:08:31 +00:00
|
|
|
)
|