mirror of
https://github.com/ShareX/ShareX.git
synced 2024-10-02 18:26:27 +13:00
fixed #1138: Ugly workaround for Imgur related token expire issues
This commit is contained in:
parent
479c2234b0
commit
6e9c4ebcd3
3 changed files with 38 additions and 9 deletions
|
@ -53,7 +53,7 @@ public MediaCrushUploader(string apiURL)
|
|||
|
||||
public override UploadResult Upload(Stream stream, string fileName)
|
||||
{
|
||||
ThrowWebExceptions = true;
|
||||
WebExceptionThrow = true;
|
||||
|
||||
string hash = CreateHash(stream);
|
||||
|
||||
|
|
|
@ -25,7 +25,9 @@ You should have received a copy of the GNU General Public License
|
|||
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using ShareX.HelpersLib;
|
||||
using ShareX.UploadersLib.HelperClasses;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Specialized;
|
||||
using System.ComponentModel;
|
||||
|
@ -58,9 +60,12 @@ public sealed class Imgur : ImageUploader, IOAuth2
|
|||
public bool DirectLink { get; set; }
|
||||
public bool UseGIFV { get; set; }
|
||||
|
||||
private bool refreshTokenOnError;
|
||||
|
||||
public Imgur(OAuth2Info oauth)
|
||||
{
|
||||
AuthInfo = oauth;
|
||||
refreshTokenOnError = true;
|
||||
}
|
||||
|
||||
public string GetAuthorizationURL()
|
||||
|
@ -200,6 +205,7 @@ public override UploadResult Upload(Stream stream, string fileName)
|
|||
headers.Add("Authorization", "Client-ID " + AuthInfo.Client_ID);
|
||||
}
|
||||
|
||||
WebExceptionReturnResponse = true;
|
||||
UploadResult result = UploadData(stream, "https://api.imgur.com/3/image", fileName, "image", args, headers);
|
||||
|
||||
if (!string.IsNullOrEmpty(result.Response))
|
||||
|
@ -260,7 +266,22 @@ public override UploadResult Upload(Stream stream, string fileName)
|
|||
}
|
||||
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 int BufferSize { 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
|
||||
{
|
||||
|
@ -67,10 +68,8 @@ public bool IsError
|
|||
public Uploader()
|
||||
{
|
||||
Errors = new List<string>();
|
||||
IsUploading = false;
|
||||
BufferSize = 8192;
|
||||
AllowReportProgress = true;
|
||||
ThrowWebExceptions = false;
|
||||
|
||||
ServicePointManager.DefaultConnectionLimit = 25;
|
||||
ServicePointManager.Expect100Continue = false;
|
||||
|
@ -215,7 +214,7 @@ private HttpWebResponse GetResponse(HttpMethod method, string url, Dictionary<st
|
|||
{
|
||||
if (!StopUploadRequested)
|
||||
{
|
||||
if (ThrowWebExceptions && e is WebException) throw;
|
||||
if (WebExceptionThrow && e is WebException) throw;
|
||||
AddWebError(e);
|
||||
}
|
||||
}
|
||||
|
@ -311,7 +310,7 @@ private HttpWebResponse GetResponse(string url, Stream dataStream, string bounda
|
|||
{
|
||||
if (!StopUploadRequested)
|
||||
{
|
||||
if (ThrowWebExceptions && e is WebException) throw;
|
||||
if (WebExceptionThrow && e is WebException) throw;
|
||||
AddWebError(e);
|
||||
}
|
||||
}
|
||||
|
@ -372,8 +371,17 @@ protected UploadResult UploadData(Stream dataStream, string url, string fileName
|
|||
{
|
||||
if (!StopUploadRequested)
|
||||
{
|
||||
if (ThrowWebExceptions && e is WebException) throw;
|
||||
AddWebError(e);
|
||||
if (WebExceptionThrow && e is WebException)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
|
||||
string response = AddWebError(e);
|
||||
|
||||
if (WebExceptionReturnResponse && e is WebException)
|
||||
{
|
||||
result.Response = response;
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
|
|
Loading…
Reference in a new issue