From 140d583a1c21deba03683e838f4bd2d1c23c74fa Mon Sep 17 00:00:00 2001 From: tinybarks <31551149+tinybarks@users.noreply.github.com> Date: Mon, 1 Oct 2018 16:39:16 +0200 Subject: [PATCH] B2 uploader: Change bucket setting handling, add CodeMenu to fields The "bucket" setting is no longer required. Instead, show a hint to the user saying that the setting is only used if the application key has no bucket acess restriction. --- .../FileUploaders/BackblazeB2.cs | 13 ++++++----- .../Forms/UploadersConfigForm.cs | 5 +++++ .../Forms/UploadersConfigForm.resx | 22 +++++++++++++------ .../Forms/UploadersConfigFormHelper.cs | 17 ++++++++++++++ ShareX.UploadersLib/UploadersConfig.cs | 2 +- 5 files changed, 46 insertions(+), 13 deletions(-) diff --git a/ShareX.UploadersLib/FileUploaders/BackblazeB2.cs b/ShareX.UploadersLib/FileUploaders/BackblazeB2.cs index 75d19af9a..293cd48f2 100644 --- a/ShareX.UploadersLib/FileUploaders/BackblazeB2.cs +++ b/ShareX.UploadersLib/FileUploaders/BackblazeB2.cs @@ -59,8 +59,7 @@ public override bool CheckConfig(UploadersConfig config) { return !string.IsNullOrWhiteSpace(config.B2ApplicationKeyId) && - !string.IsNullOrWhiteSpace(config.B2ApplicationKey) && - !string.IsNullOrWhiteSpace(config.B2BucketName); + !string.IsNullOrWhiteSpace(config.B2ApplicationKey); } public override GenericUploader CreateUploader(UploadersConfig config, TaskReferenceHelper taskInfo) @@ -125,7 +124,7 @@ public override UploadResult Upload(Stream stream, string fileName) if (authError != null) { DebugHelper.WriteLine("B2 uploader: Failed to authorize."); - Errors.Add("Could not authenticate with B2: " + authError); + Errors.Add($"Could not authenticate with B2: {authError}"); return null; } @@ -136,7 +135,7 @@ public override UploadResult Upload(Stream stream, string fileName) var bucketId = auth.allowed?.bucketId; if (bucketId == null) { - DebugHelper.WriteLine("B2 uploader: This doesn't look like an app key, so I'm looking for a bucket ID."); + DebugHelper.WriteLine("B2 uploader: Key doesn't have a bucket ID set, so I'm looking for a bucket ID."); var newBucketId = B2ApiGetBucketId(auth, BucketName, out var getBucketError); if (getBucketError != null) @@ -300,7 +299,11 @@ private string B2ApiGetBucketId(B2Authorization auth, string bucketName, out str ["Authorization"] = auth.authorizationToken }; - var reqBody = new Dictionary { ["bucketName"] = bucketName }; + var reqBody = new Dictionary + { + ["accountId"] = auth.accountId, + ["bucketName"] = bucketName + }; using (var data = CreateJsonBody(reqBody)) { diff --git a/ShareX.UploadersLib/Forms/UploadersConfigForm.cs b/ShareX.UploadersLib/Forms/UploadersConfigForm.cs index f49e783f4..df7101c90 100644 --- a/ShareX.UploadersLib/Forms/UploadersConfigForm.cs +++ b/ShareX.UploadersLib/Forms/UploadersConfigForm.cs @@ -98,6 +98,8 @@ private void InitializeControls() CodeMenu.Create(txtAmazonS3ObjectPrefix, CodeMenuEntryFilename.n, CodeMenuEntryFilename.t, CodeMenuEntryFilename.pn); CodeMenu.Create(txtMediaFirePath, CodeMenuEntryFilename.n, CodeMenuEntryFilename.t, CodeMenuEntryFilename.pn); CodeMenu.Create(txtGoogleCloudStorageObjectPrefix, CodeMenuEntryFilename.n, CodeMenuEntryFilename.t, CodeMenuEntryFilename.pn); + CodeMenu.Create(txtB2UploadPath, CodeMenuEntryFilename.n, CodeMenuEntryFilename.t, CodeMenuEntryFilename.pn); + CodeMenu.Create(txtB2CustomUrl, CodeMenuEntryFilename.n, CodeMenuEntryFilename.t, CodeMenuEntryFilename.pn); CodeMenuItem codeMenuItemInput = new CodeMenuItem("$input$", "Text/URL input"); CodeMenuItem codeMenuItemFilename = new CodeMenuItem("$filename$", "File name"); @@ -121,6 +123,9 @@ private void InitializeControls() cbCustomUploaderRequestType.Items.AddRange(Enum.GetNames(typeof(CustomUploaderRequestType))); cbCustomUploaderResponseType.Items.AddRange(Helpers.GetLocalizedEnumDescriptions()); + // Backblaze B2 + SetPlaceholderText(txtB2Bucket, "Optional, only used if you didn't set a bucket when you made the key"); + #if DEBUG btnCheveretoTestAll.Visible = true; btnPomfTest.Visible = true; diff --git a/ShareX.UploadersLib/Forms/UploadersConfigForm.resx b/ShareX.UploadersLib/Forms/UploadersConfigForm.resx index 568472c8b..c20d57e20 100644 --- a/ShareX.UploadersLib/Forms/UploadersConfigForm.resx +++ b/ShareX.UploadersLib/Forms/UploadersConfigForm.resx @@ -261,9 +261,15 @@ For example, if your bucket is called bucket.example.com then URL will be http:/ Use custom URL (supports pattern format): - If enabled, the URL you receive will be the upload path appended to the given URL. This is useful in conjunction with a service like CloudFlare or another service acting as frontend to B2. See the article "Creating a Vanity URL with B2" in the B2 knowledge base. + If enabled, the URL you receive will be the upload path +appended to the given URL. -If disabled, then the URL you will receive will be determined by B2. +This is useful in conjunction with a service like CloudFlare or +another service acting as frontend to B2. See the article +"Creating a Vanity URL with B2" in the B2 knowledge base. + +If disabled, then the URL you will receive will be determined +by Backblaze B2. cbB2CustomUrl @@ -289,7 +295,9 @@ If disabled, then the URL you will receive will be determined by B2. Where to store your files. -The bucket needs to be public as the Backblaze B2 Cloud Storage API does not allow for download links that last longer than a week. +This only takes effect when you did not set +"Allow access to bucket(s)" to a bucket +when you made the application key. txtB2Bucket @@ -337,7 +345,7 @@ The bucket needs to be public as the Backblaze B2 Cloud Storage API does not all 3 - The application key. + The application key. Looks alphanumeric. txtB2ApplicationKey @@ -361,7 +369,7 @@ The bucket needs to be public as the Backblaze B2 Cloud Storage API does not all 2 - The Application Key ID looks like an all-lowercase hexadecimal string. + The Application Key ID. Looks like a lowercase hexadecimal string. txtB2ApplicationKeyId @@ -8047,13 +8055,13 @@ store.book[0].title 2 - 4, 40 + 4, 220 3, 3, 3, 3 - 972, 519 + 178, 0 30 diff --git a/ShareX.UploadersLib/Forms/UploadersConfigFormHelper.cs b/ShareX.UploadersLib/Forms/UploadersConfigFormHelper.cs index 12d5db582..135f54fbd 100644 --- a/ShareX.UploadersLib/Forms/UploadersConfigFormHelper.cs +++ b/ShareX.UploadersLib/Forms/UploadersConfigFormHelper.cs @@ -1576,5 +1576,22 @@ private void ConfigureOAuthStatus(OAuthControl oauth2, bool result) } #endregion Generic OAuth2 + + #region Miscellaneous + + /// + /// Adds or replaces placeholder text in a text box without affecting the content (.Text) of the + /// text box. + /// + /// The TextBox to operate on. + /// Text to display to the user. + public static void SetPlaceholderText(TextBox textBox, string text) + { + // https://stackoverflow.com/a/22405509 + const int EM_SETCUEBANNER = 0x1501; + NativeMethods.SendMessage(textBox.Handle, EM_SETCUEBANNER, 0, text); + } + + #endregion Miscellaneous } } \ No newline at end of file diff --git a/ShareX.UploadersLib/UploadersConfig.cs b/ShareX.UploadersLib/UploadersConfig.cs index 679cd3af6..f400808c3 100644 --- a/ShareX.UploadersLib/UploadersConfig.cs +++ b/ShareX.UploadersLib/UploadersConfig.cs @@ -380,7 +380,7 @@ public class UploadersConfig : SettingsBase public string B2ApplicationKeyId = ""; public string B2ApplicationKey = ""; - public string B2BucketName = "mybucket"; + public string B2BucketName = ""; public string B2UploadPath = "ShareX/%y/%mo/"; public bool B2UseCustomUrl = false; public string B2CustomUrl = "https://example.com/";