Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
lostb1t committed Sep 22, 2024
1 parent ccafb34 commit cb4884d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 49 deletions.
20 changes: 5 additions & 15 deletions backend/icloud/api/drive.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ type UpdateFileInfo struct {
Receipt string `json:"receipt,omitempty"`
ReferenceSignature string `json:"reference_signature,omitempty"`
Signature string `json:"signature,omitempty"`
Size int `json:"size,omitempty"`
Size int64 `json:"size,omitempty"`
WrappingKey string `json:"wrapping_key,omitempty"`
} `json:"data,omitempty"`
DocumentID string `json:"document_id"`
Expand Down Expand Up @@ -653,22 +653,12 @@ func (d *DriveItemRaw) CreatedTime() time.Time {
return time.UnixMilli(i)
}

// Size returns the size of the drive item.
//
// It parses the size from the ItemInfo struct and returns it as an int64.
func (d *DriveItemRaw) Size() int64 {
if n, err := strconv.ParseInt(d.ItemInfo.Size, 10, 64); err == nil {
return n
}
return 0
}

// DriveItemRawInfo is the raw information about a drive item.
type DriveItemRawInfo struct {
Name string `json:"name"`
// Extension is absolutely borked on endpoints so dont use it.
Extension string `json:"extension"`
Size string `json:"size"`
Size int64 `json:"size,string`
Type string `json:"type"`
Version string `json:"version"`
ModifiedAt string `json:"modified_at"`
Expand All @@ -692,7 +682,7 @@ func (d *DriveItemRaw) IntoDriveItem() *DriveItem {
Etag: d.ItemInfo.Version,
DateModified: d.ModTime(),
DateCreated: d.CreatedTime(),
Size: d.Size(),
Size: d.ItemInfo.Size,
Urls: d.ItemInfo.Urls,
}
}
Expand Down Expand Up @@ -757,7 +747,7 @@ func (d *Document) DriveID() string {
type DocumentData struct {
Signature string `json:"signature"`
Owner string `json:"owner"`
Size int `json:"size"`
Size int64 `json:"size"`
ReferenceSignature string `json:"reference_signature"`
WrappingKey string `json:"wrapping_key"`
PcsInfo string `json:"pcsInfo"`
Expand All @@ -771,7 +761,7 @@ type SingleFileResponse struct {
// SingleFileInfo represents the information of a single file.
type SingleFileInfo struct {
ReferenceSignature string `json:"referenceChecksum"`
Size int `json:"size"`
Size int64 `json:"size"`
Signature string `json:"fileChecksum"`
WrappingKey string `json:"wrappingKey"`
Receipt string `json:"receipt"`
Expand Down
71 changes: 38 additions & 33 deletions backend/icloud/icloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ func init() {
Required: true,
Sensitive: true,
}, {
// Password is not required, it will be left blank for 2FA
Name: configPassword,
Help: "Password.",
Required: true,
Expand All @@ -74,24 +73,23 @@ func init() {
Required: false,
Sensitive: true,
Hide: fs.OptionHideBoth,
}, {
Name: configCookies,
Help: "cookies (internal use only)",
Required: false,
Advanced: false,
Sensitive: true,
Hide: fs.OptionHideBoth,
},
{
Name: configCookies,
Help: "cookies (internal use only)",
Required: false,
Advanced: false,
Sensitive: true,
Hide: fs.OptionHideBoth,
},
{
Name: config.ConfigEncoding,
Help: config.ConfigEncodingHelp,
Advanced: true,
Default: (encoder.Display |
//encoder.EncodeDot |
encoder.EncodeBackSlash |
encoder.EncodeInvalidUtf8),
}},
{
Name: config.ConfigEncoding,
Help: config.ConfigEncodingHelp,
Advanced: true,
Default: (encoder.Display |
//encoder.EncodeDot |
encoder.EncodeBackSlash |
encoder.EncodeInvalidUtf8),
}},
})
}

Expand Down Expand Up @@ -296,15 +294,13 @@ func (f *Fs) Purge(ctx context.Context, dir string) error {
}

