Added synchronous Picture downloader.

This commit is contained in:
Michael Bucari-Tovo 2021-08-10 20:16:34 -06:00
parent 2f3c0e8a95
commit 4989cda93c
3 changed files with 31 additions and 54 deletions

View file

@ -6,7 +6,7 @@ using System.Net.Http;
namespace FileManager
{
public enum PictureSize { _80x80, _300x300, _500x500 }
public enum PictureSize { _80x80 = 80, _300x300 = 300, _500x500 = 500 }
public struct PictureDefinition
{
public string PictureId { get; }
@ -54,6 +54,26 @@ namespace FileManager
return (cache[def] == null, cache[def] ?? getDefaultImage(def.Size));
}
public static byte[] GetPictureSynchronously(PictureDefinition def)
{
if (!cache.ContainsKey(def) || cache[def] == null)
{
var path = getPath(def);
byte[] bytes;
if (File.Exists(path))
bytes = File.ReadAllBytes(path);
else
{
bytes = downloadBytes(def);
saveFile(def, bytes);
}
cache[def] = bytes;
}
return cache[def];
}
private static Dictionary<PictureSize, byte[]> defaultImages { get; } = new Dictionary<PictureSize, byte[]>();
public static void SetDefaultImage(PictureSize pictureSize, byte[] bytes)
=> defaultImages[pictureSize] = bytes;
@ -100,7 +120,7 @@ namespace FileManager
private static HttpClient imageDownloadClient { get; } = new HttpClient();
private static byte[] downloadBytes(PictureDefinition def)
{
var sz = def.Size.ToString().Split('x')[1];
var sz = ((int)def.Size).ToString();
return imageDownloadClient.GetByteArrayAsync("ht" + $"tps://images-na.ssl-images-amazon.com/images/I/{def.PictureId}._SL{sz}_.jpg").Result;
}