Skip to content

Commit 0e4267f

Browse files
authored
Renaming class for storage to have unique settings field in configuration, code updates
1 parent 4154fa1 commit 0e4267f

File tree

7 files changed

+126
-61
lines changed

7 files changed

+126
-61
lines changed

HttpApiUploadPlugin/EnumExtensions.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Linq;
3+
using System.Reflection;
4+
5+
namespace HttpApiUploadPlugin
6+
{
7+
internal static class EnumExtensions
8+
{
9+
public static string GetDescription(this Enum value)
10+
{
11+
Type enumType = value.GetType();
12+
13+
MemberInfo[] memberInfo = enumType.GetMember(value.ToString());
14+
15+
if (memberInfo.Length > 0)
16+
{
17+
object[] attributes = memberInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);
18+
19+
if ((attributes.Length > 0))
20+
{
21+
return ((System.ComponentModel.DescriptionAttribute) attributes.ElementAt(0)).Description;
22+
}
23+
}
24+
25+
return value.ToString();
26+
}
27+
}
28+
}

HttpApiUploadPlugin/HttpApiUploadPlugin.csproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
77
<ProjectGuid>{19577DE2-F9C4-4979-84E0-9C4AAA260AE1}</ProjectGuid>
88
<OutputType>Library</OutputType>
9-
<RootNamespace>HttpApiPlugin</RootNamespace>
10-
<AssemblyName>HttpApiPlugin</AssemblyName>
9+
<RootNamespace>HttpApiUploadPlugin</RootNamespace>
10+
<AssemblyName>HttpApiUploadPlugin</AssemblyName>
1111
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
1212
<FileAlignment>512</FileAlignment>
1313
<Deterministic>true</Deterministic>
@@ -40,6 +40,8 @@
4040
<Reference Include="System.Windows.Forms" />
4141
</ItemGroup>
4242
<ItemGroup>
43+
<Compile Include="EnumExtensions.cs" />
44+
<Compile Include="HttpApiUploadStorage.cs" />
4345
<Compile Include="LocalResources.Designer.cs">
4446
<DependentUpon>LocalResources.resx</DependentUpon>
4547
<AutoGen>True</AutoGen>
@@ -52,7 +54,6 @@
5254
<DependentUpon>SettingsControl.cs</DependentUpon>
5355
</Compile>
5456
<Compile Include="PluginSettings.cs" />
55-
<Compile Include="Storage.cs" />
5657
<Compile Include="Properties\AssemblyInfo.cs" />
5758
<Service Include="{94E38DFF-614B-4cbd-B67C-F211BB35CE8B}" />
5859
</ItemGroup>

HttpApiUploadPlugin/Storage.cs renamed to HttpApiUploadPlugin/HttpApiUploadStorage.cs

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,67 +11,65 @@
1111

1212
namespace HttpApiUploadPlugin
1313
{
14-
public class Storage : ImageStorage<PluginSettings>
14+
public class HttpApiUploadStorage : ImageStorage<PluginSettings>
1515
{
1616
public override Image Logo { get; }
1717

18-
private static readonly HttpClient client = new HttpClient();
19-
20-
public Storage() : base("HTTP API Upload", "Upload image to a generic HTTP API")
18+
public HttpApiUploadStorage() : base("HTTP API Upload", "Upload image to a generic HTTP API")
2119
{
22-
DpiResourcesManager manager = new DpiResourcesManager(LocalResources.ResourceManager);
20+
var manager = new DpiResourcesManager(LocalResources.ResourceManager);
2321
Logo = manager.GetImage(nameof(LocalResources.folder));
2422
}
2523

2624
public override async Task<StorageSaveResult> SaveImage(byte[] data, string shotName)
2725
{
2826
PluginSettings settings = GetSettings();
2927

30-
if (settings.ApiUrl == "")
28+
if (string.IsNullOrEmpty(settings.ApiUrl))
3129
{
3230
return new StorageSaveResult(SavingResultCode.RequiresOptionsSetup, "API URL has to be set");
3331
}
3432

3533
var values = new Dictionary<string, string>
3634
{
37-
{ "dataurl", GetDataURL(data, shotName) }
35+
{"dataurl", GetDataURL(data, shotName)}
3836
};
3937

40-
var content = new FormUrlEncodedContent(values);
41-
42-
client.DefaultRequestHeaders.Accept.Clear();
43-
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain"));
44-
45-
var response = await client.PostAsync(settings.ApiUrl, content);
46-
4738
try
4839
{
49-
response.EnsureSuccessStatusCode();
50-
var responseString = await response.Content.ReadAsStringAsync();
51-
52-
string stringToClipboard;
53-
54-
switch (settings.CopyStyle)
40+
using (var client = new HttpClient())
5541
{
56-
case 1:
57-
stringToClipboard = GetUrlAsImgTag(responseString);
58-
break;
59-
case 2:
60-
stringToClipboard = GetUrlAsImgTagWithWidth(responseString);
61-
break;
62-
case 0:
63-
default:
64-
stringToClipboard = responseString;
65-
break;
42+
var content = new FormUrlEncodedContent(values);
43+
client.DefaultRequestHeaders.Accept.Clear();
44+
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain"));
45+
46+
HttpResponseMessage response = await client.PostAsync(settings.ApiUrl, content);
47+
48+
response.EnsureSuccessStatusCode();
49+
50+
string responseString = await response.Content.ReadAsStringAsync();
51+
string stringToClipboard;
52+
53+
switch (settings.CopyStyle)
54+
{
55+
case CopyStyle.ImgTag:
56+
stringToClipboard = GetUrlAsImgTag(responseString);
57+
break;
58+
case CopyStyle.ImgTagWithEmptyWidth:
59+
stringToClipboard = GetUrlAsImgTagWithWidth(responseString);
60+
break;
61+
default:
62+
stringToClipboard = responseString;
63+
break;
64+
}
65+
66+
return new StorageSaveResult(shotName, stringToClipboard);
6667
}
67-
68-
return new StorageSaveResult(shotName, stringToClipboard);
6968
}
7069
catch (HttpRequestException ex)
7170
{
7271
return new StorageSaveResult(SavingResultCode.Failed, ex);
7372
}
74-
7573
}
7674

7775
public static string GetDataURL(byte[] data, string shotName)
@@ -84,11 +82,12 @@ public static string GetUrlAsImgTag(string url)
8482
{
8583
return "<img src=\"" + url + "\">";
8684
}
85+
8786
public static string GetUrlAsImgTagWithWidth(string url)
8887
{
8988
return "<img src=\"" + url + "\" width=\"\">";
9089
}
9190

9291
public override ISettingsControl CreateSettingsControl() => new SettingsControl(this);
9392
}
94-
}
93+
}

