public static byte[] GetPictureSynchronously(PictureDefinition def) { lock (cacheLocker) { if (!cache.ContainsKey(def) || cache[def] == null) { var path = getPath(def); var bytes = File.Exists(path) ? File.ReadAllBytes(path) : downloadBytes(def); cache[def] = bytes; } return(cache[def]); } }
public static (bool isDefault, byte[] bytes) GetPicture(PictureDefinition def) { lock (cacheLocker) { if (cache.ContainsKey(def)) { return(false, cache[def]); } var path = getPath(def); if (File.Exists(path)) { cache[def] = File.ReadAllBytes(path); return(false, cache[def]); } DownloadQueue.Add(def); return(true, getDefaultImage(def.Size)); } }
private static byte[] downloadBytes(PictureDefinition def) { if (def.PictureId is null) { return(getDefaultImage(def.Size)); } try { var sz = (int)def.Size; var bytes = imageDownloadClient.GetByteArrayAsync("ht" + $"tps://images-na.ssl-images-amazon.com/images/I/{def.PictureId}._SL{sz}_.jpg").Result; // save image file. make sure to not save default image var path = getPath(def); File.WriteAllBytes(path, bytes); return(bytes); } catch { return(getDefaultImage(def.Size)); } }
private static string getPath(PictureDefinition def) => Path.Combine(ImagesDirectory, $"{def.PictureId}{def.Size}.jpg");