mirror of
https://github.com/ShareX/ShareX.git
synced 2024-07-04 22:21:23 +12:00
fixed #1138: Ugly workaround for Imgur related token expire issues
This commit is contained in:
parent
479c2234b0
commit
6e9c4ebcd3
|
@ -53,7 +53,7 @@ public MediaCrushUploader(string apiURL)
|
||||||
|
|
||||||
public override UploadResult Upload(Stream stream, string fileName)
|
public override UploadResult Upload(Stream stream, string fileName)
|
||||||
{
|
{
|
||||||
ThrowWebExceptions = true;
|
WebExceptionThrow = true;
|
||||||
|
|
||||||
string hash = CreateHash(stream);
|
string hash = CreateHash(stream);
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,9 @@
|
||||||
|
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
|
using ShareX.HelpersLib;
|
||||||
using ShareX.UploadersLib.HelperClasses;
|
using ShareX.UploadersLib.HelperClasses;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Specialized;
|
using System.Collections.Specialized;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
|
@ -58,9 +60,12 @@ public sealed class Imgur : ImageUploader, IOAuth2
|
||||||
public bool DirectLink { get; set; }
|
public bool DirectLink { get; set; }
|
||||||
public bool UseGIFV { get; set; }
|
public bool UseGIFV { get; set; }
|
||||||
|
|
||||||
|
private bool refreshTokenOnError;
|
||||||
|
|
||||||
public Imgur(OAuth2Info oauth)
|
public Imgur(OAuth2Info oauth)
|
||||||
{
|
{
|
||||||
AuthInfo = oauth;
|
AuthInfo = oauth;
|
||||||
|
refreshTokenOnError = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetAuthorizationURL()
|
public string GetAuthorizationURL()
|
||||||
|
@ -200,6 +205,7 @@ public override UploadResult Upload(Stream stream, string fileName)
|
||||||
headers.Add("Authorization", "Client-ID " + AuthInfo.Client_ID);
|
headers.Add("Authorization", "Client-ID " + AuthInfo.Client_ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
WebExceptionReturnResponse = true;
|
||||||
UploadResult result = UploadData(stream, "https://api.imgur.com/3/image", fileName, "image", args, headers);
|
UploadResult result = UploadData(stream, "https://api.imgur.com/3/image", fileName, "image", args, headers);
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(result.Response))
|
if (!string.IsNullOrEmpty(result.Response))
|
||||||
|
@ -260,7 +266,22 @@ public override UploadResult Upload(Stream stream, string fileName)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
HandleErrors(imgurResponse);
|
ImgurErrorData errorData = ((JObject)imgurResponse.data).ToObject<ImgurErrorData>();
|
||||||
|
|
||||||
|
if (errorData != null)
|
||||||
|
{
|
||||||
|
if (refreshTokenOnError && errorData.error.Equals("The access token provided is invalid.", StringComparison.InvariantCultureIgnoreCase) && RefreshAccessToken())
|
||||||
|
{
|
||||||
|
DebugHelper.WriteLine("Imgur access token refreshed, reuploading image.");
|
||||||
|
|
||||||
|
refreshTokenOnError = false;
|
||||||
|
return Upload(stream, fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
string errorMessage = string.Format("Imgur upload failed: ({0}) {1}", imgurResponse.status, errorData.error);
|
||||||
|
Errors.Clear();
|
||||||
|
Errors.Add(errorMessage);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,7 +50,8 @@ public class Uploader
|
||||||
public bool IsUploading { get; protected set; }
|
public bool IsUploading { get; protected set; }
|
||||||
public int BufferSize { get; set; }
|
public int BufferSize { get; set; }
|
||||||
public bool AllowReportProgress { get; set; }
|
public bool AllowReportProgress { get; set; }
|
||||||
public bool ThrowWebExceptions { get; set; }
|
public bool WebExceptionReturnResponse { get; set; }
|
||||||
|
public bool WebExceptionThrow { get; set; }
|
||||||
|
|
||||||
public bool IsError
|
public bool IsError
|
||||||
{
|
{
|
||||||
|
@ -67,10 +68,8 @@ public bool IsError
|
||||||
public Uploader()
|
public Uploader()
|
||||||
{
|
{
|
||||||
Errors = new List<string>();
|
Errors = new List<string>();
|
||||||
IsUploading = false;
|
|
||||||
BufferSize = 8192;
|
BufferSize = 8192;
|
||||||
AllowReportProgress = true;
|
AllowReportProgress = true;
|
||||||
ThrowWebExceptions = false;
|
|
||||||
|
|
||||||
ServicePointManager.DefaultConnectionLimit = 25;
|
ServicePointManager.DefaultConnectionLimit = 25;
|
||||||
ServicePointManager.Expect100Continue = false;
|
ServicePointManager.Expect100Continue = false;
|
||||||
|
@ -215,7 +214,7 @@ public virtual void StopUpload()
|
||||||
{
|
{
|
||||||
if (!StopUploadRequested)
|
if (!StopUploadRequested)
|
||||||
{
|
{
|
||||||
if (ThrowWebExceptions && e is WebException) throw;
|
if (WebExceptionThrow && e is WebException) throw;
|
||||||
AddWebError(e);
|
AddWebError(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -311,7 +310,7 @@ protected string SendRequestJSON(string url, string json, NameValueCollection he
|
||||||
{
|
{
|
||||||
if (!StopUploadRequested)
|
if (!StopUploadRequested)
|
||||||
{
|
{
|
||||||
if (ThrowWebExceptions && e is WebException) throw;
|
if (WebExceptionThrow && e is WebException) throw;
|
||||||
AddWebError(e);
|
AddWebError(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -372,8 +371,17 @@ protected string SendRequestJSON(string url, string json, NameValueCollection he
|
||||||
{
|
{
|
||||||
if (!StopUploadRequested)
|
if (!StopUploadRequested)
|
||||||
{
|
{
|
||||||
if (ThrowWebExceptions && e is WebException) throw;
|
if (WebExceptionThrow && e is WebException)
|
||||||
AddWebError(e);
|
{
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
|
string response = AddWebError(e);
|
||||||
|
|
||||||
|
if (WebExceptionReturnResponse && e is WebException)
|
||||||
|
{
|
||||||
|
result.Response = response;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
|
|
Loading…
Reference in a new issue