ShareX/ShareX.UploadersLib/TextUploaders/Paste_ee.cs

141 lines
4.5 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)
2017-10-03 06:15:54 +13:00
using Newtonsoft.Json;
2016-06-28 05:20:55 +12:00
using ShareX.UploadersLib.Properties;
2017-10-03 06:15:54 +13:00
using System;
using System.Collections.Specialized;
2016-06-28 05:20:55 +12:00
using System.Drawing;
using System.Windows.Forms;
2013-11-03 23:53:49 +13:00
2014-12-11 09:25:20 +13:00
namespace ShareX.UploadersLib.TextUploaders
2013-11-03 23:53:49 +13:00
{
public class Paste_eeTextUploaderService : TextUploaderService
{
public override TextDestination EnumValue { get; } = TextDestination.Paste_ee;
2016-06-30 08:21:05 +12:00
public override Image ServiceImage => Resources.document;
2016-06-28 05:20:55 +12:00
public override bool CheckConfig(UploadersConfig config) => true;
public override GenericUploader CreateUploader(UploadersConfig config, TaskReferenceHelper taskInfo)
{
2017-10-03 06:15:54 +13:00
string apiKey;
if (!string.IsNullOrEmpty(config.Paste_eeUserKey))
{
apiKey = config.Paste_eeUserKey;
}
else
{
apiKey = APIKeys.Paste_eeApplicationKey;
}
2018-04-06 08:50:54 +12:00
return new Paste_ee(apiKey)
{
EncryptPaste = config.Paste_eeEncryptPaste
};
}
public override TabPage GetUploadersConfigTabPage(UploadersConfigForm form) => form.tpPaste_ee;
}
2013-11-03 23:53:49 +13:00
public sealed class Paste_ee : TextUploader
{
public string APIKey { get; private set; }
2018-04-06 08:50:54 +12:00
public bool EncryptPaste { get; set; }
2013-11-03 23:53:49 +13:00
public Paste_ee(string apiKey)
{
APIKey = apiKey;
}
public override UploadResult UploadText(string text, string fileName)
{
2017-10-03 06:15:54 +13:00
if (string.IsNullOrEmpty(APIKey))
{
throw new Exception("API key is missing.");
}
2013-11-03 23:53:49 +13:00
UploadResult ur = new UploadResult();
if (!string.IsNullOrEmpty(text))
{
2017-10-03 06:15:54 +13:00
Paste_eeSubmitRequestBody requestBody = new Paste_eeSubmitRequestBody()
2013-11-03 23:53:49 +13:00
{
2018-04-06 08:50:54 +12:00
encrypted = EncryptPaste,
2017-10-03 06:15:54 +13:00
description = "",
expiration = "never",
2017-10-03 06:15:54 +13:00
sections = new Paste_eeSubmitRequestBodySection[]
{
new Paste_eeSubmitRequestBodySection()
{
name = "",
syntax = "autodetect",
2017-10-03 06:15:54 +13:00
contents = text
}
}
};
string json = JsonConvert.SerializeObject(requestBody);
NameValueCollection headers = new NameValueCollection();
headers.Add("X-Auth-Token", APIKey);
2018-10-18 05:06:06 +13:00
ur.Response = SendRequest(HttpMethod.POST, "https://api.paste.ee/v1/pastes", json, UploadHelpers.ContentTypeJSON, null, headers);
2017-10-03 06:15:54 +13:00
if (!string.IsNullOrEmpty(ur.Response))
2013-11-03 23:53:49 +13:00
{
2017-10-03 06:15:54 +13:00
Paste_eeSubmitResponse response = JsonConvert.DeserializeObject<Paste_eeSubmitResponse>(ur.Response);
ur.URL = response.link;
2013-11-03 23:53:49 +13:00
}
}
return ur;
}
}
2017-10-03 06:15:54 +13:00
public class Paste_eeSubmitRequestBody
{
public bool encrypted { get; set; }
public string description { get; set; }
public string expiration { get; set; }
2017-10-03 06:15:54 +13:00
public Paste_eeSubmitRequestBodySection[] sections { get; set; }
}
public class Paste_eeSubmitRequestBodySection
{
public string name { get; set; }
public string syntax { get; set; }
2017-10-03 06:15:54 +13:00
public string contents { get; set; }
}
public class Paste_eeSubmitResponse
{
public string id { get; set; }
public string link { get; set; }
}
2013-11-03 23:53:49 +13:00
}