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
|
2015-08-13 13:07:38 +12:00
|
|
|
|
Copyright (c) 2007-2015 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;
|
2014-07-22 12:12:07 +12:00
|
|
|
|
using Newtonsoft.Json.Linq;
|
2014-12-11 12:19:28 +13:00
|
|
|
|
using ShareX.UploadersLib.HelperClasses;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.Specialized;
|
2014-04-23 07:13:40 +12:00
|
|
|
|
using System.ComponentModel;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
2014-12-11 09:25:20 +13:00
|
|
|
|
namespace ShareX.UploadersLib.ImageUploaders
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-04-23 07:13:40 +12:00
|
|
|
|
public enum ImgurThumbnailType
|
|
|
|
|
{
|
2014-07-22 12:12:07 +12:00
|
|
|
|
[Description("Small square")]
|
2014-04-23 07:13:40 +12:00
|
|
|
|
Small_Square,
|
2014-07-22 12:12:07 +12:00
|
|
|
|
[Description("Big square")]
|
2014-04-23 07:13:40 +12:00
|
|
|
|
Big_Square,
|
2014-07-22 12:12:07 +12:00
|
|
|
|
[Description("Small thumbnail")]
|
2014-04-23 07:13:40 +12:00
|
|
|
|
Small_Thumbnail,
|
2014-07-22 12:12:07 +12:00
|
|
|
|
[Description("Medium thumbnail")]
|
2014-04-23 07:13:40 +12:00
|
|
|
|
Medium_Thumbnail,
|
2014-07-22 12:12:07 +12:00
|
|
|
|
[Description("Large thumbnail")]
|
2014-04-23 07:13:40 +12:00
|
|
|
|
Large_Thumbnail,
|
2014-07-22 12:12:07 +12:00
|
|
|
|
[Description("Huge thumbnail")]
|
2014-04-23 07:13:40 +12:00
|
|
|
|
Huge_Thumbnail
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-20 06:11:34 +12:00
|
|
|
|
public sealed class Imgur : ImageUploader, IOAuth2
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
public AccountType UploadMethod { get; set; }
|
|
|
|
|
public OAuth2Info AuthInfo { get; set; }
|
|
|
|
|
public ImgurThumbnailType ThumbnailType { get; set; }
|
|
|
|
|
public string UploadAlbumID { get; set; }
|
2014-07-22 12:12:07 +12:00
|
|
|
|
public bool DirectLink { get; set; }
|
2015-08-08 23:38:37 +12:00
|
|
|
|
public bool UseGIFV { get; set; }
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2015-07-20 06:11:34 +12:00
|
|
|
|
public Imgur(OAuth2Info oauth)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
AuthInfo = oauth;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetAuthorizationURL()
|
|
|
|
|
{
|
2013-12-21 04:59:26 +13:00
|
|
|
|
Dictionary<string, string> args = new Dictionary<string, string>();
|
|
|
|
|
args.Add("client_id", AuthInfo.Client_ID);
|
|
|
|
|
args.Add("response_type", "pin");
|
|
|
|
|
|
|
|
|
|
return CreateQuery("https://api.imgur.com/oauth2/authorize", args);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool GetAccessToken(string pin)
|
|
|
|
|
{
|
|
|
|
|
Dictionary<string, string> args = new Dictionary<string, string>();
|
|
|
|
|
args.Add("client_id", AuthInfo.Client_ID);
|
|
|
|
|
args.Add("client_secret", AuthInfo.Client_Secret);
|
|
|
|
|
args.Add("grant_type", "pin");
|
|
|
|
|
args.Add("pin", pin);
|
|
|
|
|
|
2014-04-17 05:55:07 +12:00
|
|
|
|
string response = SendRequest(HttpMethod.POST, "https://api.imgur.com/oauth2/token", args);
|
2013-11-03 23:53:49 +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))
|
|
|
|
|
{
|
|
|
|
|
Dictionary<string, string> args = new Dictionary<string, string>();
|
|
|
|
|
args.Add("refresh_token", AuthInfo.Token.refresh_token);
|
|
|
|
|
args.Add("client_id", AuthInfo.Client_ID);
|
|
|
|
|
args.Add("client_secret", AuthInfo.Client_Secret);
|
|
|
|
|
args.Add("grant_type", "refresh_token");
|
|
|
|
|
|
2014-04-17 05:55:07 +12:00
|
|
|
|
string response = SendRequest(HttpMethod.POST, "https://api.imgur.com/oauth2/token", args);
|
2013-11-03 23:53:49 +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;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 07:11:44 +12:00
|
|
|
|
private NameValueCollection GetAuthHeaders()
|
|
|
|
|
{
|
|
|
|
|
NameValueCollection headers = new NameValueCollection();
|
|
|
|
|
headers.Add("Authorization", "Bearer " + AuthInfo.Token.access_token);
|
|
|
|
|
return headers;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-03 23:53:49 +13:00
|
|
|
|
public bool CheckAuthorization()
|
|
|
|
|
{
|
|
|
|
|
if (OAuth2Info.CheckOAuth(AuthInfo))
|
|
|
|
|
{
|
|
|
|
|
if (AuthInfo.Token.IsExpired && !RefreshAccessToken())
|
|
|
|
|
{
|
|
|
|
|
Errors.Add("Refresh access token failed.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-04-17 04:18:25 +12:00
|
|
|
|
Errors.Add("Imgur login is required.");
|
2013-11-03 23:53:49 +13:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ImgurAlbumData> GetAlbums()
|
|
|
|
|
{
|
|
|
|
|
if (CheckAuthorization())
|
|
|
|
|
{
|
2014-06-09 07:11:44 +12:00
|
|
|
|
string response = SendRequest(HttpMethod.GET, "https://api.imgur.com/3/account/me/albums", headers: GetAuthHeaders());
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2015-07-20 06:11:34 +12:00
|
|
|
|
ImgurResponse imgurResponse = JsonConvert.DeserializeObject<ImgurResponse>(response);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2015-07-20 06:11:34 +12:00
|
|
|
|
if (imgurResponse != null)
|
|
|
|
|
{
|
|
|
|
|
if (imgurResponse.success && imgurResponse.status == 200)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2015-07-20 06:11:34 +12:00
|
|
|
|
return ((JArray)imgurResponse.data).ToObject<List<ImgurAlbumData>>();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
HandleErrors(imgurResponse);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override UploadResult Upload(Stream stream, string fileName)
|
|
|
|
|
{
|
|
|
|
|
Dictionary<string, string> args = new Dictionary<string, string>();
|
2014-06-09 07:11:44 +12:00
|
|
|
|
NameValueCollection headers;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
|
|
|
|
if (UploadMethod == AccountType.User)
|
|
|
|
|
{
|
|
|
|
|
if (!CheckAuthorization())
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(UploadAlbumID))
|
|
|
|
|
{
|
|
|
|
|
args.Add("album", UploadAlbumID);
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 07:11:44 +12:00
|
|
|
|
headers = GetAuthHeaders();
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
2014-06-09 07:11:44 +12:00
|
|
|
|
else
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-06-09 07:11:44 +12:00
|
|
|
|
headers = new NameValueCollection();
|
2013-11-03 23:53:49 +13:00
|
|
|
|
headers.Add("Authorization", "Client-ID " + AuthInfo.Client_ID);
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-07 04:55:50 +12:00
|
|
|
|
UploadResult result = UploadData(stream, "https://api.imgur.com/3/image", fileName, "image", args, headers);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(result.Response))
|
|
|
|
|
{
|
2015-07-20 06:11:34 +12:00
|
|
|
|
ImgurResponse imgurResponse = JsonConvert.DeserializeObject<ImgurResponse>(result.Response);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2015-07-20 06:11:34 +12:00
|
|
|
|
if (imgurResponse != null)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2015-07-20 06:11:34 +12:00
|
|
|
|
if (imgurResponse.success && imgurResponse.status == 200)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2015-07-20 06:11:34 +12:00
|
|
|
|
ImgurImageData imageData = ((JObject)imgurResponse.data).ToObject<ImgurImageData>();
|
2014-07-22 12:12:07 +12:00
|
|
|
|
|
2015-07-20 06:11:34 +12:00
|
|
|
|
if (imageData != null && !string.IsNullOrEmpty(imageData.link))
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-07-22 12:12:07 +12:00
|
|
|
|
if (DirectLink)
|
|
|
|
|
{
|
2015-08-08 23:38:37 +12:00
|
|
|
|
if (UseGIFV && !string.IsNullOrEmpty(imageData.gifv))
|
2015-07-20 06:48:07 +12:00
|
|
|
|
{
|
|
|
|
|
result.URL = imageData.gifv;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
result.URL = imageData.link;
|
|
|
|
|
}
|
2014-07-22 12:12:07 +12:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2015-07-20 06:11:34 +12:00
|
|
|
|
result.URL = "http://imgur.com/" + imageData.id;
|
2014-07-22 12:12:07 +12:00
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
|
|
|
|
int index = result.URL.LastIndexOf('.');
|
2014-04-23 07:13:40 +12:00
|
|
|
|
string thumbnail = string.Empty;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2014-04-23 07:13:40 +12:00
|
|
|
|
switch (ThumbnailType)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2014-04-23 07:13:40 +12:00
|
|
|
|
case ImgurThumbnailType.Small_Square:
|
|
|
|
|
thumbnail = "s";
|
|
|
|
|
break;
|
|
|
|
|
case ImgurThumbnailType.Big_Square:
|
|
|
|
|
thumbnail = "b";
|
|
|
|
|
break;
|
|
|
|
|
case ImgurThumbnailType.Small_Thumbnail:
|
|
|
|
|
thumbnail = "t";
|
|
|
|
|
break;
|
|
|
|
|
case ImgurThumbnailType.Medium_Thumbnail:
|
|
|
|
|
thumbnail = "m";
|
|
|
|
|
break;
|
|
|
|
|
case ImgurThumbnailType.Large_Thumbnail:
|
|
|
|
|
thumbnail = "l";
|
|
|
|
|
break;
|
|
|
|
|
case ImgurThumbnailType.Huge_Thumbnail:
|
|
|
|
|
thumbnail = "h";
|
|
|
|
|
break;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-20 06:11:34 +12:00
|
|
|
|
result.ThumbnailURL = string.Format("http://i.imgur.com/{0}{1}.jpg", imageData.id, thumbnail); // Thumbnails always jpg
|
|
|
|
|
result.DeletionURL = "http://imgur.com/delete/" + imageData.deletehash;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2015-07-20 06:11:34 +12:00
|
|
|
|
HandleErrors(imgurResponse);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2015-07-20 06:11:34 +12:00
|
|
|
|
|
|
|
|
|
private void HandleErrors(ImgurResponse response)
|
|
|
|
|
{
|
|
|
|
|
ImgurErrorData errorData = ((JObject)response.data).ToObject<ImgurErrorData>();
|
|
|
|
|
|
|
|
|
|
if (errorData != null)
|
|
|
|
|
{
|
|
|
|
|
string errorMessage = string.Format("Status: {0}, Request: {1}, Error: {2}", response.status, errorData.request, errorData.error);
|
|
|
|
|
Errors.Add(errorMessage);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-20 06:11:34 +12:00
|
|
|
|
public class ImgurResponse
|
|
|
|
|
{
|
|
|
|
|
public object data { get; set; }
|
|
|
|
|
public bool success { get; set; }
|
|
|
|
|
public int status { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ImgurErrorData
|
|
|
|
|
{
|
|
|
|
|
public string error { get; set; }
|
|
|
|
|
public string request { get; set; }
|
|
|
|
|
public string method { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ImgurImageData
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
public string id { get; set; }
|
2015-07-20 06:11:34 +12:00
|
|
|
|
public string title { get; set; }
|
|
|
|
|
public string description { get; set; }
|
|
|
|
|
public int datetime { get; set; }
|
|
|
|
|
public string type { get; set; }
|
|
|
|
|
public bool animated { get; set; }
|
|
|
|
|
public int width { get; set; }
|
|
|
|
|
public int height { get; set; }
|
|
|
|
|
public int size { get; set; }
|
|
|
|
|
public int views { get; set; }
|
|
|
|
|
public int bandwidth { get; set; }
|
2013-11-03 23:53:49 +13:00
|
|
|
|
public string deletehash { get; set; }
|
2015-07-20 06:11:34 +12:00
|
|
|
|
public string name { get; set; }
|
|
|
|
|
public string section { get; set; }
|
2013-11-03 23:53:49 +13:00
|
|
|
|
public string link { get; set; }
|
2015-07-20 06:11:34 +12:00
|
|
|
|
public string gifv { get; set; }
|
|
|
|
|
public string mp4 { get; set; }
|
|
|
|
|
public string webm { get; set; }
|
|
|
|
|
public bool looping { get; set; }
|
|
|
|
|
public bool favorite { get; set; }
|
|
|
|
|
public bool? nsfw { get; set; }
|
|
|
|
|
public string vote { get; set; }
|
|
|
|
|
public string account_url { get; set; }
|
|
|
|
|
public int? account_id { get; set; }
|
|
|
|
|
public string comment_preview { get; set; }
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ImgurAlbumData
|
|
|
|
|
{
|
|
|
|
|
public string id { get; set; }
|
|
|
|
|
public string title { get; set; }
|
|
|
|
|
public string description { get; set; }
|
2015-07-20 06:11:34 +12:00
|
|
|
|
public int datetime { get; set; }
|
|
|
|
|
public string cover { get; set; }
|
|
|
|
|
public string cover_width { get; set; }
|
|
|
|
|
public string cover_height { get; set; }
|
2013-11-03 23:53:49 +13:00
|
|
|
|
public string account_url { get; set; }
|
2015-07-20 06:11:34 +12:00
|
|
|
|
public int? account_id { get; set; }
|
2013-11-03 23:53:49 +13:00
|
|
|
|
public string privacy { get; set; }
|
|
|
|
|
public string layout { get; set; }
|
|
|
|
|
public int views { get; set; }
|
|
|
|
|
public string link { get; set; }
|
|
|
|
|
public bool favorite { get; set; }
|
2015-07-20 06:11:34 +12:00
|
|
|
|
public bool? nsfw { get; set; }
|
|
|
|
|
public string section { get; set; }
|
|
|
|
|
public int order { get; set; }
|
|
|
|
|
public string deletehash { get; set; }
|
|
|
|
|
public int images_count { get; set; }
|
|
|
|
|
public ImgurImageData[] images { get; set; }
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|