using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Threading;
using PicView.Core.FileHandling;
using PicView.WPF.ChangeImage;
using PicView.WPF.ImageHandling;
using PicView.WPF.UILogic;
using static PicView.WPF.ChangeImage.ErrorHandling;
using static PicView.WPF.UILogic.Tooltip;
namespace PicView.WPF.FileHandling
{
public abstract class HttpFunctions
{
///
/// Attempts to download image and display it
///
///
internal static async Task LoadPicFromUrlAsync(string url)
{
ChangeFolder(true);
string destination;
try
{
destination = await DownloadDataAsync(url).ConfigureAwait(false);
}
catch (Exception e)
{
#if DEBUG
Trace.WriteLine("LoadPicFromUrlAsync exception = \n" + e.Message);
#endif
await ConfigureWindows.GetMainWindow.Dispatcher.InvokeAsync(async () =>
{
await ReloadAsync(true).ConfigureAwait(false);
ShowTooltipMessage(e.Message, true);
});
return;
}
var check = CheckIfLoadableString(destination);
switch (check)
{
default:
var pic = await Image2BitmapSource.ReturnBitmapSourceAsync(new FileInfo(check)).ConfigureAwait(false);
await UpdateImage.UpdateImageAsync(url, pic,
Path.GetExtension(url).Contains(".gif", StringComparison.OrdinalIgnoreCase), destination)
.ConfigureAwait(false);
break;
case "base64":
await UpdateImage.UpdateImageFromBase64PicAsync(destination).ConfigureAwait(false);
break;
case "zip":
await LoadPic.LoadPicFromArchiveAsync(check).ConfigureAwait(false);
break;
case "directory":
case "":
ConfigureWindows.GetMainWindow.Dispatcher.Invoke(DispatcherPriority.Render, () => Unload(true));
return;
}
await ConfigureWindows.GetMainWindow.Dispatcher.InvokeAsync(() =>
{
// Fix not having focus after drag and drop
if (!ConfigureWindows.GetMainWindow.IsFocused)
{
ConfigureWindows.GetMainWindow.Focus();
}
});
FileHistoryNavigation.Add(url);
Navigation.InitialPath = url;
}
///
/// Downloads data from the specified URL to a temporary directory and returns the path to the downloaded file.
///
/// The URL of the data to be downloaded.
/// True if a progress display should be updated during the download, otherwise false.
/// The path to the downloaded file in the temporary directory.
internal static async Task DownloadDataAsync(string url, bool displayProgress = true)
{
// Create temp directory
var tempPath = Path.GetTempPath();
var fileName = Path.GetFileName(url);
Core.FileHandling.ArchiveExtraction.CreateTempDirectory(tempPath);
// Remove past "?" to not get file exceptions
var index = fileName.IndexOf("?", StringComparison.InvariantCulture);
if (index >= 0)
{
fileName = fileName[..index];
}
Core.FileHandling.ArchiveExtraction.TempFilePath = tempPath + fileName;
using (var client = new HttpHelper.HttpClientDownloadWithProgress(url, Core.FileHandling.ArchiveExtraction.TempFilePath))
{
if (displayProgress) // Set up progress display
{
client.ProgressChanged += UpdateProgressDisplay;
}
await client.StartDownloadAsync().ConfigureAwait(false);
}
return Core.FileHandling.ArchiveExtraction.TempFilePath;
}
///
/// Updates the progress display during the download.
///
/// The total size of the file to be downloaded.
/// The total number of bytes downloaded so far.
/// The percentage of the download that has been completed.
private static void UpdateProgressDisplay(long? totalFileSize, long? totalBytesDownloaded,
double? progressPercentage)
{
if (!totalFileSize.HasValue || !totalBytesDownloaded.HasValue || !progressPercentage.HasValue) return;
var percentComplete = (string)Application.Current.Resources["PercentComplete"];
var displayProgress =
$"{(int)totalBytesDownloaded}/{(int)totalBytesDownloaded} {(int)progressPercentage} {percentComplete}";
ConfigureWindows.GetMainWindow.Dispatcher.Invoke(DispatcherPriority.Normal, () =>
{
ConfigureWindows.GetMainWindow.Title = displayProgress;
ConfigureWindows.GetMainWindow.TitleText.Text = displayProgress;
ConfigureWindows.GetMainWindow.TitleText.ToolTip = displayProgress;
});
}
}
}