This is a quick Imgur fix, and reducing the amount of traffic.

This commit is contained in:
Robin 2016-11-03 21:51:27 +01:00
commit a9a745a439
22 changed files with 1136 additions and 757 deletions

View file

@ -1,9 +1,9 @@
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2015 Thomas Braun, Jens Klingen, Robin Krom
* Copyright (C) 2007-2016 Thomas Braun, Jens Klingen, Robin Krom
*
* For more information see: http://getgreenshot.org/
* The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/
* The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot
*
* 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
@ -27,8 +27,9 @@ namespace GreenshotImgurPlugin
/// <summary>
/// Description of ImgurInfo.
/// </summary>
public class ImgurInfo : IDisposable {
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(ImgurInfo));
public class ImgurInfo : IDisposable
{
private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(typeof(ImgurInfo));
public string Hash
{
@ -36,11 +37,13 @@ namespace GreenshotImgurPlugin
set;
}
private string deleteHash;
public string DeleteHash {
get {return deleteHash;}
set {
deleteHash = value;
private string _deleteHash;
public string DeleteHash
{
get { return _deleteHash; }
set
{
_deleteHash = value;
DeletePage = "https://imgur.com/delete/" + value;
}
}
@ -93,25 +96,23 @@ namespace GreenshotImgurPlugin
set;
}
private Image image;
public Image Image {
get {return image;}
set {
if (image != null) {
image.Dispose();
}
image = value;
private Image _image;
public Image Image
{
get { return _image; }
set
{
_image?.Dispose();
_image = value;
}
}
public ImgurInfo() {
}
/// <summary>
/// The public accessible Dispose
/// Will call the GarbageCollector to SuppressFinalize, preventing being cleaned twice
/// </summary>
public void Dispose() {
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
@ -121,16 +122,17 @@ namespace GreenshotImgurPlugin
/// When disposing==true all non-managed resources should be freed too!
/// </summary>
/// <param name="disposing"></param>
protected virtual void Dispose(bool disposing) {
if (disposing) {
if (image != null) {
image.Dispose();
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_image?.Dispose();
}
image = null;
_image = null;
}
public static ImgurInfo ParseResponse(string response) {
LOG.Debug(response);
public static ImgurInfo ParseResponse(string response)
{
Log.Debug(response);
// 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
// Added all the encodings I found quickly, I guess these are not all... but it should fix the issue for now.
@ -142,35 +144,41 @@ namespace GreenshotImgurPlugin
response = response.Replace("&reg;", "&#174;");
ImgurInfo imgurInfo = new ImgurInfo();
try {
try
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(response);
XmlNodeList nodes = doc.GetElementsByTagName("id");
if(nodes.Count > 0) {
imgurInfo.Hash = nodes.Item(0).InnerText;
if (nodes.Count > 0)
{
imgurInfo.Hash = nodes.Item(0)?.InnerText;
}
nodes = doc.GetElementsByTagName("hash");
if (nodes.Count > 0)
{
imgurInfo.Hash = nodes.Item(0).InnerText;
imgurInfo.Hash = nodes.Item(0)?.InnerText;
}
nodes = doc.GetElementsByTagName("deletehash");
if(nodes.Count > 0) {
imgurInfo.DeleteHash = nodes.Item(0).InnerText;
if (nodes.Count > 0)
{
imgurInfo.DeleteHash = nodes.Item(0)?.InnerText;
}
nodes = doc.GetElementsByTagName("type");
if(nodes.Count > 0) {
imgurInfo.ImageType = nodes.Item(0).InnerText;
if (nodes.Count > 0)
{
imgurInfo.ImageType = nodes.Item(0)?.InnerText;
}
nodes = doc.GetElementsByTagName("title");
if(nodes.Count > 0) {
imgurInfo.Title = nodes.Item(0).InnerText;
if (nodes.Count > 0)
{
imgurInfo.Title = nodes.Item(0)?.InnerText;
}
nodes = doc.GetElementsByTagName("datetime");
if(nodes.Count > 0) {
if (nodes.Count > 0)
{
// Version 3 has seconds since Epoch
double secondsSince;
if (double.TryParse(nodes.Item(0).InnerText, out secondsSince))
if (double.TryParse(nodes.Item(0)?.InnerText, out secondsSince))
{
var epoch = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);
imgurInfo.Timestamp = epoch.AddSeconds(secondsSince).DateTime;
@ -179,35 +187,30 @@ namespace GreenshotImgurPlugin
nodes = doc.GetElementsByTagName("original");
if (nodes.Count > 0)
{
imgurInfo.Original = nodes.Item(0).InnerText.Replace("http:", "https:");
imgurInfo.Original = nodes.Item(0)?.InnerText.Replace("http:", "https:");
}
// Version 3 API only has Link
nodes = doc.GetElementsByTagName("link");
if (nodes.Count > 0)
{
imgurInfo.Original = nodes.Item(0).InnerText.Replace("http:", "https:");
imgurInfo.Original = nodes.Item(0)?.InnerText.Replace("http:", "https:");
}
nodes = doc.GetElementsByTagName("imgur_page");
if (nodes.Count > 0)
{
imgurInfo.Page = nodes.Item(0).InnerText.Replace("http:", "https:");
imgurInfo.Page = nodes.Item(0)?.InnerText.Replace("http:", "https:");
}
else
{
// Version 3 doesn't have a page link in the response
imgurInfo.Page = string.Format("https://imgur.com/{0}", imgurInfo.Hash);
imgurInfo.Page = $"https://imgur.com/{imgurInfo.Hash}";
}
nodes = doc.GetElementsByTagName("small_square");
if(nodes.Count > 0) {
imgurInfo.SmallSquare = nodes.Item(0).InnerText;
}
nodes = doc.GetElementsByTagName("large_thumbnail");
if(nodes.Count > 0)
{
imgurInfo.LargeThumbnail = nodes.Item(0).InnerText.Replace("http:", "https:");
}
} catch(Exception e) {
LOG.ErrorFormat("Could not parse Imgur response due to error {0}, response was: {1}", e.Message, response);
imgurInfo.SmallSquare = nodes.Count > 0 ? nodes.Item(0)?.InnerText : $"http://i.imgur.com/{imgurInfo.Hash}s.png";
}
catch (Exception e)
{
Log.ErrorFormat("Could not parse Imgur response due to error {0}, response was: {1}", e.Message, response);
}
return imgurInfo;
}