Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added login support to Lithiio
  • Loading branch information
Jaex committed Nov 10, 2017
1 parent a7aea34 commit 7388cd3
Show file tree
Hide file tree
Showing 5 changed files with 10,561 additions and 4,020 deletions.
5 changes: 3 additions & 2 deletions ShareX.HelpersLib/Extensions/Extensions.cs
Expand Up @@ -592,9 +592,10 @@ public static void RefreshSelectedItem(this ListBox lb)
}
}

public static void ShowError(this Exception e)
public static void ShowError(this Exception e, bool fullError = true)
{
MessageBox.Show(e.ToString(), "ShareX - " + Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
string error = fullError ? e.ToString() : e.Message;
MessageBox.Show(error, "ShareX - " + Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
57 changes: 48 additions & 9 deletions ShareX.UploadersLib/FileUploaders/Lithiio.cs
Expand Up @@ -27,6 +27,7 @@

using Newtonsoft.Json;
using ShareX.UploadersLib.Properties;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
Expand Down Expand Up @@ -57,41 +58,79 @@ public sealed class Lithiio : FileUploader
{
public LithiioSettings Config { get; private set; }

public Lithiio()
{
}

public Lithiio(LithiioSettings config)
{
Config = config;
}

public override UploadResult Upload(Stream stream, string fileName)
{
Dictionary<string, string> arguments = new Dictionary<string, string>();
arguments.Add("key", Config.UserAPIKey);
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("key", Config.UserAPIKey);

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

if (result.IsSuccess)
{
LithiioResponse response = JsonConvert.DeserializeObject<LithiioResponse>(result.Response);
LithiioUploadResponse uploadResponse = JsonConvert.DeserializeObject<LithiioUploadResponse>(result.Response);

if (response.Success)
if (uploadResponse.Success)
{
result.URL = response.URL;
result.URL = uploadResponse.URL;
}
else
{
Errors.Add(response.Error);
Errors.Add(uploadResponse.Error);
}
}

return result;
}

public class LithiioResponse
public string FetchAPIKey(string email, string password)
{
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("email", email);
args.Add("password", password);

string response = SendRequestMultiPart("https://lithi.io/api/v1/fetch-api-key.php", args);

if (!string.IsNullOrEmpty(response))
{
LithiioFetchAPIKeyResponse apiKeyResponse = JsonConvert.DeserializeObject<LithiioFetchAPIKeyResponse>(response);

if (apiKeyResponse.Success)
{
return apiKeyResponse.APIKey;
}
else
{
throw new Exception(apiKeyResponse.Error);
}
}

return null;
}

private class LithiioResponse
{
public bool Success { get; set; }
public string URL { get; set; }
public string Error { get; set; }
}

private class LithiioUploadResponse : LithiioResponse
{
public string URL { get; set; }
}

private class LithiioFetchAPIKeyResponse : LithiioResponse
{
public string APIKey { get; set; }
}
}

public class LithiioSettings
Expand Down
59 changes: 51 additions & 8 deletions ShareX.UploadersLib/Forms/UploadersConfigForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions ShareX.UploadersLib/Forms/UploadersConfigForm.cs
Expand Up @@ -2807,6 +2807,26 @@ private void txtLithiioApiKey_TextChanged(object sender, EventArgs e)
Config.LithiioSettings.UserAPIKey = txtLithiioApiKey.Text;
}

private void btnLithiioLogin_Click(object sender, EventArgs e)
{
try
{
Cursor = Cursors.WaitCursor;

Lithiio lithiio = new Lithiio();
string apiKey = lithiio.FetchAPIKey(txtLithiioEmail.Text, txtLithiioPassword.Text);
txtLithiioApiKey.Text = apiKey ?? "";
}
catch (Exception ex)
{
ex.ShowError(false);
}
finally
{
Cursor = Cursors.Default;
}
}

private void btnLithiioGetAPIKey_Click(object sender, EventArgs e)
{
URLHelpers.OpenURL("https://lithi.io/my-account.php");
Expand Down

0 comments on commit 7388cd3

Please sign in to comment.