ShareX/ShareX.UploadersLib/FileUploaders/Ge_tt.cs

165 lines
5.4 KiB
C#
Raw Normal View History

2013-11-03 23:53:49 +13:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2019-01-02 20:43:52 +13:00
Copyright (c) 2007-2019 ShareX Team
2013-11-03 23:53:49 +13:00
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;
2017-09-24 20:14:27 +13:00
using ShareX.HelpersLib;
2016-06-28 05:20:55 +12:00
using ShareX.UploadersLib.Properties;
2013-11-03 23:53:49 +13:00
using System.Collections.Generic;
2016-06-28 05:20:55 +12:00
using System.Drawing;
2013-11-03 23:53:49 +13:00
using System.IO;
using System.Windows.Forms;
2013-11-03 23:53:49 +13:00
2014-12-11 09:25:20 +13:00
namespace ShareX.UploadersLib.FileUploaders
2013-11-03 23:53:49 +13:00
{
public class Ge_ttFileUploaderService : FileUploaderService
{
public override FileDestination EnumValue { get; } = FileDestination.Ge_tt;
2016-06-28 05:20:55 +12:00
public override Icon ServiceIcon => Resources.Gett;
public override bool CheckConfig(UploadersConfig config)
{
return config.Ge_ttLogin != null && !string.IsNullOrEmpty(config.Ge_ttLogin.AccessToken);
}
public override GenericUploader CreateUploader(UploadersConfig config, TaskReferenceHelper taskInfo)
{
return new Ge_tt(APIKeys.Ge_ttKey)
{
AccessToken = config.Ge_ttLogin.AccessToken
};
}
public override TabPage GetUploadersConfigTabPage(UploadersConfigForm form) => form.tpGe_tt;
}
2013-11-03 23:53:49 +13:00
public sealed class Ge_tt : FileUploader
{
private const string APIURL = "http://api.ge.tt/1";
2013-11-03 23:53:49 +13:00
public string APIKey { get; private set; }
public string AccessToken { get; set; }
public Ge_tt(string apiKey)
{
APIKey = apiKey;
}
public Ge_ttLogin Login(string email, string password)
{
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("apikey", APIKey);
args.Add("email", email);
args.Add("password", password);
2016-12-23 20:55:16 +13:00
string json = JsonConvert.SerializeObject(args);
2013-11-03 23:53:49 +13:00
string url = URLHelpers.CombineURL(APIURL, "users/login");
2019-03-15 00:06:54 +13:00
string response = SendRequest(HttpMethod.POST, url, json, RequestHelpers.ContentTypeJSON);
2013-11-03 23:53:49 +13:00
return JsonConvert.DeserializeObject<Ge_ttLogin>(response);
}
public Ge_ttShare CreateShare(string accessToken)
{
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("accesstoken", accessToken);
string url = URLHelpers.CreateQueryString(URLHelpers.CombineURL(APIURL, "shares/create"), args);
string response = SendRequest(HttpMethod.POST, url);
2013-11-03 23:53:49 +13:00
return JsonConvert.DeserializeObject<Ge_ttShare>(response);
}
public Ge_ttFile CreateFile(string accessToken, string shareName, string fileName)
{
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("accesstoken", accessToken);
Dictionary<string, string> args2 = new Dictionary<string, string>();
args2.Add("filename", fileName);
2016-12-23 20:55:16 +13:00
string json = JsonConvert.SerializeObject(args2);
2013-11-03 23:53:49 +13:00
2019-03-15 00:06:54 +13:00
string response = SendRequest(HttpMethod.POST, URLHelpers.CombineURL(APIURL, "files", shareName, "create"), json, RequestHelpers.ContentTypeJSON, args);
2013-11-03 23:53:49 +13:00
return JsonConvert.DeserializeObject<Ge_ttFile>(response);
}
public override UploadResult Upload(Stream stream, string fileName)
{
UploadResult result = null;
Ge_ttShare share = CreateShare(AccessToken);
if (share != null)
{
Ge_ttFile file = CreateFile(AccessToken, share.ShareName, fileName);
if (file != null)
{
2018-11-29 04:15:58 +13:00
result = SendRequestFile(file.Upload.PostURL, stream, fileName, "file");
2013-11-03 23:53:49 +13:00
if (result.IsSuccess)
{
result.URL = file.GettURL;
}
}
}
return result;
}
}
public class Ge_ttLogin
{
public string Expires { get; set; }
public string AccessToken { get; set; }
public string RefreshToken { get; set; }
}
public class Ge_ttShare
{
public string Created { get; set; }
public string UserID { get; set; }
public string ShareName { get; set; }
public string ReadyState { get; set; }
public string FullName { get; set; }
public string Type { get; set; }
public string GettURL { get; set; }
}
public class Ge_ttFile
{
public string GettURL { get; set; }
public Ge_ttUpload Upload { get; set; }
}
public class Ge_ttUpload
{
public string PostURL { get; set; }
public string PutURL { get; set; }
}
}