2015-12-07 05:01:03 +01:00
|
|
|
// Package yandex provides an interface to the Yandex Disk storage.
|
|
|
|
//
|
|
|
|
// dibu28 <dibu28@gmail.com> github.com/dibu28
|
|
|
|
package yandex
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ncw/rclone/fs"
|
|
|
|
"github.com/ncw/rclone/oauthutil"
|
|
|
|
yandex "github.com/ncw/rclone/yandex/api"
|
2016-06-06 22:23:54 +02:00
|
|
|
"github.com/pkg/errors"
|
2015-12-07 05:01:03 +01:00
|
|
|
"golang.org/x/oauth2"
|
|
|
|
)
|
|
|
|
|
|
|
|
//oAuth
|
|
|
|
const (
|
2016-02-28 20:57:19 +01:00
|
|
|
rcloneClientID = "ac39b43b9eba4cae8ffb788c06d816a8"
|
2016-08-14 13:04:43 +02:00
|
|
|
rcloneEncryptedClientSecret = "EfyyNZ3YUEwXM5yAhi72G9YwKn2mkFrYwJNS7cY0TJAhFlX9K-uJFbGlpO-RYjrJ"
|
2015-12-07 05:01:03 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Globals
|
|
|
|
var (
|
|
|
|
// Description of how to auth for this app
|
|
|
|
oauthConfig = &oauth2.Config{
|
|
|
|
Endpoint: oauth2.Endpoint{
|
|
|
|
AuthURL: "https://oauth.yandex.com/authorize", //same as https://oauth.yandex.ru/authorize
|
|
|
|
TokenURL: "https://oauth.yandex.com/token", //same as https://oauth.yandex.ru/token
|
|
|
|
},
|
|
|
|
ClientID: rcloneClientID,
|
2016-08-14 13:04:43 +02:00
|
|
|
ClientSecret: fs.MustReveal(rcloneEncryptedClientSecret),
|
2015-12-07 05:01:03 +01:00
|
|
|
RedirectURL: oauthutil.RedirectURL,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// Register with Fs
|
|
|
|
func init() {
|
2016-02-18 12:35:25 +01:00
|
|
|
fs.Register(&fs.RegInfo{
|
2016-02-15 19:11:53 +01:00
|
|
|
Name: "yandex",
|
|
|
|
Description: "Yandex Disk",
|
|
|
|
NewFs: NewFs,
|
2015-12-07 05:01:03 +01:00
|
|
|
Config: func(name string) {
|
2016-01-04 16:13:36 +01:00
|
|
|
err := oauthutil.Config("yandex", name, oauthConfig)
|
2015-12-07 05:01:03 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to configure token: %v", err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Options: []fs.Option{{
|
2016-01-07 16:20:32 +01:00
|
|
|
Name: fs.ConfigClientID,
|
2015-12-07 05:01:03 +01:00
|
|
|
Help: "Yandex Client Id - leave blank normally.",
|
|
|
|
}, {
|
2016-01-07 16:20:32 +01:00
|
|
|
Name: fs.ConfigClientSecret,
|
2015-12-07 05:01:03 +01:00
|
|
|
Help: "Yandex Client Secret - leave blank normally.",
|
|
|
|
}},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fs represents a remote yandex
|
|
|
|
type Fs struct {
|
|
|
|
name string
|
|
|
|
root string //root path
|
2017-01-13 18:21:47 +01:00
|
|
|
features *fs.Features // optional features
|
|
|
|
yd *yandex.Client // client for rest api
|
2015-12-07 05:01:03 +01:00
|
|
|
diskRoot string //root path with "disk:/" container name
|
|
|
|
mkdircache map[string]int
|
|
|
|
}
|
|
|
|
|
|
|
|
// Object describes a swift object
|
|
|
|
type Object struct {
|
2016-09-21 23:13:24 +02:00
|
|
|
fs *Fs // what this object is part of
|
|
|
|
remote string // The remote path
|
|
|
|
md5sum string // The MD5Sum of the object
|
|
|
|
bytes uint64 // Bytes in the object
|
|
|
|
modTime time.Time // Modified time of the object
|
|
|
|
mimeType string // Content type according to the server
|
2015-12-07 05:01:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
|
|
|
// Name of the remote (as passed into NewFs)
|
|
|
|
func (f *Fs) Name() string {
|
|
|
|
return f.name
|
|
|
|
}
|
|
|
|
|
|
|
|
// Root of the remote (as passed into NewFs)
|
|
|
|
func (f *Fs) Root() string {
|
|
|
|
return f.root
|
|
|
|
}
|
|
|
|
|
|
|
|
// String converts this Fs to a string
|
|
|
|
func (f *Fs) String() string {
|
|
|
|
return fmt.Sprintf("Yandex %s", f.root)
|
|
|
|
}
|
|
|
|
|
2017-01-13 18:21:47 +01:00
|
|
|
// Features returns the optional features of this Fs
|
|
|
|
func (f *Fs) Features() *fs.Features {
|
|
|
|
return f.features
|
|
|
|
}
|
|
|
|
|
2015-12-07 05:01:03 +01:00
|
|
|
// read access token from ConfigFile string
|
|
|
|
func getAccessToken(name string) (*oauth2.Token, error) {
|
|
|
|
// Read the token from the config file
|
2016-12-20 19:03:09 +01:00
|
|
|
tokenConfig := fs.ConfigFileGet(name, "token")
|
2015-12-07 05:01:03 +01:00
|
|
|
//Get access token from config string
|
|
|
|
decoder := json.NewDecoder(strings.NewReader(tokenConfig))
|
|
|
|
var result *oauth2.Token
|
|
|
|
err := decoder.Decode(&result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFs constructs an Fs from the path, container:path
|
|
|
|
func NewFs(name, root string) (fs.Fs, error) {
|
|
|
|
//read access token from config
|
|
|
|
token, err := getAccessToken(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
//create new client
|
2015-12-30 14:30:01 +01:00
|
|
|
yandexDisk := yandex.NewClient(token.AccessToken, fs.Config.Client())
|
2015-12-07 05:01:03 +01:00
|
|
|
|
|
|
|
f := &Fs{
|
2017-07-03 14:39:31 +02:00
|
|
|
name: name,
|
|
|
|
yd: yandexDisk,
|
2015-12-07 05:01:03 +01:00
|
|
|
}
|
2017-08-09 16:27:43 +02:00
|
|
|
f.features = (&fs.Features{
|
|
|
|
ReadMimeType: true,
|
|
|
|
WriteMimeType: true,
|
|
|
|
CanHaveEmptyDirectories: true,
|
|
|
|
}).Fill(f)
|
2015-12-07 05:01:03 +01:00
|
|
|
f.setRoot(root)
|
|
|
|
|
|
|
|
// Check to see if the object exists and is a file
|
|
|
|
//request object meta info
|
|
|
|
var opt2 yandex.ResourceInfoRequestOptions
|
|
|
|
if ResourceInfoResponse, err := yandexDisk.NewResourceInfoRequest(root, opt2).Exec(); err != nil {
|
|
|
|
//return err
|
|
|
|
} else {
|
|
|
|
if ResourceInfoResponse.ResourceType == "file" {
|
|
|
|
f.setRoot(path.Dir(root))
|
2016-06-21 19:01:53 +02:00
|
|
|
// return an error with an fs which points to the parent
|
|
|
|
return f, fs.ErrorIsFile
|
2015-12-07 05:01:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets root in f
|
|
|
|
func (f *Fs) setRoot(root string) {
|
|
|
|
//Set root path
|
|
|
|
f.root = strings.Trim(root, "/")
|
|
|
|
//Set disk root path.
|
|
|
|
//Adding "disk:" to root path as all paths on disk start with it
|
2017-06-13 12:22:16 +02:00
|
|
|
var diskRoot string
|
2015-12-07 05:01:03 +01:00
|
|
|
if f.root == "" {
|
|
|
|
diskRoot = "disk:/"
|
|
|
|
} else {
|
|
|
|
diskRoot = "disk:/" + f.root + "/"
|
|
|
|
}
|
|
|
|
f.diskRoot = diskRoot
|
|
|
|
}
|
|
|
|
|
2017-06-30 11:54:14 +02:00
|
|
|
// Convert a list item into a DirEntry
|
|
|
|
func (f *Fs) itemToDirEntry(remote string, object *yandex.ResourceInfoResponse) (fs.DirEntry, error) {
|
2017-06-11 23:43:31 +02:00
|
|
|
switch object.ResourceType {
|
|
|
|
case "dir":
|
|
|
|
t, err := time.Parse(time.RFC3339Nano, object.Modified)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error parsing time in directory item")
|
|
|
|
}
|
2017-06-30 14:37:29 +02:00
|
|
|
d := fs.NewDir(remote, t).SetSize(int64(object.Size))
|
2017-06-11 23:43:31 +02:00
|
|
|
return d, nil
|
|
|
|
case "file":
|
|
|
|
o, err := f.newObjectWithInfo(remote, object)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return o, nil
|
|
|
|
default:
|
|
|
|
fs.Debugf(f, "Unknown resource type %q", object.ResourceType)
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
2016-04-21 21:06:21 +02:00
|
|
|
|
2017-06-11 23:43:31 +02:00
|
|
|
// List the objects and directories in dir into entries. The
|
|
|
|
// entries can be returned in any order but should be for a
|
|
|
|
// complete directory.
|
|
|
|
//
|
|
|
|
// dir should be "" to list the root, and should not have
|
|
|
|
// trailing slashes.
|
|
|
|
//
|
|
|
|
// This should return ErrDirNotFound if the directory isn't
|
|
|
|
// found.
|
|
|
|
func (f *Fs) List(dir string) (entries fs.DirEntries, err error) {
|
2016-04-21 21:06:21 +02:00
|
|
|
//request object meta info
|
|
|
|
var opt yandex.ResourceInfoRequestOptions
|
2017-02-25 14:39:16 +01:00
|
|
|
root := f.diskRoot
|
|
|
|
if dir != "" {
|
|
|
|
root += dir + "/"
|
2016-04-21 21:06:21 +02:00
|
|
|
}
|
2017-02-25 14:39:16 +01:00
|
|
|
var limit uint32 = 1000 // max number of object per request
|
|
|
|
var itemsCount uint32 //number of items per page in response
|
|
|
|
var offset uint32 //for the next page of request
|
|
|
|
opt.Limit = &limit
|
|
|
|
opt.Offset = &offset
|
|
|
|
|
|
|
|
//query each page of list until itemCount is less then limit
|
|
|
|
for {
|
|
|
|
ResourceInfoResponse, err := f.yd.NewResourceInfoRequest(root, opt).Exec()
|
|
|
|
if err != nil {
|
2017-04-26 19:16:59 +02:00
|
|
|
yErr, ok := err.(yandex.DiskClientError)
|
|
|
|
if ok && yErr.Code == "DiskNotFoundError" {
|
2017-06-11 23:43:31 +02:00
|
|
|
return nil, fs.ErrorDirNotFound
|
2017-04-26 19:16:59 +02:00
|
|
|
}
|
2017-06-11 23:43:31 +02:00
|
|
|
return nil, err
|
2017-02-25 14:39:16 +01:00
|
|
|
}
|
|
|
|
itemsCount = uint32(len(ResourceInfoResponse.Embedded.Items))
|
|
|
|
|
|
|
|
if ResourceInfoResponse.ResourceType == "dir" {
|
|
|
|
//list all subdirs
|
2017-06-11 23:43:31 +02:00
|
|
|
for _, element := range ResourceInfoResponse.Embedded.Items {
|
2017-02-25 14:39:16 +01:00
|
|
|
remote := path.Join(dir, element.Name)
|
2017-06-11 23:43:31 +02:00
|
|
|
entry, err := f.itemToDirEntry(remote, &element)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if entry != nil {
|
|
|
|
entries = append(entries, entry)
|
2016-04-21 21:06:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-25 14:39:16 +01:00
|
|
|
|
|
|
|
//offset for the next page of items
|
|
|
|
offset += itemsCount
|
|
|
|
//check if we reached end of list
|
|
|
|
if itemsCount < limit {
|
|
|
|
break
|
|
|
|
}
|
2016-04-21 21:06:21 +02:00
|
|
|
}
|
2017-06-11 23:43:31 +02:00
|
|
|
return entries, nil
|
2016-04-21 21:06:21 +02:00
|
|
|
}
|
|
|
|
|
2017-06-11 23:43:31 +02:00
|
|
|
// ListR lists the objects and directories of the Fs starting
|
|
|
|
// from dir recursively into out.
|
2015-12-07 05:01:03 +01:00
|
|
|
//
|
2017-06-11 23:43:31 +02:00
|
|
|
// dir should be "" to start from the root, and should not
|
|
|
|
// have trailing slashes.
|
|
|
|
//
|
|
|
|
// This should return ErrDirNotFound if the directory isn't
|
|
|
|
// found.
|
|
|
|
//
|
|
|
|
// It should call callback for each tranche of entries read.
|
|
|
|
// These need not be returned in any particular order. If
|
|
|
|
// callback returns an error then the listing will stop
|
|
|
|
// immediately.
|
|
|
|
//
|
|
|
|
// Don't implement this unless you have a more efficient way
|
|
|
|
// of listing recursively that doing a directory traversal.
|
|
|
|
func (f *Fs) ListR(dir string, callback fs.ListRCallback) (err error) {
|
2015-12-07 05:01:03 +01:00
|
|
|
//request files list. list is divided into pages. We send request for each page
|
|
|
|
//items per page is limited by limit
|
|
|
|
//TODO may be add config parameter for the items per page limit
|
|
|
|
var limit uint32 = 1000 // max number of object per request
|
|
|
|
var itemsCount uint32 //number of items per page in response
|
|
|
|
var offset uint32 //for the next page of request
|
|
|
|
// yandex disk api request options
|
|
|
|
var opt yandex.FlatFileListRequestOptions
|
|
|
|
opt.Limit = &limit
|
|
|
|
opt.Offset = &offset
|
2016-04-23 22:46:52 +02:00
|
|
|
prefix := f.diskRoot
|
|
|
|
if dir != "" {
|
|
|
|
prefix += dir + "/"
|
|
|
|
}
|
2015-12-07 05:01:03 +01:00
|
|
|
//query each page of list until itemCount is less then limit
|
|
|
|
for {
|
|
|
|
//send request
|
|
|
|
info, err := f.yd.NewFlatFileListRequest(opt).Exec()
|
|
|
|
if err != nil {
|
2017-04-26 19:16:59 +02:00
|
|
|
yErr, ok := err.(yandex.DiskClientError)
|
|
|
|
if ok && yErr.Code == "DiskNotFoundError" {
|
|
|
|
return fs.ErrorDirNotFound
|
|
|
|
}
|
2016-04-21 21:06:21 +02:00
|
|
|
return err
|
2015-12-07 05:01:03 +01:00
|
|
|
}
|
|
|
|
itemsCount = uint32(len(info.Items))
|
|
|
|
|
|
|
|
//list files
|
2017-06-11 23:43:31 +02:00
|
|
|
entries := make(fs.DirEntries, 0, len(info.Items))
|
2015-12-07 05:01:03 +01:00
|
|
|
for _, item := range info.Items {
|
|
|
|
// filter file list and get only files we need
|
2016-04-23 22:46:52 +02:00
|
|
|
if strings.HasPrefix(item.Path, prefix) {
|
2015-12-07 05:01:03 +01:00
|
|
|
//trim root folder from filename
|
|
|
|
var name = strings.TrimPrefix(item.Path, f.diskRoot)
|
2017-06-11 23:43:31 +02:00
|
|
|
entry, err := f.itemToDirEntry(name, &item)
|
2016-04-21 21:06:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-11 23:43:31 +02:00
|
|
|
if entry != nil {
|
|
|
|
entries = append(entries, entry)
|
|
|
|
}
|
2015-12-07 05:01:03 +01:00
|
|
|
}
|
|
|
|
}
|
2017-06-11 23:43:31 +02:00
|
|
|
// send the listing
|
|
|
|
err = callback(entries)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-07 05:01:03 +01:00
|
|
|
|
|
|
|
//offset for the next page of items
|
|
|
|
offset += itemsCount
|
|
|
|
//check if we reached end of list
|
|
|
|
if itemsCount < limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2016-04-21 21:06:21 +02:00
|
|
|
return nil
|
2015-12-07 05:01:03 +01:00
|
|
|
}
|
|
|
|
|
2016-06-25 22:23:20 +02:00
|
|
|
// NewObject finds the Object at remote. If it can't be found it
|
|
|
|
// returns the error fs.ErrorObjectNotFound.
|
|
|
|
func (f *Fs) NewObject(remote string) (fs.Object, error) {
|
2016-06-25 22:58:34 +02:00
|
|
|
return f.newObjectWithInfo(remote, nil)
|
2015-12-07 05:01:03 +01:00
|
|
|
}
|
|
|
|
|
2016-06-25 22:58:34 +02:00
|
|
|
// Return an Object from a path
|
2015-12-07 05:01:03 +01:00
|
|
|
//
|
2016-06-25 22:23:20 +02:00
|
|
|
// If it can't be found it returns the error fs.ErrorObjectNotFound.
|
2017-02-25 16:23:27 +01:00
|
|
|
func (f *Fs) newObjectWithInfo(remote string, info *yandex.ResourceInfoResponse) (fs.Object, error) {
|
|
|
|
o := &Object{
|
2015-12-07 05:01:03 +01:00
|
|
|
fs: f,
|
|
|
|
remote: remote,
|
|
|
|
}
|
2017-02-25 16:23:27 +01:00
|
|
|
var err error
|
2015-12-07 05:01:03 +01:00
|
|
|
if info != nil {
|
2017-02-25 12:09:57 +01:00
|
|
|
err = o.setMetaData(info)
|
2015-12-07 05:01:03 +01:00
|
|
|
} else {
|
2017-02-25 12:09:57 +01:00
|
|
|
err = o.readMetaData()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-12-07 05:01:03 +01:00
|
|
|
}
|
2016-06-25 22:23:20 +02:00
|
|
|
return o, nil
|
2015-12-07 05:01:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// setMetaData sets the fs data from a storage.Object
|
2017-02-25 12:09:57 +01:00
|
|
|
func (o *Object) setMetaData(info *yandex.ResourceInfoResponse) (err error) {
|
|
|
|
if info.ResourceType != "file" {
|
|
|
|
return errors.Wrapf(fs.ErrorNotAFile, "%q", o.remote)
|
|
|
|
}
|
2015-12-07 05:01:03 +01:00
|
|
|
o.bytes = info.Size
|
|
|
|
o.md5sum = info.Md5
|
2016-09-21 23:13:24 +02:00
|
|
|
o.mimeType = info.MimeType
|
2015-12-07 05:01:03 +01:00
|
|
|
|
2016-09-21 23:13:24 +02:00
|
|
|
var modTimeString string
|
|
|
|
modTimeObj, ok := info.CustomProperties["rclone_modified"]
|
|
|
|
if ok {
|
|
|
|
// read modTime from rclone_modified custom_property of object
|
|
|
|
modTimeString, ok = modTimeObj.(string)
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
// read modTime from Modified property of object as a fallback
|
|
|
|
modTimeString = info.Modified
|
|
|
|
}
|
|
|
|
t, err := time.Parse(time.RFC3339Nano, modTimeString)
|
|
|
|
if err != nil {
|
2017-02-25 12:09:57 +01:00
|
|
|
return errors.Wrapf(err, "failed to parse modtime from %q", modTimeString)
|
2015-12-07 05:01:03 +01:00
|
|
|
}
|
2017-02-25 12:09:57 +01:00
|
|
|
o.modTime = t
|
|
|
|
return nil
|
2015-12-07 05:01:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// readMetaData gets the info if it hasn't already been fetched
|
|
|
|
func (o *Object) readMetaData() (err error) {
|
2016-09-21 23:13:24 +02:00
|
|
|
// exit if already fetched
|
|
|
|
if !o.modTime.IsZero() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-07 05:01:03 +01:00
|
|
|
//request meta info
|
|
|
|
var opt2 yandex.ResourceInfoRequestOptions
|
|
|
|
ResourceInfoResponse, err := o.fs.yd.NewResourceInfoRequest(o.remotePath(), opt2).Exec()
|
|
|
|
if err != nil {
|
2016-06-25 22:23:20 +02:00
|
|
|
if dcErr, ok := err.(yandex.DiskClientError); ok {
|
|
|
|
if dcErr.Code == "DiskNotFoundError" {
|
|
|
|
return fs.ErrorObjectNotFound
|
|
|
|
}
|
|
|
|
}
|
2015-12-07 05:01:03 +01:00
|
|
|
return err
|
|
|
|
}
|
2017-02-25 12:09:57 +01:00
|
|
|
return o.setMetaData(ResourceInfoResponse)
|
2015-12-07 05:01:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Put the object
|
|
|
|
//
|
|
|
|
// Copy the reader in to the new object which is returned
|
|
|
|
//
|
|
|
|
// The new object may have been created if an error is returned
|
2017-05-28 13:44:22 +02:00
|
|
|
func (f *Fs) Put(in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) (fs.Object, error) {
|
2016-02-18 12:35:25 +01:00
|
|
|
remote := src.Remote()
|
|
|
|
size := src.Size()
|
|
|
|
modTime := src.ModTime()
|
|
|
|
|
2015-12-07 05:01:03 +01:00
|
|
|
o := &Object{
|
|
|
|
fs: f,
|
|
|
|
remote: remote,
|
|
|
|
bytes: uint64(size),
|
|
|
|
modTime: modTime,
|
|
|
|
}
|
|
|
|
//TODO maybe read metadata after upload to check if file uploaded successfully
|
2017-05-28 13:44:22 +02:00
|
|
|
return o, o.Update(in, src, options...)
|
2015-12-07 05:01:03 +01:00
|
|
|
}
|
|
|
|
|
2017-08-19 14:07:23 +02:00
|
|
|
// PutStream uploads to the remote path with the modTime given of indeterminate size
|
|
|
|
func (f *Fs) PutStream(in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) (fs.Object, error) {
|
|
|
|
return f.Put(in, src, options...)
|
|
|
|
}
|
|
|
|
|
2015-12-07 05:01:03 +01:00
|
|
|
// Mkdir creates the container if it doesn't exist
|
2016-11-25 22:52:43 +01:00
|
|
|
func (f *Fs) Mkdir(dir string) error {
|
|
|
|
root := f.diskRoot
|
|
|
|
if dir != "" {
|
|
|
|
root += dir + "/"
|
|
|
|
}
|
|
|
|
return mkDirFullPath(f.yd, root)
|
2015-12-07 05:01:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rmdir deletes the container
|
|
|
|
//
|
|
|
|
// Returns an error if it isn't empty
|
2016-11-25 22:52:43 +01:00
|
|
|
func (f *Fs) Rmdir(dir string) error {
|
|
|
|
return f.purgeCheck(dir, true)
|
2015-12-07 05:01:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// purgeCheck remotes the root directory, if check is set then it
|
|
|
|
// refuses to do so if it has anything in
|
2016-11-25 22:52:43 +01:00
|
|
|
func (f *Fs) purgeCheck(dir string, check bool) error {
|
|
|
|
root := f.diskRoot
|
|
|
|
if dir != "" {
|
|
|
|
root += dir + "/"
|
|
|
|
}
|
2015-12-07 05:01:03 +01:00
|
|
|
if check {
|
|
|
|
//to comply with rclone logic we check if the directory is empty before delete.
|
|
|
|
//send request to get list of objects in this directory.
|
|
|
|
var opt yandex.ResourceInfoRequestOptions
|
2016-11-25 22:52:43 +01:00
|
|
|
ResourceInfoResponse, err := f.yd.NewResourceInfoRequest(root, opt).Exec()
|
2015-12-07 05:01:03 +01:00
|
|
|
if err != nil {
|
2016-06-12 16:06:02 +02:00
|
|
|
return errors.Wrap(err, "rmdir failed")
|
2015-12-07 05:01:03 +01:00
|
|
|
}
|
|
|
|
if len(ResourceInfoResponse.Embedded.Items) != 0 {
|
2016-06-12 16:06:02 +02:00
|
|
|
return errors.New("rmdir failed: directory not empty")
|
2015-12-07 05:01:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//delete directory
|
2016-11-25 22:52:43 +01:00
|
|
|
return f.yd.Delete(root, true)
|
2015-12-07 05:01:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Precision return the precision of this Fs
|
|
|
|
func (f *Fs) Precision() time.Duration {
|
2015-12-30 21:47:44 +01:00
|
|
|
return time.Nanosecond
|
2015-12-07 05:01:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Purge deletes all the files and the container
|
|
|
|
//
|
|
|
|
// Optional interface: Only implement this if you have a way of
|
|
|
|
// deleting all the files quicker than just running Remove() on the
|
|
|
|
// result of List()
|
|
|
|
func (f *Fs) Purge() error {
|
2016-11-25 22:52:43 +01:00
|
|
|
return f.purgeCheck("", false)
|
2015-12-07 05:01:03 +01:00
|
|
|
}
|
|
|
|
|
2017-09-07 19:10:39 +02:00
|
|
|
// CleanUp permanently deletes all trashed files/folders
|
|
|
|
func (f *Fs) CleanUp() error {
|
|
|
|
return f.yd.EmptyTrash()
|
|
|
|
}
|
|
|
|
|
2016-01-11 13:39:33 +01:00
|
|
|
// Hashes returns the supported hash sets.
|
|
|
|
func (f *Fs) Hashes() fs.HashSet {
|
|
|
|
return fs.HashSet(fs.HashMD5)
|
|
|
|
}
|
|
|
|
|
2015-12-07 05:01:03 +01:00
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
|
|
|
// Fs returns the parent Fs
|
2016-02-18 12:35:25 +01:00
|
|
|
func (o *Object) Fs() fs.Info {
|
2015-12-07 05:01:03 +01: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 13:39:33 +01:00
|
|
|
// Hash returns the Md5sum of an object returning a lowercase hex string
|
|
|
|
func (o *Object) Hash(t fs.HashType) (string, error) {
|
|
|
|
if t != fs.HashMD5 {
|
|
|
|
return "", fs.ErrHashUnsupported
|
|
|
|
}
|
2015-12-07 05:01:03 +01:00
|
|
|
return o.md5sum, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns the size of an object in bytes
|
|
|
|
func (o *Object) Size() int64 {
|
|
|
|
var size = int64(o.bytes) //need to cast from uint64 in yandex disk to int64 in rclone. can cause overflow
|
|
|
|
return size
|
|
|
|
}
|
|
|
|
|
|
|
|
// ModTime returns the modification time of the object
|
|
|
|
//
|
|
|
|
// It attempts to read the objects mtime and if that isn't present the
|
|
|
|
// LastModified returned in the http headers
|
|
|
|
func (o *Object) ModTime() time.Time {
|
|
|
|
err := o.readMetaData()
|
|
|
|
if err != nil {
|
2017-02-09 12:01:20 +01:00
|
|
|
fs.Logf(o, "Failed to read metadata: %v", err)
|
2015-12-07 05:01:03 +01:00
|
|
|
return time.Now()
|
|
|
|
}
|
|
|
|
return o.modTime
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open an object for read
|
2016-09-10 12:29:57 +02:00
|
|
|
func (o *Object) Open(options ...fs.OpenOption) (in io.ReadCloser, err error) {
|
|
|
|
return o.fs.yd.Download(o.remotePath(), fs.OpenOptionHeaders(options))
|
2015-12-07 05:01:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove an object
|
|
|
|
func (o *Object) Remove() error {
|
|
|
|
return o.fs.yd.Delete(o.remotePath(), true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetModTime sets the modification time of the local fs object
|
|
|
|
//
|
|
|
|
// Commits the datastore
|
2016-03-22 16:07:10 +01:00
|
|
|
func (o *Object) SetModTime(modTime time.Time) error {
|
2015-12-07 05:01:03 +01:00
|
|
|
remote := o.remotePath()
|
2016-09-21 23:13:24 +02:00
|
|
|
// set custom_property 'rclone_modified' of object to modTime
|
|
|
|
err := o.fs.yd.SetCustomProperty(remote, "rclone_modified", modTime.Format(time.RFC3339Nano))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
o.modTime = modTime
|
|
|
|
return nil
|
2015-12-07 05:01:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Storable returns whether this object is storable
|
|
|
|
func (o *Object) Storable() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the remote path for the object
|
|
|
|
func (o *Object) remotePath() string {
|
|
|
|
return o.fs.diskRoot + o.remote
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the already existing object
|
|
|
|
//
|
|
|
|
// Copy the reader into the object updating modTime and size
|
|
|
|
//
|
|
|
|
// The new object may have been created if an error is returned
|
2017-08-19 14:07:23 +02:00
|
|
|
func (o *Object) Update(in0 io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) error {
|
|
|
|
in := fs.NewCountingReader(in0)
|
2016-02-18 12:35:25 +01:00
|
|
|
modTime := src.ModTime()
|
|
|
|
|
2015-12-07 05:01:03 +01:00
|
|
|
remote := o.remotePath()
|
|
|
|
//create full path to file before upload.
|
|
|
|
err1 := mkDirFullPath(o.fs.yd, remote)
|
|
|
|
if err1 != nil {
|
|
|
|
return err1
|
|
|
|
}
|
|
|
|
//upload file
|
|
|
|
overwrite := true //overwrite existing file
|
2016-09-21 23:13:24 +02:00
|
|
|
mimeType := fs.MimeType(src)
|
|
|
|
err := o.fs.yd.Upload(in, remote, overwrite, mimeType)
|
2015-12-07 05:01:03 +01:00
|
|
|
if err == nil {
|
2016-02-18 12:35:25 +01:00
|
|
|
//if file uploaded sucessfully then return metadata
|
2017-08-19 14:07:23 +02:00
|
|
|
o.bytes = in.BytesRead()
|
2015-12-07 05:01:03 +01:00
|
|
|
o.modTime = modTime
|
|
|
|
o.md5sum = "" // according to unit tests after put the md5 is empty.
|
|
|
|
//and set modTime of uploaded file
|
2016-03-22 16:07:10 +01:00
|
|
|
err = o.SetModTime(modTime)
|
2015-12-07 05:01:03 +01:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// utility funcs-------------------------------------------------------------------
|
|
|
|
|
|
|
|
// mkDirExecute execute mkdir
|
|
|
|
func mkDirExecute(client *yandex.Client, path string) (int, string, error) {
|
|
|
|
statusCode, jsonErrorString, err := client.Mkdir(path)
|
|
|
|
if statusCode == 409 { // dir already exist
|
|
|
|
return statusCode, jsonErrorString, err
|
|
|
|
}
|
|
|
|
if statusCode == 201 { // dir was created
|
|
|
|
return statusCode, jsonErrorString, err
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
// error creating directory
|
2016-06-06 22:23:54 +02:00
|
|
|
return statusCode, jsonErrorString, errors.Wrap(err, "failed to create folder")
|
2015-12-07 05:01:03 +01:00
|
|
|
}
|
|
|
|
return 0, "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//mkDirFullPath Creates Each Directory in the path if needed. Send request once for every directory in the path.
|
|
|
|
func mkDirFullPath(client *yandex.Client, path string) error {
|
|
|
|
//trim filename from path
|
|
|
|
dirString := strings.TrimSuffix(path, filepath.Base(path))
|
|
|
|
//trim "disk:/" from path
|
|
|
|
dirString = strings.TrimPrefix(dirString, "disk:/")
|
|
|
|
|
|
|
|
//1 Try to create directory first
|
|
|
|
if _, jsonErrorString, err := mkDirExecute(client, dirString); err != nil {
|
|
|
|
er2, _ := client.ParseAPIError(jsonErrorString)
|
|
|
|
if er2 != "DiskPathPointsToExistentDirectoryError" {
|
|
|
|
//2 if it fails then create all directories in the path from root.
|
|
|
|
dirs := strings.Split(dirString, "/") //path separator /
|
|
|
|
var mkdirpath = "/" //path separator /
|
|
|
|
for _, element := range dirs {
|
|
|
|
if element != "" {
|
|
|
|
mkdirpath += element + "/" //path separator /
|
|
|
|
_, _, err2 := mkDirExecute(client, mkdirpath)
|
|
|
|
if err2 != nil {
|
|
|
|
//we continue even if some directories exist.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-21 23:13:24 +02:00
|
|
|
// MimeType of an Object if known, "" otherwise
|
|
|
|
func (o *Object) MimeType() string {
|
|
|
|
err := o.readMetaData()
|
|
|
|
if err != nil {
|
2017-02-09 12:01:20 +01:00
|
|
|
fs.Logf(o, "Failed to read metadata: %v", err)
|
2016-09-21 23:13:24 +02:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return o.mimeType
|
|
|
|
}
|
|
|
|
|
2015-12-07 05:01:03 +01:00
|
|
|
// Check the interfaces are satisfied
|
|
|
|
var (
|
2017-08-19 14:07:23 +02:00
|
|
|
_ fs.Fs = (*Fs)(nil)
|
|
|
|
_ fs.Purger = (*Fs)(nil)
|
2017-09-30 17:33:39 +02:00
|
|
|
_ fs.CleanUpper = (*Fs)(nil)
|
2017-08-19 14:07:23 +02:00
|
|
|
_ fs.PutStreamer = (*Fs)(nil)
|
|
|
|
_ fs.ListRer = (*Fs)(nil)
|
2015-12-07 05:01:03 +01:00
|
|
|
//_ fs.Copier = (*Fs)(nil)
|
2017-06-11 23:43:31 +02:00
|
|
|
_ fs.ListRer = (*Fs)(nil)
|
2016-09-21 23:13:24 +02:00
|
|
|
_ fs.Object = (*Object)(nil)
|
|
|
|
_ fs.MimeTyper = &Object{}
|
2015-12-07 05:01:03 +01:00
|
|
|
)
|