From 46ce0ba1fbaebccfe5079a3c34c9477852a710b0 Mon Sep 17 00:00:00 2001 From: Arno Hautala Date: Sat, 22 Feb 2020 22:12:36 -0500 Subject: [PATCH 1/2] support downloading from a custom URL pointed at B2 --- src/duplicacy_b2client.go | 7 +++++-- src/duplicacy_b2client_test.go | 2 +- src/duplicacy_b2storage.go | 4 ++-- src/duplicacy_storage.go | 3 ++- src/duplicacy_storage_test.go | 2 +- 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/duplicacy_b2client.go b/src/duplicacy_b2client.go index 28b662c..5f13dc5 100644 --- a/src/duplicacy_b2client.go +++ b/src/duplicacy_b2client.go @@ -75,7 +75,7 @@ func B2Escape(path string) string { return strings.Join(components, "/") } -func NewB2Client(applicationKeyID string, applicationKey string, storageDir string, threads int) *B2Client { +func NewB2Client(applicationKeyID string, applicationKey string, downloadURL string, storageDir string, threads int) *B2Client { for storageDir != "" && storageDir[0] == '/' { storageDir = storageDir[1:] @@ -95,6 +95,7 @@ func NewB2Client(applicationKeyID string, applicationKey string, storageDir stri HTTPClient: http.DefaultClient, ApplicationKeyID: applicationKeyID, ApplicationKey: applicationKey, + DownloadURL: downloadURL, StorageDir: storageDir, UploadURLs: make([]string, threads), UploadTokens: make([]string, threads), @@ -325,7 +326,9 @@ func (client *B2Client) AuthorizeAccount(threadIndex int) (err error, allowed bo client.AuthorizationToken = output.AuthorizationToken client.APIURL = output.APIURL - client.DownloadURL = output.DownloadURL + if client.DownloadURL == "" { + client.DownloadURL = output.DownloadURL + } client.IsAuthorized = true client.LastAuthorizationTime = time.Now().Unix() diff --git a/src/duplicacy_b2client_test.go b/src/duplicacy_b2client_test.go index ebcc5df..cb9932a 100644 --- a/src/duplicacy_b2client_test.go +++ b/src/duplicacy_b2client_test.go @@ -37,7 +37,7 @@ func createB2ClientForTest(t *testing.T) (*B2Client, string) { return nil, "" } - return NewB2Client(b2["account"], b2["key"], b2["directory"], 1), b2["bucket"] + return NewB2Client(b2["account"], b2["key"], "", b2["directory"], 1), b2["bucket"] } diff --git a/src/duplicacy_b2storage.go b/src/duplicacy_b2storage.go index 08af6a6..d7f1f20 100644 --- a/src/duplicacy_b2storage.go +++ b/src/duplicacy_b2storage.go @@ -15,9 +15,9 @@ type B2Storage struct { } // CreateB2Storage creates a B2 storage object. -func CreateB2Storage(accountID string, applicationKey string, bucket string, storageDir string, threads int) (storage *B2Storage, err error) { +func CreateB2Storage(accountID string, applicationKey string, downloadURL string, bucket string, storageDir string, threads int) (storage *B2Storage, err error) { - client := NewB2Client(accountID, applicationKey, storageDir, threads) + client := NewB2Client(accountID, applicationKey, downloadURL, storageDir, threads) err, _ = client.AuthorizeAccount(0) if err != nil { diff --git a/src/duplicacy_storage.go b/src/duplicacy_storage.go index 7cf85cf..3652e4a 100644 --- a/src/duplicacy_storage.go +++ b/src/duplicacy_storage.go @@ -530,8 +530,9 @@ func CreateStorage(preference Preference, resetPassword bool, threads int) (stor accountID := GetPassword(preference, "b2_id", "Enter Backblaze account or application id:", true, resetPassword) applicationKey := GetPassword(preference, "b2_key", "Enter corresponding Backblaze application key:", true, resetPassword) + downloadURL := preference.Keys["b2_download_url"] - b2Storage, err := CreateB2Storage(accountID, applicationKey, bucket, storageDir, threads) + b2Storage, err := CreateB2Storage(accountID, applicationKey, downloadURL, bucket, storageDir, threads) if err != nil { LOG_ERROR("STORAGE_CREATE", "Failed to load the Backblaze B2 storage at %s: %v", storageURL, err) return nil diff --git a/src/duplicacy_storage_test.go b/src/duplicacy_storage_test.go index 4cffdbc..ce4c0e8 100644 --- a/src/duplicacy_storage_test.go +++ b/src/duplicacy_storage_test.go @@ -109,7 +109,7 @@ func loadStorage(localStoragePath string, threads int) (Storage, error) { storage.SetDefaultNestingLevels([]int{2, 3}, 2) return storage, err } else if testStorageName == "b2" { - storage, err := CreateB2Storage(config["account"], config["key"], config["bucket"], config["directory"], threads) + storage, err := CreateB2Storage(config["account"], config["key"], "", config["bucket"], config["directory"], threads) storage.SetDefaultNestingLevels([]int{2, 3}, 2) return storage, err } else if testStorageName == "gcs-s3" { From 499b612a0d20e6c2e766818e762108335fd21d16 Mon Sep 17 00:00:00 2001 From: Arno Hautala Date: Tue, 25 Feb 2020 20:53:19 -0500 Subject: [PATCH 2/2] moving download url config from a key to the storage url pattern --- src/duplicacy_b2client.go | 1 + src/duplicacy_storage.go | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/duplicacy_b2client.go b/src/duplicacy_b2client.go index 5f13dc5..ff9687a 100644 --- a/src/duplicacy_b2client.go +++ b/src/duplicacy_b2client.go @@ -329,6 +329,7 @@ func (client *B2Client) AuthorizeAccount(threadIndex int) (err error, allowed bo if client.DownloadURL == "" { client.DownloadURL = output.DownloadURL } + LOG_INFO("BACKBLAZE_URL", "download URL is: %s", client.DownloadURL) client.IsAuthorized = true client.LastAuthorizationTime = time.Now().Unix() diff --git a/src/duplicacy_storage.go b/src/duplicacy_storage.go index 3652e4a..c9ab70d 100644 --- a/src/duplicacy_storage.go +++ b/src/duplicacy_storage.go @@ -530,7 +530,24 @@ func CreateStorage(preference Preference, resetPassword bool, threads int) (stor accountID := GetPassword(preference, "b2_id", "Enter Backblaze account or application id:", true, resetPassword) applicationKey := GetPassword(preference, "b2_key", "Enter corresponding Backblaze application key:", true, resetPassword) - downloadURL := preference.Keys["b2_download_url"] + + b2Storage, err := CreateB2Storage(accountID, applicationKey, "", bucket, storageDir, threads) + if err != nil { + LOG_ERROR("STORAGE_CREATE", "Failed to load the Backblaze B2 storage at %s: %v", storageURL, err) + return nil + } + SavePassword(preference, "b2_id", accountID) + SavePassword(preference, "b2_key", applicationKey) + return b2Storage + } else if matched[1] == "b2-custom" { + b2customUrlRegex := regexp.MustCompile(`^b2-custom://([^/]+)/([^/]+)(/(.+))?`) + matched := b2customUrlRegex.FindStringSubmatch(storageURL) + downloadURL := "https://" + matched[1] + bucket := matched[2] + storageDir := matched[4] + + accountID := GetPassword(preference, "b2_id", "Enter Backblaze account or application id:", true, resetPassword) + applicationKey := GetPassword(preference, "b2_key", "Enter corresponding Backblaze application key:", true, resetPassword) b2Storage, err := CreateB2Storage(accountID, applicationKey, downloadURL, bucket, storageDir, threads) if err != nil {