From 937d27ae9d6aa571ab9327f138bfba1b84c158db Mon Sep 17 00:00:00 2001 From: LukePulverenti Luke Pulverenti luke pulverenti Date: Sun, 19 Aug 2012 16:38:31 -0400 Subject: One async call leads to another, and another, all the way up the call stack... --- MediaBrowser.Api/HttpHandlers/ImageHandler.cs | 106 ++++++++++++-------------- 1 file changed, 47 insertions(+), 59 deletions(-) (limited to 'MediaBrowser.Api/HttpHandlers/ImageHandler.cs') diff --git a/MediaBrowser.Api/HttpHandlers/ImageHandler.cs b/MediaBrowser.Api/HttpHandlers/ImageHandler.cs index 826438098..4a6b2a481 100644 --- a/MediaBrowser.Api/HttpHandlers/ImageHandler.cs +++ b/MediaBrowser.Api/HttpHandlers/ImageHandler.cs @@ -13,39 +13,57 @@ namespace MediaBrowser.Api.HttpHandlers public class ImageHandler : BaseHandler { private string _ImagePath = null; - private string ImagePath + private async Task GetImagePath() { - get + if (_ImagePath == null) { - if (_ImagePath == null) - { - _ImagePath = GetImagePath(); - } - - return _ImagePath; + _ImagePath = await DiscoverImagePath(); } + + return _ImagePath; } - private Stream _SourceStream = null; - private Stream SourceStream + private async Task DiscoverImagePath() { - get + string path = QueryString["path"] ?? string.Empty; + + if (!string.IsNullOrEmpty(path)) { - EnsureSourceStream(); + return path; + } - return _SourceStream; + string personName = QueryString["personname"]; + + if (!string.IsNullOrEmpty(personName)) + { + Person person = await Kernel.Instance.ItemController.GetPerson(personName); + + return person.PrimaryImagePath; } + + BaseItem item = ApiService.GetItemById(QueryString["id"]); + + string imageIndex = QueryString["index"]; + int index = string.IsNullOrEmpty(imageIndex) ? 0 : int.Parse(imageIndex); + + return GetImagePathFromTypes(item, ImageType, index); } + private Stream _SourceStream = null; + private async Task GetSourceStream() + { + await EnsureSourceStream(); + return _SourceStream; + } private bool _SourceStreamEnsured = false; - private void EnsureSourceStream() + private async Task EnsureSourceStream() { if (!_SourceStreamEnsured) { try { - _SourceStream = File.OpenRead(ImagePath); + _SourceStream = File.OpenRead(await GetImagePath()); } catch (FileNotFoundException ex) { @@ -68,20 +86,17 @@ namespace MediaBrowser.Api.HttpHandlers } } } - - public override string ContentType + + public async override Task GetContentType() { - get - { - EnsureSourceStream(); + await EnsureSourceStream(); - if (SourceStream == null) - { - return null; - } - - return MimeTypes.GetMimeType(ImagePath); + if (await GetSourceStream() == null) + { + return null; } + + return MimeTypes.GetMimeType(await GetImagePath()); } public override TimeSpan CacheDuration @@ -92,16 +107,16 @@ namespace MediaBrowser.Api.HttpHandlers } } - protected override DateTime? GetLastDateModified() + protected async override Task GetLastDateModified() { - EnsureSourceStream(); + await EnsureSourceStream(); - if (SourceStream == null) + if (await GetSourceStream() == null) { return null; } - return File.GetLastWriteTime(ImagePath); + return File.GetLastWriteTime(await GetImagePath()); } private int? Height @@ -194,36 +209,9 @@ namespace MediaBrowser.Api.HttpHandlers } } - protected override Task WriteResponseToOutputStream(Stream stream) + protected override async Task WriteResponseToOutputStream(Stream stream) { - return Task.Run(() => - { - ImageProcessor.ProcessImage(SourceStream, stream, Width, Height, MaxWidth, MaxHeight, Quality); - }); - } - - private string GetImagePath() - { - string path = QueryString["path"] ?? string.Empty; - - if (!string.IsNullOrEmpty(path)) - { - return path; - } - - string personName = QueryString["personname"]; - - if (!string.IsNullOrEmpty(personName)) - { - return Kernel.Instance.ItemController.GetPerson(personName).PrimaryImagePath; - } - - BaseItem item = ApiService.GetItemById(QueryString["id"]); - - string imageIndex = QueryString["index"]; - int index = string.IsNullOrEmpty(imageIndex) ? 0 : int.Parse(imageIndex); - - return GetImagePathFromTypes(item, ImageType, index); + ImageProcessor.ProcessImage(await GetSourceStream(), stream, Width, Height, MaxWidth, MaxHeight, Quality); } private string GetImagePathFromTypes(BaseItem item, ImageType imageType, int imageIndex) -- cgit v1.2.3