HttpApiUploadPlugin/LocalResources.Designer.cs

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

HttpApiUploadPlugin/PluginSettings.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
using System;
2-
using System.IO;
1+
using System.ComponentModel;
32

43
namespace HttpApiUploadPlugin
54
{
5+
public enum CopyStyle
6+
{
7+
[Description("URL")] Url = 0,
8+
[Description("IMG tag")] ImgTag,
9+
[Description("IMG tag with empty WIDTH attribute")] ImgTagWithEmptyWidth
10+
}
11+
612
public class PluginSettings
713
{
8-
public string ApiUrl { get; set; } = "";
14+
public string ApiUrl { get; set; } = string.Empty;
915

10-
public int CopyStyle { get; set; } = 0;
16+
public CopyStyle CopyStyle { get; set; } = CopyStyle.Url;
1117
}
12-
}
18+
}
Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,66 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
24
using System.Windows.Forms;
35
using CloudShot.Core.Interfaces;
46

57
namespace HttpApiUploadPlugin
68
{
79
public partial class SettingsControl : UserControl, ISettingsControl
810
{
9-
private readonly Storage _storage;
11+
private class CopyStyleItem
12+
{
13+
public CopyStyle CopyStyle { get; }
14+
15+
public string DisplayName { get; }
16+
17+
public CopyStyleItem(CopyStyle copyStyle)
18+
{
19+
CopyStyle = copyStyle;
20+
DisplayName = copyStyle.GetDescription();
21+
}
22+
}
23+
24+
private readonly HttpApiUploadStorage _storage;
25+
private readonly List<CopyStyleItem> _copyStyleItems = new List<CopyStyleItem>();
1026

1127
public IImageStorage Storage => _storage;
1228

1329
public Control Control => this;
1430

15-
public SettingsControl(Storage storage)
31+
public SettingsControl(HttpApiUploadStorage storage)
1632
{
1733
_storage = storage;
1834

35+
foreach (CopyStyle value in Enum.GetValues(typeof(CopyStyle)))
36+
{
37+
_copyStyleItems.Add(new CopyStyleItem(value));
38+
}
39+
1940
InitializeComponent();
2041

21-
apiUrlTextBox.Text = "";
42+
apiUrlTextBox.Text = string.Empty;
43+
copyStyleDropDown.DataSource = _copyStyleItems;
44+
copyStyleDropDown.ValueMember = nameof(CopyStyleItem.CopyStyle);
45+
copyStyleDropDown.DisplayMember = nameof(CopyStyleItem.DisplayName);
2246
}
2347

2448
public void Loading()
2549
{
2650
PluginSettings settings = _storage.GetSettings();
2751
apiUrlTextBox.Text = settings.ApiUrl;
28-
copyStyleDropDown.SelectedIndex = settings.CopyStyle;
52+
copyStyleDropDown.SelectedValue = settings.CopyStyle;
2953
}
3054

3155
public bool ValidateData()
3256
{
33-
Uri uriResult;
34-
bool result = Uri.TryCreate(apiUrlTextBox.Text, UriKind.Absolute, out uriResult);
57+
bool result = Uri.TryCreate(apiUrlTextBox.Text, UriKind.Absolute, out Uri _);
3558

36-
if (result || apiUrlTextBox.Text == "") {
59+
if (result || string.IsNullOrEmpty(apiUrlTextBox.Text))
60+
{
3761
return true;
3862
}
39-
63+
4064
MessageBox.Show("Invalid URL", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
4165
apiUrlTextBox.Focus();
4266
return false;
@@ -46,10 +70,17 @@ public void Save()
4670
{
4771
PluginSettings settings = _storage.GetSettings();
4872
settings.ApiUrl = apiUrlTextBox.Text;
49-
settings.CopyStyle = copyStyleDropDown.SelectedIndex;
73+
settings.CopyStyle = (CopyStyle) copyStyleDropDown.SelectedValue;
5074
_storage.SaveSettings(settings);
5175
}
5276

53-
public void CancelClicked() { }
77+
public void CancelClicked()
78+
{
79+
}
80+
81+
private void OnLinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
82+
{
83+
Process.Start("https://github.com/finwe/cloudshot-http-api-upload-plugin");
84+
}
5485
}
55-
}
86+
}

HttpApiUploadPlugin/SettingsControl.designer.cs

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)