func (f *Fs) listAll(ctx context.Context, dirID string) (items []*api.DriveItem, err error) {
service, _ := f.icloud.DriveService()

var item *api.DriveItem
var resp *http.Response

if err = f.pacer.Call(func() (bool, error) {
id, _ := f.parseNormalizedID(dirID)
//itemsRaw, resp, err = service.GetItemsInFolder(ctx, id, 100000)
item, resp, err = service.GetItemByDriveID(ctx, "FOLDER::com.apple.CloudDocs::"+id, true)
item, resp, err = f.service.GetItemByDriveID(ctx, "FOLDER::com.apple.CloudDocs::"+id, true)
return shouldRetry(ctx, resp, err)
}); err != nil {
return nil, err
Expand All @@ -329,8 +325,12 @@ func (f *Fs) List(ctx context.Context, dir string) (entries fs.DirEntries, err e
}

entries = make(fs.DirEntries, 0)
items, _ := f.listAll(ctx, dirRemoteID)
items, err := f.listAll(ctx, dirRemoteID)

if err != nil {
return nil, err
}

for _, item := range items {
id := item.Docwsid
name := item.FullName()
Expand All @@ -340,7 +340,10 @@ func (f *Fs) List(ctx context.Context, dir string) (entries fs.DirEntries, err e
d := fs.NewDir(remote, item.DateModified).SetID(jid)
entries = append(entries, d)
} else {
o, _ := f.NewObjectFromDriveItem(ctx, remote, item)
o, err := f.NewObjectFromDriveItem(ctx, remote, item)
if err != nil {
return nil, err
}
entries = append(entries, o)
}
}
Expand Down Expand Up @@ -431,8 +434,8 @@ func (f *Fs) Copy(ctx context.Context, src fs.Object, remote string) (fs.Object,
r.Data.ReferenceSignature = doc.Data.ReferenceSignature
r.Data.WrappingKey = doc.Data.WrappingKey
r.Data.Size = doc.Data.Size
r.Mtime = srcObj.modTime.Unix() * 1000
r.Btime = srcObj.modTime.Unix() * 1000
r.Mtime = srcObj.modTime.UnixMilli()
r.Btime = srcObj.modTime.UnixMilli()

var item *api.DriveItem
if err = f.pacer.Call(func() (bool, error) {
Expand All @@ -443,10 +446,10 @@ func (f *Fs) Copy(ctx context.Context, src fs.Object, remote string) (fs.Object,
}

o, err := f.NewObjectFromDriveItem(ctx, remote, item)
obj, _ := o.(*Object)
if err != nil {
return nil, err
}
obj := o.(*Object)

// cheat unit tests
obj.modTime = srcObj.modTime
Expand Down Expand Up @@ -646,10 +649,6 @@ func (f *Fs) move(ctx context.Context, ID, srcDirectoryID, srcLeaf, srcEtag, dst
var item *api.DriveItem
var err error

fs.Debugf(ID, "ID")
fs.Debugf(srcDirectoryID, "srcDirectoryID")
fs.Debugf(srcLeaf, "srcLeaf")

// move
if srcDirectoryID != dstDirectoryID {
if err = f.pacer.Call(func() (bool, error) {
Expand Down Expand Up @@ -790,7 +789,7 @@ func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, e
}

if opt.TrustToken == "" {
return nil, fmt.Errorf("missing icloud trust token, authenticate through config")
return nil, fmt.Errorf("missing icloud trust token: try refreshing it with \"rclone config reconnect %s:\"", name)
}

cookies := ReadCookies(opt.Cookies)
Expand All @@ -799,13 +798,16 @@ func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, e
m.Set(configCookies, session.GetCookieString())
}

icloud, _ := api.New(
icloud, err := api.New(
opt.AppleID,
opt.Password,
opt.TrustToken,
cookies,
callback,
)
if err != nil {
return nil, err
}

if err := icloud.Authenticate(ctx); err != nil {
return nil, err
Expand All @@ -831,7 +833,10 @@ func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, e
}).Fill(ctx, f)

rootID := f.rootID
f.service, _ = icloud.DriveService()
f.service, err = icloud.DriveService()
if err != nil {
return nil, err
}

f.dirCache = dircache.New(
root,
Expand Down
2 changes: 1 addition & 1 deletion docs/content/iclouddrive.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Option Storage.
Type of storage to configure.
Choose a number from below, or type in your own value.
[snip]
55 / iCloud Drive
XX / iCloud Drive
\ (iclouddrive)
[snip]
Storage> iclouddrive
Expand Down

0 comments on commit cb4884d

Please sign in to comment.