forked from rclone/rclone
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ulozto.go
1276 lines (1052 loc) · 32.6 KB
/
ulozto.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Package ulozto provides an interface to the Uloz.to storage system.
package ulozto
import (
"bytes"
"context"
"encoding/base64"
"encoding/gob"
"encoding/hex"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"path"
"strconv"
"strings"
"time"
"github.com/artpar/rclone/backend/ulozto/api"
"github.com/artpar/rclone/fs"
"github.com/artpar/rclone/fs/config"
"github.com/artpar/rclone/fs/config/configmap"
"github.com/artpar/rclone/fs/config/configstruct"
"github.com/artpar/rclone/fs/config/obscure"
"github.com/artpar/rclone/fs/fserrors"
"github.com/artpar/rclone/fs/fshttp"
"github.com/artpar/rclone/fs/hash"
"github.com/artpar/rclone/lib/dircache"
"github.com/artpar/rclone/lib/encoder"
"github.com/artpar/rclone/lib/pacer"
"github.com/artpar/rclone/lib/rest"
)
// TODO Uloz.to only supports file names of 255 characters or less and silently truncates names that are longer.
const (
minSleep = 10 * time.Millisecond
maxSleep = 2 * time.Second
decayConstant = 2 // bigger for slower decay, exponential
rootURL = "https://apis.uloz.to"
)
// Options defines the configuration for this backend
type Options struct {
AppToken string `config:"app_token"`
Username string `config:"username"`
Password string `config:"password"`
RootFolderSlug string `config:"root_folder_slug"`
Enc encoder.MultiEncoder `config:"encoding"`
ListPageSize int `config:"list_page_size"`
}
func init() {
fs.Register(&fs.RegInfo{
Name: "ulozto",
Description: "Uloz.to",
NewFs: NewFs,
Options: []fs.Option{
{
Name: "app_token",
Default: "",
Help: `The application token identifying the app. An app API key can be either found in the API
doc https://uloz.to/upload-resumable-api-beta or obtained from customer service.`,
Sensitive: true,
},
{
Name: "username",
Default: "",
Help: "The username of the principal to operate as.",
Sensitive: true,
},
{
Name: "password",
Default: "",
Help: "The password for the user.",
IsPassword: true,
},
{
Name: "root_folder_slug",
Help: `If set, rclone will use this folder as the root folder for all operations. For example,
if the slug identifies 'foo/bar/', 'ulozto:baz' is equivalent to 'ulozto:foo/bar/baz' without
any root slug set.`,
Default: "",
Advanced: true,
Sensitive: true,
},
{
Name: "list_page_size",
Default: 500,
Help: "The size of a single page for list commands. 1-500",
Advanced: true,
},
{
Name: config.ConfigEncoding,
Help: config.ConfigEncodingHelp,
Advanced: true,
Default: encoder.Display | encoder.EncodeInvalidUtf8 | encoder.EncodeBackSlash,
},
}})
}
// Fs represents a remote uloz.to storage
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
rest *rest.Client // REST client with authentication headers set, used to communicate with API endpoints
cdn *rest.Client // REST client without authentication headers set, used for CDN payload upload/download
dirCache *dircache.DirCache // Map of directory path to directory id
pacer *fs.Pacer // pacer for API calls
}
// NewFs constructs a Fs from the path, container:path
func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, error) {
// Parse config into Options struct
opt := new(Options)
err := configstruct.Set(m, opt)
if err != nil {
return nil, err
}
client := fshttp.NewClient(ctx)
f := &Fs{
name: name,
root: root,
opt: *opt,
cdn: rest.NewClient(client),
rest: rest.NewClient(client).SetRoot(rootURL),
pacer: fs.NewPacer(ctx, pacer.NewDefault(pacer.MinSleep(minSleep), pacer.MaxSleep(maxSleep), pacer.DecayConstant(decayConstant))),
}
f.features = (&fs.Features{
DuplicateFiles: true,
CanHaveEmptyDirectories: true,
}).Fill(ctx, f)
f.rest.SetErrorHandler(errorHandler)
f.rest.SetHeader("X-Auth-Token", f.opt.AppToken)
auth, err := f.authenticate(ctx)
if err != nil {
return f, err
}
var rootSlug string
if opt.RootFolderSlug == "" {
rootSlug = auth.Session.User.RootFolderSlug
} else {
rootSlug = opt.RootFolderSlug
}
f.dirCache = dircache.New(root, rootSlug, f)
err = f.dirCache.FindRoot(ctx, false)
if errors.Is(err, fs.ErrorDirNotFound) {
// All good, we'll create the folder later on.
return f, nil
}
if errors.Is(err, fs.ErrorIsFile) {
rootFolder, _ := dircache.SplitPath(root)
f.root = rootFolder
f.dirCache = dircache.New(rootFolder, rootSlug, f)
err = f.dirCache.FindRoot(ctx, false)
if err != nil {
return f, err
}
return f, fs.ErrorIsFile
}
return f, err
}
// 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.Debugf(nil, "Couldn't decode error response: %v", err)
}
if errResponse.StatusCode == 0 {
errResponse.StatusCode = resp.StatusCode
}
return errResponse
}
// 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
}
// shouldRetry returns a boolean whether this resp and err should be retried.
// It also returns the err for convenience.
func (f *Fs) shouldRetry(ctx context.Context, resp *http.Response, err error, reauth bool) (bool, error) {
if err == nil {
return false, nil
}
if fserrors.ContextError(ctx, &err) {
return false, err
}
var apiErr *api.Error
if resp != nil && resp.StatusCode == 401 && errors.As(err, &apiErr) && apiErr.ErrorCode == 70001 {
fs.Debugf(nil, "Should retry: %v", err)
if reauth {
_, err = f.authenticate(ctx)
if err != nil {
return false, err
}
}
return true, err
}
return fserrors.ShouldRetry(err) || fserrors.ShouldRetryHTTP(resp, retryErrorCodes), err
}
func (f *Fs) authenticate(ctx context.Context) (response *api.AuthenticateResponse, err error) {
// TODO only reauth once if the token expires
// Remove the old user token
f.rest.RemoveHeader("X-User-Token")
opts := rest.Opts{
Method: "PUT",
Path: "/v6/session",
}
clearPassword, err := obscure.Reveal(f.opt.Password)
if err != nil {
return nil, err
}
authRequest := api.AuthenticateRequest{
Login: f.opt.Username,
Password: clearPassword,
}
err = f.pacer.Call(func() (bool, error) {
httpResp, err := f.rest.CallJSON(ctx, &opts, &authRequest, &response)
return f.shouldRetry(ctx, httpResp, err, false)
})
if err != nil {
return nil, err
}
f.rest.SetHeader("X-User-Token", response.TokenID)
return response, nil
}
// UploadSession represents a single Uloz.to upload session.
//
// Uloz.to supports uploading multiple files at once and committing them atomically. This functionality isn't being used
// by the backend implementation and for simplicity, each session corresponds to a single file being uploaded.
type UploadSession struct {
Filesystem *Fs
URL string
PrivateSlug string
ValidUntil time.Time
}
func (f *Fs) createUploadSession(ctx context.Context) (session *UploadSession, err error) {
session = &UploadSession{
Filesystem: f,
}
err = session.renewUploadSession(ctx)
if err != nil {
return nil, err
}
return session, nil
}
func (session *UploadSession) renewUploadSession(ctx context.Context) error {
opts := rest.Opts{
Method: "POST",
Path: "/v5/upload/link",
Parameters: url.Values{},
}
createUploadURLReq := api.CreateUploadURLRequest{
UserLogin: session.Filesystem.opt.Username,
Realm: "ulozto",
}
if session.PrivateSlug != "" {
createUploadURLReq.ExistingSessionSlug = session.PrivateSlug
}
var err error
var response api.CreateUploadURLResponse
err = session.Filesystem.pacer.Call(func() (bool, error) {
httpResp, err := session.Filesystem.rest.CallJSON(ctx, &opts, &createUploadURLReq, &response)
return session.Filesystem.shouldRetry(ctx, httpResp, err, true)
})
if err != nil {
return err
}
session.PrivateSlug = response.PrivateSlug
session.URL = response.UploadURL
session.ValidUntil = response.ValidUntil
return nil
}
func (f *Fs) uploadUnchecked(ctx context.Context, name, parentSlug string, info fs.ObjectInfo, payload io.Reader) (fs.Object, error) {
session, err := f.createUploadSession(ctx)
if err != nil {
return nil, err
}
hashes := hash.NewHashSet(hash.MD5, hash.SHA256)
hasher, err := hash.NewMultiHasherTypes(hashes)
if err != nil {
return nil, err
}
payload = io.TeeReader(payload, hasher)
encodedName := f.opt.Enc.FromStandardName(name)
opts := rest.Opts{
Method: "POST",
Body: payload,
// Not using Parameters as the session URL has parameters itself
RootURL: session.URL + "&batch_file_id=1&is_porn=false",
MultipartContentName: "file",
MultipartFileName: encodedName,
Parameters: url.Values{},
}
if info.Size() > 0 {
size := info.Size()
opts.ContentLength = &size
}
var uploadResponse api.SendFilePayloadResponse
err = f.pacer.CallNoRetry(func() (bool, error) {
httpResp, err := f.cdn.CallJSON(ctx, &opts, nil, &uploadResponse)
return f.shouldRetry(ctx, httpResp, err, true)
})
if err != nil {
return nil, err
}
sha256digest, err := hasher.Sum(hash.SHA256)
if err != nil {
return nil, err
}
md5digest, err := hasher.Sum(hash.MD5)
if err != nil {
return nil, err
}
if hex.EncodeToString(md5digest) != uploadResponse.Md5 {
return nil, errors.New("MD5 digest mismatch")
}
metadata := DescriptionEncodedMetadata{
Md5Hash: md5digest,
Sha256Hash: sha256digest,
ModTimeEpochMicros: info.ModTime(ctx).UnixMicro(),
}
encodedMetadata, err := metadata.encode()
if err != nil {
return nil, err
}
// Successfully uploaded, now move the file where it belongs and commit it
updateReq := api.BatchUpdateFilePropertiesRequest{
Name: encodedName,
FolderSlug: parentSlug,
Description: encodedMetadata,
Slugs: []string{uploadResponse.Slug},
UploadTokens: map[string]string{uploadResponse.Slug: session.PrivateSlug + ":1"},
}
var updateResponse []api.File
opts = rest.Opts{
Method: "PATCH",
Path: "/v8/file-list/private",
Parameters: url.Values{},
}
err = f.pacer.Call(func() (bool, error) {
httpResp, err := session.Filesystem.rest.CallJSON(ctx, &opts, &updateReq, &updateResponse)
return f.shouldRetry(ctx, httpResp, err, true)
})
if err != nil {
return nil, err
}
if len(updateResponse) != 1 {
return nil, errors.New("unexpected number of files in the response")
}
opts = rest.Opts{
Method: "PATCH",
Path: "/v8/upload-batch/private/" + session.PrivateSlug,
Parameters: url.Values{},
}
commitRequest := api.CommitUploadBatchRequest{
Status: "confirmed",
OwnerLogin: f.opt.Username,
}
var commitResponse api.CommitUploadBatchResponse
err = f.pacer.Call(func() (bool, error) {
httpResp, err := session.Filesystem.rest.CallJSON(ctx, &opts, &commitRequest, &commitResponse)
return f.shouldRetry(ctx, httpResp, err, true)
})
if err != nil {
return nil, err
}
file, err := f.newObjectWithInfo(ctx, info.Remote(), &updateResponse[0])
return file, err
}
// Put implements the mandatory method fs.Fs.Put.
func (f *Fs) Put(ctx context.Context, in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) (fs.Object, error) {
existingObj, err := f.NewObject(ctx, src.Remote())
switch {
case err == nil:
return existingObj, existingObj.Update(ctx, in, src, options...)
case errors.Is(err, fs.ErrorObjectNotFound):
// Not found so create it
return f.PutUnchecked(ctx, in, src, options...)
default:
return nil, err
}
}
// PutUnchecked implements the optional interface fs.PutUncheckeder.
//
// Uloz.to allows to have multiple files of the same name in the same folder.
func (f *Fs) PutUnchecked(ctx context.Context, in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) (fs.Object, error) {
filename, folderSlug, err := f.dirCache.FindPath(ctx, src.Remote(), true)
if err != nil {
return nil, err
}
return f.uploadUnchecked(ctx, filename, folderSlug, src, in)
}
// Mkdir implements the mandatory method fs.Fs.Mkdir.
func (f *Fs) Mkdir(ctx context.Context, dir string) (err error) {
_, err = f.dirCache.FindDir(ctx, dir, true)
return err
}
func (f *Fs) isDirEmpty(ctx context.Context, slug string) (empty bool, err error) {
folders, err := f.fetchListFolderPage(ctx, slug, "", 1, 0)
if err != nil {
return false, err
}
if len(folders) > 0 {
return false, nil
}
files, err := f.fetchListFilePage(ctx, slug, "", 1, 0)
if err != nil {
return false, err
}
if len(files) > 0 {
return false, nil
}
return true, nil
}
// Rmdir implements the mandatory method fs.Fs.Rmdir.
func (f *Fs) Rmdir(ctx context.Context, dir string) error {
slug, err := f.dirCache.FindDir(ctx, dir, false)
if err != nil {
return err
}
empty, err := f.isDirEmpty(ctx, slug)
if err != nil {
return err
}
if !empty {
return fs.ErrorDirectoryNotEmpty
}
opts := rest.Opts{
Method: "DELETE",
Path: "/v5/user/" + f.opt.Username + "/folder-list",
}
req := api.DeleteFoldersRequest{Slugs: []string{slug}}
err = f.pacer.Call(func() (bool, error) {
httpResp, err := f.rest.CallJSON(ctx, &opts, req, nil)
return f.shouldRetry(ctx, httpResp, err, true)
})
if err != nil {
return err
}
f.dirCache.FlushDir(dir)
return nil
}
// Move implements the optional method fs.Mover.Move.
func (f *Fs) Move(ctx context.Context, src fs.Object, remote string) (fs.Object, error) {
if remote == src.Remote() {
// Already there, do nothing
return src, nil
}
srcObj, ok := src.(*Object)
if !ok {
fs.Debugf(src, "Can't move - not same remote type")
return nil, fs.ErrorCantMove
}
filename, folderSlug, err := f.dirCache.FindPath(ctx, remote, true)
if err != nil {
return nil, err
}
newObj := &Object{}
newObj.copyFrom(srcObj)
newObj.remote = remote
return newObj, newObj.updateFileProperties(ctx, api.MoveFileRequest{
ParentFolderSlug: folderSlug,
NewFilename: filename,
})
}
// DirMove implements the optional method fs.DirMover.DirMove.
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
}
srcSlug, _, srcName, dstParentSlug, dstName, err := f.dirCache.DirMove(ctx, srcFs.dirCache, srcFs.root, srcRemote, f.root, dstRemote)
if err != nil {
return err
}
opts := rest.Opts{
Method: "PATCH",
Path: "/v6/user/" + f.opt.Username + "/folder-list/parent-folder",
}
req := api.MoveFolderRequest{
FolderSlugs: []string{srcSlug},
NewParentFolderSlug: dstParentSlug,
}
err = f.pacer.Call(func() (bool, error) {
httpResp, err := f.rest.CallJSON(ctx, &opts, &req, nil)
return f.shouldRetry(ctx, httpResp, err, true)
})
if err != nil {
return err
}
// The old folder doesn't exist anymore so clear the cache now instead of after renaming
srcFs.dirCache.FlushDir(srcRemote)
if srcName != dstName {
// There's no endpoint to rename the folder alongside moving it, so this has to happen separately.
opts = rest.Opts{
Method: "PATCH",
Path: "/v7/user/" + f.opt.Username + "/folder/" + srcSlug,
}
renameReq := api.RenameFolderRequest{
NewName: dstName,
}
err = f.pacer.Call(func() (bool, error) {
httpResp, err := f.rest.CallJSON(ctx, &opts, &renameReq, nil)
return f.shouldRetry(ctx, httpResp, err, true)
})
return err
}
return nil
}
// 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("uloz.to root '%s'", f.root)
}
// Features returns the optional features of this Fs
func (f *Fs) Features() *fs.Features {
return f.features
}
// Precision return the precision of this Fs
func (f *Fs) Precision() time.Duration {
return time.Microsecond
}
// Hashes implements fs.Fs.Hashes by returning the supported hash types of the filesystem.
func (f *Fs) Hashes() hash.Set {
return hash.NewHashSet(hash.SHA256, hash.MD5)
}
// DescriptionEncodedMetadata represents a set of metadata encoded as Uloz.to description.
//
// Uloz.to doesn't support setting metadata such as mtime but allows the user to set an arbitrary description field.
// The content of this structure will be serialized and stored in the backend.
//
// The files themselves are immutable so there's no danger that the file changes, and we'll forget to update the hashes.
// It is theoretically possible to rewrite the description to provide incorrect information for a file. However, in case
// it's a real attack vector, a nefarious person already has write access to the repo, and the situation is above
// rclone's pay grade already.
type DescriptionEncodedMetadata struct {
Md5Hash []byte // The MD5 hash of the file
Sha256Hash []byte // The SHA256 hash of the file
ModTimeEpochMicros int64 // The mtime of the file, as set by rclone
}
func (md *DescriptionEncodedMetadata) encode() (string, error) {
b := bytes.Buffer{}
e := gob.NewEncoder(&b)
err := e.Encode(md)
if err != nil {
return "", err
}
// Version the encoded string from the beginning even though we don't need it yet.
return "1;" + base64.StdEncoding.EncodeToString(b.Bytes()), nil
}
func decodeDescriptionMetadata(str string) (*DescriptionEncodedMetadata, error) {
// The encoded data starts with a version number which is not a part iof the serialized object
spl := strings.SplitN(str, ";", 2)
if len(spl) < 2 || spl[0] != "1" {
return nil, errors.New("can't decode, unknown encoded metadata version")
}
m := DescriptionEncodedMetadata{}
by, err := base64.StdEncoding.DecodeString(spl[1])
if err != nil {
return nil, err
}
b := bytes.Buffer{}
b.Write(by)
d := gob.NewDecoder(&b)
err = d.Decode(&m)
if err != nil {
return nil, err
}
return &m, nil
}
// Object describes an uloz.to object.
//
// Valid objects will always have all fields but encodedMetadata set.
type Object struct {
fs *Fs // what this object is part of
remote string // The remote path
name string // The file name
size int64 // size of the object
slug string // ID of the object
remoteFsMtime time.Time // The time the object was last modified in the remote fs.
// Metadata not available natively and encoded in the description field. May not be present if the encoded metadata
// is not present (e.g. if file wasn't uploaded by rclone) or invalid.
encodedMetadata *DescriptionEncodedMetadata
}
// Storable implements the mandatory method fs.ObjectInfo.Storable
func (o *Object) Storable() bool {
return true
}
func (o *Object) updateFileProperties(ctx context.Context, req interface{}) (err error) {
var resp *api.File
opts := rest.Opts{
Method: "PATCH",
Path: "/v8/file/" + o.slug + "/private",
}
err = o.fs.pacer.Call(func() (bool, error) {
httpResp, err := o.fs.rest.CallJSON(ctx, &opts, &req, &resp)
return o.fs.shouldRetry(ctx, httpResp, err, true)
})
if err != nil {
return err
}
return o.setMetaData(resp)
}
// SetModTime implements the mandatory method fs.Object.SetModTime
func (o *Object) SetModTime(ctx context.Context, t time.Time) (err error) {
var newMetadata DescriptionEncodedMetadata
if o.encodedMetadata == nil {
newMetadata = DescriptionEncodedMetadata{}
} else {
newMetadata = *o.encodedMetadata
}
newMetadata.ModTimeEpochMicros = t.UnixMicro()
encoded, err := newMetadata.encode()
if err != nil {
return err
}
return o.updateFileProperties(ctx, api.UpdateDescriptionRequest{
Description: encoded,
})
}
// Open implements the mandatory method fs.Object.Open
func (o *Object) Open(ctx context.Context, options ...fs.OpenOption) (rc io.ReadCloser, err error) {
opts := rest.Opts{
Method: "POST",
Path: "/v5/file/download-link/vipdata",
}
req := &api.GetDownloadLinkRequest{
Slug: o.slug,
UserLogin: o.fs.opt.Username,
// Has to be set but doesn't seem to be used server side.
DeviceID: "foobar",
}
var resp *api.GetDownloadLinkResponse
err = o.fs.pacer.Call(func() (bool, error) {
httpResp, err := o.fs.rest.CallJSON(ctx, &opts, &req, &resp)
return o.fs.shouldRetry(ctx, httpResp, err, true)
})
if err != nil {
return nil, err
}
opts = rest.Opts{
Method: "GET",
RootURL: resp.Link,
Options: options,
}
var httpResp *http.Response
err = o.fs.pacer.Call(func() (bool, error) {
httpResp, err = o.fs.cdn.Call(ctx, &opts)
return o.fs.shouldRetry(ctx, httpResp, err, true)
})
if err != nil {
return nil, err
}
return httpResp.Body, err
}
func (o *Object) copyFrom(other *Object) {
o.fs = other.fs
o.remote = other.remote
o.size = other.size
o.slug = other.slug
o.remoteFsMtime = other.remoteFsMtime
o.encodedMetadata = other.encodedMetadata
}
// RenamingObjectInfoProxy is a delegating proxy for fs.ObjectInfo
// with the option of specifying a different remote path.
type RenamingObjectInfoProxy struct {
delegate fs.ObjectInfo
remote string
}
// Remote implements fs.ObjectInfo.Remote by delegating to the wrapped instance.
func (s *RenamingObjectInfoProxy) String() string {
return s.delegate.String()
}
// Remote implements fs.ObjectInfo.Remote by returning the specified remote path.
func (s *RenamingObjectInfoProxy) Remote() string {
return s.remote
}
// ModTime implements fs.ObjectInfo.ModTime by delegating to the wrapped instance.
func (s *RenamingObjectInfoProxy) ModTime(ctx context.Context) time.Time {
return s.delegate.ModTime(ctx)
}
// Size implements fs.ObjectInfo.Size by delegating to the wrapped instance.
func (s *RenamingObjectInfoProxy) Size() int64 {
return s.delegate.Size()
}
// Fs implements fs.ObjectInfo.Fs by delegating to the wrapped instance.
func (s *RenamingObjectInfoProxy) Fs() fs.Info {
return s.delegate.Fs()
}
// Hash implements fs.ObjectInfo.Hash by delegating to the wrapped instance.
func (s *RenamingObjectInfoProxy) Hash(ctx context.Context, ty hash.Type) (string, error) {
return s.delegate.Hash(ctx, ty)
}
// Storable implements fs.ObjectInfo.Storable by delegating to the wrapped instance.
func (s *RenamingObjectInfoProxy) Storable() bool {
return s.delegate.Storable()
}
// Update implements the mandatory method fs.Object.Update
func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) error {
// The backend allows to store multiple files with the same name, so simply upload the new file and remove the old
// one afterwards.
info := &RenamingObjectInfoProxy{
delegate: src,
remote: o.Remote(),
}
newo, err := o.fs.PutUnchecked(ctx, in, info, options...)
if err != nil {
return err
}
err = o.Remove(ctx)
if err != nil {
return err
}
o.copyFrom(newo.(*Object))
return nil
}
// Remove implements the mandatory method fs.Object.Remove
func (o *Object) Remove(ctx context.Context) error {
for i := 0; i < 2; i++ {
// First call moves the item to recycle bin, second deletes it for good
var err error
opts := rest.Opts{
Method: "DELETE",
Path: "/v6/file/" + o.slug + "/private",
}
err = o.fs.pacer.Call(func() (bool, error) {
httpResp, err := o.fs.rest.CallJSON(ctx, &opts, nil, nil)
return o.fs.shouldRetry(ctx, httpResp, err, true)
})
if err != nil {
return err
}
}
return nil
}
// ModTime implements the mandatory method fs.Object.ModTime
func (o *Object) ModTime(ctx context.Context) time.Time {
if o.encodedMetadata != nil {
return time.UnixMicro(o.encodedMetadata.ModTimeEpochMicros)
}
// The time the object was last modified on the server - a handwavy guess, but we don't have any better
return o.remoteFsMtime
}
// Fs implements the mandatory method fs.Object.Fs
func (o *Object) Fs() fs.Info {
return o.fs
}
// String returns the string representation of the remote object reference.
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
}
// Size returns the size of an object in bytes
func (o *Object) Size() int64 {
return o.size
}
// Hash implements the mandatory method fs.Object.Hash.
//
// Supports SHA256 and MD5 hashes.
func (o *Object) Hash(ctx context.Context, t hash.Type) (string, error) {
if t != hash.MD5 && t != hash.SHA256 {
return "", hash.ErrUnsupported
}
if o.encodedMetadata == nil {
return "", nil
}
switch t {
case hash.MD5:
return hex.EncodeToString(o.encodedMetadata.Md5Hash), nil
case hash.SHA256:
return hex.EncodeToString(o.encodedMetadata.Sha256Hash), nil
}
panic("Should never get here")
}
// FindLeaf implements dircache.DirCacher.FindLeaf by successively walking through the folder hierarchy until
// the desired folder is found, or there's nowhere to continue.
func (f *Fs) FindLeaf(ctx context.Context, folderSlug, leaf string) (leafSlug string, found bool, err error) {
folders, err := f.listFolders(ctx, folderSlug, leaf)
if err != nil {
if errors.Is(err, fs.ErrorDirNotFound) {
return "", false, nil
}
return "", false, err
}
for _, folder := range folders {
if folder.Name == leaf {
return folder.Slug, true, nil
}
}
// Uloz.to allows creation of multiple files / folders with the same name in the same parent folder. rclone always
// expects folder paths to be unique (no other file or folder with the same name should exist). As a result we also
// need to look at the files to return the correct error if necessary.
files, err := f.listFiles(ctx, folderSlug, leaf)
if err != nil {
return "", false, err
}
for _, file := range files {
if file.Name == leaf {
return "", false, fs.ErrorIsFile
}
}
// The parent folder exists but no file or folder with the given name was found in it.
return "", false, nil
}
// CreateDir implements dircache.DirCacher.CreateDir by creating a folder with the given name under a folder identified
// by parentSlug.
func (f *Fs) CreateDir(ctx context.Context, parentSlug, leaf string) (newID string, err error) {
var folder *api.Folder