ShareX/ShareX.UploadersLib/FileUploaders/Lithiio.cs

140 lines
4.3 KiB
C#
Raw Normal View History

#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2020-02-05 20:19:48 +13:00
Copyright (c) 2007-2020 ShareX Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
2016-05-16 00:22:13 +12:00
// Credits: https://github.com/lithium720
using Newtonsoft.Json;
2016-06-28 05:20:55 +12:00
using ShareX.UploadersLib.Properties;
2017-11-11 11:21:04 +13:00
using System;
using System.Collections.Generic;
2016-06-28 05:20:55 +12:00
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace ShareX.UploadersLib.FileUploaders
{
public class LithiioFileUploaderService : FileUploaderService
{
public override FileDestination EnumValue { get; } = FileDestination.Lithiio;
2016-06-28 05:20:55 +12:00
2019-09-14 09:22:48 +12:00
public override Image ServiceImage => Resources.Lithiio;
public override bool CheckConfig(UploadersConfig config)
{
2016-07-23 17:15:46 +12:00
return config.LithiioSettings != null && !string.IsNullOrEmpty(config.LithiioSettings.UserAPIKey);
}
public override GenericUploader CreateUploader(UploadersConfig config, TaskReferenceHelper taskInfo)
{
return new Lithiio(config.LithiioSettings);
}
public override TabPage GetUploadersConfigTabPage(UploadersConfigForm form) => form.tpLithiio;
}
public sealed class Lithiio : FileUploader
{
public LithiioSettings Config { get; private set; }
2017-11-11 11:21:04 +13:00
public Lithiio()
{
}
public Lithiio(LithiioSettings config)
{
Config = config;
}
public override UploadResult Upload(Stream stream, string fileName)
{
2017-11-11 11:21:04 +13:00
Dictionary<string, string> args = new Dictionary<string, string>();
2019-09-14 09:25:52 +12:00
args.Add("api_key", Config.UserAPIKey);
2019-12-23 17:29:56 +13:00
UploadResult result = SendRequestFile("https://lithi.io/api/v2/upload", stream, fileName, "file", args);
if (result.IsSuccess)
{
2017-11-11 11:21:04 +13:00
LithiioUploadResponse uploadResponse = JsonConvert.DeserializeObject<LithiioUploadResponse>(result.Response);
2017-11-11 11:21:04 +13:00
if (uploadResponse.Success)
{
2017-11-11 11:21:04 +13:00
result.URL = uploadResponse.URL;
}
else
{
2017-11-11 11:21:04 +13:00
Errors.Add(uploadResponse.Error);
}
}
return result;
}
2017-11-11 11:21:04 +13:00
public string FetchAPIKey(string email, string password)
{
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("email", email);
args.Add("password", password);
2019-12-23 17:29:56 +13:00
string response = SendRequestMultiPart("https://lithi.io/api/v2/fetch-api-key", args);
2017-11-11 11:21:04 +13:00
if (!string.IsNullOrEmpty(response))
{
LithiioFetchAPIKeyResponse apiKeyResponse = JsonConvert.DeserializeObject<LithiioFetchAPIKeyResponse>(response);
if (apiKeyResponse.Success)
{
2019-09-14 09:25:52 +12:00
return apiKeyResponse.API_Key;
2017-11-11 11:21:04 +13:00
}
else
{
throw new Exception(apiKeyResponse.Error);
}
}
return null;
}
private class LithiioResponse
{
public bool Success { get; set; }
public string Error { get; set; }
}
2017-11-11 11:21:04 +13:00
private class LithiioUploadResponse : LithiioResponse
{
public string URL { get; set; }
}
private class LithiioFetchAPIKeyResponse : LithiioResponse
{
2019-09-14 09:25:52 +12:00
public string API_Key { get; set; }
2017-11-11 11:21:04 +13:00
}
}
public class LithiioSettings
{
2016-05-25 06:15:45 +12:00
public string UserAPIKey { get; set; } = "";
}
2016-05-25 06:15:45 +12:00
}