2019-07-06 07:16:57 +08:00
|
|
|
// Package premiumizeme provides an interface to the premiumize.me
|
|
|
|
// object storage system.
|
|
|
|
package premiumizeme
|
|
|
|
|
2019-10-02 16:54:36 +08:00
|
|
|
/*
|
2019-07-06 07:16:57 +08:00
|
|
|
Run of rclone info
|
|
|
|
stringNeedsEscaping = []rune{
|
|
|
|
0x00, 0x0A, 0x0D, 0x22, 0x2F, 0x5C, 0xBF, 0xFE
|
|
|
|
0x00, 0x0A, 0x0D, '"', '/', '\\', 0xBF, 0xFE
|
|
|
|
}
|
|
|
|
maxFileLength = 255
|
|
|
|
canWriteUnnormalized = true
|
|
|
|
canReadUnnormalized = true
|
|
|
|
canReadRenormalized = false
|
|
|
|
canStream = false
|
|
|
|
*/
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2021-11-04 18:12:57 +08:00
|
|
|
"errors"
|
2019-07-06 07:16:57 +08:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/rclone/rclone/backend/premiumizeme/api"
|
|
|
|
"github.com/rclone/rclone/fs"
|
2020-01-15 01:33:35 +08:00
|
|
|
"github.com/rclone/rclone/fs/config"
|
2019-07-06 07:16:57 +08:00
|
|
|
"github.com/rclone/rclone/fs/config/configmap"
|
|
|
|
"github.com/rclone/rclone/fs/config/configstruct"
|
|
|
|
"github.com/rclone/rclone/fs/config/obscure"
|
|
|
|
"github.com/rclone/rclone/fs/fserrors"
|
|
|
|
"github.com/rclone/rclone/fs/fshttp"
|
|
|
|
"github.com/rclone/rclone/fs/hash"
|
|
|
|
"github.com/rclone/rclone/lib/dircache"
|
2020-01-15 01:33:35 +08:00
|
|
|
"github.com/rclone/rclone/lib/encoder"
|
2019-07-06 07:16:57 +08:00
|
|
|
"github.com/rclone/rclone/lib/oauthutil"
|
|
|
|
"github.com/rclone/rclone/lib/pacer"
|
2019-08-25 15:39:31 +08:00
|
|
|
"github.com/rclone/rclone/lib/random"
|
2019-07-06 07:16:57 +08:00
|
|
|
"github.com/rclone/rclone/lib/rest"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
rcloneClientID = "658922194"
|
|
|
|
rcloneEncryptedClientSecret = "B5YIvQoRIhcpAYs8HYeyjb9gK-ftmZEbqdh_gNfc4RgO9Q"
|
|
|
|
minSleep = 10 * time.Millisecond
|
|
|
|
maxSleep = 2 * time.Second
|
|
|
|
decayConstant = 2 // bigger for slower decay, exponential
|
|
|
|
rootID = "0" // ID of root folder is always this
|
|
|
|
rootURL = "https://www.premiumize.me/api"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Globals
|
|
|
|
var (
|
|
|
|
// Description of how to auth for this app
|
2022-07-26 14:28:37 +08:00
|
|
|
oauthConfig = &oauthutil.Config{
|
|
|
|
Scopes: nil,
|
|
|
|
AuthURL: "https://www.premiumize.me/authorize",
|
|
|
|
TokenURL: "https://www.premiumize.me/token",
|
2019-07-06 07:16:57 +08:00
|
|
|
ClientID: rcloneClientID,
|
|
|
|
ClientSecret: obscure.MustReveal(rcloneEncryptedClientSecret),
|
|
|
|
RedirectURL: oauthutil.RedirectURL,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// Register with Fs
|
|
|
|
func init() {
|
|
|
|
fs.Register(&fs.RegInfo{
|
|
|
|
Name: "premiumizeme",
|
|
|
|
Description: "premiumize.me",
|
|
|
|
NewFs: NewFs,
|
2021-04-29 16:28:18 +08:00
|
|
|
Config: func(ctx context.Context, name string, m configmap.Mapper, config fs.ConfigIn) (*fs.ConfigOut, error) {
|
|
|
|
return oauthutil.ConfigOut("", &oauthutil.Options{
|
|
|
|
OAuth2Config: oauthConfig,
|
|
|
|
})
|
2019-07-06 07:16:57 +08:00
|
|
|
},
|
2023-07-07 00:55:53 +08:00
|
|
|
Options: append(oauthutil.SharedOptions, []fs.Option{{
|
2019-07-06 07:16:57 +08:00
|
|
|
Name: "api_key",
|
|
|
|
Help: `API Key.
|
|
|
|
|
|
|
|
This is not normally used - use oauth instead.
|
|
|
|
`,
|
2023-07-07 00:55:53 +08:00
|
|
|
Hide: fs.OptionHideBoth,
|
|
|
|
Default: "",
|
|
|
|
Sensitive: true,
|
2020-01-15 01:33:35 +08:00
|
|
|
}, {
|
|
|
|
Name: config.ConfigEncoding,
|
|
|
|
Help: config.ConfigEncodingHelp,
|
|
|
|
Advanced: true,
|
2020-01-15 05:51:49 +08:00
|
|
|
// Encode invalid UTF-8 bytes as json doesn't handle them properly.
|
|
|
|
Default: (encoder.Display |
|
|
|
|
encoder.EncodeBackSlash |
|
|
|
|
encoder.EncodeDoubleQuote |
|
|
|
|
encoder.EncodeInvalidUtf8),
|
2023-07-07 00:55:53 +08:00
|
|
|
}}...),
|
2019-07-06 07:16:57 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Options defines the configuration for this backend
|
|
|
|
type Options struct {
|
2020-01-15 01:33:35 +08:00
|
|
|
APIKey string `config:"api_key"`
|
|
|
|
Enc encoder.MultiEncoder `config:"encoding"`
|
2019-07-06 07:16:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fs represents a remote cloud storage system
|
|
|
|
type Fs struct {
|
|
|
|
name string // name of this remote
|
|
|
|
root string // the path we are working on
|
|
|
|
opt Options // parsed options
|
|
|
|
features *fs.Features // optional features
|
|
|
|
srv *rest.Client // the connection to the server
|
|
|
|
dirCache *dircache.DirCache // Map of directory path to directory id
|
|
|
|
pacer *fs.Pacer // pacer for API calls
|
|
|
|
tokenRenewer *oauthutil.Renew // renew the token on expiry
|
|
|
|
}
|
|
|
|
|
|
|
|
// Object describes a file
|
|
|
|
type Object struct {
|
|
|
|
fs *Fs // what this object is part of
|
|
|
|
remote string // The remote path
|
|
|
|
hasMetaData bool // metadata is present and correct
|
|
|
|
size int64 // size of the object
|
|
|
|
modTime time.Time // modification time of the object
|
|
|
|
id string // ID of the object
|
|
|
|
parentID string // ID of parent directory
|
|
|
|
mimeType string // Mime type of object
|
|
|
|
url string // URL to download file
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
|
|
|
// 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("premiumize.me root '%s'", f.root)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Features returns the optional features of this Fs
|
|
|
|
func (f *Fs) Features() *fs.Features {
|
|
|
|
return f.features
|
|
|
|
}
|
|
|
|
|
|
|
|
// parsePath parses a premiumize.me '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
|
2021-03-11 22:44:01 +08:00
|
|
|
func shouldRetry(ctx context.Context, resp *http.Response, err error) (bool, error) {
|
|
|
|
if fserrors.ContextError(ctx, &err) {
|
|
|
|
return false, err
|
|
|
|
}
|
2019-07-06 07:16:57 +08:00
|
|
|
return fserrors.ShouldRetry(err) || fserrors.ShouldRetryHTTP(resp, retryErrorCodes), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// readMetaDataForPath reads the metadata from the path
|
|
|
|
func (f *Fs) readMetaDataForPath(ctx context.Context, path string, directoriesOnly bool, filesOnly bool) (info *api.Item, err error) {
|
|
|
|
// defer fs.Trace(f, "path=%q", path)("info=%+v, err=%v", &info, &err)
|
2020-05-12 00:24:37 +08:00
|
|
|
leaf, directoryID, err := f.dirCache.FindPath(ctx, path, false)
|
2019-07-06 07:16:57 +08:00
|
|
|
if err != nil {
|
|
|
|
if err == fs.ErrorDirNotFound {
|
|
|
|
return nil, fs.ErrorObjectNotFound
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
lcLeaf := strings.ToLower(leaf)
|
2021-11-01 03:07:43 +08:00
|
|
|
_, found, err := f.listAll(ctx, directoryID, directoriesOnly, filesOnly, func(item *api.Item) bool {
|
2019-07-06 07:16:57 +08:00
|
|
|
if strings.ToLower(item.Name) == lcLeaf {
|
|
|
|
info = item
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return nil, fs.ErrorObjectNotFound
|
|
|
|
}
|
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// errorHandler parses a non 2xx error response into an error
|
|
|
|
func errorHandler(resp *http.Response) error {
|
|
|
|
body, err := rest.ReadBody(resp)
|
|
|
|
if err != nil {
|
|
|
|
body = nil
|
|
|
|
}
|
|
|
|
var e = api.Response{
|
|
|
|
Message: string(body),
|
|
|
|
Status: fmt.Sprintf("%s (%d)", resp.Status, resp.StatusCode),
|
|
|
|
}
|
|
|
|
if body != nil {
|
|
|
|
_ = json.Unmarshal(body, &e)
|
|
|
|
}
|
|
|
|
return &e
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return a url.Values with the api key in
|
|
|
|
func (f *Fs) baseParams() url.Values {
|
|
|
|
params := url.Values{}
|
|
|
|
if f.opt.APIKey != "" {
|
|
|
|
params.Add("apikey", f.opt.APIKey)
|
|
|
|
}
|
|
|
|
return params
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFs constructs an Fs from the path, container:path
|
2020-11-05 23:18:51 +08:00
|
|
|
func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, error) {
|
2019-07-06 07:16:57 +08:00
|
|
|
// Parse config into Options struct
|
|
|
|
opt := new(Options)
|
|
|
|
err := configstruct.Set(m, opt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root = parsePath(root)
|
|
|
|
|
|
|
|
var client *http.Client
|
|
|
|
var ts *oauthutil.TokenSource
|
|
|
|
if opt.APIKey == "" {
|
2020-11-06 02:02:26 +08:00
|
|
|
client, ts, err = oauthutil.NewClient(ctx, name, m, oauthConfig)
|
2019-07-06 07:16:57 +08:00
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return nil, fmt.Errorf("failed to configure premiumize.me: %w", err)
|
2019-07-06 07:16:57 +08:00
|
|
|
}
|
|
|
|
} else {
|
2020-11-13 23:24:43 +08:00
|
|
|
client = fshttp.NewClient(ctx)
|
2019-07-06 07:16:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
f := &Fs{
|
|
|
|
name: name,
|
|
|
|
root: root,
|
|
|
|
opt: *opt,
|
|
|
|
srv: rest.NewClient(client).SetRoot(rootURL),
|
2020-11-05 19:33:32 +08:00
|
|
|
pacer: fs.NewPacer(ctx, pacer.NewDefault(pacer.MinSleep(minSleep), pacer.MaxSleep(maxSleep), pacer.DecayConstant(decayConstant))),
|
2019-07-06 07:16:57 +08:00
|
|
|
}
|
|
|
|
f.features = (&fs.Features{
|
|
|
|
CaseInsensitive: true,
|
|
|
|
CanHaveEmptyDirectories: true,
|
|
|
|
ReadMimeType: true,
|
2020-11-06 00:00:40 +08:00
|
|
|
}).Fill(ctx, f)
|
2019-07-06 07:16:57 +08:00
|
|
|
f.srv.SetErrorHandler(errorHandler)
|
|
|
|
|
|
|
|
// Renew the token in the background
|
|
|
|
if ts != nil {
|
|
|
|
f.tokenRenewer = oauthutil.NewRenew(f.String(), ts, func() error {
|
|
|
|
_, err := f.About(ctx)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get rootID
|
|
|
|
f.dirCache = dircache.New(root, rootID, f)
|
|
|
|
|
|
|
|
// Find the current root
|
|
|
|
err = f.dirCache.FindRoot(ctx, false)
|
|
|
|
if err != nil {
|
|
|
|
// Assume it is a file
|
|
|
|
newRoot, remote := dircache.SplitPath(root)
|
|
|
|
tempF := *f
|
|
|
|
tempF.dirCache = dircache.New(newRoot, rootID, &tempF)
|
|
|
|
tempF.root = newRoot
|
|
|
|
// Make new Fs which is the parent
|
|
|
|
err = tempF.dirCache.FindRoot(ctx, false)
|
|
|
|
if err != nil {
|
|
|
|
// No root so return old f
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
_, err := tempF.newObjectWithInfo(ctx, remote, nil)
|
|
|
|
if err != nil {
|
|
|
|
if err == fs.ErrorObjectNotFound {
|
|
|
|
// File doesn't exist so return old f
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-11-06 00:00:40 +08:00
|
|
|
f.features.Fill(ctx, &tempF)
|
2019-07-06 07:16:57 +08:00
|
|
|
// XXX: update the old f here instead of returning tempF, since
|
|
|
|
// `features` were already filled with functions having *f as a receiver.
|
|
|
|
// See https://github.com/rclone/rclone/issues/2182
|
|
|
|
f.dirCache = tempF.dirCache
|
|
|
|
f.root = tempF.root
|
|
|
|
// return an error with an fs which points to the parent
|
|
|
|
return f, fs.ErrorIsFile
|
|
|
|
}
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return an Object from a path
|
|
|
|
//
|
|
|
|
// If it can't be found it returns the error fs.ErrorObjectNotFound.
|
|
|
|
func (f *Fs) newObjectWithInfo(ctx context.Context, remote string, info *api.Item) (fs.Object, error) {
|
|
|
|
o := &Object{
|
|
|
|
fs: f,
|
|
|
|
remote: remote,
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
if info != nil {
|
|
|
|
// Set info
|
|
|
|
err = o.setMetaData(info)
|
|
|
|
} else {
|
|
|
|
err = o.readMetaData(ctx) // reads info and meta, returning an error
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return o, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewObject finds the Object at remote. If it can't be found
|
|
|
|
// it returns the error fs.ErrorObjectNotFound.
|
|
|
|
func (f *Fs) NewObject(ctx context.Context, remote string) (fs.Object, error) {
|
|
|
|
return f.newObjectWithInfo(ctx, remote, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindLeaf finds a directory of name leaf in the folder with ID pathID
|
|
|
|
func (f *Fs) FindLeaf(ctx context.Context, pathID, leaf string) (pathIDOut string, found bool, err error) {
|
|
|
|
// Find the leaf in pathID
|
2021-11-01 03:07:43 +08:00
|
|
|
var newDirID string
|
|
|
|
newDirID, found, err = f.listAll(ctx, pathID, true, false, func(item *api.Item) bool {
|
2021-01-27 22:01:38 +08:00
|
|
|
if strings.EqualFold(item.Name, leaf) {
|
2019-07-06 07:16:57 +08:00
|
|
|
pathIDOut = item.ID
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
2021-11-01 03:07:43 +08:00
|
|
|
// Update the Root directory ID to its actual value
|
|
|
|
if pathID == rootID {
|
|
|
|
f.dirCache.SetRootIDAlias(newDirID)
|
|
|
|
}
|
2019-07-06 07:16:57 +08:00
|
|
|
return pathIDOut, found, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateDir makes a directory with pathID as parent and name leaf
|
|
|
|
func (f *Fs) CreateDir(ctx context.Context, pathID, leaf string) (newID string, err error) {
|
|
|
|
// fs.Debugf(f, "CreateDir(%q, %q)\n", pathID, leaf)
|
|
|
|
var resp *http.Response
|
|
|
|
var info api.FolderCreateResponse
|
|
|
|
opts := rest.Opts{
|
|
|
|
Method: "POST",
|
|
|
|
Path: "/folder/create",
|
|
|
|
Parameters: f.baseParams(),
|
|
|
|
MultipartParams: url.Values{
|
2020-01-15 01:33:35 +08:00
|
|
|
"name": {f.opt.Enc.FromStandardName(leaf)},
|
2019-07-06 07:16:57 +08:00
|
|
|
"parent_id": {pathID},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
2019-09-05 03:00:37 +08:00
|
|
|
resp, err = f.srv.CallJSON(ctx, &opts, nil, &info)
|
2021-03-11 22:44:01 +08:00
|
|
|
return shouldRetry(ctx, resp, err)
|
2019-07-06 07:16:57 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
//fmt.Printf("...Error %v\n", err)
|
2021-11-04 18:12:57 +08:00
|
|
|
return "", fmt.Errorf("CreateDir http: %w", err)
|
2019-07-06 07:16:57 +08:00
|
|
|
}
|
|
|
|
if err = info.AsErr(); err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return "", fmt.Errorf("CreateDir: %w", err)
|
2019-07-06 07:16:57 +08:00
|
|
|
}
|
|
|
|
// 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
|
2021-11-01 03:07:43 +08:00
|
|
|
//
|
|
|
|
// It returns a newDirID which is what the system returned as the directory ID
|
|
|
|
func (f *Fs) listAll(ctx context.Context, dirID string, directoriesOnly bool, filesOnly bool, fn listAllFn) (newDirID string, found bool, err error) {
|
2019-07-06 07:16:57 +08:00
|
|
|
opts := rest.Opts{
|
|
|
|
Method: "GET",
|
|
|
|
Path: "/folder/list",
|
|
|
|
Parameters: f.baseParams(),
|
|
|
|
}
|
2021-11-01 00:13:39 +08:00
|
|
|
if dirID != rootID {
|
|
|
|
opts.Parameters.Set("id", dirID)
|
|
|
|
}
|
2019-07-06 07:16:57 +08:00
|
|
|
opts.Parameters.Set("includebreadcrumbs", "false")
|
|
|
|
|
|
|
|
var result api.FolderListResponse
|
|
|
|
var resp *http.Response
|
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
2019-09-05 03:00:37 +08:00
|
|
|
resp, err = f.srv.CallJSON(ctx, &opts, nil, &result)
|
2021-03-11 22:44:01 +08:00
|
|
|
return shouldRetry(ctx, resp, err)
|
2019-07-06 07:16:57 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return newDirID, found, fmt.Errorf("couldn't list files: %w", err)
|
2019-07-06 07:16:57 +08:00
|
|
|
}
|
|
|
|
if err = result.AsErr(); err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return newDirID, found, fmt.Errorf("error while listing: %w", err)
|
2019-07-06 07:16:57 +08:00
|
|
|
}
|
2021-11-01 03:07:43 +08:00
|
|
|
newDirID = result.FolderID
|
2019-07-06 07:16:57 +08:00
|
|
|
for i := range result.Content {
|
|
|
|
item := &result.Content[i]
|
|
|
|
if item.Type == api.ItemTypeFolder {
|
|
|
|
if filesOnly {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else if item.Type == api.ItemTypeFile {
|
|
|
|
if directoriesOnly {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fs.Debugf(f, "Ignoring %q - unknown type %q", item.Name, item.Type)
|
|
|
|
continue
|
|
|
|
}
|
2020-01-15 01:33:35 +08:00
|
|
|
item.Name = f.opt.Enc.ToStandardName(item.Name)
|
2019-07-06 07:16:57 +08:00
|
|
|
if fn(item) {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// List the objects and directories in dir into entries. The
|
|
|
|
// entries can be returned in any order but should be for a
|
|
|
|
// complete directory.
|
|
|
|
//
|
|
|
|
// dir should be "" to list the root, and should not have
|
|
|
|
// trailing slashes.
|
|
|
|
//
|
|
|
|
// This should return ErrDirNotFound if the directory isn't
|
|
|
|
// found.
|
|
|
|
func (f *Fs) List(ctx context.Context, dir string) (entries fs.DirEntries, err error) {
|
|
|
|
directoryID, err := f.dirCache.FindDir(ctx, dir, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var iErr error
|
2021-11-01 03:07:43 +08:00
|
|
|
_, _, err = f.listAll(ctx, directoryID, false, false, func(info *api.Item) bool {
|
2019-07-06 07:16:57 +08:00
|
|
|
remote := path.Join(dir, info.Name)
|
|
|
|
if info.Type == api.ItemTypeFolder {
|
|
|
|
// cache the directory ID for later lookups
|
|
|
|
f.dirCache.Put(remote, info.ID)
|
|
|
|
d := fs.NewDir(remote, time.Unix(info.CreatedAt, 0)).SetID(info.ID)
|
|
|
|
entries = append(entries, d)
|
|
|
|
} else if info.Type == api.ItemTypeFile {
|
|
|
|
o, err := f.newObjectWithInfo(ctx, remote, info)
|
|
|
|
if err != nil {
|
|
|
|
iErr = err
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
entries = append(entries, o)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if iErr != nil {
|
|
|
|
return nil, iErr
|
|
|
|
}
|
|
|
|
return entries, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates from the parameters passed in a half finished Object which
|
|
|
|
// must have setMetaData called on it
|
|
|
|
//
|
2022-08-05 23:35:41 +08:00
|
|
|
// Returns the object, leaf, directoryID and error.
|
2019-07-06 07:16:57 +08:00
|
|
|
//
|
|
|
|
// Used to create new objects
|
|
|
|
func (f *Fs) createObject(ctx context.Context, remote string, modTime time.Time, size int64) (o *Object, leaf string, directoryID string, err error) {
|
|
|
|
// Create the directory for the object if it doesn't exist
|
2020-05-12 00:24:37 +08:00
|
|
|
leaf, directoryID, err = f.dirCache.FindPath(ctx, remote, true)
|
2019-07-06 07:16:57 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Temporary Object under construction
|
|
|
|
o = &Object{
|
|
|
|
fs: f,
|
|
|
|
remote: remote,
|
|
|
|
}
|
|
|
|
return o, leaf, directoryID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put the object
|
|
|
|
//
|
2022-08-05 23:35:41 +08:00
|
|
|
// Copy the reader in to the new object which is returned.
|
2019-07-06 07:16:57 +08:00
|
|
|
//
|
|
|
|
// The new object may have been created if an error is returned
|
|
|
|
func (f *Fs) Put(ctx context.Context, in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) (fs.Object, error) {
|
|
|
|
existingObj, err := f.newObjectWithInfo(ctx, src.Remote(), nil)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
return existingObj, existingObj.Update(ctx, in, src, options...)
|
|
|
|
case fs.ErrorObjectNotFound:
|
|
|
|
// Not found so create it
|
2020-03-22 06:16:24 +08:00
|
|
|
return f.PutUnchecked(ctx, in, src, options...)
|
2019-07-06 07:16:57 +08:00
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutUnchecked the object into the container
|
|
|
|
//
|
2022-08-05 23:35:41 +08:00
|
|
|
// This will produce an error if the object already exists.
|
2019-07-06 07:16:57 +08:00
|
|
|
//
|
2022-08-05 23:35:41 +08:00
|
|
|
// Copy the reader in to the new object which is returned.
|
2019-07-06 07:16:57 +08:00
|
|
|
//
|
|
|
|
// The new object may have been created if an error is returned
|
|
|
|
func (f *Fs) PutUnchecked(ctx context.Context, in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) (fs.Object, error) {
|
|
|
|
remote := src.Remote()
|
|
|
|
size := src.Size()
|
|
|
|
modTime := src.ModTime(ctx)
|
|
|
|
|
|
|
|
o, _, _, err := f.createObject(ctx, remote, modTime, size)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return o, o.Update(ctx, in, src, options...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mkdir creates the container if it doesn't exist
|
|
|
|
func (f *Fs) Mkdir(ctx context.Context, dir string) error {
|
2020-05-12 00:24:37 +08:00
|
|
|
_, err := f.dirCache.FindDir(ctx, dir, true)
|
2019-07-06 07:16:57 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// purgeCheck removes the root directory, if check is set then it
|
|
|
|
// refuses to do so if it has anything in
|
|
|
|
func (f *Fs) purgeCheck(ctx context.Context, dir string, check bool) error {
|
|
|
|
root := path.Join(f.root, dir)
|
|
|
|
if root == "" {
|
|
|
|
return errors.New("can't purge root directory")
|
|
|
|
}
|
|
|
|
dc := f.dirCache
|
|
|
|
rootID, err := dc.FindDir(ctx, dir, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// need to check if empty as it will delete recursively by default
|
|
|
|
if check {
|
2021-11-01 03:07:43 +08:00
|
|
|
_, found, err := f.listAll(ctx, rootID, false, false, func(item *api.Item) bool {
|
2019-07-06 07:16:57 +08:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return fmt.Errorf("purgeCheck: %w", err)
|
2019-07-06 07:16:57 +08:00
|
|
|
}
|
|
|
|
if found {
|
|
|
|
return fs.ErrorDirectoryNotEmpty
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := rest.Opts{
|
|
|
|
Method: "POST",
|
|
|
|
Path: "/folder/delete",
|
|
|
|
MultipartParams: url.Values{
|
|
|
|
"id": {rootID},
|
|
|
|
},
|
|
|
|
Parameters: f.baseParams(),
|
|
|
|
}
|
|
|
|
var resp *http.Response
|
|
|
|
var result api.Response
|
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
2019-09-05 03:00:37 +08:00
|
|
|
resp, err = f.srv.CallJSON(ctx, &opts, nil, &result)
|
2021-03-11 22:44:01 +08:00
|
|
|
return shouldRetry(ctx, resp, err)
|
2019-07-06 07:16:57 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return fmt.Errorf("rmdir failed: %w", err)
|
2019-07-06 07:16:57 +08:00
|
|
|
}
|
|
|
|
if err = result.AsErr(); err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return fmt.Errorf("rmdir: %w", err)
|
2019-07-06 07:16:57 +08:00
|
|
|
}
|
|
|
|
f.dirCache.FlushDir(dir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rmdir deletes the root folder
|
|
|
|
//
|
|
|
|
// Returns an error if it isn't empty
|
|
|
|
func (f *Fs) Rmdir(ctx context.Context, dir string) error {
|
|
|
|
return f.purgeCheck(ctx, dir, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Precision return the precision of this Fs
|
|
|
|
func (f *Fs) Precision() time.Duration {
|
|
|
|
return fs.ModTimeNotSupported
|
|
|
|
}
|
|
|
|
|
2020-06-05 05:25:14 +08:00
|
|
|
// Purge deletes all the files in the directory
|
2019-07-06 07:16:57 +08:00
|
|
|
//
|
|
|
|
// Optional interface: Only implement this if you have a way of
|
|
|
|
// deleting all the files quicker than just running Remove() on the
|
|
|
|
// result of List()
|
2020-06-05 05:25:14 +08:00
|
|
|
func (f *Fs) Purge(ctx context.Context, dir string) error {
|
|
|
|
return f.purgeCheck(ctx, dir, false)
|
2019-07-06 07:16:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// move a file or folder
|
|
|
|
//
|
|
|
|
// This is complicated by the fact that there is an API to move files
|
|
|
|
// between directories and a separate one to rename them. We try to
|
|
|
|
// call the minimum number of API calls.
|
|
|
|
func (f *Fs) move(ctx context.Context, isFile bool, id, oldLeaf, newLeaf, oldDirectoryID, newDirectoryID string) (err error) {
|
2020-01-15 01:33:35 +08:00
|
|
|
newLeaf = f.opt.Enc.FromStandardName(newLeaf)
|
|
|
|
oldLeaf = f.opt.Enc.FromStandardName(oldLeaf)
|
2019-07-06 07:16:57 +08:00
|
|
|
doRenameLeaf := oldLeaf != newLeaf
|
|
|
|
doMove := oldDirectoryID != newDirectoryID
|
|
|
|
|
|
|
|
// Now rename the leaf to a temporary name if we are moving to
|
|
|
|
// another directory to make sure we don't overwrite something
|
|
|
|
// in the destination directory by accident
|
|
|
|
if doRenameLeaf && doMove {
|
2019-08-25 15:39:31 +08:00
|
|
|
tmpLeaf := newLeaf + "." + random.String(8)
|
2019-07-06 07:16:57 +08:00
|
|
|
err = f.renameLeaf(ctx, isFile, id, tmpLeaf)
|
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return fmt.Errorf("Move rename leaf: %w", err)
|
2019-07-06 07:16:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move the object to a new directory (with the existing name)
|
|
|
|
// if required
|
|
|
|
if doMove {
|
|
|
|
opts := rest.Opts{
|
|
|
|
Method: "POST",
|
|
|
|
Path: "/folder/paste",
|
|
|
|
Parameters: f.baseParams(),
|
|
|
|
MultipartParams: url.Values{
|
|
|
|
"id": {newDirectoryID},
|
|
|
|
},
|
|
|
|
}
|
2021-11-01 00:26:33 +08:00
|
|
|
opts.MultipartParams.Set("items[0][id]", id)
|
2019-07-06 07:16:57 +08:00
|
|
|
if isFile {
|
2021-11-01 00:26:33 +08:00
|
|
|
opts.MultipartParams.Set("items[0][type]", "file")
|
2019-07-06 07:16:57 +08:00
|
|
|
} else {
|
2021-11-01 00:26:33 +08:00
|
|
|
opts.MultipartParams.Set("items[0][type]", "folder")
|
2019-07-06 07:16:57 +08:00
|
|
|
}
|
2019-10-02 16:54:36 +08:00
|
|
|
//replacedLeaf := enc.FromStandardName(leaf)
|
2019-07-06 07:16:57 +08:00
|
|
|
var resp *http.Response
|
|
|
|
var result api.Response
|
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
2019-09-05 03:00:37 +08:00
|
|
|
resp, err = f.srv.CallJSON(ctx, &opts, nil, &result)
|
2021-03-11 22:44:01 +08:00
|
|
|
return shouldRetry(ctx, resp, err)
|
2019-07-06 07:16:57 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return fmt.Errorf("Move http: %w", err)
|
2019-07-06 07:16:57 +08:00
|
|
|
}
|
|
|
|
if err = result.AsErr(); err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return fmt.Errorf("Move: %w", err)
|
2019-07-06 07:16:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rename the leaf to its final name if required
|
|
|
|
if doRenameLeaf {
|
|
|
|
err = f.renameLeaf(ctx, isFile, id, newLeaf)
|
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return fmt.Errorf("Move rename leaf: %w", err)
|
2019-07-06 07:16:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-14 05:43:40 +08:00
|
|
|
// Move src to this remote using server-side move operations.
|
2019-07-06 07:16:57 +08:00
|
|
|
//
|
2022-08-05 23:35:41 +08:00
|
|
|
// This is stored with the remote path given.
|
2019-07-06 07:16:57 +08:00
|
|
|
//
|
2022-08-05 23:35:41 +08:00
|
|
|
// It returns the destination Object and a possible error.
|
2019-07-06 07:16:57 +08:00
|
|
|
//
|
|
|
|
// Will only be called if src.Fs().Name() == f.Name()
|
|
|
|
//
|
|
|
|
// If it isn't possible then return fs.ErrorCantMove
|
|
|
|
func (f *Fs) Move(ctx context.Context, 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(ctx, remote, srcObj.modTime, srcObj.size)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do the move
|
|
|
|
err = f.move(ctx, true, srcObj.id, path.Base(srcObj.remote), leaf, srcObj.parentID, directoryID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = dstObj.readMetaData(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return dstObj, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DirMove moves src, srcRemote to this remote at dstRemote
|
2020-10-14 05:43:40 +08:00
|
|
|
// using server-side move operations.
|
2019-07-06 07:16:57 +08:00
|
|
|
//
|
|
|
|
// Will only be called if src.Fs().Name() == f.Name()
|
|
|
|
//
|
|
|
|
// If it isn't possible then return fs.ErrorCantDirMove
|
|
|
|
//
|
|
|
|
// If destination exists then return fs.ErrorDirExists
|
|
|
|
func (f *Fs) DirMove(ctx context.Context, src fs.Fs, srcRemote, dstRemote string) error {
|
|
|
|
srcFs, ok := src.(*Fs)
|
|
|
|
if !ok {
|
|
|
|
fs.Debugf(srcFs, "Can't move directory - not same remote type")
|
|
|
|
return fs.ErrorCantDirMove
|
|
|
|
}
|
|
|
|
|
2020-05-12 18:39:30 +08:00
|
|
|
srcID, srcDirectoryID, srcLeaf, dstDirectoryID, dstLeaf, err := f.dirCache.DirMove(ctx, srcFs.dirCache, srcFs.root, srcRemote, f.root, dstRemote)
|
2019-07-06 07:16:57 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do the move
|
2020-05-12 18:39:30 +08:00
|
|
|
err = f.move(ctx, false, srcID, srcLeaf, dstLeaf, srcDirectoryID, dstDirectoryID)
|
2019-07-06 07:16:57 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
srcFs.dirCache.FlushDir(srcRemote)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PublicLink adds a "readable by anyone with link" permission on the given file or folder.
|
2020-06-01 05:18:01 +08:00
|
|
|
func (f *Fs) PublicLink(ctx context.Context, remote string, expire fs.Duration, unlink bool) (string, error) {
|
2019-07-06 07:16:57 +08:00
|
|
|
_, err := f.dirCache.FindDir(ctx, remote, false)
|
|
|
|
if err == nil {
|
|
|
|
return "", fs.ErrorCantShareDirectories
|
|
|
|
}
|
|
|
|
o, err := f.NewObject(ctx, remote)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return o.(*Object).url, nil
|
|
|
|
}
|
|
|
|
|
2023-12-08 12:33:51 +08:00
|
|
|
// Shutdown shutdown the fs
|
|
|
|
func (f *Fs) Shutdown(ctx context.Context) error {
|
|
|
|
f.tokenRenewer.Shutdown()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-06 07:16:57 +08:00
|
|
|
// About gets quota information
|
|
|
|
func (f *Fs) About(ctx context.Context) (usage *fs.Usage, err error) {
|
|
|
|
var resp *http.Response
|
|
|
|
var info api.AccountInfoResponse
|
|
|
|
opts := rest.Opts{
|
|
|
|
Method: "POST",
|
|
|
|
Path: "/account/info",
|
|
|
|
Parameters: f.baseParams(),
|
|
|
|
}
|
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
2019-09-05 03:00:37 +08:00
|
|
|
resp, err = f.srv.CallJSON(ctx, &opts, nil, &info)
|
2021-03-11 22:44:01 +08:00
|
|
|
return shouldRetry(ctx, resp, err)
|
2019-07-06 07:16:57 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
2022-01-15 05:18:32 +08:00
|
|
|
return nil, err
|
2019-07-06 07:16:57 +08:00
|
|
|
}
|
|
|
|
if err = info.AsErr(); err != nil {
|
2022-01-15 05:18:32 +08:00
|
|
|
return nil, err
|
2019-07-06 07:16:57 +08:00
|
|
|
}
|
|
|
|
usage = &fs.Usage{
|
|
|
|
Used: fs.NewUsageValue(int64(info.SpaceUsed)),
|
|
|
|
}
|
|
|
|
return usage, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DirCacheFlush resets the directory cache - used in testing as an
|
|
|
|
// optional interface
|
|
|
|
func (f *Fs) DirCacheFlush() {
|
|
|
|
f.dirCache.ResetRoot()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hashes returns the supported hash sets.
|
|
|
|
func (f *Fs) Hashes() hash.Set {
|
|
|
|
return hash.Set(hash.None)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
|
|
|
// Fs returns the parent Fs
|
|
|
|
func (o *Object) Fs() fs.Info {
|
|
|
|
return o.fs
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return a string version
|
|
|
|
func (o *Object) String() string {
|
|
|
|
if o == nil {
|
|
|
|
return "<nil>"
|
|
|
|
}
|
|
|
|
return o.remote
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remote returns the remote path
|
|
|
|
func (o *Object) Remote() string {
|
|
|
|
return o.remote
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hash returns the SHA-1 of an object returning a lowercase hex string
|
|
|
|
func (o *Object) Hash(ctx context.Context, t hash.Type) (string, error) {
|
|
|
|
return "", hash.ErrUnsupported
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns the size of an object in bytes
|
|
|
|
func (o *Object) Size() int64 {
|
|
|
|
err := o.readMetaData(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
fs.Logf(o, "Failed to read metadata: %v", err)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return o.size
|
|
|
|
}
|
|
|
|
|
|
|
|
// setMetaData sets the metadata from info
|
|
|
|
func (o *Object) setMetaData(info *api.Item) (err error) {
|
|
|
|
if info.Type != "file" {
|
2021-11-04 18:12:57 +08:00
|
|
|
return fmt.Errorf("%q is %q: %w", o.remote, info.Type, fs.ErrorNotAFile)
|
2019-07-06 07:16:57 +08:00
|
|
|
}
|
|
|
|
o.hasMetaData = true
|
|
|
|
o.size = info.Size
|
|
|
|
o.modTime = time.Unix(info.CreatedAt, 0)
|
|
|
|
o.id = info.ID
|
|
|
|
o.mimeType = info.MimeType
|
|
|
|
o.url = info.Link
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// readMetaData gets the metadata if it hasn't already been fetched
|
|
|
|
//
|
|
|
|
// it also sets the info
|
|
|
|
func (o *Object) readMetaData(ctx context.Context) (err error) {
|
|
|
|
if o.hasMetaData {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
info, err := o.fs.readMetaDataForPath(ctx, o.remote, false, true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return o.setMetaData(info)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ModTime returns the modification time of the object
|
|
|
|
//
|
|
|
|
// It attempts to read the objects mtime and if that isn't present the
|
|
|
|
// LastModified returned in the http headers
|
|
|
|
func (o *Object) ModTime(ctx context.Context) time.Time {
|
|
|
|
err := o.readMetaData(ctx)
|
|
|
|
if err != nil {
|
|
|
|
fs.Logf(o, "Failed to read metadata: %v", err)
|
|
|
|
return time.Now()
|
|
|
|
}
|
|
|
|
return o.modTime
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetModTime sets the modification time of the local fs object
|
|
|
|
func (o *Object) SetModTime(ctx context.Context, modTime time.Time) error {
|
|
|
|
return fs.ErrorCantSetModTime
|
|
|
|
}
|
|
|
|
|
|
|
|
// Storable returns a boolean showing whether this object storable
|
|
|
|
func (o *Object) Storable() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open an object for read
|
|
|
|
func (o *Object) Open(ctx context.Context, options ...fs.OpenOption) (in io.ReadCloser, err error) {
|
|
|
|
if o.url == "" {
|
|
|
|
return nil, errors.New("can't download - no URL")
|
|
|
|
}
|
|
|
|
fs.FixRangeOption(options, o.size)
|
|
|
|
var resp *http.Response
|
|
|
|
opts := rest.Opts{
|
|
|
|
Path: "",
|
|
|
|
RootURL: o.url,
|
|
|
|
Method: "GET",
|
|
|
|
Options: options,
|
|
|
|
}
|
|
|
|
err = o.fs.pacer.Call(func() (bool, error) {
|
2019-09-05 03:00:37 +08:00
|
|
|
resp, err = o.fs.srv.Call(ctx, &opts)
|
2021-03-11 22:44:01 +08:00
|
|
|
return shouldRetry(ctx, resp, err)
|
2019-07-06 07:16:57 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp.Body, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the object with the contents of the io.Reader, modTime and size
|
|
|
|
//
|
2022-08-05 23:35:41 +08:00
|
|
|
// If existing is set then it updates the object rather than creating a new one.
|
2019-07-06 07:16:57 +08:00
|
|
|
//
|
|
|
|
// The new object may have been created if an error is returned
|
|
|
|
func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) (err error) {
|
|
|
|
remote := o.Remote()
|
|
|
|
size := src.Size()
|
|
|
|
|
|
|
|
// Create the directory for the object if it doesn't exist
|
2020-05-12 00:24:37 +08:00
|
|
|
leaf, directoryID, err := o.fs.dirCache.FindPath(ctx, remote, true)
|
2019-07-06 07:16:57 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-15 01:33:35 +08:00
|
|
|
leaf = o.fs.opt.Enc.FromStandardName(leaf)
|
2019-07-06 07:16:57 +08:00
|
|
|
|
|
|
|
var resp *http.Response
|
|
|
|
var info api.FolderUploadinfoResponse
|
|
|
|
opts := rest.Opts{
|
|
|
|
Method: "POST",
|
|
|
|
Path: "/folder/uploadinfo",
|
|
|
|
Parameters: o.fs.baseParams(),
|
2020-03-22 06:16:24 +08:00
|
|
|
Options: options,
|
2019-07-06 07:16:57 +08:00
|
|
|
MultipartParams: url.Values{
|
|
|
|
"id": {directoryID},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = o.fs.pacer.Call(func() (bool, error) {
|
2019-09-05 03:00:37 +08:00
|
|
|
resp, err = o.fs.srv.CallJSON(ctx, &opts, nil, &info)
|
2019-07-06 07:16:57 +08:00
|
|
|
if err != nil {
|
2021-03-11 22:44:01 +08:00
|
|
|
return shouldRetry(ctx, resp, err)
|
2019-07-06 07:16:57 +08:00
|
|
|
}
|
|
|
|
// Just check the download URL resolves - sometimes
|
|
|
|
// the URLs returned by premiumize.me don't resolve so
|
|
|
|
// this needs a retry.
|
|
|
|
var u *url.URL
|
|
|
|
u, err = url.Parse(info.URL)
|
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return true, fmt.Errorf("failed to parse download URL: %w", err)
|
2019-07-06 07:16:57 +08:00
|
|
|
}
|
|
|
|
_, err = net.LookupIP(u.Hostname())
|
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return true, fmt.Errorf("failed to resolve download URL: %w", err)
|
2019-07-06 07:16:57 +08:00
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return fmt.Errorf("upload get URL http: %w", err)
|
2019-07-06 07:16:57 +08:00
|
|
|
}
|
|
|
|
if err = info.AsErr(); err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return fmt.Errorf("upload get URL: %w", err)
|
2019-07-06 07:16:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// if file exists then rename it out the way otherwise uploads can fail
|
|
|
|
uploaded := false
|
|
|
|
var oldID = o.id
|
|
|
|
if o.hasMetaData {
|
2019-08-25 15:39:31 +08:00
|
|
|
newLeaf := leaf + "." + random.String(8)
|
2019-07-06 07:16:57 +08:00
|
|
|
fs.Debugf(o, "Moving old file out the way to %q", newLeaf)
|
|
|
|
err = o.fs.renameLeaf(ctx, true, oldID, newLeaf)
|
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return fmt.Errorf("upload rename old file: %w", err)
|
2019-07-06 07:16:57 +08:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
// on failed upload rename old file back
|
|
|
|
if !uploaded {
|
|
|
|
fs.Debugf(o, "Renaming old file back (from %q to %q) since upload failed", leaf, newLeaf)
|
|
|
|
newErr := o.fs.renameLeaf(ctx, true, oldID, leaf)
|
|
|
|
if newErr != nil && err == nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
err = fmt.Errorf("upload renaming old file back: %w", newErr)
|
2019-07-06 07:16:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
opts = rest.Opts{
|
|
|
|
Method: "POST",
|
|
|
|
RootURL: info.URL,
|
|
|
|
Body: in,
|
|
|
|
MultipartParams: url.Values{
|
|
|
|
"token": {info.Token},
|
|
|
|
},
|
|
|
|
MultipartContentName: "file", // ..name of the parameter which is the attached file
|
|
|
|
MultipartFileName: leaf, // ..name of the file for the attached file
|
|
|
|
ContentLength: &size,
|
|
|
|
}
|
|
|
|
var result api.Response
|
|
|
|
err = o.fs.pacer.CallNoRetry(func() (bool, error) {
|
2019-09-05 03:00:37 +08:00
|
|
|
resp, err = o.fs.srv.CallJSON(ctx, &opts, nil, &result)
|
2021-03-11 22:44:01 +08:00
|
|
|
return shouldRetry(ctx, resp, err)
|
2019-07-06 07:16:57 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return fmt.Errorf("upload file http: %w", err)
|
2019-07-06 07:16:57 +08:00
|
|
|
}
|
|
|
|
if err = result.AsErr(); err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return fmt.Errorf("upload file: %w", err)
|
2019-07-06 07:16:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// on successful upload, remove old file if it exists
|
|
|
|
uploaded = true
|
|
|
|
if o.hasMetaData {
|
|
|
|
fs.Debugf(o, "Removing old file")
|
|
|
|
err := o.fs.remove(ctx, oldID)
|
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return fmt.Errorf("upload remove old file: %w", err)
|
2019-07-06 07:16:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
o.hasMetaData = false
|
|
|
|
return o.readMetaData(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rename the leaf of a file or directory in a directory
|
|
|
|
func (f *Fs) renameLeaf(ctx context.Context, isFile bool, id string, newLeaf string) (err error) {
|
|
|
|
opts := rest.Opts{
|
|
|
|
Method: "POST",
|
|
|
|
MultipartParams: url.Values{
|
|
|
|
"id": {id},
|
|
|
|
"name": {newLeaf},
|
|
|
|
},
|
|
|
|
Parameters: f.baseParams(),
|
|
|
|
}
|
|
|
|
if isFile {
|
|
|
|
opts.Path = "/item/rename"
|
|
|
|
} else {
|
|
|
|
opts.Path = "/folder/rename"
|
|
|
|
}
|
|
|
|
var resp *http.Response
|
|
|
|
var result api.Response
|
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
2019-09-05 03:00:37 +08:00
|
|
|
resp, err = f.srv.CallJSON(ctx, &opts, nil, &result)
|
2021-03-11 22:44:01 +08:00
|
|
|
return shouldRetry(ctx, resp, err)
|
2019-07-06 07:16:57 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return fmt.Errorf("rename http: %w", err)
|
2019-07-06 07:16:57 +08:00
|
|
|
}
|
|
|
|
if err = result.AsErr(); err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return fmt.Errorf("rename: %w", err)
|
2019-07-06 07:16:57 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove an object by ID
|
|
|
|
func (f *Fs) remove(ctx context.Context, id string) (err error) {
|
|
|
|
opts := rest.Opts{
|
|
|
|
Method: "POST",
|
|
|
|
Path: "/item/delete",
|
|
|
|
MultipartParams: url.Values{
|
|
|
|
"id": {id},
|
|
|
|
},
|
|
|
|
Parameters: f.baseParams(),
|
|
|
|
}
|
|
|
|
var resp *http.Response
|
|
|
|
var result api.Response
|
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
2019-09-05 03:00:37 +08:00
|
|
|
resp, err = f.srv.CallJSON(ctx, &opts, nil, &result)
|
2021-03-11 22:44:01 +08:00
|
|
|
return shouldRetry(ctx, resp, err)
|
2019-07-06 07:16:57 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return fmt.Errorf("remove http: %w", err)
|
2019-07-06 07:16:57 +08:00
|
|
|
}
|
|
|
|
if err = result.AsErr(); err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return fmt.Errorf("remove: %w", err)
|
2019-07-06 07:16:57 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove an object
|
|
|
|
func (o *Object) Remove(ctx context.Context) error {
|
|
|
|
err := o.readMetaData(ctx)
|
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return fmt.Errorf("Remove: Failed to read metadata: %w", err)
|
2019-07-06 07:16:57 +08:00
|
|
|
}
|
|
|
|
return o.fs.remove(ctx, o.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MimeType of an Object if known, "" otherwise
|
|
|
|
func (o *Object) MimeType(ctx context.Context) string {
|
|
|
|
return o.mimeType
|
|
|
|
}
|
|
|
|
|
|
|
|
// ID returns the ID of the Object if known, or "" if not
|
|
|
|
func (o *Object) ID() string {
|
|
|
|
return o.id
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the interfaces are satisfied
|
|
|
|
var (
|
|
|
|
_ fs.Fs = (*Fs)(nil)
|
|
|
|
_ fs.Purger = (*Fs)(nil)
|
|
|
|
_ fs.Mover = (*Fs)(nil)
|
|
|
|
_ fs.DirMover = (*Fs)(nil)
|
|
|
|
_ fs.DirCacheFlusher = (*Fs)(nil)
|
|
|
|
_ fs.Abouter = (*Fs)(nil)
|
|
|
|
_ fs.PublicLinker = (*Fs)(nil)
|
2023-12-08 12:33:51 +08:00
|
|
|
_ fs.Shutdowner = (*Fs)(nil)
|
2019-07-06 07:16:57 +08:00
|
|
|
_ fs.Object = (*Object)(nil)
|
|
|
|
_ fs.MimeTyper = (*Object)(nil)
|
|
|
|
_ fs.IDer = (*Object)(nil)
|
|
|
|
)
|