ShareX/ShareX.UploadersLib/FileUploaders/LobFile.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
2024-01-03 12:57:14 +13:00
Copyright (c) 2007-2024 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)
using Newtonsoft.Json;
2020-06-01 13:17:04 +12:00
using ShareX.HelpersLib;
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 LobFileFileUploaderService : FileUploaderService
{
public override FileDestination EnumValue { get; } = FileDestination.Lithiio;
2016-06-28 05:20:55 +12:00
public override Image ServiceImage => Resources.LobFile;
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 LobFile(config.LithiioSettings);
}
public override TabPage GetUploadersConfigTabPage(UploadersConfigForm form) => form.tpLithiio;
}
public sealed class LobFile : FileUploader
{
2022-02-04 00:23:32 +13:00
public LobFileSettings Config { get; private set; }
public LobFile()
2017-11-11 11:21:04 +13:00
{
}
2022-02-04 00:23:32 +13:00
public LobFile(LobFileSettings 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);
2022-02-04 00:23:32 +13:00
UploadResult result = SendRequestFile("https://lobfile.com/api/v3/upload", stream, fileName, "file", args);
if (result.IsSuccess)
{
2022-02-04 00:23:32 +13:00
LobFileUploadResponse uploadResponse = JsonConvert.DeserializeObject<LobFileUploadResponse>(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);
2022-02-20 12:44:24 +13:00
string response = SendRequestMultiPart("https://lobfile.com/api/v3/fetch-api-key", args);
2017-11-11 11:21:04 +13:00
if (!string.IsNullOrEmpty(response))
{
2022-02-04 00:23:32 +13:00
LobFileFetchAPIKeyResponse apiKeyResponse = JsonConvert.DeserializeObject<LobFileFetchAPIKeyResponse>(response);
2017-11-11 11:21:04 +13:00
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;
}
2022-02-04 00:23:32 +13:00
private class LobFileResponse
{
public bool Success { get; set; }
public string Error { get; set; }
}
2017-11-11 11:21:04 +13:00
2022-02-04 00:23:32 +13:00
private class LobFileUploadResponse : LobFileResponse
2017-11-11 11:21:04 +13:00
{
public string URL { get; set; }
}
2022-02-04 00:23:32 +13:00
private class LobFileFetchAPIKeyResponse : LobFileResponse
2017-11-11 11:21:04 +13:00
{
2019-09-14 09:25:52 +12:00
public string API_Key { get; set; }
2017-11-11 11:21:04 +13:00
}
}
2022-02-04 00:23:32 +13:00
public class LobFileSettings
{
2020-06-01 13:17:04 +12:00
[JsonEncrypt]
2016-05-25 06:15:45 +12:00
public string UserAPIKey { get; set; } = "";
}
2023-10-30 01:05:47 +13:00
}