BUG-1949: Fixed imgur issues, especially the delete.

This commit is contained in:
Robin 2016-04-26 23:12:45 +02:00
commit e781bdcd5c
3 changed files with 79 additions and 48 deletions

View file

@ -32,8 +32,6 @@ namespace GreenshotImgurPlugin {
/// </summary> /// </summary>
[IniSection("Imgur", Description="Greenshot Imgur Plugin configuration")] [IniSection("Imgur", Description="Greenshot Imgur Plugin configuration")]
public class ImgurConfiguration : IniSection { public class ImgurConfiguration : IniSection {
[IniProperty("ImgurApiUrl", Description="Url to Imgur system.", DefaultValue= "http://api.imgur.com/2")]
public string ImgurApiUrl;
[IniProperty("ImgurApi3Url", Description = "Url to Imgur system.", DefaultValue = "https://api.imgur.com/3")] [IniProperty("ImgurApi3Url", Description = "Url to Imgur system.", DefaultValue = "https://api.imgur.com/3")]
public string ImgurApi3Url; public string ImgurApi3Url;

View file

@ -27,7 +27,8 @@ namespace GreenshotImgurPlugin
/// <summary> /// <summary>
/// Description of ImgurInfo. /// Description of ImgurInfo.
/// </summary> /// </summary>
public class ImgurInfo : IDisposable { public class ImgurInfo : IDisposable
{
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(ImgurInfo)); private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(ImgurInfo));
public string Hash public string Hash
@ -37,9 +38,11 @@ namespace GreenshotImgurPlugin
} }
private string deleteHash; private string deleteHash;
public string DeleteHash { public string DeleteHash
get {return deleteHash;} {
set { get { return deleteHash; }
set
{
deleteHash = value; deleteHash = value;
DeletePage = "https://imgur.com/delete/" + value; DeletePage = "https://imgur.com/delete/" + value;
} }
@ -94,24 +97,29 @@ namespace GreenshotImgurPlugin
} }
private Image image; private Image image;
public Image Image { public Image Image
get {return image;} {
set { get { return image; }
if (image != null) { set
{
if (image != null)
{
image.Dispose(); image.Dispose();
} }
image = value; image = value;
} }
} }
public ImgurInfo() { public ImgurInfo()
{
} }
/// <summary> /// <summary>
/// The public accessible Dispose /// The public accessible Dispose
/// Will call the GarbageCollector to SuppressFinalize, preventing being cleaned twice /// Will call the GarbageCollector to SuppressFinalize, preventing being cleaned twice
/// </summary> /// </summary>
public void Dispose() { public void Dispose()
{
Dispose(true); Dispose(true);
GC.SuppressFinalize(this); GC.SuppressFinalize(this);
} }
@ -121,15 +129,19 @@ namespace GreenshotImgurPlugin
/// When disposing==true all non-managed resources should be freed too! /// When disposing==true all non-managed resources should be freed too!
/// </summary> /// </summary>
/// <param name="disposing"></param> /// <param name="disposing"></param>
protected virtual void Dispose(bool disposing) { protected virtual void Dispose(bool disposing)
if (disposing) { {
if (image != null) { if (disposing)
{
if (image != null)
{
image.Dispose(); image.Dispose();
} }
} }
image = null; image = null;
} }
public static ImgurInfo ParseResponse(string response) { public static ImgurInfo ParseResponse(string response)
{
LOG.Debug(response); LOG.Debug(response);
// This is actually a hack for BUG-1695 // This is actually a hack for BUG-1695
// The problem is the (C) sign, we send it HTML encoded "&reg;" to Imgur and get it HTML encoded in the XML back // The problem is the (C) sign, we send it HTML encoded "&reg;" to Imgur and get it HTML encoded in the XML back
@ -142,11 +154,13 @@ namespace GreenshotImgurPlugin
response = response.Replace("&reg;", "&#174;"); response = response.Replace("&reg;", "&#174;");
ImgurInfo imgurInfo = new ImgurInfo(); ImgurInfo imgurInfo = new ImgurInfo();
try { try
{
XmlDocument doc = new XmlDocument(); XmlDocument doc = new XmlDocument();
doc.LoadXml(response); doc.LoadXml(response);
XmlNodeList nodes = doc.GetElementsByTagName("id"); XmlNodeList nodes = doc.GetElementsByTagName("id");
if(nodes.Count > 0) { if (nodes.Count > 0)
{
imgurInfo.Hash = nodes.Item(0).InnerText; imgurInfo.Hash = nodes.Item(0).InnerText;
} }
nodes = doc.GetElementsByTagName("hash"); nodes = doc.GetElementsByTagName("hash");
@ -155,19 +169,23 @@ namespace GreenshotImgurPlugin
imgurInfo.Hash = nodes.Item(0).InnerText; imgurInfo.Hash = nodes.Item(0).InnerText;
} }
nodes = doc.GetElementsByTagName("deletehash"); nodes = doc.GetElementsByTagName("deletehash");
if(nodes.Count > 0) { if (nodes.Count > 0)
{
imgurInfo.DeleteHash = nodes.Item(0).InnerText; imgurInfo.DeleteHash = nodes.Item(0).InnerText;
} }
nodes = doc.GetElementsByTagName("type"); nodes = doc.GetElementsByTagName("type");
if(nodes.Count > 0) { if (nodes.Count > 0)
{
imgurInfo.ImageType = nodes.Item(0).InnerText; imgurInfo.ImageType = nodes.Item(0).InnerText;
} }
nodes = doc.GetElementsByTagName("title"); nodes = doc.GetElementsByTagName("title");
if(nodes.Count > 0) { if (nodes.Count > 0)
{
imgurInfo.Title = nodes.Item(0).InnerText; imgurInfo.Title = nodes.Item(0).InnerText;
} }
nodes = doc.GetElementsByTagName("datetime"); nodes = doc.GetElementsByTagName("datetime");
if(nodes.Count > 0) { if (nodes.Count > 0)
{
// Version 3 has seconds since Epoch // Version 3 has seconds since Epoch
double secondsSince; double secondsSince;
if (double.TryParse(nodes.Item(0).InnerText, out secondsSince)) if (double.TryParse(nodes.Item(0).InnerText, out secondsSince))
@ -198,15 +216,17 @@ namespace GreenshotImgurPlugin
imgurInfo.Page = string.Format("https://imgur.com/{0}", imgurInfo.Hash); imgurInfo.Page = string.Format("https://imgur.com/{0}", imgurInfo.Hash);
} }
nodes = doc.GetElementsByTagName("small_square"); nodes = doc.GetElementsByTagName("small_square");
if(nodes.Count > 0) { if (nodes.Count > 0)
{
imgurInfo.SmallSquare = nodes.Item(0).InnerText; imgurInfo.SmallSquare = nodes.Item(0).InnerText;
} }
nodes = doc.GetElementsByTagName("large_thumbnail"); else
if(nodes.Count > 0)
{ {
imgurInfo.LargeThumbnail = nodes.Item(0).InnerText.Replace("http:", "https:"); imgurInfo.SmallSquare = string.Format("http://i.imgur.com/{0}s.png",imgurInfo.Hash);
} }
} catch(Exception e) { }
catch (Exception e)
{
LOG.ErrorFormat("Could not parse Imgur response due to error {0}, response was: {1}", e.Message, response); LOG.ErrorFormat("Could not parse Imgur response due to error {0}, response was: {1}", e.Message, response);
} }
return imgurInfo; return imgurInfo;

View file

@ -119,7 +119,7 @@ namespace GreenshotImgurPlugin {
if (filename != null && Config.AddFilename) { if (filename != null && Config.AddFilename) {
otherParameters.Add("name", filename); otherParameters.Add("name", filename);
} }
string responseString; string responseString = null;
if (Config.AnonymousAccess) { if (Config.AnonymousAccess) {
// add key, we only use the other parameters for the AnonymousAccess // add key, we only use the other parameters for the AnonymousAccess
//otherParameters.Add("key", IMGUR_ANONYMOUS_API_KEY); //otherParameters.Add("key", IMGUR_ANONYMOUS_API_KEY);
@ -133,11 +133,17 @@ namespace GreenshotImgurPlugin {
ImageOutput.SaveToStream(surfaceToUpload, requestStream, outputSettings); ImageOutput.SaveToStream(surfaceToUpload, requestStream, outputSettings);
} }
using (WebResponse response = webRequest.GetResponse()) { using (WebResponse response = webRequest.GetResponse())
using (StreamReader reader = new StreamReader(response.GetResponseStream(), true)) { {
responseString = reader.ReadToEnd();
}
LogRateLimitInfo(response); LogRateLimitInfo(response);
var responseStream = response.GetResponseStream();
if (responseStream != null)
{
using (StreamReader reader = new StreamReader(responseStream, true))
{
responseString = reader.ReadToEnd();
}
}
} }
} catch (Exception ex) { } catch (Exception ex) {
LOG.Error("Upload to imgur gave an exeption: ", ex); LOG.Error("Upload to imgur gave an exeption: ", ex);
@ -145,20 +151,22 @@ namespace GreenshotImgurPlugin {
} }
} else { } else {
var oauth2Settings = new OAuth2Settings(); var oauth2Settings = new OAuth2Settings
oauth2Settings.AuthUrlPattern = AuthUrlPattern; {
oauth2Settings.TokenUrl = TokenUrl; AuthUrlPattern = AuthUrlPattern,
oauth2Settings.RedirectUrl = "https://imgur.com"; TokenUrl = TokenUrl,
oauth2Settings.CloudServiceName = "Imgur"; RedirectUrl = "https://imgur.com",
oauth2Settings.ClientId = ImgurCredentials.CONSUMER_KEY; CloudServiceName = "Imgur",
oauth2Settings.ClientSecret = ImgurCredentials.CONSUMER_SECRET; ClientId = ImgurCredentials.CONSUMER_KEY,
oauth2Settings.AuthorizeMode = OAuth2AuthorizeMode.EmbeddedBrowser; ClientSecret = ImgurCredentials.CONSUMER_SECRET,
oauth2Settings.BrowserSize = new Size(680, 880); AuthorizeMode = OAuth2AuthorizeMode.EmbeddedBrowser,
BrowserSize = new Size(680, 880),
RefreshToken = Config.RefreshToken,
AccessToken = Config.AccessToken,
AccessTokenExpires = Config.AccessTokenExpires
};
// Copy the settings from the config, which is kept in memory and on the disk // Copy the settings from the config, which is kept in memory and on the disk
oauth2Settings.RefreshToken = Config.RefreshToken;
oauth2Settings.AccessToken = Config.AccessToken;
oauth2Settings.AccessTokenExpires = Config.AccessTokenExpires;
try try
{ {
@ -179,6 +187,10 @@ namespace GreenshotImgurPlugin {
IniConfig.Save(); IniConfig.Save();
} }
} }
if (string.IsNullOrEmpty(responseString))
{
return null;
}
return ImgurInfo.ParseResponse(responseString); return ImgurInfo.ParseResponse(responseString);
} }
@ -194,7 +206,8 @@ namespace GreenshotImgurPlugin {
LOG.InfoFormat("Retrieving Imgur image for {0} with url {1}", imgurInfo.Hash, imgurInfo.SmallSquare); LOG.InfoFormat("Retrieving Imgur image for {0} with url {1}", imgurInfo.Hash, imgurInfo.SmallSquare);
HttpWebRequest webRequest = NetworkHelper.CreateWebRequest(string.Format(SmallUrlPattern, imgurInfo.Hash), HTTPMethod.GET); HttpWebRequest webRequest = NetworkHelper.CreateWebRequest(string.Format(SmallUrlPattern, imgurInfo.Hash), HTTPMethod.GET);
webRequest.ServicePoint.Expect100Continue = false; webRequest.ServicePoint.Expect100Continue = false;
SetClientId(webRequest); // Not for getting the thumbnail, in anonymous modus
//SetClientId(webRequest);
using (WebResponse response = webRequest.GetResponse()) { using (WebResponse response = webRequest.GetResponse()) {
LogRateLimitInfo(response); LogRateLimitInfo(response);
Stream responseStream = response.GetResponseStream(); Stream responseStream = response.GetResponseStream();
@ -212,7 +225,7 @@ namespace GreenshotImgurPlugin {
/// <param name="deleteHash"></param> /// <param name="deleteHash"></param>
/// <returns>ImgurInfo</returns> /// <returns>ImgurInfo</returns>
public static ImgurInfo RetrieveImgurInfo(string hash, string deleteHash) { public static ImgurInfo RetrieveImgurInfo(string hash, string deleteHash) {
string url = Config.ImgurApiUrl + "/image/" + hash; string url = Config.ImgurApi3Url + "/image/" + hash + ".xml";
LOG.InfoFormat("Retrieving Imgur info for {0} with url {1}", hash, url); LOG.InfoFormat("Retrieving Imgur info for {0} with url {1}", hash, url);
HttpWebRequest webRequest = NetworkHelper.CreateWebRequest(url, HTTPMethod.GET); HttpWebRequest webRequest = NetworkHelper.CreateWebRequest(url, HTTPMethod.GET);
webRequest.ServicePoint.Expect100Continue = false; webRequest.ServicePoint.Expect100Continue = false;
@ -247,8 +260,8 @@ namespace GreenshotImgurPlugin {
LOG.InfoFormat("Deleting Imgur image for {0}", imgurInfo.DeleteHash); LOG.InfoFormat("Deleting Imgur image for {0}", imgurInfo.DeleteHash);
try { try {
string url = Config.ImgurApiUrl + "/delete/" + imgurInfo.DeleteHash; string url = Config.ImgurApi3Url + "/image/" + imgurInfo.DeleteHash + ".xml";
HttpWebRequest webRequest = NetworkHelper.CreateWebRequest(url, HTTPMethod.GET); HttpWebRequest webRequest = NetworkHelper.CreateWebRequest(url, HTTPMethod.DELETE);
webRequest.ServicePoint.Expect100Continue = false; webRequest.ServicePoint.Expect100Continue = false;
SetClientId(webRequest); SetClientId(webRequest);
string responseString; string responseString;