From b3bb8ade201467d61a5fe8d08a3f9375eb3c1dee Mon Sep 17 00:00:00 2001 From: Chun-Hung Tseng Date: Wed, 12 Jul 2023 22:13:05 +0200 Subject: [PATCH] Refactor GetLink in preparation for caching --- cache.go | 16 ++++++++++++++++ delete.go | 4 ++-- drive.go | 3 +-- file.go | 20 ++++++++++---------- folder.go | 18 +++++++++--------- keyring.go | 8 ++++---- search.go | 8 ++++---- 7 files changed, 46 insertions(+), 31 deletions(-) create mode 100644 cache.go diff --git a/cache.go b/cache.go new file mode 100644 index 0000000..615a931 --- /dev/null +++ b/cache.go @@ -0,0 +1,16 @@ +package proton_api_bridge + +import ( + "context" + + "github.com/henrybear327/go-proton-api" +) + +func (protonDrive *ProtonDrive) getLink(ctx context.Context, linkID string) (*proton.Link, error) { + link, err := protonDrive.c.GetLink(ctx, protonDrive.MainShare.ShareID, linkID) + if err != nil { + return nil, err + } + + return &link, nil +} diff --git a/delete.go b/delete.go index f3fd40b..f0ce7a2 100644 --- a/delete.go +++ b/delete.go @@ -16,7 +16,7 @@ func (protonDrive *ProtonDrive) moveToTrash(ctx context.Context, parentLinkID st } func (protonDrive *ProtonDrive) MoveFileToTrashByID(ctx context.Context, linkID string) error { - fileLink, err := protonDrive.c.GetLink(ctx, protonDrive.MainShare.ShareID, linkID) + fileLink, err := protonDrive.getLink(ctx, linkID) if err != nil { return err } @@ -28,7 +28,7 @@ func (protonDrive *ProtonDrive) MoveFileToTrashByID(ctx context.Context, linkID } func (protonDrive *ProtonDrive) MoveFolderToTrashByID(ctx context.Context, linkID string, onlyOnEmpty bool) error { - folderLink, err := protonDrive.c.GetLink(ctx, protonDrive.MainShare.ShareID, linkID) + folderLink, err := protonDrive.getLink(ctx, linkID) if err != nil { return err } diff --git a/drive.go b/drive.go index 52999b9..8860fa2 100644 --- a/drive.go +++ b/drive.go @@ -157,6 +157,5 @@ func (protonDrive *ProtonDrive) About(ctx context.Context) (*proton.User, error) } func (protonDrive *ProtonDrive) GetLink(ctx context.Context, linkID string) (*proton.Link, error) { - link, err := protonDrive.c.GetLink(ctx, protonDrive.MainShare.ShareID, linkID) - return &link, err + return protonDrive.getLink(ctx, linkID) } diff --git a/file.go b/file.go index 247dd09..5e818b8 100644 --- a/file.go +++ b/file.go @@ -23,12 +23,12 @@ type FileSystemAttrs struct { } func (protonDrive *ProtonDrive) DownloadFileByID(ctx context.Context, linkID string) ([]byte, *FileSystemAttrs, error) { - link, err := protonDrive.c.GetLink(ctx, protonDrive.MainShare.ShareID, linkID) + link, err := protonDrive.getLink(ctx, linkID) if err != nil { return nil, nil, err } - return protonDrive.DownloadFile(ctx, &link) + return protonDrive.DownloadFile(ctx, link) } func (protonDrive *ProtonDrive) GetRevisions(ctx context.Context, link *proton.Link, revisionType proton.RevisionState) ([]*proton.RevisionMetadata, error) { @@ -136,12 +136,12 @@ func (protonDrive *ProtonDrive) DownloadFile(ctx context.Context, link *proton.L } func (protonDrive *ProtonDrive) UploadFileByReader(ctx context.Context, parentLinkID string, filename string, modTime time.Time, file io.Reader, testParam int) (*proton.Link, int64, error) { - parentLink, err := protonDrive.c.GetLink(ctx, protonDrive.MainShare.ShareID, parentLinkID) + parentLink, err := protonDrive.getLink(ctx, parentLinkID) if err != nil { return nil, 0, err } - return protonDrive.uploadFile(ctx, &parentLink, filename, modTime, file, testParam) + return protonDrive.uploadFile(ctx, parentLink, filename, modTime, file, testParam) } func (protonDrive *ProtonDrive) UploadFileByPath(ctx context.Context, parentLink *proton.Link, filename string, filePath string, testParam int) (*proton.Link, int64, error) { @@ -528,11 +528,11 @@ func (protonDrive *ProtonDrive) uploadFile(ctx context.Context, parentLink *prot if testParam == 1 { // for integration tests // we try to simulate only draft is created but no upload is performed yet - finalLink, err := protonDrive.c.GetLink(ctx, protonDrive.MainShare.ShareID, linkID) + finalLink, err := protonDrive.getLink(ctx, linkID) if err != nil { return nil, 0, err } - return &finalLink, 0, nil + return finalLink, 0, nil } /* step 2: upload blocks and collect block data */ @@ -544,11 +544,11 @@ func (protonDrive *ProtonDrive) uploadFile(ctx context.Context, parentLink *prot if testParam == 2 { // for integration tests // we try to simulate blocks uploaded but not yet commited - finalLink, err := protonDrive.c.GetLink(ctx, protonDrive.MainShare.ShareID, linkID) + finalLink, err := protonDrive.getLink(ctx, linkID) if err != nil { return nil, 0, err } - return &finalLink, 0, nil + return finalLink, 0, nil } /* step 3: mark the file as active by commiting the revision */ @@ -557,11 +557,11 @@ func (protonDrive *ProtonDrive) uploadFile(ctx context.Context, parentLink *prot return nil, 0, err } - finalLink, err := protonDrive.c.GetLink(ctx, protonDrive.MainShare.ShareID, linkID) + finalLink, err := protonDrive.getLink(ctx, linkID) if err != nil { return nil, 0, err } - return &finalLink, fileSize, nil + return finalLink, fileSize, nil } /* diff --git a/folder.go b/folder.go index e80541b..5f3203f 100644 --- a/folder.go +++ b/folder.go @@ -20,7 +20,7 @@ func (protonDrive *ProtonDrive) ListDirectory( folderLinkID string) ([]*ProtonDirectoryData, error) { ret := make([]*ProtonDirectoryData, 0) - folderLink, err := protonDrive.c.GetLink(ctx, protonDrive.MainShare.ShareID, folderLinkID) + folderLink, err := protonDrive.getLink(ctx, folderLinkID) if err != nil { return nil, err } @@ -158,12 +158,12 @@ func (protonDrive *ProtonDrive) ListDirectoriesRecursively( } func (protonDrive *ProtonDrive) CreateNewFolderByID(ctx context.Context, parentLinkID string, folderName string) (string, error) { - parentLink, err := protonDrive.c.GetLink(ctx, protonDrive.MainShare.ShareID, parentLinkID) + parentLink, err := protonDrive.getLink(ctx, parentLinkID) if err != nil { return "", err } - return protonDrive.CreateNewFolder(ctx, &parentLink, folderName) + return protonDrive.CreateNewFolder(ctx, parentLink, folderName) } func (protonDrive *ProtonDrive) CreateNewFolder(ctx context.Context, parentLink *proton.Link, folderName string) (string, error) { @@ -227,7 +227,7 @@ func (protonDrive *ProtonDrive) CreateNewFolder(ctx context.Context, parentLink } func (protonDrive *ProtonDrive) MoveFileByID(ctx context.Context, srcLinkID, dstParentLinkID string, dstName string) error { - srcLink, err := protonDrive.c.GetLink(ctx, protonDrive.MainShare.ShareID, srcLinkID) + srcLink, err := protonDrive.getLink(ctx, srcLinkID) if err != nil { return err } @@ -235,7 +235,7 @@ func (protonDrive *ProtonDrive) MoveFileByID(ctx context.Context, srcLinkID, dst return ErrLinkMustBeActive } - dstParentLink, err := protonDrive.c.GetLink(ctx, protonDrive.MainShare.ShareID, dstParentLinkID) + dstParentLink, err := protonDrive.getLink(ctx, dstParentLinkID) if err != nil { return err } @@ -243,7 +243,7 @@ func (protonDrive *ProtonDrive) MoveFileByID(ctx context.Context, srcLinkID, dst return ErrLinkMustBeActive } - return protonDrive.MoveFile(ctx, &srcLink, &dstParentLink, dstName) + return protonDrive.MoveFile(ctx, srcLink, dstParentLink, dstName) } func (protonDrive *ProtonDrive) MoveFile(ctx context.Context, srcLink *proton.Link, dstParentLink *proton.Link, dstName string) error { @@ -251,7 +251,7 @@ func (protonDrive *ProtonDrive) MoveFile(ctx context.Context, srcLink *proton.Li } func (protonDrive *ProtonDrive) MoveFolderByID(ctx context.Context, srcLinkID, dstParentLinkID, dstName string) error { - srcLink, err := protonDrive.c.GetLink(ctx, protonDrive.MainShare.ShareID, srcLinkID) + srcLink, err := protonDrive.getLink(ctx, srcLinkID) if err != nil { return err } @@ -259,7 +259,7 @@ func (protonDrive *ProtonDrive) MoveFolderByID(ctx context.Context, srcLinkID, d return ErrLinkMustBeActive } - dstParentLink, err := protonDrive.c.GetLink(ctx, protonDrive.MainShare.ShareID, dstParentLinkID) + dstParentLink, err := protonDrive.getLink(ctx, dstParentLinkID) if err != nil { return err } @@ -267,7 +267,7 @@ func (protonDrive *ProtonDrive) MoveFolderByID(ctx context.Context, srcLinkID, d return ErrLinkMustBeActive } - return protonDrive.MoveFolder(ctx, &srcLink, &dstParentLink, dstName) + return protonDrive.MoveFolder(ctx, srcLink, dstParentLink, dstName) } func (protonDrive *ProtonDrive) MoveFolder(ctx context.Context, srcLink *proton.Link, dstParentLink *proton.Link, dstName string) error { diff --git a/keyring.go b/keyring.go index f8de460..9783ef0 100644 --- a/keyring.go +++ b/keyring.go @@ -13,12 +13,12 @@ func (protonDrive *ProtonDrive) getNodeKRByID(ctx context.Context, linkID string return protonDrive.MainShareKR.Copy() // we need to return a deep copy since the keyring will be freed by the caller when it finishes using the keyring } - link, err := protonDrive.c.GetLink(ctx, protonDrive.MainShare.ShareID, linkID) + link, err := protonDrive.getLink(ctx, linkID) if err != nil { return nil, err } - return protonDrive.getNodeKR(ctx, &link) + return protonDrive.getNodeKR(ctx, link) } func (protonDrive *ProtonDrive) getNodeKR(ctx context.Context, link *proton.Link) (*crypto.KeyRing, error) { @@ -31,13 +31,13 @@ func (protonDrive *ProtonDrive) getNodeKR(ctx context.Context, link *proton.Link return nodeKR, nil } - parentLink, err := protonDrive.c.GetLink(ctx, protonDrive.MainShare.ShareID, link.ParentLinkID) + parentLink, err := protonDrive.getLink(ctx, link.ParentLinkID) if err != nil { return nil, err } // parentNodeKR is used to decrypt the current node's KR, as each node has its keyring, which can be decrypted by its parent - parentNodeKR, err := protonDrive.getNodeKR(ctx, &parentLink) + parentNodeKR, err := protonDrive.getNodeKR(ctx, parentLink) if err != nil { return nil, err } diff --git a/search.go b/search.go index 31c6629..faf54d7 100644 --- a/search.go +++ b/search.go @@ -22,7 +22,7 @@ func (protonDrive *ProtonDrive) SearchByNameRecursivelyFromRoot(ctx context.Cont } func (protonDrive *ProtonDrive) SearchByNameRecursivelyByID(ctx context.Context, folderLinkID string, targetName string, isFolder bool, listAllActiveOrDraftFiles bool) (*proton.Link, error) { - folderLink, err := protonDrive.c.GetLink(ctx, protonDrive.MainShare.ShareID, folderLinkID) + folderLink, err := protonDrive.getLink(ctx, folderLinkID) if err != nil { return nil, err } @@ -41,7 +41,7 @@ func (protonDrive *ProtonDrive) SearchByNameRecursivelyByID(ctx context.Context, if err != nil { return nil, err } - return protonDrive.searchByNameRecursively(ctx, folderKeyRing, &folderLink, targetName, linkType, listAllActiveOrDraftFiles) + return protonDrive.searchByNameRecursively(ctx, folderKeyRing, folderLink, targetName, linkType, listAllActiveOrDraftFiles) } func (protonDrive *ProtonDrive) SearchByNameRecursively(ctx context.Context, folderLink *proton.Link, targetName string, isFolder bool, listAllActiveOrDraftFiles bool) (*proton.Link, error) { @@ -121,12 +121,12 @@ func (protonDrive *ProtonDrive) SearchByNameInActiveFolderByID(ctx context.Conte targetName string, searchForFile, searchForFolder bool, targetState proton.LinkState) (*proton.Link, error) { - folderLink, err := protonDrive.c.GetLink(ctx, protonDrive.MainShare.ShareID, folderLinkID) + folderLink, err := protonDrive.getLink(ctx, folderLinkID) if err != nil { return nil, err } - return protonDrive.SearchByNameInActiveFolder(ctx, &folderLink, targetName, searchForFile, searchForFolder, targetState) + return protonDrive.SearchByNameInActiveFolder(ctx, folderLink, targetName, searchForFile, searchForFolder, targetState) } func (protonDrive *ProtonDrive) SearchByNameInActiveFolder(