mirror of
https://github.com/greenshot/greenshot
synced 2025-08-21 05:53:27 -07:00
BUG-1894: Postponing history loading as long as possible, this reduces the Imgur activity dramatically (as it's not loaded during start) and should at least prevent a 429 early on... hopefully. It would be better to cache everything locally on the user's PC, but this is unrealistic with .NET 2.0 technology (not impossible, but so time consuming that it would be better to wait for the next Greenshot version with 4.5).
This commit is contained in:
parent
a9fd39fd45
commit
ede5bfef97
8 changed files with 85 additions and 52 deletions
|
@ -22,6 +22,7 @@ using System;
|
|||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using Greenshot.IniFile;
|
||||
using Greenshot.Plugin;
|
||||
|
@ -29,7 +30,7 @@ using GreenshotPlugin.Core;
|
|||
|
||||
namespace GreenshotImgurPlugin {
|
||||
/// <summary>
|
||||
/// Description of ImgurUtils.
|
||||
/// A collection of Imgur helper methods
|
||||
/// </summary>
|
||||
public static class ImgurUtils {
|
||||
private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(typeof(ImgurUtils));
|
||||
|
@ -38,44 +39,62 @@ namespace GreenshotImgurPlugin {
|
|||
private const string AuthUrlPattern = "https://api.imgur.com/oauth2/authorize?response_type=code&client_id={ClientId}&redirect_uri={RedirectUrl}&state={State}";
|
||||
private const string TokenUrl = "https://api.imgur.com/oauth2/token";
|
||||
|
||||
/// <summary>
|
||||
/// Check if we need to load the history
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static bool IsHistoryLoadingNeeded()
|
||||
{
|
||||
Log.InfoFormat("Checking if imgur cache loading needed, configuration has {0} imgur hashes, loaded are {1} hashes.", Config.ImgurUploadHistory.Count, Config.runtimeImgurHistory.Count);
|
||||
return Config.runtimeImgurHistory.Count != Config.ImgurUploadHistory.Count;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Load the complete history of the imgur uploads, with the corresponding information
|
||||
/// </summary>
|
||||
public static void LoadHistory() {
|
||||
if (Config.runtimeImgurHistory.Count == Config.ImgurUploadHistory.Count) {
|
||||
if (!IsHistoryLoadingNeeded())
|
||||
{
|
||||
return;
|
||||
}
|
||||
// Load the ImUr history
|
||||
IList<string> hashes = new List<string>();
|
||||
foreach(string hash in Config.ImgurUploadHistory.Keys) {
|
||||
hashes.Add(hash);
|
||||
}
|
||||
|
||||
|
||||
bool saveNeeded = false;
|
||||
|
||||
foreach(string hash in hashes) {
|
||||
// Load the ImUr history
|
||||
foreach (string hash in Config.ImgurUploadHistory.Keys.ToList()) {
|
||||
if (Config.runtimeImgurHistory.ContainsKey(hash)) {
|
||||
// Already loaded
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
ImgurInfo imgurInfo = RetrieveImgurInfo(hash, Config.ImgurUploadHistory[hash]);
|
||||
|
||||
try
|
||||
{
|
||||
var deleteHash = Config.ImgurUploadHistory[hash];
|
||||
ImgurInfo imgurInfo = RetrieveImgurInfo(hash, deleteHash);
|
||||
if (imgurInfo != null) {
|
||||
RetrieveImgurThumbnail(imgurInfo);
|
||||
Config.runtimeImgurHistory[hash] = imgurInfo;
|
||||
} else {
|
||||
Log.DebugFormat("Deleting not found ImgUr {0} from config.", hash);
|
||||
Log.InfoFormat("Deleting unknown ImgUr {0} from config, delete hash was {1}.", hash, deleteHash);
|
||||
Config.ImgurUploadHistory.Remove(hash);
|
||||
Config.runtimeImgurHistory.Remove(hash);
|
||||
saveNeeded = true;
|
||||
}
|
||||
} catch (WebException wE) {
|
||||
bool redirected = false;
|
||||
if (wE.Status == WebExceptionStatus.ProtocolError) {
|
||||
HttpWebResponse response = ((HttpWebResponse)wE.Response);
|
||||
// Image no longer available
|
||||
HttpWebResponse response = (HttpWebResponse)wE.Response;
|
||||
|
||||
if (response.StatusCode == HttpStatusCode.Forbidden)
|
||||
{
|
||||
Log.Error("Imgur loading forbidden", wE);
|
||||
break;
|
||||
}
|
||||
// Image no longer available?
|
||||
if (response.StatusCode == HttpStatusCode.Redirect) {
|
||||
Log.InfoFormat("ImgUr image for hash {0} is no longer available", hash);
|
||||
Log.InfoFormat("ImgUr image for hash {0} is no longer available, removing it from the history", hash);
|
||||
Config.ImgurUploadHistory.Remove(hash);
|
||||
Config.runtimeImgurHistory.Remove(hash);
|
||||
redirected = true;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue