2014-06-01 03:04:52 +12: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
|
2014-06-01 03:04:52 +12: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)
|
|
|
|
|
|
2014-06-01 20:59:37 +12:00
|
|
|
|
// Credits: https://github.com/Dinnerbone
|
|
|
|
|
|
2014-06-01 03:04:52 +12:00
|
|
|
|
using Newtonsoft.Json;
|
2014-12-11 12:19:28 +13:00
|
|
|
|
using ShareX.HelpersLib;
|
2017-03-28 13:46:05 +13:00
|
|
|
|
using ShareX.UploadersLib.Properties;
|
2017-03-30 08:22:20 +13:00
|
|
|
|
using System;
|
2014-06-01 03:04:52 +12:00
|
|
|
|
using System.Collections.Generic;
|
2017-03-28 12:05:28 +13:00
|
|
|
|
using System.Collections.Specialized;
|
2017-03-28 13:46:05 +13:00
|
|
|
|
using System.Drawing;
|
2014-06-01 03:04:52 +12:00
|
|
|
|
using System.IO;
|
2014-06-01 20:59:37 +12:00
|
|
|
|
using System.Threading;
|
2017-03-28 12:05:28 +13:00
|
|
|
|
using System.Windows.Forms;
|
2014-06-01 03:04:52 +12:00
|
|
|
|
|
2014-12-11 09:25:20 +13:00
|
|
|
|
namespace ShareX.UploadersLib.FileUploaders
|
2014-06-01 03:04:52 +12:00
|
|
|
|
{
|
2016-03-23 06:24:41 +13:00
|
|
|
|
public class GfycatFileUploaderService : FileUploaderService
|
|
|
|
|
{
|
|
|
|
|
public override FileDestination EnumValue { get; } = FileDestination.Gfycat;
|
|
|
|
|
|
2017-03-28 13:46:05 +13:00
|
|
|
|
public override Image ServiceImage => Resources.Gfycat;
|
|
|
|
|
|
2017-03-28 12:05:28 +13:00
|
|
|
|
public override bool CheckConfig(UploadersConfig config)
|
|
|
|
|
{
|
|
|
|
|
return config.GfycatAccountType == AccountType.Anonymous || OAuth2Info.CheckOAuth(config.GfycatOAuth2Info);
|
|
|
|
|
}
|
2016-03-23 06:24:41 +13:00
|
|
|
|
|
2016-04-04 21:53:11 +12:00
|
|
|
|
public override GenericUploader CreateUploader(UploadersConfig config, TaskReferenceHelper taskInfo)
|
2016-03-23 06:24:41 +13:00
|
|
|
|
{
|
2017-03-28 12:05:28 +13:00
|
|
|
|
if (config.GfycatOAuth2Info == null)
|
|
|
|
|
{
|
|
|
|
|
config.GfycatOAuth2Info = new OAuth2Info(APIKeys.GfycatClientID, APIKeys.GfycatClientSecret);
|
|
|
|
|
}
|
2017-03-28 13:46:05 +13:00
|
|
|
|
|
2017-03-28 12:05:28 +13:00
|
|
|
|
return new GfycatUploader(config.GfycatOAuth2Info)
|
|
|
|
|
{
|
|
|
|
|
UploadMethod = config.GfycatAccountType,
|
|
|
|
|
Private = !config.GfycatIsPublic,
|
|
|
|
|
};
|
2016-03-23 06:24:41 +13:00
|
|
|
|
}
|
2017-03-28 12:05:28 +13:00
|
|
|
|
|
2017-03-28 13:46:05 +13:00
|
|
|
|
public override TabPage GetUploadersConfigTabPage(UploadersConfigForm form) => form.tpGfycat;
|
2016-03-23 06:24:41 +13:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-28 12:05:28 +13:00
|
|
|
|
public class GfycatUploader : FileUploader, IOAuth2
|
2014-06-01 03:04:52 +12:00
|
|
|
|
{
|
2017-03-28 12:05:28 +13:00
|
|
|
|
public OAuth2Info AuthInfo { get; set; }
|
|
|
|
|
public AccountType UploadMethod { get; set; }
|
|
|
|
|
public OAuth2Token AnonymousToken { get; set; }
|
2017-03-28 13:46:05 +13:00
|
|
|
|
public bool NoResize { get; set; }
|
|
|
|
|
public bool IgnoreExisting { get; set; }
|
|
|
|
|
public bool Private { get; set; }
|
2014-06-01 20:59:37 +12:00
|
|
|
|
|
2017-03-28 12:05:28 +13:00
|
|
|
|
private const string URL_AUTHORIZE = "https://gfycat.com/oauth/authorize";
|
|
|
|
|
private const string URL_UPLOAD = "https://filedrop.gfycat.com";
|
|
|
|
|
private const string URL_API = "https://api.gfycat.com/v1";
|
|
|
|
|
private const string URL_API_TOKEN = URL_API + "/oauth/token";
|
|
|
|
|
private const string URL_API_CREATE_GFY = URL_API + "/gfycats";
|
|
|
|
|
private const string URL_API_STATUS = URL_API + "/gfycats/fetch/status/";
|
|
|
|
|
|
|
|
|
|
public GfycatUploader(OAuth2Info oauth)
|
2014-06-01 03:04:52 +12:00
|
|
|
|
{
|
2017-03-28 12:05:28 +13:00
|
|
|
|
AuthInfo = oauth;
|
2014-06-01 20:59:37 +12:00
|
|
|
|
NoResize = true;
|
2017-07-22 05:45:49 +12:00
|
|
|
|
IgnoreExisting = true;
|
2014-06-01 20:59:37 +12:00
|
|
|
|
}
|
2014-06-01 03:04:52 +12:00
|
|
|
|
|
2017-03-28 12:05:28 +13:00
|
|
|
|
public string GetAuthorizationURL()
|
2014-06-01 20:59:37 +12:00
|
|
|
|
{
|
|
|
|
|
Dictionary<string, string> args = new Dictionary<string, string>();
|
2017-03-28 12:05:28 +13:00
|
|
|
|
args.Add("client_id", AuthInfo.Client_ID);
|
|
|
|
|
args.Add("scope", "all");
|
2017-03-28 13:46:05 +13:00
|
|
|
|
args.Add("state", "ShareX");
|
2017-03-28 12:05:28 +13:00
|
|
|
|
args.Add("response_type", "code");
|
|
|
|
|
args.Add("redirect_uri", Links.URL_CALLBACK);
|
|
|
|
|
|
2019-01-07 00:47:37 +13:00
|
|
|
|
return URLHelpers.CreateQueryString(URL_AUTHORIZE, args);
|
2017-03-28 12:05:28 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool GetAccessToken(string code)
|
|
|
|
|
{
|
|
|
|
|
string request = JsonConvert.SerializeObject(new
|
|
|
|
|
{
|
|
|
|
|
client_id = AuthInfo.Client_ID,
|
|
|
|
|
client_secret = AuthInfo.Client_Secret,
|
|
|
|
|
grant_type = "authorization_code",
|
|
|
|
|
redirect_uri = Links.URL_CALLBACK,
|
|
|
|
|
code = code,
|
|
|
|
|
});
|
|
|
|
|
|
2018-10-18 05:06:06 +13:00
|
|
|
|
string response = SendRequest(HttpMethod.POST, URL_API_TOKEN, request, UploadHelpers.ContentTypeJSON);
|
2017-03-28 12:05:28 +13:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(response))
|
|
|
|
|
{
|
|
|
|
|
OAuth2Token token = JsonConvert.DeserializeObject<OAuth2Token>(response);
|
|
|
|
|
|
|
|
|
|
if (token != null && !string.IsNullOrEmpty(token.access_token))
|
|
|
|
|
{
|
|
|
|
|
token.UpdateExpireDate();
|
|
|
|
|
AuthInfo.Token = token;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool RefreshAccessToken()
|
|
|
|
|
{
|
|
|
|
|
if (OAuth2Info.CheckOAuth(AuthInfo) && !string.IsNullOrEmpty(AuthInfo.Token.refresh_token))
|
|
|
|
|
{
|
|
|
|
|
string request = JsonConvert.SerializeObject(new
|
|
|
|
|
{
|
|
|
|
|
refresh_token = AuthInfo.Token.refresh_token,
|
|
|
|
|
client_id = AuthInfo.Client_ID,
|
|
|
|
|
client_secret = AuthInfo.Client_Secret,
|
|
|
|
|
grant_type = "refresh",
|
|
|
|
|
});
|
|
|
|
|
|
2018-10-18 05:06:06 +13:00
|
|
|
|
string response = SendRequest(HttpMethod.POST, URL_API_TOKEN, request, UploadHelpers.ContentTypeJSON);
|
2017-03-28 12:05:28 +13:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(response))
|
|
|
|
|
{
|
|
|
|
|
OAuth2Token token = JsonConvert.DeserializeObject<OAuth2Token>(response);
|
|
|
|
|
|
|
|
|
|
if (token != null && !string.IsNullOrEmpty(token.access_token))
|
|
|
|
|
{
|
|
|
|
|
token.UpdateExpireDate();
|
|
|
|
|
AuthInfo.Token = token;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool CheckAuthorization()
|
|
|
|
|
{
|
|
|
|
|
if (OAuth2Info.CheckOAuth(AuthInfo))
|
|
|
|
|
{
|
|
|
|
|
if (AuthInfo.Token.IsExpired && !RefreshAccessToken())
|
|
|
|
|
{
|
|
|
|
|
Errors.Add("Refresh access token failed.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Errors.Add("Gfycat login is required.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-06-01 20:59:37 +12:00
|
|
|
|
|
2017-03-28 12:05:28 +13:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2014-06-01 03:04:52 +12:00
|
|
|
|
|
2017-03-28 12:05:28 +13:00
|
|
|
|
public override UploadResult Upload(Stream stream, string fileName)
|
|
|
|
|
{
|
|
|
|
|
OAuth2Token token = GetOrCreateToken();
|
|
|
|
|
if (token == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2017-03-28 13:46:05 +13:00
|
|
|
|
|
2017-03-28 12:05:28 +13:00
|
|
|
|
NameValueCollection headers = new NameValueCollection();
|
|
|
|
|
headers.Add("Authorization", "Bearer " + token.access_token);
|
2017-03-28 13:46:05 +13:00
|
|
|
|
|
2017-03-28 12:05:28 +13:00
|
|
|
|
GfycatCreateResponse gfy = CreateGfycat(headers);
|
|
|
|
|
if (gfy == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2017-03-28 13:46:05 +13:00
|
|
|
|
|
2017-03-28 12:05:28 +13:00
|
|
|
|
Dictionary<string, string> args = new Dictionary<string, string>();
|
|
|
|
|
args.Add("key", gfy.GfyName);
|
|
|
|
|
UploadResult result = SendRequestFile(URL_UPLOAD, stream, fileName, "file", args);
|
2014-06-01 03:04:52 +12:00
|
|
|
|
if (!result.IsError)
|
|
|
|
|
{
|
2017-03-28 12:05:28 +13:00
|
|
|
|
WaitForTranscode(gfy.GfyName, result);
|
2014-06-01 03:04:52 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-28 12:05:28 +13:00
|
|
|
|
private void WaitForTranscode(string name, UploadResult result)
|
2014-06-01 03:04:52 +12:00
|
|
|
|
{
|
2017-03-28 12:05:28 +13:00
|
|
|
|
ProgressManager progress = new ProgressManager(10000);
|
2014-06-01 20:59:37 +12:00
|
|
|
|
|
2017-03-28 12:05:28 +13:00
|
|
|
|
if (AllowReportProgress)
|
|
|
|
|
{
|
|
|
|
|
OnProgressChanged(progress);
|
|
|
|
|
}
|
2014-06-01 03:04:52 +12:00
|
|
|
|
|
2017-03-28 12:05:28 +13:00
|
|
|
|
int iterations = 0;
|
|
|
|
|
while (!StopUploadRequested)
|
2014-06-01 03:04:52 +12:00
|
|
|
|
{
|
2017-03-28 12:05:28 +13:00
|
|
|
|
string statusJson = SendRequest(HttpMethod.GET, URL_API_STATUS + name);
|
|
|
|
|
GfycatStatusResponse response = JsonConvert.DeserializeObject<GfycatStatusResponse>(statusJson);
|
2014-06-01 20:59:37 +12:00
|
|
|
|
|
2017-03-28 12:05:28 +13:00
|
|
|
|
if (response.Error != null)
|
2014-06-01 05:52:18 +12:00
|
|
|
|
{
|
2017-03-28 12:05:28 +13:00
|
|
|
|
Errors.Add(response.Error);
|
|
|
|
|
result.IsSuccess = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-08-28 18:43:50 +12:00
|
|
|
|
else if (response.Task.Equals("error", StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
Errors.Add(response.Description);
|
|
|
|
|
result.IsSuccess = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-03-28 12:05:28 +13:00
|
|
|
|
else if (response.GfyName != null)
|
|
|
|
|
{
|
|
|
|
|
result.IsSuccess = true;
|
|
|
|
|
result.URL = "https://gfycat.com/" + response.GfyName;
|
|
|
|
|
break;
|
2017-03-28 13:46:05 +13:00
|
|
|
|
}
|
2017-03-30 08:22:20 +13:00
|
|
|
|
else if ((response.Task.Equals("NotFoundo", StringComparison.InvariantCultureIgnoreCase) ||
|
|
|
|
|
response.Task.Equals("NotFound", StringComparison.InvariantCultureIgnoreCase)) && iterations > 10)
|
2017-03-28 12:05:28 +13:00
|
|
|
|
{
|
|
|
|
|
Errors.Add("Gfy not found");
|
|
|
|
|
result.IsSuccess = false;
|
|
|
|
|
break;
|
2014-06-01 05:52:18 +12:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-28 12:05:28 +13:00
|
|
|
|
if (AllowReportProgress && progress.UpdateProgress((progress.Length - progress.Position) / response.Time))
|
2014-06-01 05:52:18 +12:00
|
|
|
|
{
|
2017-03-28 12:05:28 +13:00
|
|
|
|
OnProgressChanged(progress);
|
|
|
|
|
}
|
2014-06-03 03:24:24 +12:00
|
|
|
|
|
2017-03-28 12:05:28 +13:00
|
|
|
|
Thread.Sleep(100);
|
|
|
|
|
iterations++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-06-01 20:59:37 +12:00
|
|
|
|
|
2017-03-28 12:05:28 +13:00
|
|
|
|
private GfycatCreateResponse CreateGfycat(NameValueCollection headers)
|
|
|
|
|
{
|
|
|
|
|
Dictionary<string, object> args = new Dictionary<string, object>();
|
|
|
|
|
args.Add("private", Private);
|
|
|
|
|
args.Add("noResize", NoResize);
|
|
|
|
|
args.Add("noMd5", IgnoreExisting);
|
2017-03-28 13:46:05 +13:00
|
|
|
|
|
|
|
|
|
string json = JsonConvert.SerializeObject(args);
|
|
|
|
|
|
2018-10-18 05:06:06 +13:00
|
|
|
|
string response = SendRequest(HttpMethod.POST, URL_API_CREATE_GFY, json, UploadHelpers.ContentTypeJSON, null, headers);
|
2017-03-28 12:05:28 +13:00
|
|
|
|
if (!string.IsNullOrEmpty(response))
|
|
|
|
|
{
|
|
|
|
|
return JsonConvert.DeserializeObject<GfycatCreateResponse>(response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2014-06-01 20:59:37 +12:00
|
|
|
|
|
2017-03-28 12:05:28 +13:00
|
|
|
|
private OAuth2Token GetOrCreateToken()
|
|
|
|
|
{
|
|
|
|
|
if (UploadMethod == AccountType.User)
|
|
|
|
|
{
|
|
|
|
|
if (!CheckAuthorization())
|
|
|
|
|
{
|
|
|
|
|
return null;
|
2014-06-01 05:52:18 +12:00
|
|
|
|
}
|
2017-03-28 12:05:28 +13:00
|
|
|
|
return AuthInfo.Token;
|
2014-06-01 03:04:52 +12:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-03-28 12:05:28 +13:00
|
|
|
|
if (AnonymousToken == null || AnonymousToken.IsExpired)
|
|
|
|
|
{
|
|
|
|
|
string request = JsonConvert.SerializeObject(new
|
|
|
|
|
{
|
|
|
|
|
client_id = AuthInfo.Client_ID,
|
|
|
|
|
client_secret = AuthInfo.Client_Secret,
|
|
|
|
|
grant_type = "client_credentials",
|
|
|
|
|
});
|
|
|
|
|
|
2018-10-18 05:06:06 +13:00
|
|
|
|
string response = SendRequest(HttpMethod.POST, URL_API_TOKEN, request, UploadHelpers.ContentTypeJSON);
|
2017-03-28 12:05:28 +13:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(response))
|
|
|
|
|
{
|
|
|
|
|
AnonymousToken = JsonConvert.DeserializeObject<OAuth2Token>(response);
|
|
|
|
|
|
|
|
|
|
if (AnonymousToken != null && !string.IsNullOrEmpty(AnonymousToken.access_token))
|
|
|
|
|
{
|
|
|
|
|
AnonymousToken.UpdateExpireDate();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-28 13:46:05 +13:00
|
|
|
|
|
2017-03-28 12:05:28 +13:00
|
|
|
|
return AnonymousToken;
|
2014-06-01 03:04:52 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-28 12:05:28 +13:00
|
|
|
|
public class GfycatCreateResponse
|
2014-06-01 03:04:52 +12:00
|
|
|
|
{
|
2017-03-28 12:05:28 +13:00
|
|
|
|
public string GfyName { get; set; }
|
|
|
|
|
public string Secret { get; set; }
|
2014-06-01 03:04:52 +12:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-01 05:52:18 +12:00
|
|
|
|
public class GfycatStatusResponse
|
|
|
|
|
{
|
|
|
|
|
public string Task { get; set; }
|
|
|
|
|
public int Time { get; set; }
|
|
|
|
|
public string GfyName { get; set; }
|
|
|
|
|
public string Error { get; set; }
|
2017-08-28 18:43:50 +12:00
|
|
|
|
public string Description { get; set; }
|
2014-06-01 03:04:52 +12:00
|
|
|
|
}
|
|
|
|
|
}
|