Refactor GetLink in preparation for caching

This commit is contained in:
Chun-Hung Tseng
2023-07-12 22:13:05 +02:00
parent d306c8423d
commit b3bb8ade20
7 changed files with 46 additions and 31 deletions

16
cache.go Normal file
View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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)
}

20
file.go
View File

@@ -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
}
/*

View File

@@ -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 {

View File

@@ -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
}

View File

@@ -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(