Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
lostb1t committed Aug 3, 2024
1 parent 23d4bec commit f22066d
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 115 deletions.
14 changes: 7 additions & 7 deletions backend/icloud/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"bytes"
"context"
"encoding/json"
"net/http"
"fmt"
"strings"
"net/http"
"strings"

"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/fshttp"
Expand Down Expand Up @@ -135,10 +135,10 @@ func IntoReader(values any) (*bytes.Reader, error) {
return bytes.NewReader(m), nil
}

// Sometimes icloud returns a 200 but the request status is unkown
// RequestError holds info on a result state, icloud can return a 200 but the result is unknown
type RequestError struct {
Status string
Text string
Status string
Text string
}

// Error satisfy the error interface.
Expand All @@ -148,8 +148,8 @@ func (e *RequestError) Error() string {

func newRequestError(Status string, Text string) *RequestError {
return &RequestError{
Status: strings.ToLower(Status),
Text: Text,
Status: strings.ToLower(Status),
Text: Text,
}
}

Expand Down
32 changes: 16 additions & 16 deletions backend/icloud/api/drive.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ func (d *DriveService) GetItemsByDriveID(ctx context.Context, ids []string, incl
}

//if items[0].Status != statusOk {
// err = newRequestErrorf(items[0].Status, "unkown inner status for: %s %s", opts.Method, resp.Request.URL)
//return nil, resp, fmt.Errorf("%s %s failed, status %s", opts.Method, resp.Request.URL, items[0].Status)
// err = newRequestErrorf(items[0].Status, "unknown inner status for: %s %s", opts.Method, resp.Request.URL)
//return nil, resp, fmt.Errorf("%s %s failed, status %s", opts.Method, resp.Request.URL, items[0].Status)
//}

return items, resp, err
Expand Down Expand Up @@ -203,7 +203,7 @@ func (d *DriveService) GetItemsInFolder(ctx context.Context, id string, limit in
return items.Items, resp, err
}

// GetDownloadURL retrieves the download URL for a file in the DriveService.
// GetDownloadURLByDriveID retrieves the download URL for a file in the DriveService.
func (d *DriveService) GetDownloadURLByDriveID(ctx context.Context, id string) (string, *http.Response, error) {
_, zone, docid := DeconstructDriveID(id)
values := url.Values{}
Expand Down Expand Up @@ -304,7 +304,7 @@ func (d *DriveService) MoveItemToTrashByID(ctx context.Context, drivewsid, etag
return d.MoveItemToTrashByID(ctx, drivewsid, item.Items[0].Etag, false)
}

err = newRequestError(item.Items[0].Status, "unknown request status")
err = newRequestError(item.Items[0].Status, "unknown request status")
//return nil, resp, fmt.Errorf("%s %s failed, inner_status %s", opts.Method, resp.Request.URL, item.Items[0].Status)
}

Expand Down Expand Up @@ -378,12 +378,12 @@ func (d *DriveService) CreateNewFolderByDriveID(ctx context.Context, drivewsid,
}
var fResp *CreateFoldersResponse
resp, err := d.icloud.Request(ctx, opts, nil, &fResp)
if err != nil {
if err != nil {
return nil, resp, err
}
status := fResp.Folders[0].Status
if status != statusOk {
err = newRequestError(status, "unknown request status")
err = newRequestError(status, "unknown request status")
}

return fResp.Folders[0], resp, err
Expand Down Expand Up @@ -431,7 +431,7 @@ func (d *DriveService) RenameItemByDriveID(ctx context.Context, id, etag, name s
if force && status == "ETAG_CONFLICT" {
return d.RenameItemByDriveID(ctx, id, items.Items[0].Etag, name, false)
}
err = newRequestErrorf(status, "unkown inner status for: %s %s", opts.Method, resp.Request.URL)
err = newRequestErrorf(status, "unknown inner status for: %s %s", opts.Method, resp.Request.URL)
}

return items.Items[0], resp, err
Expand Down Expand Up @@ -483,7 +483,7 @@ func (d *DriveService) MoveItemByDriveID(ctx context.Context, id, etag, dstID st
if force && status == "ETAG_CONFLICT" {
return d.MoveItemByDriveID(ctx, id, items.Items[0].Etag, dstID, false)
}
err = newRequestErrorf(status, "unkown inner status for: %s %s", opts.Method, resp.Request.URL)
err = newRequestErrorf(status, "unknown inner status for: %s %s", opts.Method, resp.Request.URL)
}

return items.Items[0], resp, err
Expand Down Expand Up @@ -522,8 +522,8 @@ func (d *DriveService) CreateUpload(ctx context.Context, size int64, name string
// api requires a mime type passed in
mimeType = "text/plain"
}
// first we need to request an upload url

// first we need to request an upload url
values := map[string]any{
"filename": name,
"type": "FILE",
Expand All @@ -540,14 +540,14 @@ func (d *DriveService) CreateUpload(ctx context.Context, size int64, name string
}
var responseInfo []*UploadResponse
resp, err := d.icloud.Request(ctx, opts, nil, &responseInfo)
if err != nil {
if err != nil {
return nil, resp, err
}
return responseInfo[0], resp, err
return responseInfo[0], resp, err
}

// Upload uploads a file to the given url
func (d *DriveService) Upload(ctx context.Context, in io.Reader, size int64, name, uploadUrl string) (*SingleFileResponse, *http.Response, error) {
func (d *DriveService) Upload(ctx context.Context, in io.Reader, size int64, name, uploadURL string) (*SingleFileResponse, *http.Response, error) {
// detect MIME type by looking at the filename only
mimeType := mime.TypeByExtension(filepath.Ext(name))
if mimeType == "" {
Expand All @@ -558,7 +558,7 @@ func (d *DriveService) Upload(ctx context.Context, in io.Reader, size int64, nam
opts := rest.Opts{
Method: "POST",
ExtraHeaders: d.icloud.Session.GetHeaders(map[string]string{}),
RootURL: uploadUrl,
RootURL: uploadURL,
Body: in,
ContentLength: &size,
ContentType: mimeType,
Expand All @@ -570,7 +570,7 @@ func (d *DriveService) Upload(ctx context.Context, in io.Reader, size int64, nam
if err != nil {
return nil, resp, err
}
return singleFileResponse, resp, err
return singleFileResponse, resp, err
}

// UpdateFile updates a file in the DriveService.
Expand Down Expand Up @@ -831,7 +831,7 @@ type SingleFileInfo struct {
Receipt string `json:"receipt"`
}

// UploadUrlResponse is the response of an upload request.
// UploadResponse is the response of an upload request.
type UploadResponse struct {
URL string `json:"url"`
DocumentID string `json:"document_id"`
Expand Down
8 changes: 4 additions & 4 deletions backend/icloud/api/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ func (s *Session) Request(ctx context.Context, opts rest.Opts, request interface
resp, err := s.srv.CallJSON(ctx, &opts, &request, &response)

if err != nil {
//fs.PrettyPrint(opts, "opts", fs.LogLevelDebug)
//fs.PrettyPrint(resp, "resp", fs.LogLevelDebug)
return resp, err
//return resp, fmt.Errorf("%s %s failed, status %d, err: %s", opts.Method, resp.Request.URL, resp.StatusCode, err)
//fs.PrettyPrint(opts, "opts", fs.LogLevelDebug)
//fs.PrettyPrint(resp, "resp", fs.LogLevelDebug)
return resp, err
//return resp, fmt.Errorf("%s %s failed, status %d, err: %s", opts.Method, resp.Request.URL, resp.StatusCode, err)
}

if val := resp.Header.Get("X-Apple-ID-Account-Country"); val != "" {
Expand Down
Loading

0 comments on commit f22066d

Please sign in to comment.