private async Task <Stream> GetStreamAsync(string path, ImageSource source) { Stream stream = null; if (string.IsNullOrWhiteSpace(path)) { return(null); } try { switch (source) { case ImageSource.ApplicationBundle: stream = Context.Assets.Open(path, Access.Streaming); break; case ImageSource.Filepath: stream = FileStore.GetInputStream(path); break; case ImageSource.Url: stream = await DownloadCache.GetStreamAsync(path, Parameters.CacheDuration).ConfigureAwait(false); break; } } catch (Exception ex) { Logger.Error("Unable to retrieve image data", ex); return(null); } return(stream); }
protected async virtual Task <GenerateResult> TryDownloadImageAsync() { try { if (Parameters.Source != ImageSource.Url) { throw new InvalidOperationException("DownloadOnly: Only Url ImageSource is supported."); } var data = await DownloadCache.GetStreamAsync(Parameters.Path, CancellationToken.Token, Parameters.OnDownloadStarted, Parameters.CacheDuration, Parameters.CustomCacheKey, Parameters.CacheType).ConfigureAwait(false); using (var imageStream = data.ImageStream) { if (!data.RetrievedFromDiskCache) { Logger?.Debug(string.Format("DownloadOnly: {0} successfully downloaded.", Parameters.Path)); } } return(GenerateResult.Success); } catch (Exception ex) { if (ex is OperationCanceledException) { return(GenerateResult.Canceled); } Logger?.Error(string.Format("DownloadOnly: {0} downloaded failed.", Parameters.Path), ex); return(GenerateResult.Failed); } }