diff --git a/src/duplicacy_b2client.go b/src/duplicacy_b2client.go index 3b115c8..aac2d80 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,10 @@ 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 + } + LOG_INFO("BACKBLAZE_URL", "download URL is: %s", client.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 660bb26..52a9457 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..c9ab70d 100644 --- a/src/duplicacy_storage.go +++ b/src/duplicacy_storage.go @@ -531,7 +531,25 @@ 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) - b2Storage, err := CreateB2Storage(accountID, applicationKey, bucket, storageDir, threads) + 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 { 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" {