2015-11-28 02:25:52 +08:00
|
|
|
// Package b2 provides an interface to the Backblaze B2 object storage system
|
|
|
|
package b2
|
|
|
|
|
2016-01-19 01:53:03 +08:00
|
|
|
// FIXME should we remove sha1 checks from here as rclone now supports
|
|
|
|
// checking SHA1s?
|
|
|
|
|
2015-11-28 02:25:52 +08:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/sha1"
|
|
|
|
"fmt"
|
|
|
|
"hash"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ncw/rclone/b2/api"
|
|
|
|
"github.com/ncw/rclone/fs"
|
2016-02-24 06:15:20 +08:00
|
|
|
"github.com/ncw/rclone/pacer"
|
2015-11-28 02:25:52 +08:00
|
|
|
"github.com/ncw/rclone/rest"
|
2016-06-12 22:06:02 +08:00
|
|
|
"github.com/pkg/errors"
|
2015-11-28 02:25:52 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-08-15 21:59:19 +08:00
|
|
|
defaultEndpoint = "https://api.backblazeb2.com"
|
2016-07-01 23:23:23 +08:00
|
|
|
headerPrefix = "x-bz-info-" // lower case as that is what the server returns
|
|
|
|
timeKey = "src_last_modified_millis"
|
|
|
|
timeHeader = headerPrefix + timeKey
|
|
|
|
sha1Key = "large_file_sha1"
|
|
|
|
sha1Header = "X-Bz-Content-Sha1"
|
2016-09-06 00:26:04 +08:00
|
|
|
sha1InfoHeader = headerPrefix + sha1Key
|
2016-07-01 23:23:23 +08:00
|
|
|
testModeHeader = "X-Bz-Test-Mode"
|
|
|
|
retryAfterHeader = "Retry-After"
|
|
|
|
minSleep = 10 * time.Millisecond
|
2016-08-02 05:57:52 +08:00
|
|
|
maxSleep = 5 * time.Minute
|
2016-07-01 23:23:23 +08:00
|
|
|
decayConstant = 1 // bigger for slower decay, exponential
|
|
|
|
maxParts = 10000
|
2016-07-05 18:26:02 +08:00
|
|
|
maxVersions = 100 // maximum number of versions we search in --b2-versions mode
|
2016-06-16 01:49:11 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Globals
|
|
|
|
var (
|
2016-07-05 18:26:02 +08:00
|
|
|
minChunkSize = fs.SizeSuffix(100E6)
|
|
|
|
chunkSize = fs.SizeSuffix(96 * 1024 * 1024)
|
|
|
|
uploadCutoff = fs.SizeSuffix(200E6)
|
2016-12-20 23:50:46 +08:00
|
|
|
b2TestMode = fs.StringP("b2-test-mode", "", "", "A flag string for X-Bz-Test-Mode header.")
|
|
|
|
b2Versions = fs.BoolP("b2-versions", "", false, "Include old versions in directory listings.")
|
2016-07-05 18:26:02 +08:00
|
|
|
errNotWithVersions = errors.New("can't modify or delete files in --b2-versions mode")
|
2015-11-28 02:25:52 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Register with Fs
|
|
|
|
func init() {
|
2016-02-18 19:35:25 +08:00
|
|
|
fs.Register(&fs.RegInfo{
|
2016-02-16 02:11:53 +08:00
|
|
|
Name: "b2",
|
|
|
|
Description: "Backblaze B2",
|
|
|
|
NewFs: NewFs,
|
2015-11-28 02:25:52 +08:00
|
|
|
Options: []fs.Option{{
|
|
|
|
Name: "account",
|
|
|
|
Help: "Account ID",
|
|
|
|
}, {
|
|
|
|
Name: "key",
|
|
|
|
Help: "Application Key",
|
|
|
|
}, {
|
|
|
|
Name: "endpoint",
|
|
|
|
Help: "Endpoint for the service - leave blank normally.",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2016-12-20 23:50:46 +08:00
|
|
|
fs.VarP(&uploadCutoff, "b2-upload-cutoff", "", "Cutoff for switching to chunked upload")
|
|
|
|
fs.VarP(&chunkSize, "b2-chunk-size", "", "Upload chunk size. Must fit in memory.")
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fs represents a remote b2 server
|
|
|
|
type Fs struct {
|
|
|
|
name string // name of this remote
|
2017-01-14 01:21:47 +08:00
|
|
|
root string // the path we are working on if any
|
|
|
|
features *fs.Features // optional features
|
2016-02-24 05:19:33 +08:00
|
|
|
account string // account name
|
|
|
|
key string // auth key
|
|
|
|
endpoint string // name of the starting api endpoint
|
2015-11-28 02:25:52 +08:00
|
|
|
srv *rest.Client // the connection to the b2 server
|
|
|
|
bucket string // the bucket we are working on
|
|
|
|
bucketIDMutex sync.Mutex // mutex to protect _bucketID
|
|
|
|
_bucketID string // the ID of the bucket we are working on
|
|
|
|
info api.AuthorizeAccountResponse // result of authorize call
|
|
|
|
uploadMu sync.Mutex // lock for upload variable
|
2016-02-27 21:00:35 +08:00
|
|
|
uploads []*api.GetUploadURLResponse // result of get upload URL calls
|
2016-02-24 05:19:33 +08:00
|
|
|
authMu sync.Mutex // lock for authorizing the account
|
2016-02-24 06:15:20 +08:00
|
|
|
pacer *pacer.Pacer // To pace and retry the API calls
|
2017-01-30 06:21:39 +08:00
|
|
|
bufferTokens chan []byte // control concurrency of multipart uploads
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Object describes a b2 object
|
|
|
|
type Object struct {
|
2016-09-22 05:13:24 +08:00
|
|
|
fs *Fs // what this object is part of
|
|
|
|
remote string // The remote path
|
|
|
|
id string // b2 id of the file
|
|
|
|
modTime time.Time // The modified time of the object if known
|
|
|
|
sha1 string // SHA-1 hash if known
|
|
|
|
size int64 // Size of the object
|
|
|
|
mimeType string // Content-Type of the object
|
2015-11-28 02:25:52 +08: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 {
|
|
|
|
if f.root == "" {
|
|
|
|
return f.bucket
|
|
|
|
}
|
|
|
|
return f.bucket + "/" + f.root
|
|
|
|
}
|
|
|
|
|
|
|
|
// String converts this Fs to a string
|
|
|
|
func (f *Fs) String() string {
|
|
|
|
if f.root == "" {
|
|
|
|
return fmt.Sprintf("B2 bucket %s", f.bucket)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("B2 bucket %s path %s", f.bucket, f.root)
|
|
|
|
}
|
|
|
|
|
2017-01-14 01:21:47 +08:00
|
|
|
// Features returns the optional features of this Fs
|
|
|
|
func (f *Fs) Features() *fs.Features {
|
|
|
|
return f.features
|
|
|
|
}
|
|
|
|
|
2015-11-28 02:25:52 +08:00
|
|
|
// Pattern to match a b2 path
|
|
|
|
var matcher = regexp.MustCompile(`^([^/]*)(.*)$`)
|
|
|
|
|
|
|
|
// parseParse parses a b2 'url'
|
|
|
|
func parsePath(path string) (bucket, directory string, err error) {
|
|
|
|
parts := matcher.FindStringSubmatch(path)
|
|
|
|
if parts == nil {
|
2016-06-12 22:06:02 +08:00
|
|
|
err = errors.Errorf("couldn't find bucket in b2 path %q", path)
|
2015-11-28 02:25:52 +08:00
|
|
|
} else {
|
|
|
|
bucket, directory = parts[1], parts[2]
|
|
|
|
directory = strings.Trim(directory, "/")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-02-24 06:15:20 +08:00
|
|
|
// retryErrorCodes is a slice of error codes that we will retry
|
|
|
|
var retryErrorCodes = []int{
|
|
|
|
401, // Unauthorized (eg "Token has expired")
|
|
|
|
408, // Request Timeout
|
|
|
|
429, // Rate exceeded.
|
|
|
|
500, // Get occasional 500 Internal Server Error
|
|
|
|
503, // Service Unavailable
|
|
|
|
504, // Gateway Time-out
|
|
|
|
}
|
|
|
|
|
|
|
|
// shouldRetryNoAuth returns a boolean as to whether this resp and err
|
|
|
|
// deserve to be retried. It returns the err as a convenience
|
|
|
|
func (f *Fs) shouldRetryNoReauth(resp *http.Response, err error) (bool, error) {
|
2016-07-01 23:23:23 +08:00
|
|
|
// For 429 or 503 errors look at the Retry-After: header and
|
|
|
|
// set the retry appropriately, starting with a minimum of 1
|
|
|
|
// second if it isn't set.
|
|
|
|
if resp != nil && (resp.StatusCode == 429 || resp.StatusCode == 503) {
|
|
|
|
var retryAfter = 1
|
|
|
|
retryAfterString := resp.Header.Get(retryAfterHeader)
|
|
|
|
if retryAfterString != "" {
|
|
|
|
var err error
|
|
|
|
retryAfter, err = strconv.Atoi(retryAfterString)
|
|
|
|
if err != nil {
|
|
|
|
fs.ErrorLog(f, "Malformed %s header %q: %v", retryAfterHeader, retryAfterString, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
retryAfterDuration := time.Duration(retryAfter) * time.Second
|
|
|
|
if f.pacer.GetSleep() < retryAfterDuration {
|
|
|
|
fs.Debug(f, "Setting sleep to %v after error: %v", retryAfterDuration, err)
|
|
|
|
// We set 1/2 the value here because the pacer will double it immediately
|
|
|
|
f.pacer.SetSleep(retryAfterDuration / 2)
|
|
|
|
}
|
|
|
|
return true, err
|
|
|
|
}
|
2016-02-24 06:15:20 +08:00
|
|
|
return fs.ShouldRetry(err) || fs.ShouldRetryHTTP(resp, retryErrorCodes), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// shouldRetry returns a boolean as to whether this resp and err
|
|
|
|
// deserve to be retried. It returns the err as a convenience
|
|
|
|
func (f *Fs) shouldRetry(resp *http.Response, err error) (bool, error) {
|
2016-07-01 18:47:42 +08:00
|
|
|
if resp != nil && resp.StatusCode == 401 {
|
|
|
|
fs.Debug(f, "Unauthorized: %v", err)
|
2016-02-24 06:15:20 +08:00
|
|
|
// Reauth
|
|
|
|
authErr := f.authorizeAccount()
|
|
|
|
if authErr != nil {
|
|
|
|
err = authErr
|
|
|
|
}
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
return f.shouldRetryNoReauth(resp, err)
|
|
|
|
}
|
|
|
|
|
2015-11-28 02:25:52 +08: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 {
|
|
|
|
fs.Debug(nil, "Couldn't decode error response: %v", err)
|
|
|
|
}
|
|
|
|
if errResponse.Code == "" {
|
|
|
|
errResponse.Code = "unknown"
|
|
|
|
}
|
|
|
|
if errResponse.Status == 0 {
|
|
|
|
errResponse.Status = resp.StatusCode
|
|
|
|
}
|
|
|
|
if errResponse.Message == "" {
|
|
|
|
errResponse.Message = "Unknown " + resp.Status
|
|
|
|
}
|
|
|
|
return errResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFs contstructs an Fs from the path, bucket:path
|
|
|
|
func NewFs(name, root string) (fs.Fs, error) {
|
2016-06-16 01:49:11 +08:00
|
|
|
if uploadCutoff < chunkSize {
|
|
|
|
return nil, errors.Errorf("b2: upload cutoff must be less than chunk size %v - was %v", chunkSize, uploadCutoff)
|
|
|
|
}
|
|
|
|
if chunkSize < minChunkSize {
|
|
|
|
return nil, errors.Errorf("b2: chunk size can't be less than %v - was %v", minChunkSize, chunkSize)
|
|
|
|
}
|
2015-11-28 02:25:52 +08:00
|
|
|
bucket, directory, err := parsePath(root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-21 02:03:09 +08:00
|
|
|
account := fs.ConfigFileGet(name, "account")
|
2015-11-28 02:25:52 +08:00
|
|
|
if account == "" {
|
|
|
|
return nil, errors.New("account not found")
|
|
|
|
}
|
2016-12-21 02:03:09 +08:00
|
|
|
key := fs.ConfigFileGet(name, "key")
|
2015-11-28 02:25:52 +08:00
|
|
|
if key == "" {
|
|
|
|
return nil, errors.New("key not found")
|
|
|
|
}
|
2016-12-21 02:03:09 +08:00
|
|
|
endpoint := fs.ConfigFileGet(name, "endpoint", defaultEndpoint)
|
2016-02-24 05:19:33 +08:00
|
|
|
f := &Fs{
|
2016-07-01 17:04:52 +08:00
|
|
|
name: name,
|
|
|
|
bucket: bucket,
|
|
|
|
root: directory,
|
|
|
|
account: account,
|
|
|
|
key: key,
|
|
|
|
endpoint: endpoint,
|
|
|
|
srv: rest.NewClient(fs.Config.Client()).SetErrorHandler(errorHandler),
|
|
|
|
pacer: pacer.New().SetMinSleep(minSleep).SetMaxSleep(maxSleep).SetDecayConstant(decayConstant),
|
2017-01-30 06:21:39 +08:00
|
|
|
bufferTokens: make(chan []byte, fs.Config.Transfers),
|
2016-07-01 17:04:52 +08:00
|
|
|
}
|
2017-01-14 01:21:47 +08:00
|
|
|
f.features = (&fs.Features{ReadMimeType: true, WriteMimeType: true}).Fill(f)
|
2016-07-01 18:30:09 +08:00
|
|
|
// Set the test flag if required
|
|
|
|
if *b2TestMode != "" {
|
|
|
|
testMode := strings.TrimSpace(*b2TestMode)
|
|
|
|
f.srv.SetHeader(testModeHeader, testMode)
|
|
|
|
fs.Debug(f, "Setting test header \"%s: %s\"", testModeHeader, testMode)
|
|
|
|
}
|
2017-01-30 06:21:39 +08:00
|
|
|
// Fill up the buffer tokens
|
2016-07-01 17:04:52 +08:00
|
|
|
for i := 0; i < fs.Config.Transfers; i++ {
|
2017-01-30 06:21:39 +08:00
|
|
|
f.bufferTokens <- nil
|
2016-02-24 06:15:20 +08:00
|
|
|
}
|
|
|
|
err = f.authorizeAccount()
|
|
|
|
if err != nil {
|
2016-06-12 22:06:02 +08:00
|
|
|
return nil, errors.Wrap(err, "failed to authorize account")
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
|
|
|
if f.root != "" {
|
|
|
|
f.root += "/"
|
|
|
|
// Check to see if the (bucket,directory) is actually an existing file
|
|
|
|
oldRoot := f.root
|
|
|
|
remote := path.Base(directory)
|
|
|
|
f.root = path.Dir(directory)
|
|
|
|
if f.root == "." {
|
|
|
|
f.root = ""
|
|
|
|
} else {
|
|
|
|
f.root += "/"
|
|
|
|
}
|
2016-06-26 04:23:20 +08:00
|
|
|
_, err := f.NewObject(remote)
|
|
|
|
if err != nil {
|
|
|
|
if err == fs.ErrorObjectNotFound {
|
|
|
|
// File doesn't exist so return old f
|
|
|
|
f.root = oldRoot
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
2016-06-26 04:23:20 +08:00
|
|
|
// return an error with an fs which points to the parent
|
|
|
|
return f, fs.ErrorIsFile
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
2016-02-24 05:19:33 +08:00
|
|
|
// authorizeAccount gets the API endpoint and auth token. Can be used
|
|
|
|
// for reauthentication too.
|
|
|
|
func (f *Fs) authorizeAccount() error {
|
|
|
|
f.authMu.Lock()
|
|
|
|
defer f.authMu.Unlock()
|
|
|
|
opts := rest.Opts{
|
|
|
|
Absolute: true,
|
|
|
|
Method: "GET",
|
|
|
|
Path: f.endpoint + "/b2api/v1/b2_authorize_account",
|
|
|
|
UserName: f.account,
|
|
|
|
Password: f.key,
|
|
|
|
ExtraHeaders: map[string]string{"Authorization": ""}, // unset the Authorization for this request
|
|
|
|
}
|
2016-02-24 06:15:20 +08:00
|
|
|
err := f.pacer.Call(func() (bool, error) {
|
|
|
|
resp, err := f.srv.CallJSON(&opts, nil, &f.info)
|
|
|
|
return f.shouldRetryNoReauth(resp, err)
|
|
|
|
})
|
2016-02-24 05:19:33 +08:00
|
|
|
if err != nil {
|
2016-06-12 22:06:02 +08:00
|
|
|
return errors.Wrap(err, "failed to authenticate")
|
2016-02-24 05:19:33 +08:00
|
|
|
}
|
|
|
|
f.srv.SetRoot(f.info.APIURL+"/b2api/v1").SetHeader("Authorization", f.info.AuthorizationToken)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-27 21:00:35 +08:00
|
|
|
// getUploadURL returns the upload info with the UploadURL and the AuthorizationToken
|
|
|
|
//
|
|
|
|
// This should be returned with returnUploadURL when finished
|
|
|
|
func (f *Fs) getUploadURL() (upload *api.GetUploadURLResponse, err error) {
|
2015-11-28 02:25:52 +08:00
|
|
|
f.uploadMu.Lock()
|
|
|
|
defer f.uploadMu.Unlock()
|
|
|
|
bucketID, err := f.getBucketID()
|
|
|
|
if err != nil {
|
2016-02-27 21:00:35 +08:00
|
|
|
return nil, err
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
2016-02-27 21:00:35 +08:00
|
|
|
if len(f.uploads) == 0 {
|
2015-11-28 02:25:52 +08:00
|
|
|
opts := rest.Opts{
|
|
|
|
Method: "POST",
|
|
|
|
Path: "/b2_get_upload_url",
|
|
|
|
}
|
|
|
|
var request = api.GetUploadURLRequest{
|
|
|
|
BucketID: bucketID,
|
|
|
|
}
|
2016-02-24 06:15:20 +08:00
|
|
|
err := f.pacer.Call(func() (bool, error) {
|
2016-02-27 21:00:35 +08:00
|
|
|
resp, err := f.srv.CallJSON(&opts, &request, &upload)
|
2016-06-16 01:49:11 +08:00
|
|
|
return f.shouldRetry(resp, err)
|
2016-02-24 06:15:20 +08:00
|
|
|
})
|
2015-11-28 02:25:52 +08:00
|
|
|
if err != nil {
|
2016-06-12 22:06:02 +08:00
|
|
|
return nil, errors.Wrap(err, "failed to get upload URL")
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
2016-02-27 21:00:35 +08:00
|
|
|
} else {
|
|
|
|
upload, f.uploads = f.uploads[0], f.uploads[1:]
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
2016-02-27 21:00:35 +08:00
|
|
|
return upload, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// returnUploadURL returns the UploadURL to the cache
|
|
|
|
func (f *Fs) returnUploadURL(upload *api.GetUploadURLResponse) {
|
2016-06-16 01:49:11 +08:00
|
|
|
if upload == nil {
|
|
|
|
return
|
|
|
|
}
|
2016-02-27 21:00:35 +08:00
|
|
|
f.uploadMu.Lock()
|
|
|
|
f.uploads = append(f.uploads, upload)
|
|
|
|
f.uploadMu.Unlock()
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// clearUploadURL clears the current UploadURL and the AuthorizationToken
|
|
|
|
func (f *Fs) clearUploadURL() {
|
|
|
|
f.uploadMu.Lock()
|
2016-02-27 21:00:35 +08:00
|
|
|
f.uploads = nil
|
|
|
|
f.uploadMu.Unlock()
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
|
|
|
|
2017-01-30 06:21:39 +08:00
|
|
|
// getUploadBlock gets a block from the pool of size chunkSize
|
|
|
|
func (f *Fs) getUploadBlock() []byte {
|
|
|
|
buf := <-f.bufferTokens
|
|
|
|
if buf == nil {
|
|
|
|
buf = make([]byte, chunkSize)
|
2016-10-10 22:57:56 +08:00
|
|
|
}
|
2017-01-30 06:21:39 +08:00
|
|
|
// fs.Debug(f, "Getting upload block %p", buf)
|
|
|
|
return buf
|
2016-10-10 22:57:56 +08:00
|
|
|
}
|
|
|
|
|
2017-01-30 06:21:39 +08:00
|
|
|
// putUploadBlock returns a block to the pool of size chunkSize
|
|
|
|
func (f *Fs) putUploadBlock(buf []byte) {
|
|
|
|
buf = buf[:cap(buf)]
|
|
|
|
if len(buf) != int(chunkSize) {
|
|
|
|
panic("bad blocksize returned to pool")
|
2016-10-10 22:57:56 +08:00
|
|
|
}
|
2017-01-30 06:21:39 +08:00
|
|
|
// fs.Debug(f, "Returning upload block %p", buf)
|
|
|
|
f.bufferTokens <- buf
|
2016-10-10 22:57:56 +08:00
|
|
|
}
|
|
|
|
|
2016-06-26 04:58:34 +08:00
|
|
|
// Return an Object from a path
|
2015-11-28 02:25:52 +08:00
|
|
|
//
|
2016-06-26 04:23:20 +08:00
|
|
|
// If it can't be found it returns the error fs.ErrorObjectNotFound.
|
|
|
|
func (f *Fs) newObjectWithInfo(remote string, info *api.File) (fs.Object, error) {
|
2015-11-28 02:25:52 +08:00
|
|
|
o := &Object{
|
|
|
|
fs: f,
|
|
|
|
remote: remote,
|
|
|
|
}
|
|
|
|
if info != nil {
|
2016-03-22 22:39:56 +08:00
|
|
|
err := o.decodeMetaData(info)
|
|
|
|
if err != nil {
|
2016-06-26 04:23:20 +08:00
|
|
|
return nil, err
|
2016-03-22 22:39:56 +08:00
|
|
|
}
|
2015-11-28 02:25:52 +08:00
|
|
|
} else {
|
|
|
|
err := o.readMetaData() // reads info and headers, returning an error
|
|
|
|
if err != nil {
|
2016-06-26 04:23:20 +08:00
|
|
|
return nil, err
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
|
|
|
}
|
2016-06-26 04:23:20 +08:00
|
|
|
return o, nil
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
|
|
|
|
2016-06-26 04:23:20 +08: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-26 04:58:34 +08:00
|
|
|
return f.newObjectWithInfo(remote, nil)
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// listFn is called from list to handle an object
|
2016-04-22 03:06:21 +08:00
|
|
|
type listFn func(remote string, object *api.File, isDirectory bool) error
|
2015-11-28 02:25:52 +08:00
|
|
|
|
2016-02-19 20:09:11 +08:00
|
|
|
// errEndList is a sentinel used to end the list iteration now.
|
|
|
|
// listFn should return it to end the iteration with no errors.
|
|
|
|
var errEndList = errors.New("end list")
|
|
|
|
|
2015-11-28 02:25:52 +08:00
|
|
|
// list lists the objects into the function supplied from
|
|
|
|
// the bucket and root supplied
|
|
|
|
//
|
2016-12-15 01:37:26 +08:00
|
|
|
// dir is the starting directory, "" for root
|
|
|
|
//
|
2016-04-22 03:06:21 +08:00
|
|
|
// level is the depth to search to
|
|
|
|
//
|
2015-11-28 02:25:52 +08:00
|
|
|
// If prefix is set then startFileName is used as a prefix which all
|
|
|
|
// files must have
|
|
|
|
//
|
|
|
|
// If limit is > 0 then it limits to that many files (must be less
|
|
|
|
// than 1000)
|
|
|
|
//
|
|
|
|
// If hidden is set then it will list the hidden (deleted) files too.
|
2016-04-24 04:46:52 +08:00
|
|
|
func (f *Fs) list(dir string, level int, prefix string, limit int, hidden bool, fn listFn) error {
|
|
|
|
root := f.root
|
|
|
|
if dir != "" {
|
|
|
|
root += dir + "/"
|
|
|
|
}
|
2016-12-15 01:37:26 +08:00
|
|
|
delimiter := ""
|
|
|
|
switch level {
|
|
|
|
case 1:
|
|
|
|
delimiter = "/"
|
|
|
|
case fs.MaxLevel:
|
|
|
|
default:
|
|
|
|
return fs.ErrorLevelNotSupported
|
|
|
|
}
|
2015-11-28 02:25:52 +08:00
|
|
|
bucketID, err := f.getBucketID()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
chunkSize := 1000
|
|
|
|
if limit > 0 {
|
|
|
|
chunkSize = limit
|
|
|
|
}
|
|
|
|
var request = api.ListFileNamesRequest{
|
|
|
|
BucketID: bucketID,
|
|
|
|
MaxFileCount: chunkSize,
|
2016-12-15 01:37:26 +08:00
|
|
|
Prefix: root,
|
|
|
|
Delimiter: delimiter,
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
2016-04-24 04:46:52 +08:00
|
|
|
prefix = root + prefix
|
2015-11-28 02:25:52 +08:00
|
|
|
if prefix != "" {
|
|
|
|
request.StartFileName = prefix
|
|
|
|
}
|
|
|
|
var response api.ListFileNamesResponse
|
|
|
|
opts := rest.Opts{
|
|
|
|
Method: "POST",
|
|
|
|
Path: "/b2_list_file_names",
|
|
|
|
}
|
|
|
|
if hidden {
|
|
|
|
opts.Path = "/b2_list_file_versions"
|
|
|
|
}
|
|
|
|
for {
|
2016-02-24 06:15:20 +08:00
|
|
|
err := f.pacer.Call(func() (bool, error) {
|
|
|
|
resp, err := f.srv.CallJSON(&opts, &request, &response)
|
|
|
|
return f.shouldRetry(resp, err)
|
|
|
|
})
|
2015-11-28 02:25:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for i := range response.Files {
|
|
|
|
file := &response.Files[i]
|
|
|
|
// Finish if file name no longer has prefix
|
2016-12-15 01:37:26 +08:00
|
|
|
if prefix != "" && !strings.HasPrefix(file.Name, prefix) {
|
2015-11-28 02:25:52 +08:00
|
|
|
return nil
|
|
|
|
}
|
2016-12-15 01:37:26 +08:00
|
|
|
if !strings.HasPrefix(file.Name, f.root) {
|
|
|
|
fs.Log(f, "Odd name received %q", file.Name)
|
|
|
|
continue
|
|
|
|
}
|
2016-04-22 03:06:21 +08:00
|
|
|
remote := file.Name[len(f.root):]
|
2016-12-15 01:37:26 +08:00
|
|
|
// Check for directory
|
|
|
|
isDirectory := level != 0 && strings.HasSuffix(remote, "/")
|
|
|
|
if isDirectory {
|
|
|
|
remote = remote[:len(remote)-1]
|
2016-04-22 03:06:21 +08:00
|
|
|
}
|
2016-12-15 01:37:26 +08:00
|
|
|
// Send object
|
|
|
|
err = fn(remote, file, isDirectory)
|
|
|
|
if err != nil {
|
|
|
|
if err == errEndList {
|
|
|
|
return nil
|
2016-03-22 22:39:56 +08:00
|
|
|
}
|
2016-12-15 01:37:26 +08:00
|
|
|
return err
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// end if no NextFileName
|
|
|
|
if response.NextFileName == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
request.StartFileName = *response.NextFileName
|
|
|
|
if response.NextFileID != nil {
|
|
|
|
request.StartFileID = *response.NextFileID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-22 03:06:21 +08:00
|
|
|
// listFiles walks the path returning files and directories to out
|
2016-04-24 04:46:52 +08:00
|
|
|
func (f *Fs) listFiles(out fs.ListOpts, dir string) {
|
2016-04-22 03:06:21 +08:00
|
|
|
defer out.Finished()
|
|
|
|
// List the objects
|
2016-07-05 18:26:02 +08:00
|
|
|
last := ""
|
|
|
|
err := f.list(dir, out.Level(), "", 0, *b2Versions, func(remote string, object *api.File, isDirectory bool) error {
|
2016-04-22 03:06:21 +08:00
|
|
|
if isDirectory {
|
|
|
|
dir := &fs.Dir{
|
|
|
|
Name: remote,
|
|
|
|
Bytes: -1,
|
|
|
|
Count: -1,
|
|
|
|
}
|
|
|
|
if out.AddDir(dir) {
|
|
|
|
return fs.ErrorListAborted
|
|
|
|
}
|
|
|
|
} else {
|
2016-07-05 18:26:02 +08:00
|
|
|
if remote == last {
|
|
|
|
remote = object.UploadTimestamp.AddVersion(remote)
|
|
|
|
} else {
|
|
|
|
last = remote
|
|
|
|
}
|
|
|
|
// hide objects represent deleted files which we don't list
|
|
|
|
if object.Action == "hide" {
|
|
|
|
return nil
|
|
|
|
}
|
2016-06-26 04:23:20 +08:00
|
|
|
o, err := f.newObjectWithInfo(remote, object)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if out.Add(o) {
|
|
|
|
return fs.ErrorListAborted
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
2016-04-22 03:06:21 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
out.SetError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// listBuckets returns all the buckets to out
|
2016-04-24 04:46:52 +08:00
|
|
|
func (f *Fs) listBuckets(out fs.ListOpts, dir string) {
|
2016-04-22 03:06:21 +08:00
|
|
|
defer out.Finished()
|
2016-04-24 04:46:52 +08:00
|
|
|
if dir != "" {
|
|
|
|
out.SetError(fs.ErrorListOnlyRoot)
|
|
|
|
return
|
|
|
|
}
|
2016-04-22 03:06:21 +08:00
|
|
|
err := f.listBucketsToFn(func(bucket *api.Bucket) error {
|
|
|
|
dir := &fs.Dir{
|
|
|
|
Name: bucket.Name,
|
|
|
|
Bytes: -1,
|
|
|
|
Count: -1,
|
|
|
|
}
|
|
|
|
if out.AddDir(dir) {
|
|
|
|
return fs.ErrorListAborted
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
out.SetError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// List walks the path returning files and directories to out
|
2016-04-24 04:46:52 +08:00
|
|
|
func (f *Fs) List(out fs.ListOpts, dir string) {
|
2016-04-22 03:06:21 +08:00
|
|
|
if f.bucket == "" {
|
2016-04-24 04:46:52 +08:00
|
|
|
f.listBuckets(out, dir)
|
2016-04-22 03:06:21 +08:00
|
|
|
} else {
|
2016-04-24 04:46:52 +08:00
|
|
|
f.listFiles(out, dir)
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
2016-04-22 03:06:21 +08:00
|
|
|
return
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
|
|
|
|
2016-04-22 03:06:21 +08:00
|
|
|
// listBucketFn is called from listBucketsToFn to handle a bucket
|
|
|
|
type listBucketFn func(*api.Bucket) error
|
2015-11-28 02:25:52 +08:00
|
|
|
|
2016-04-22 03:06:21 +08:00
|
|
|
// listBucketsToFn lists the buckets to the function supplied
|
|
|
|
func (f *Fs) listBucketsToFn(fn listBucketFn) error {
|
2015-11-28 02:25:52 +08:00
|
|
|
var account = api.Account{ID: f.info.AccountID}
|
|
|
|
var response api.ListBucketsResponse
|
|
|
|
opts := rest.Opts{
|
|
|
|
Method: "POST",
|
|
|
|
Path: "/b2_list_buckets",
|
|
|
|
}
|
2016-02-24 06:15:20 +08:00
|
|
|
err := f.pacer.Call(func() (bool, error) {
|
|
|
|
resp, err := f.srv.CallJSON(&opts, &account, &response)
|
|
|
|
return f.shouldRetry(resp, err)
|
|
|
|
})
|
2015-11-28 02:25:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for i := range response.Buckets {
|
2016-04-22 03:06:21 +08:00
|
|
|
err = fn(&response.Buckets[i])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getBucketID finds the ID for the current bucket name
|
|
|
|
func (f *Fs) getBucketID() (bucketID string, err error) {
|
|
|
|
f.bucketIDMutex.Lock()
|
|
|
|
defer f.bucketIDMutex.Unlock()
|
|
|
|
if f._bucketID != "" {
|
|
|
|
return f._bucketID, nil
|
|
|
|
}
|
2016-04-22 03:06:21 +08:00
|
|
|
err = f.listBucketsToFn(func(bucket *api.Bucket) error {
|
2015-11-28 02:25:52 +08:00
|
|
|
if bucket.Name == f.bucket {
|
|
|
|
bucketID = bucket.ID
|
|
|
|
}
|
2016-04-22 03:06:21 +08:00
|
|
|
return nil
|
|
|
|
|
2015-11-28 02:25:52 +08:00
|
|
|
})
|
|
|
|
if bucketID == "" {
|
2016-06-12 22:06:02 +08:00
|
|
|
err = fs.ErrorDirNotFound
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
|
|
|
f._bucketID = bucketID
|
|
|
|
return bucketID, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// setBucketID sets the ID for the current bucket name
|
|
|
|
func (f *Fs) setBucketID(ID string) {
|
|
|
|
f.bucketIDMutex.Lock()
|
|
|
|
f._bucketID = ID
|
|
|
|
f.bucketIDMutex.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// clearBucketID clears the ID for the current bucket name
|
|
|
|
func (f *Fs) clearBucketID() {
|
|
|
|
f.bucketIDMutex.Lock()
|
|
|
|
f._bucketID = ""
|
|
|
|
f.bucketIDMutex.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put the object into the bucket
|
|
|
|
//
|
|
|
|
// Copy the reader in to the new object which is returned
|
|
|
|
//
|
|
|
|
// The new object may have been created if an error is returned
|
2016-02-18 19:35:25 +08:00
|
|
|
func (f *Fs) Put(in io.Reader, src fs.ObjectInfo) (fs.Object, error) {
|
2015-11-28 02:25:52 +08:00
|
|
|
// Temporary Object under construction
|
|
|
|
fs := &Object{
|
|
|
|
fs: f,
|
2016-02-18 19:35:25 +08:00
|
|
|
remote: src.Remote(),
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
2016-02-18 19:35:25 +08:00
|
|
|
return fs, fs.Update(in, src)
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mkdir creates the bucket if it doesn't exist
|
2016-11-26 05:52:43 +08:00
|
|
|
func (f *Fs) Mkdir(dir string) error {
|
|
|
|
// Can't create subdirs
|
|
|
|
if dir != "" {
|
|
|
|
return nil
|
|
|
|
}
|
2015-11-28 02:25:52 +08:00
|
|
|
opts := rest.Opts{
|
|
|
|
Method: "POST",
|
|
|
|
Path: "/b2_create_bucket",
|
|
|
|
}
|
|
|
|
var request = api.CreateBucketRequest{
|
|
|
|
AccountID: f.info.AccountID,
|
|
|
|
Name: f.bucket,
|
|
|
|
Type: "allPrivate",
|
|
|
|
}
|
|
|
|
var response api.Bucket
|
2016-02-24 06:15:20 +08:00
|
|
|
err := f.pacer.Call(func() (bool, error) {
|
|
|
|
resp, err := f.srv.CallJSON(&opts, &request, &response)
|
|
|
|
return f.shouldRetry(resp, err)
|
|
|
|
})
|
2015-11-28 02:25:52 +08:00
|
|
|
if err != nil {
|
|
|
|
if apiErr, ok := err.(*api.Error); ok {
|
|
|
|
if apiErr.Code == "duplicate_bucket_name" {
|
2016-08-26 04:43:43 +08:00
|
|
|
// Check this is our bucket - buckets are globally unique and this
|
|
|
|
// might be someone elses.
|
|
|
|
_, getBucketErr := f.getBucketID()
|
|
|
|
if getBucketErr == nil {
|
|
|
|
// found so it is our bucket
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if getBucketErr != fs.ErrorDirNotFound {
|
|
|
|
fs.Debug(f, "Error checking bucket exists: %v", getBucketErr)
|
|
|
|
}
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
|
|
|
}
|
2016-06-12 22:06:02 +08:00
|
|
|
return errors.Wrap(err, "failed to create bucket")
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
|
|
|
f.setBucketID(response.ID)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rmdir deletes the bucket if the fs is at the root
|
|
|
|
//
|
|
|
|
// Returns an error if it isn't empty
|
2016-11-26 05:52:43 +08:00
|
|
|
func (f *Fs) Rmdir(dir string) error {
|
|
|
|
if f.root != "" || dir != "" {
|
2015-11-28 02:25:52 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
opts := rest.Opts{
|
|
|
|
Method: "POST",
|
|
|
|
Path: "/b2_delete_bucket",
|
|
|
|
}
|
|
|
|
bucketID, err := f.getBucketID()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var request = api.DeleteBucketRequest{
|
|
|
|
ID: bucketID,
|
|
|
|
AccountID: f.info.AccountID,
|
|
|
|
}
|
|
|
|
var response api.Bucket
|
2016-02-24 06:15:20 +08:00
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
|
|
|
resp, err := f.srv.CallJSON(&opts, &request, &response)
|
|
|
|
return f.shouldRetry(resp, err)
|
|
|
|
})
|
2015-11-28 02:25:52 +08:00
|
|
|
if err != nil {
|
2016-06-12 22:06:02 +08:00
|
|
|
return errors.Wrap(err, "failed to delete bucket")
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
|
|
|
f.clearBucketID()
|
|
|
|
f.clearUploadURL()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Precision of the remote
|
|
|
|
func (f *Fs) Precision() time.Duration {
|
2016-03-24 23:23:27 +08:00
|
|
|
return time.Millisecond
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// deleteByID deletes a file version given Name and ID
|
|
|
|
func (f *Fs) deleteByID(ID, Name string) error {
|
|
|
|
opts := rest.Opts{
|
|
|
|
Method: "POST",
|
|
|
|
Path: "/b2_delete_file_version",
|
|
|
|
}
|
|
|
|
var request = api.DeleteFileRequest{
|
|
|
|
ID: ID,
|
|
|
|
Name: Name,
|
|
|
|
}
|
|
|
|
var response api.File
|
2016-02-24 06:15:20 +08:00
|
|
|
err := f.pacer.Call(func() (bool, error) {
|
|
|
|
resp, err := f.srv.CallJSON(&opts, &request, &response)
|
|
|
|
return f.shouldRetry(resp, err)
|
|
|
|
})
|
2015-11-28 02:25:52 +08:00
|
|
|
if err != nil {
|
2016-06-12 22:06:02 +08:00
|
|
|
return errors.Wrapf(err, "failed to delete %q", Name)
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-03 00:03:08 +08:00
|
|
|
// purge deletes all the files and directories
|
|
|
|
//
|
|
|
|
// if oldOnly is true then it deletes only non current files.
|
2015-11-28 02:25:52 +08:00
|
|
|
//
|
|
|
|
// Implemented here so we can make sure we delete old versions.
|
2016-07-03 00:03:08 +08:00
|
|
|
func (f *Fs) purge(oldOnly bool) error {
|
2015-11-28 02:25:52 +08:00
|
|
|
var errReturn error
|
|
|
|
var checkErrMutex sync.Mutex
|
|
|
|
var checkErr = func(err error) {
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
checkErrMutex.Lock()
|
|
|
|
defer checkErrMutex.Unlock()
|
|
|
|
if errReturn == nil {
|
|
|
|
errReturn = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete Config.Transfers in parallel
|
|
|
|
toBeDeleted := make(chan *api.File, fs.Config.Transfers)
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(fs.Config.Transfers)
|
|
|
|
for i := 0; i < fs.Config.Transfers; i++ {
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
for object := range toBeDeleted {
|
2016-09-13 01:15:58 +08:00
|
|
|
fs.Stats.Checking(object.Name)
|
2015-11-28 02:25:52 +08:00
|
|
|
checkErr(f.deleteByID(object.ID, object.Name))
|
2016-09-13 01:15:58 +08:00
|
|
|
fs.Stats.DoneChecking(object.Name)
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2016-07-03 00:03:08 +08:00
|
|
|
last := ""
|
2016-04-24 04:46:52 +08:00
|
|
|
checkErr(f.list("", fs.MaxLevel, "", 0, true, func(remote string, object *api.File, isDirectory bool) error {
|
2016-04-22 03:06:21 +08:00
|
|
|
if !isDirectory {
|
2016-07-03 00:03:08 +08:00
|
|
|
fs.Stats.Checking(remote)
|
|
|
|
if oldOnly && last != remote {
|
2016-08-19 01:36:00 +08:00
|
|
|
if object.Action == "hide" {
|
|
|
|
fs.Debug(remote, "Deleting current version (id %q) as it is a hide marker", object.ID)
|
|
|
|
toBeDeleted <- object
|
|
|
|
} else {
|
|
|
|
fs.Debug(remote, "Not deleting current version (id %q) %q", object.ID, object.Action)
|
|
|
|
}
|
2016-07-03 00:03:08 +08:00
|
|
|
} else {
|
|
|
|
fs.Debug(remote, "Deleting (id %q)", object.ID)
|
|
|
|
toBeDeleted <- object
|
|
|
|
}
|
|
|
|
last = remote
|
|
|
|
fs.Stats.DoneChecking(remote)
|
2016-04-22 03:06:21 +08:00
|
|
|
}
|
2015-11-28 02:25:52 +08:00
|
|
|
return nil
|
|
|
|
}))
|
|
|
|
close(toBeDeleted)
|
|
|
|
wg.Wait()
|
|
|
|
|
2016-07-03 00:03:08 +08:00
|
|
|
if !oldOnly {
|
2016-11-26 05:52:43 +08:00
|
|
|
checkErr(f.Rmdir(""))
|
2016-07-03 00:03:08 +08:00
|
|
|
}
|
2015-11-28 02:25:52 +08:00
|
|
|
return errReturn
|
|
|
|
}
|
|
|
|
|
2016-07-03 00:03:08 +08:00
|
|
|
// Purge deletes all the files and directories including the old versions.
|
|
|
|
func (f *Fs) Purge() error {
|
|
|
|
return f.purge(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CleanUp deletes all the hidden files.
|
|
|
|
func (f *Fs) CleanUp() error {
|
|
|
|
return f.purge(true)
|
|
|
|
}
|
|
|
|
|
2016-01-11 20:39:33 +08:00
|
|
|
// Hashes returns the supported hash sets.
|
|
|
|
func (f *Fs) Hashes() fs.HashSet {
|
|
|
|
return fs.HashSet(fs.HashSHA1)
|
|
|
|
}
|
|
|
|
|
2015-11-28 02:25:52 +08:00
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
|
|
|
// Fs returns the parent Fs
|
2016-02-18 19:35:25 +08:00
|
|
|
func (o *Object) Fs() fs.Info {
|
2015-11-28 02:25:52 +08: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
|
|
|
|
}
|
|
|
|
|
2016-01-11 20:39:33 +08:00
|
|
|
// Hash returns the Sha-1 of an object returning a lowercase hex string
|
|
|
|
func (o *Object) Hash(t fs.HashType) (string, error) {
|
|
|
|
if t != fs.HashSHA1 {
|
|
|
|
return "", fs.ErrHashUnsupported
|
|
|
|
}
|
2016-01-19 16:20:23 +08:00
|
|
|
if o.sha1 == "" {
|
2016-03-22 22:39:56 +08:00
|
|
|
// Error is logged in readMetaData
|
|
|
|
err := o.readMetaData()
|
2016-01-19 16:20:23 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2016-01-19 01:53:03 +08:00
|
|
|
}
|
2016-01-11 20:39:33 +08:00
|
|
|
return o.sha1, nil
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns the size of an object in bytes
|
|
|
|
func (o *Object) Size() int64 {
|
2016-03-22 22:39:56 +08:00
|
|
|
return o.size
|
|
|
|
}
|
|
|
|
|
2016-06-16 01:49:11 +08:00
|
|
|
// decodeMetaDataRaw sets the metadata from the data passed in
|
2016-03-22 22:39:56 +08:00
|
|
|
//
|
|
|
|
// Sets
|
|
|
|
// o.id
|
|
|
|
// o.modTime
|
|
|
|
// o.size
|
|
|
|
// o.sha1
|
2016-09-22 05:13:24 +08:00
|
|
|
func (o *Object) decodeMetaDataRaw(ID, SHA1 string, Size int64, UploadTimestamp api.Timestamp, Info map[string]string, mimeType string) (err error) {
|
2016-06-16 01:49:11 +08:00
|
|
|
o.id = ID
|
|
|
|
o.sha1 = SHA1
|
2016-09-22 05:13:24 +08:00
|
|
|
o.mimeType = mimeType
|
2016-06-16 01:49:11 +08:00
|
|
|
// Read SHA1 from metadata if it exists and isn't set
|
|
|
|
if o.sha1 == "" || o.sha1 == "none" {
|
|
|
|
o.sha1 = Info[sha1Key]
|
|
|
|
}
|
|
|
|
o.size = Size
|
2016-03-22 22:39:56 +08:00
|
|
|
// Use the UploadTimestamp if can't get file info
|
2016-06-16 01:49:11 +08:00
|
|
|
o.modTime = time.Time(UploadTimestamp)
|
|
|
|
return o.parseTimeString(Info[timeKey])
|
|
|
|
}
|
|
|
|
|
|
|
|
// decodeMetaData sets the metadata in the object from an api.File
|
|
|
|
//
|
|
|
|
// Sets
|
|
|
|
// o.id
|
|
|
|
// o.modTime
|
|
|
|
// o.size
|
|
|
|
// o.sha1
|
|
|
|
func (o *Object) decodeMetaData(info *api.File) (err error) {
|
2016-09-22 05:13:24 +08:00
|
|
|
return o.decodeMetaDataRaw(info.ID, info.SHA1, info.Size, info.UploadTimestamp, info.Info, info.ContentType)
|
2016-06-16 01:49:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// decodeMetaDataFileInfo sets the metadata in the object from an api.FileInfo
|
|
|
|
//
|
|
|
|
// Sets
|
|
|
|
// o.id
|
|
|
|
// o.modTime
|
|
|
|
// o.size
|
|
|
|
// o.sha1
|
|
|
|
func (o *Object) decodeMetaDataFileInfo(info *api.FileInfo) (err error) {
|
2016-09-22 05:13:24 +08:00
|
|
|
return o.decodeMetaDataRaw(info.ID, info.SHA1, info.Size, info.UploadTimestamp, info.Info, info.ContentType)
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// readMetaData gets the metadata if it hasn't already been fetched
|
|
|
|
//
|
2016-03-22 22:39:56 +08:00
|
|
|
// Sets
|
|
|
|
// o.id
|
|
|
|
// o.modTime
|
|
|
|
// o.size
|
|
|
|
// o.sha1
|
2015-11-28 02:25:52 +08:00
|
|
|
func (o *Object) readMetaData() (err error) {
|
2016-03-22 22:39:56 +08:00
|
|
|
if o.id != "" {
|
2015-11-28 02:25:52 +08:00
|
|
|
return nil
|
|
|
|
}
|
2016-07-05 18:26:02 +08:00
|
|
|
maxSearched := 1
|
|
|
|
var timestamp api.Timestamp
|
|
|
|
baseRemote := o.remote
|
|
|
|
if *b2Versions {
|
|
|
|
timestamp, baseRemote = api.RemoveVersion(baseRemote)
|
|
|
|
maxSearched = maxVersions
|
|
|
|
}
|
2016-03-22 22:39:56 +08:00
|
|
|
var info *api.File
|
2016-07-05 18:26:02 +08:00
|
|
|
err = o.fs.list("", fs.MaxLevel, baseRemote, maxSearched, *b2Versions, func(remote string, object *api.File, isDirectory bool) error {
|
2016-04-22 03:06:21 +08:00
|
|
|
if isDirectory {
|
|
|
|
return nil
|
|
|
|
}
|
2016-07-05 18:26:02 +08:00
|
|
|
if remote == baseRemote {
|
|
|
|
if !timestamp.IsZero() && !timestamp.Equal(object.UploadTimestamp) {
|
|
|
|
return nil
|
|
|
|
}
|
2016-03-22 22:39:56 +08:00
|
|
|
info = object
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
2016-02-19 20:09:11 +08:00
|
|
|
return errEndList // read only 1 item
|
2015-11-28 02:25:52 +08:00
|
|
|
})
|
2016-03-22 22:39:56 +08:00
|
|
|
if err != nil {
|
2016-06-26 04:23:20 +08:00
|
|
|
if err == fs.ErrorDirNotFound {
|
|
|
|
return fs.ErrorObjectNotFound
|
|
|
|
}
|
2016-03-22 22:39:56 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if info == nil {
|
2016-06-26 04:23:20 +08:00
|
|
|
return fs.ErrorObjectNotFound
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
2016-03-22 22:39:56 +08:00
|
|
|
return o.decodeMetaData(info)
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// timeString returns modTime as the number of milliseconds
|
|
|
|
// elapsed since January 1, 1970 UTC as a decimal string.
|
|
|
|
func timeString(modTime time.Time) string {
|
|
|
|
return strconv.FormatInt(modTime.UnixNano()/1E6, 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseTimeString converts a decimal string number of milliseconds
|
2016-03-22 18:26:37 +08:00
|
|
|
// elapsed since January 1, 1970 UTC into a time.Time and stores it in
|
|
|
|
// the modTime variable.
|
|
|
|
func (o *Object) parseTimeString(timeString string) (err error) {
|
2015-11-28 02:25:52 +08:00
|
|
|
if timeString == "" {
|
2016-03-22 18:26:37 +08:00
|
|
|
return nil
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
|
|
|
unixMilliseconds, err := strconv.ParseInt(timeString, 10, 64)
|
|
|
|
if err != nil {
|
2016-03-22 18:26:37 +08:00
|
|
|
fs.Debug(o, "Failed to parse mod time string %q: %v", timeString, err)
|
|
|
|
return err
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
2016-03-22 18:26:37 +08:00
|
|
|
o.modTime = time.Unix(unixMilliseconds/1E3, (unixMilliseconds%1E3)*1E6).UTC()
|
|
|
|
return nil
|
2016-01-11 20:39:33 +08:00
|
|
|
}
|
|
|
|
|
2016-03-22 18:26:37 +08: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
|
|
|
|
//
|
|
|
|
// SHA-1 will also be updated once the request has completed.
|
|
|
|
func (o *Object) ModTime() (result time.Time) {
|
2016-03-22 22:39:56 +08:00
|
|
|
// The error is logged in readMetaData
|
|
|
|
_ = o.readMetaData()
|
2016-03-22 18:26:37 +08:00
|
|
|
return o.modTime
|
|
|
|
}
|
|
|
|
|
2015-11-28 02:25:52 +08:00
|
|
|
// SetModTime sets the modification time of the local fs object
|
2016-03-22 23:07:10 +08:00
|
|
|
func (o *Object) SetModTime(modTime time.Time) error {
|
2015-11-28 02:25:52 +08:00
|
|
|
// Not possible with B2
|
2016-03-22 23:07:10 +08:00
|
|
|
return fs.ErrorCantSetModTime
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Storable returns if this object is storable
|
|
|
|
func (o *Object) Storable() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// openFile represents an Object open for reading
|
|
|
|
type openFile struct {
|
|
|
|
o *Object // Object we are reading for
|
|
|
|
resp *http.Response // response of the GET
|
|
|
|
body io.Reader // reading from here
|
|
|
|
hash hash.Hash // currently accumulating SHA1
|
|
|
|
bytes int64 // number of bytes read on this connection
|
|
|
|
eof bool // whether we have read end of file
|
|
|
|
}
|
|
|
|
|
|
|
|
// newOpenFile wraps an io.ReadCloser and checks the sha1sum
|
|
|
|
func newOpenFile(o *Object, resp *http.Response) *openFile {
|
|
|
|
file := &openFile{
|
|
|
|
o: o,
|
|
|
|
resp: resp,
|
|
|
|
hash: sha1.New(),
|
|
|
|
}
|
|
|
|
file.body = io.TeeReader(resp.Body, file.hash)
|
|
|
|
return file
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read bytes from the object - see io.Reader
|
|
|
|
func (file *openFile) Read(p []byte) (n int, err error) {
|
|
|
|
n, err = file.body.Read(p)
|
|
|
|
file.bytes += int64(n)
|
|
|
|
if err == io.EOF {
|
|
|
|
file.eof = true
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the object and checks the length and SHA1 if all the object
|
|
|
|
// was read
|
|
|
|
func (file *openFile) Close() (err error) {
|
|
|
|
// Close the body at the end
|
|
|
|
defer fs.CheckClose(file.resp.Body, &err)
|
|
|
|
|
|
|
|
// If not end of file then can't check SHA1
|
|
|
|
if !file.eof {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check to see we read the correct number of bytes
|
|
|
|
if file.o.Size() != file.bytes {
|
2016-06-12 22:06:02 +08:00
|
|
|
return errors.Errorf("object corrupted on transfer - length mismatch (want %d got %d)", file.o.Size(), file.bytes)
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the SHA1
|
2016-09-06 00:26:04 +08:00
|
|
|
receivedSHA1 := file.o.sha1
|
2015-11-28 02:25:52 +08:00
|
|
|
calculatedSHA1 := fmt.Sprintf("%x", file.hash.Sum(nil))
|
|
|
|
if receivedSHA1 != calculatedSHA1 {
|
2016-06-12 22:06:02 +08:00
|
|
|
return errors.Errorf("object corrupted on transfer - SHA1 mismatch (want %q got %q)", receivedSHA1, calculatedSHA1)
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check it satisfies the interfaces
|
|
|
|
var _ io.ReadCloser = &openFile{}
|
|
|
|
|
|
|
|
// Open an object for read
|
2016-09-10 18:29:57 +08:00
|
|
|
func (o *Object) Open(options ...fs.OpenOption) (in io.ReadCloser, err error) {
|
2015-11-28 02:25:52 +08:00
|
|
|
opts := rest.Opts{
|
|
|
|
Method: "GET",
|
|
|
|
Absolute: true,
|
2016-07-05 18:26:02 +08:00
|
|
|
Path: o.fs.info.DownloadURL,
|
2016-09-10 18:29:57 +08:00
|
|
|
Options: options,
|
2016-07-05 18:26:02 +08:00
|
|
|
}
|
|
|
|
// Download by id if set otherwise by name
|
|
|
|
if o.id != "" {
|
|
|
|
opts.Path += "/b2api/v1/b2_download_file_by_id?fileId=" + urlEncode(o.id)
|
|
|
|
} else {
|
|
|
|
opts.Path += "/file/" + urlEncode(o.fs.bucket) + "/" + urlEncode(o.fs.root+o.remote)
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
2016-02-24 06:15:20 +08:00
|
|
|
var resp *http.Response
|
|
|
|
err = o.fs.pacer.Call(func() (bool, error) {
|
|
|
|
resp, err = o.fs.srv.Call(&opts)
|
|
|
|
return o.fs.shouldRetry(resp, err)
|
|
|
|
})
|
2015-11-28 02:25:52 +08:00
|
|
|
if err != nil {
|
2016-06-12 22:06:02 +08:00
|
|
|
return nil, errors.Wrap(err, "failed to open for download")
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the time out of the headers if possible
|
2016-03-22 18:26:37 +08:00
|
|
|
err = o.parseTimeString(resp.Header.Get(timeHeader))
|
2015-11-28 02:25:52 +08:00
|
|
|
if err != nil {
|
2016-03-22 18:26:37 +08:00
|
|
|
_ = resp.Body.Close()
|
|
|
|
return nil, err
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
2016-09-06 00:26:04 +08:00
|
|
|
// Read sha1 from header if it isn't set
|
2016-01-11 20:39:33 +08:00
|
|
|
if o.sha1 == "" {
|
|
|
|
o.sha1 = resp.Header.Get(sha1Header)
|
2016-09-06 00:26:04 +08:00
|
|
|
fs.Debug(o, "Reading sha1 from header - %q", o.sha1)
|
|
|
|
// if sha1 header is "none" (in big files), then need
|
|
|
|
// to read it from the metadata
|
|
|
|
if o.sha1 == "none" {
|
|
|
|
o.sha1 = resp.Header.Get(sha1InfoHeader)
|
|
|
|
fs.Debug(o, "Reading sha1 from info - %q", o.sha1)
|
|
|
|
}
|
2016-01-11 20:39:33 +08:00
|
|
|
}
|
2016-10-07 19:16:25 +08:00
|
|
|
// Don't check length or hash on partial content
|
|
|
|
if resp.StatusCode == http.StatusPartialContent {
|
|
|
|
return resp.Body, nil
|
|
|
|
}
|
2015-11-28 02:25:52 +08:00
|
|
|
return newOpenFile(o, resp), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// dontEncode is the characters that do not need percent-encoding
|
|
|
|
//
|
|
|
|
// The characters that do not need percent-encoding are a subset of
|
|
|
|
// the printable ASCII characters: upper-case letters, lower-case
|
|
|
|
// letters, digits, ".", "_", "-", "/", "~", "!", "$", "'", "(", ")",
|
|
|
|
// "*", ";", "=", ":", and "@". All other byte values in a UTF-8 must
|
|
|
|
// be replaced with "%" and the two-digit hex value of the byte.
|
|
|
|
const dontEncode = (`abcdefghijklmnopqrstuvwxyz` +
|
|
|
|
`ABCDEFGHIJKLMNOPQRSTUVWXYZ` +
|
|
|
|
`0123456789` +
|
|
|
|
`._-/~!$'()*;=:@`)
|
|
|
|
|
|
|
|
// noNeedToEncode is a bitmap of characters which don't need % encoding
|
|
|
|
var noNeedToEncode [256]bool
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
for _, c := range dontEncode {
|
|
|
|
noNeedToEncode[c] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// urlEncode encodes in with % encoding
|
|
|
|
func urlEncode(in string) string {
|
|
|
|
var out bytes.Buffer
|
|
|
|
for i := 0; i < len(in); i++ {
|
|
|
|
c := in[i]
|
|
|
|
if noNeedToEncode[c] {
|
|
|
|
_ = out.WriteByte(c)
|
|
|
|
} else {
|
|
|
|
_, _ = out.WriteString(fmt.Sprintf("%%%2X", c))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2016-02-18 19:35:25 +08:00
|
|
|
func (o *Object) Update(in io.Reader, src fs.ObjectInfo) (err error) {
|
2016-07-05 18:26:02 +08:00
|
|
|
if *b2Versions {
|
|
|
|
return errNotWithVersions
|
|
|
|
}
|
2016-02-18 19:35:25 +08:00
|
|
|
size := src.Size()
|
2016-06-16 01:49:11 +08:00
|
|
|
|
|
|
|
// If a large file upload in chunks - see upload.go
|
|
|
|
if size >= int64(uploadCutoff) {
|
|
|
|
up, err := o.fs.newLargeUpload(o, in, src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return up.Upload()
|
|
|
|
}
|
|
|
|
|
2016-02-18 19:35:25 +08:00
|
|
|
modTime := src.ModTime()
|
2016-02-19 22:45:32 +08:00
|
|
|
calculatedSha1, _ := src.Hash(fs.HashSHA1)
|
|
|
|
|
|
|
|
// If source cannot provide the hash, copy to a temporary file
|
|
|
|
// and calculate the hash while doing so.
|
|
|
|
// Then we serve the temporary file.
|
|
|
|
if calculatedSha1 == "" {
|
|
|
|
// Open a temp file to copy the input
|
|
|
|
fd, err := ioutil.TempFile("", "rclone-b2-")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_ = os.Remove(fd.Name()) // Delete the file - may not work on Windows
|
|
|
|
defer func() {
|
|
|
|
_ = fd.Close() // Ignore error may have been closed already
|
|
|
|
_ = os.Remove(fd.Name()) // Delete the file - may have been deleted already
|
|
|
|
}()
|
2016-02-18 19:35:25 +08:00
|
|
|
|
2016-02-19 22:45:32 +08:00
|
|
|
// Copy the input while calculating the sha1
|
|
|
|
hash := sha1.New()
|
|
|
|
teed := io.TeeReader(in, hash)
|
|
|
|
n, err := io.Copy(fd, teed)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if n != size {
|
2016-06-12 22:06:02 +08:00
|
|
|
return errors.Errorf("read %d bytes expecting %d", n, size)
|
2016-02-19 22:45:32 +08:00
|
|
|
}
|
|
|
|
calculatedSha1 = fmt.Sprintf("%x", hash.Sum(nil))
|
2015-11-28 02:25:52 +08:00
|
|
|
|
2016-02-19 22:45:32 +08:00
|
|
|
// Rewind the temporary file
|
|
|
|
_, err = fd.Seek(0, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Set input to temporary file
|
|
|
|
in = fd
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get upload URL
|
2016-02-27 21:00:35 +08:00
|
|
|
upload, err := o.fs.getUploadURL()
|
2015-11-28 02:25:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-18 01:34:21 +08:00
|
|
|
defer func() {
|
|
|
|
// return it like this because we might nil it out
|
|
|
|
o.fs.returnUploadURL(upload)
|
|
|
|
}()
|
2015-11-28 02:25:52 +08:00
|
|
|
|
|
|
|
// Headers for upload file
|
|
|
|
//
|
|
|
|
// Authorization
|
|
|
|
// required
|
|
|
|
// An upload authorization token, from b2_get_upload_url.
|
|
|
|
//
|
|
|
|
// X-Bz-File-Name
|
|
|
|
// required
|
|
|
|
//
|
|
|
|
// The name of the file, in percent-encoded UTF-8. See Files for requirements on file names. See String Encoding.
|
|
|
|
//
|
|
|
|
// Content-Type
|
|
|
|
// required
|
|
|
|
//
|
|
|
|
// The MIME type of the content of the file, which will be returned in
|
|
|
|
// the Content-Type header when downloading the file. Use the
|
|
|
|
// Content-Type b2/x-auto to automatically set the stored Content-Type
|
|
|
|
// post upload. In the case where a file extension is absent or the
|
|
|
|
// lookup fails, the Content-Type is set to application/octet-stream. The
|
|
|
|
// Content-Type mappings can be purused here.
|
|
|
|
//
|
|
|
|
// X-Bz-Content-Sha1
|
|
|
|
// required
|
|
|
|
//
|
|
|
|
// The SHA1 checksum of the content of the file. B2 will check this when
|
|
|
|
// the file is uploaded, to make sure that the file arrived correctly. It
|
|
|
|
// will be returned in the X-Bz-Content-Sha1 header when the file is
|
|
|
|
// downloaded.
|
|
|
|
//
|
|
|
|
// X-Bz-Info-src_last_modified_millis
|
|
|
|
// optional
|
|
|
|
//
|
|
|
|
// If the original source of the file being uploaded has a last modified
|
|
|
|
// time concept, Backblaze recommends using this spelling of one of your
|
|
|
|
// ten X-Bz-Info-* headers (see below). Using a standard spelling allows
|
|
|
|
// different B2 clients and the B2 web user interface to interoperate
|
|
|
|
// correctly. The value should be a base 10 number which represents a UTC
|
|
|
|
// time when the original source file was last modified. It is a base 10
|
|
|
|
// number of milliseconds since midnight, January 1, 1970 UTC. This fits
|
|
|
|
// in a 64 bit integer such as the type "long" in the programming
|
|
|
|
// language Java. It is intended to be compatible with Java's time
|
|
|
|
// long. For example, it can be passed directly into the Java call
|
|
|
|
// Date.setTime(long time).
|
|
|
|
//
|
|
|
|
// X-Bz-Info-*
|
|
|
|
// optional
|
|
|
|
//
|
|
|
|
// Up to 10 of these headers may be present. The * part of the header
|
|
|
|
// name is replace with the name of a custom field in the file
|
|
|
|
// information stored with the file, and the value is an arbitrary UTF-8
|
|
|
|
// string, percent-encoded. The same info headers sent with the upload
|
|
|
|
// will be returned with the download.
|
|
|
|
|
|
|
|
opts := rest.Opts{
|
|
|
|
Method: "POST",
|
|
|
|
Absolute: true,
|
2016-02-27 21:00:35 +08:00
|
|
|
Path: upload.UploadURL,
|
2016-02-19 22:45:32 +08:00
|
|
|
Body: in,
|
2015-11-28 02:25:52 +08:00
|
|
|
ExtraHeaders: map[string]string{
|
2016-02-27 21:00:35 +08:00
|
|
|
"Authorization": upload.AuthorizationToken,
|
2015-11-28 02:25:52 +08:00
|
|
|
"X-Bz-File-Name": urlEncode(o.fs.root + o.remote),
|
2016-09-22 05:13:24 +08:00
|
|
|
"Content-Type": fs.MimeType(src),
|
2015-11-28 02:25:52 +08:00
|
|
|
sha1Header: calculatedSha1,
|
|
|
|
timeHeader: timeString(modTime),
|
|
|
|
},
|
|
|
|
ContentLength: &size,
|
|
|
|
}
|
|
|
|
var response api.FileInfo
|
2016-02-24 06:15:20 +08:00
|
|
|
// Don't retry, return a retry error instead
|
|
|
|
err = o.fs.pacer.CallNoRetry(func() (bool, error) {
|
|
|
|
resp, err := o.fs.srv.CallJSON(&opts, nil, &response)
|
2016-11-07 21:30:51 +08:00
|
|
|
retry, err := o.fs.shouldRetry(resp, err)
|
2016-07-01 23:23:23 +08:00
|
|
|
// On retryable error clear UploadURL
|
|
|
|
if retry {
|
|
|
|
fs.Debug(o, "Clearing upload URL because of error: %v", err)
|
2016-06-16 01:49:11 +08:00
|
|
|
upload = nil
|
|
|
|
}
|
2016-07-01 23:23:23 +08:00
|
|
|
return retry, err
|
2016-02-24 06:15:20 +08:00
|
|
|
})
|
2015-11-28 02:25:52 +08:00
|
|
|
if err != nil {
|
2016-02-24 06:15:20 +08:00
|
|
|
return err
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
2016-06-16 01:49:11 +08:00
|
|
|
return o.decodeMetaDataFileInfo(&response)
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove an object
|
|
|
|
func (o *Object) Remove() error {
|
2016-07-05 18:26:02 +08:00
|
|
|
if *b2Versions {
|
|
|
|
return errNotWithVersions
|
|
|
|
}
|
2015-11-28 02:25:52 +08:00
|
|
|
bucketID, err := o.fs.getBucketID()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
opts := rest.Opts{
|
|
|
|
Method: "POST",
|
|
|
|
Path: "/b2_hide_file",
|
|
|
|
}
|
|
|
|
var request = api.HideFileRequest{
|
|
|
|
BucketID: bucketID,
|
2016-02-24 06:15:20 +08:00
|
|
|
Name: o.fs.root + o.remote,
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
|
|
|
var response api.File
|
2016-02-24 06:15:20 +08:00
|
|
|
err = o.fs.pacer.Call(func() (bool, error) {
|
|
|
|
resp, err := o.fs.srv.CallJSON(&opts, &request, &response)
|
|
|
|
return o.fs.shouldRetry(resp, err)
|
|
|
|
})
|
2015-11-28 02:25:52 +08:00
|
|
|
if err != nil {
|
2016-06-12 22:06:02 +08:00
|
|
|
return errors.Wrap(err, "failed to delete file")
|
2015-11-28 02:25:52 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-22 05:13:24 +08:00
|
|
|
// MimeType of an Object if known, "" otherwise
|
|
|
|
func (o *Object) MimeType() string {
|
|
|
|
return o.mimeType
|
|
|
|
}
|
|
|
|
|
2015-11-28 02:25:52 +08:00
|
|
|
// Check the interfaces are satisfied
|
|
|
|
var (
|
2016-07-03 00:03:08 +08:00
|
|
|
_ fs.Fs = &Fs{}
|
|
|
|
_ fs.Purger = &Fs{}
|
|
|
|
_ fs.CleanUpper = &Fs{}
|
|
|
|
_ fs.Object = &Object{}
|
2016-09-22 05:13:24 +08:00
|
|
|
_ fs.MimeTyper = &Object{}
|
2015-11-28 02:25:52 +08:00
|
|
|
)
|