fixed #1138: Ugly workaround for Imgur related token expire issues

This commit is contained in:
Jaex 2015-12-14 13:16:07 +02:00
parent 479c2234b0
commit 6e9c4ebcd3
3 changed files with 38 additions and 9 deletions

View file

@ -53,7 +53,7 @@ public MediaCrushUploader(string apiURL)
public override UploadResult Upload(Stream stream, string fileName)
{
ThrowWebExceptions = true;
WebExceptionThrow = true;
string hash = CreateHash(stream);

View file

@ -25,7 +25,9 @@
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);
}
}
}
}

View file

@ -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 @@ public virtual void StopUpload()
{
if (!StopUploadRequested)
{
if (ThrowWebExceptions && e is WebException) throw;
if (WebExceptionThrow && e is WebException) throw;
AddWebError(e);
}
}
@ -311,7 +310,7 @@ protected string SendRequestJSON(string url, string json, NameValueCollection he
{
if (!StopUploadRequested)
{
if (ThrowWebExceptions && e is WebException) throw;
if (WebExceptionThrow && e is WebException) throw;
AddWebError(e);
}
}
@ -372,8 +371,17 @@ protected string SendRequestJSON(string url, string json, NameValueCollection he
{
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