1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2025-05-11 20:34:07 +02:00

Move 'View image in photo viewer' handling to TwitterUtils

This commit is contained in:
chylex 2018-08-23 19:32:20 +02:00
parent 2e4dd3df3e
commit ff3dc59016
2 changed files with 30 additions and 25 deletions

View File

@ -1,5 +1,4 @@
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using CefSharp;
@ -123,33 +122,11 @@ public virtual bool OnContextMenuCommand(IWebBrowser browserControl, IBrowser br
break;
case MenuViewImage: {
void ViewImage(string path){
string ext = Path.GetExtension(path);
if (TwitterUtils.ValidImageExtensions.Contains(ext)){
WindowsUtils.OpenAssociatedProgram(path);
}
else{
FormMessage.Error("Image Download", "Invalid file extension "+ext, FormMessage.OK);
}
}
string url = Context.MediaUrl;
string file = Path.Combine(BrowserCache.CacheFolder, TwitterUtils.GetImageFileName(url) ?? Path.GetRandomFileName());
control.InvokeAsyncSafe(() => {
if (File.Exists(file)){
ViewImage(file);
}
else{
analytics.AnalyticsFile.ViewedImages.Trigger();
BrowserUtils.DownloadFileAsync(TwitterUtils.GetMediaLink(url, ImageQuality), file, () => {
ViewImage(file);
}, ex => {
FormMessage.Error("Image Download", "An error occurred while downloading the image: "+ex.Message, FormMessage.OK);
});
}
TwitterUtils.ViewImage(url, ImageQuality);
analytics.AnalyticsFile.ViewedImages.Trigger();
});
break;

View File

@ -4,8 +4,10 @@
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using TweetDuck.Core.Management;
using TweetDuck.Core.Other;
using TweetDuck.Data;
using System.Linq;
namespace TweetDuck.Core.Utils{
static class TwitterUtils{
@ -62,6 +64,32 @@ public static string GetMediaLink(string url, ImageQuality quality){
public static string GetImageFileName(string url){
return BrowserUtils.GetFileNameFromUrl(ExtractMediaBaseLink(url));
}
public static void ViewImage(string url, ImageQuality quality){
void ViewImageInternal(string path){
string ext = Path.GetExtension(path);
if (ValidImageExtensions.Contains(ext)){
WindowsUtils.OpenAssociatedProgram(path);
}
else{
FormMessage.Error("Image Download", "Invalid file extension "+ext, FormMessage.OK);
}
}
string file = Path.Combine(BrowserCache.CacheFolder, GetImageFileName(url) ?? Path.GetRandomFileName());
if (File.Exists(file)){
ViewImageInternal(file);
}
else{
BrowserUtils.DownloadFileAsync(GetMediaLink(url, quality), file, () => {
ViewImageInternal(file);
}, ex => {
FormMessage.Error("Image Download", "An error occurred while downloading the image: "+ex.Message, FormMessage.OK);
});
}
}
public static void DownloadImage(string url, string username, ImageQuality quality){
DownloadImages(new string[]{ url }, username, quality);