private static async Task <BitmapImage> DownloadImageAsync(StorageFile file, string url, Pages.ImageModel model, InAppNotify notify) { try { if (model != null) { model.IsProgressRingActived = true; } using (var hc = new HttpClient()) using (var stream = await hc.GetStreamAsync(new Uri(url))) using (var fs = await file.OpenStreamForWriteAsync()) { await stream.CopyToAsync(fs); } return(new BitmapImage(new Uri(file.Path))); } catch (FileLoadException) { return(NoPic); } catch (HttpRequestException) { var str = Windows.ApplicationModel.Resources.ResourceLoader.GetForViewIndependentUse().GetString("ImageLoadError"); if (notify == null) { UIHelper.StatusBar_ShowMessage(str); } else { notify.Show(str, UIHelper.duration); } return(NoPic); } finally { if (model != null) { model.IsProgressRingActived = false; } } }
internal static async Task <BitmapImage> GetImageAsyncOld(ImageType type, string url, Pages.ImageModel model = null, InAppNotify notify = null) { if (string.IsNullOrEmpty(url)) { return(null); } if (url.IndexOf("ms-appx", StringComparison.Ordinal) == 0) { return(new BitmapImage(new Uri(url))); } else if (model == null && SettingsHelper.Get <bool>(SettingsHelper.IsNoPicsMode)) { return(NoPic); } else { var fileName = Core.Helpers.Utils.GetMD5(url); var folder = await GetFolderAsync(type); var item = await folder.TryGetItemAsync(fileName); if (type == ImageType.SmallImage || type == ImageType.SmallAvatar) { url += ".s.jpg"; } var forceGetPic = model != null; if (item is null) { StorageFile file = await folder.CreateFileAsync(fileName, CreationCollisionOption.OpenIfExists); return(await DownloadImageAsync(file, url, model, notify)); } else { return(item is StorageFile file?GetLocalImageAsync(file.Path, forceGetPic) : null); } } }