Skip to content

Commit 7388cd3

Browse files
committed
Added login support to Lithiio
1 parent a7aea34 commit 7388cd3

File tree

5 files changed

+10561
-4020
lines changed

5 files changed

+10561
-4020
lines changed

ShareX.HelpersLib/Extensions/Extensions.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -592,9 +592,10 @@ public static void RefreshSelectedItem(this ListBox lb)
592592
}
593593
}
594594

595-
public static void ShowError(this Exception e)
595+
public static void ShowError(this Exception e, bool fullError = true)
596596
{
597-
MessageBox.Show(e.ToString(), "ShareX - " + Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
597+
string error = fullError ? e.ToString() : e.Message;
598+
MessageBox.Show(error, "ShareX - " + Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
598599
}
599600
}
600601
}

ShareX.UploadersLib/FileUploaders/Lithiio.cs

+48-9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ You should have received a copy of the GNU General Public License
2727

2828
using Newtonsoft.Json;
2929
using ShareX.UploadersLib.Properties;
30+
using System;
3031
using System.Collections.Generic;
3132
using System.Drawing;
3233
using System.IO;
@@ -57,41 +58,79 @@ public sealed class Lithiio : FileUploader
5758
{
5859
public LithiioSettings Config { get; private set; }
5960

61+
public Lithiio()
62+
{
63+
}
64+
6065
public Lithiio(LithiioSettings config)
6166
{
6267
Config = config;
6368
}
6469

6570
public override UploadResult Upload(Stream stream, string fileName)
6671
{
67-
Dictionary<string, string> arguments = new Dictionary<string, string>();
68-
arguments.Add("key", Config.UserAPIKey);
72+
Dictionary<string, string> args = new Dictionary<string, string>();
73+
args.Add("key", Config.UserAPIKey);
6974

70-
UploadResult result = SendRequestFile("https://upload.lithi.io/v1.php", stream, fileName, "file", arguments);
75+
UploadResult result = SendRequestFile("https://upload.lithi.io/v1.php", stream, fileName, "file", args);
7176

7277
if (result.IsSuccess)
7378
{
74-
LithiioResponse response = JsonConvert.DeserializeObject<LithiioResponse>(result.Response);
79+
LithiioUploadResponse uploadResponse = JsonConvert.DeserializeObject<LithiioUploadResponse>(result.Response);
7580

76-
if (response.Success)
81+
if (uploadResponse.Success)
7782
{
78-
result.URL = response.URL;
83+
result.URL = uploadResponse.URL;
7984
}
8085
else
8186
{
82-
Errors.Add(response.Error);
87+
Errors.Add(uploadResponse.Error);
8388
}
8489
}
8590

8691
return result;
8792
}
8893

89-
public class LithiioResponse
94+
public string FetchAPIKey(string email, string password)
95+
{
96+
Dictionary<string, string> args = new Dictionary<string, string>();
97+
args.Add("email", email);
98+
args.Add("password", password);
99+
100+
string response = SendRequestMultiPart("https://lithi.io/api/v1/fetch-api-key.php", args);
101+
102+
if (!string.IsNullOrEmpty(response))
103+
{
104+
LithiioFetchAPIKeyResponse apiKeyResponse = JsonConvert.DeserializeObject<LithiioFetchAPIKeyResponse>(response);
105+
106+
if (apiKeyResponse.Success)
107+
{
108+
return apiKeyResponse.APIKey;
109+
}
110+
else
111+
{
112+
throw new Exception(apiKeyResponse.Error);
113+
}
114+
}
115+
116+
return null;
117+
}
118+
119+
private class LithiioResponse
90120
{
91121
public bool Success { get; set; }
92-
public string URL { get; set; }
93122
public string Error { get; set; }
94123
}
124+
125+
private class LithiioUploadResponse : LithiioResponse
126+
{
127+
public string URL { get; set; }
128+
}
129+
130+
private class LithiioFetchAPIKeyResponse : LithiioResponse
131+
{
132+
public string APIKey { get; set; }
133+
}
95134
}
96135

97136
public class LithiioSettings

ShareX.UploadersLib/Forms/UploadersConfigForm.Designer.cs

+51-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ShareX.UploadersLib/Forms/UploadersConfigForm.cs

+20
Original file line numberDiff line numberDiff line change
@@ -2807,6 +2807,26 @@ private void txtLithiioApiKey_TextChanged(object sender, EventArgs e)
28072807
Config.LithiioSettings.UserAPIKey = txtLithiioApiKey.Text;
28082808
}
28092809

2810+
private void btnLithiioLogin_Click(object sender, EventArgs e)
2811+
{
2812+
try
2813+
{
2814+
Cursor = Cursors.WaitCursor;
2815+
2816+
Lithiio lithiio = new Lithiio();
2817+
string apiKey = lithiio.FetchAPIKey(txtLithiioEmail.Text, txtLithiioPassword.Text);
2818+
txtLithiioApiKey.Text = apiKey ?? "";
2819+
}
2820+
catch (Exception ex)
2821+
{
2822+
ex.ShowError(false);
2823+
}
2824+
finally
2825+
{
2826+
Cursor = Cursors.Default;
2827+
}
2828+
}
2829+
28102830
private void btnLithiioGetAPIKey_Click(object sender, EventArgs e)
28112831
{
28122832
URLHelpers.OpenURL("https://lithi.io/my-account.php");

0 commit comments

Comments
 (0)