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.
This commit is contained in:
tinybarks 2018-10-01 16:39:16 +02:00
parent adc09ca0e3
commit 140d583a1c
No known key found for this signature in database
GPG key ID: D999045C35D5795A
5 changed files with 46 additions and 13 deletions

View file

@ -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<string, string> { ["bucketName"] = bucketName };
var reqBody = new Dictionary<string, string>
{
["accountId"] = auth.accountId,
["bucketName"] = bucketName
};
using (var data = CreateJsonBody(reqBody))
{

View file

@ -98,6 +98,8 @@ private void InitializeControls()
CodeMenu.Create<CodeMenuEntryFilename>(txtAmazonS3ObjectPrefix, CodeMenuEntryFilename.n, CodeMenuEntryFilename.t, CodeMenuEntryFilename.pn);
CodeMenu.Create<CodeMenuEntryFilename>(txtMediaFirePath, CodeMenuEntryFilename.n, CodeMenuEntryFilename.t, CodeMenuEntryFilename.pn);
CodeMenu.Create<CodeMenuEntryFilename>(txtGoogleCloudStorageObjectPrefix, CodeMenuEntryFilename.n, CodeMenuEntryFilename.t, CodeMenuEntryFilename.pn);
CodeMenu.Create<CodeMenuEntryFilename>(txtB2UploadPath, CodeMenuEntryFilename.n, CodeMenuEntryFilename.t, CodeMenuEntryFilename.pn);
CodeMenu.Create<CodeMenuEntryFilename>(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<ResponseType>());
// 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;

View file

@ -261,9 +261,15 @@ For example, if your bucket is called bucket.example.com then URL will be http:/
<value>Use custom URL (supports pattern format):</value>
</data>
<data name="cbB2CustomUrl.ToolTip" xml:space="preserve">
<value>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.
<value>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. </value>
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. </value>
</data>
<data name="&gt;&gt;cbB2CustomUrl.Name" xml:space="preserve">
<value>cbB2CustomUrl</value>
@ -289,7 +295,9 @@ If disabled, then the URL you will receive will be determined by B2. </value>
<data name="txtB2Bucket.ToolTip" xml:space="preserve">
<value>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.</value>
This only takes effect when you did not set
"Allow access to bucket(s)" to a bucket
when you made the application key.</value>
</data>
<data name="&gt;&gt;txtB2Bucket.Name" xml:space="preserve">
<value>txtB2Bucket</value>
@ -337,7 +345,7 @@ The bucket needs to be public as the Backblaze B2 Cloud Storage API does not all
<value>3</value>
</data>
<data name="txtB2ApplicationKey.ToolTip" xml:space="preserve">
<value>The application key.</value>
<value>The application key. Looks alphanumeric.</value>
</data>
<data name="&gt;&gt;txtB2ApplicationKey.Name" xml:space="preserve">
<value>txtB2ApplicationKey</value>
@ -361,7 +369,7 @@ The bucket needs to be public as the Backblaze B2 Cloud Storage API does not all
<value>2</value>
</data>
<data name="txtB2ApplicationKeyId.ToolTip" xml:space="preserve">
<value>The Application Key ID looks like an all-lowercase hexadecimal string.</value>
<value>The Application Key ID. Looks like a lowercase hexadecimal string.</value>
</data>
<data name="&gt;&gt;txtB2ApplicationKeyId.Name" xml:space="preserve">
<value>txtB2ApplicationKeyId</value>
@ -8047,13 +8055,13 @@ store.book[0].title</value>
<value>2</value>
</data>
<data name="tpGfycat.Location" type="System.Drawing.Point, System.Drawing">
<value>4, 40</value>
<value>4, 220</value>
</data>
<data name="tpGfycat.Padding" type="System.Windows.Forms.Padding, System.Windows.Forms">
<value>3, 3, 3, 3</value>
</data>
<data name="tpGfycat.Size" type="System.Drawing.Size, System.Drawing">
<value>972, 519</value>
<value>178, 0</value>
</data>
<data name="tpGfycat.TabIndex" type="System.Int32, mscorlib">
<value>30</value>

View file

@ -1576,5 +1576,22 @@ private void ConfigureOAuthStatus(OAuthControl oauth2, bool result)
}
#endregion Generic OAuth2
#region Miscellaneous
/// <summary>
/// Adds or replaces placeholder text in a text box without affecting the content (<c>.Text</c>) of the
/// text box.
/// </summary>
/// <param name="textBox">The TextBox to operate on.</param>
/// <param name="text">Text to display to the user.</param>
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
}
}

View file

@ -380,7 +380,7 @@ public class UploadersConfig : SettingsBase<UploadersConfig>
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/";