aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Api/HttpHandlers
diff options
context:
space:
mode:
authorebr11 Eric Reed spam <ebr11 Eric Reed spam@reedsplace.com>2012-09-20 11:28:02 -0400
committerebr11 Eric Reed spam <ebr11 Eric Reed spam@reedsplace.com>2012-09-20 11:28:02 -0400
commite2ae376eef11dbb69c6d5d0ca31b202394452390 (patch)
treecb7c90b9bd1685aa3f8219686bf0e1c1b306bdc0 /MediaBrowser.Api/HttpHandlers
parent4e3ce41880d643a2951dc971f1e8f6e21f57829c (diff)
parentd8c01ded6eb57ba312e1cd62c4fa51dbcce6053a (diff)
Merge with default
Diffstat (limited to 'MediaBrowser.Api/HttpHandlers')
-rw-r--r--MediaBrowser.Api/HttpHandlers/BaseMediaHandler.cs13
-rw-r--r--MediaBrowser.Api/HttpHandlers/ImageHandler.cs184
-rw-r--r--MediaBrowser.Api/HttpHandlers/PluginAssemblyHandler.cs4
-rw-r--r--MediaBrowser.Api/HttpHandlers/PluginConfigurationHandler.cs19
-rw-r--r--MediaBrowser.Api/HttpHandlers/ServerConfigurationHandler.cs19
-rw-r--r--MediaBrowser.Api/HttpHandlers/VideoHandler.cs5
-rw-r--r--MediaBrowser.Api/HttpHandlers/WeatherHandler.cs19
7 files changed, 107 insertions, 156 deletions
diff --git a/MediaBrowser.Api/HttpHandlers/BaseMediaHandler.cs b/MediaBrowser.Api/HttpHandlers/BaseMediaHandler.cs
index 7ad0ed8aa3..96ef606813 100644
--- a/MediaBrowser.Api/HttpHandlers/BaseMediaHandler.cs
+++ b/MediaBrowser.Api/HttpHandlers/BaseMediaHandler.cs
@@ -90,14 +90,15 @@ namespace MediaBrowser.Api.HttpHandlers
}
}
- public override Task<string> GetContentType()
+ protected override Task<ResponseInfo> GetResponseInfo()
{
- return Task.FromResult(MimeTypes.GetMimeType("." + GetConversionOutputFormat()));
- }
+ ResponseInfo info = new ResponseInfo
+ {
+ ContentType = MimeTypes.GetMimeType("." + GetConversionOutputFormat()),
+ CompressResponse = false
+ };
- public override bool ShouldCompressResponse(string contentType)
- {
- return false;
+ return Task.FromResult<ResponseInfo>(info);
}
public override Task ProcessRequest(HttpListenerContext ctx)
diff --git a/MediaBrowser.Api/HttpHandlers/ImageHandler.cs b/MediaBrowser.Api/HttpHandlers/ImageHandler.cs
index f73f783afb..4aa367fb7e 100644
--- a/MediaBrowser.Api/HttpHandlers/ImageHandler.cs
+++ b/MediaBrowser.Api/HttpHandlers/ImageHandler.cs
@@ -1,13 +1,12 @@
-using MediaBrowser.Common.Logging;
-using MediaBrowser.Common.Net;
+using MediaBrowser.Common.Net;
using MediaBrowser.Common.Net.Handlers;
using MediaBrowser.Controller;
+using MediaBrowser.Controller.Drawing;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Model.Entities;
using System;
using System.ComponentModel.Composition;
using System.IO;
-using System.Linq;
using System.Net;
using System.Threading.Tasks;
@@ -20,8 +19,9 @@ namespace MediaBrowser.Api.HttpHandlers
{
return ApiService.IsApiUrlMatch("image", request);
}
-
+
private string _imagePath;
+
private async Task<string> GetImagePath()
{
_imagePath = _imagePath ?? await DiscoverImagePath();
@@ -29,119 +29,99 @@ namespace MediaBrowser.Api.HttpHandlers
return _imagePath;
}
- private async Task<string> DiscoverImagePath()
- {
- string personName = QueryString["personname"];
-
- if (!string.IsNullOrEmpty(personName))
- {
- return (await Kernel.Instance.ItemController.GetPerson(personName).ConfigureAwait(false)).PrimaryImagePath;
- }
-
- string genreName = QueryString["genre"];
-
- if (!string.IsNullOrEmpty(genreName))
- {
- return (await Kernel.Instance.ItemController.GetGenre(genreName).ConfigureAwait(false)).PrimaryImagePath;
- }
-
- string year = QueryString["year"];
-
- if (!string.IsNullOrEmpty(year))
- {
- return (await Kernel.Instance.ItemController.GetYear(int.Parse(year)).ConfigureAwait(false)).PrimaryImagePath;
- }
-
- string studio = QueryString["studio"];
-
- if (!string.IsNullOrEmpty(studio))
- {
- return (await Kernel.Instance.ItemController.GetStudio(studio).ConfigureAwait(false)).PrimaryImagePath;
- }
-
- string userId = QueryString["userid"];
-
- if (!string.IsNullOrEmpty(userId))
- {
- return ApiService.GetUserById(userId, false).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;
- private async Task<Stream> GetSourceStream()
- {
- await EnsureSourceStream().ConfigureAwait(false);
- return _sourceStream;
- }
+ private BaseEntity _sourceEntity;
- private bool _sourceStreamEnsured;
- private async Task EnsureSourceStream()
+ private async Task<BaseEntity> GetSourceEntity()
{
- if (!_sourceStreamEnsured)
+ if (_sourceEntity == null)
{
- try
+ if (!string.IsNullOrEmpty(QueryString["personname"]))
{
- _sourceStream = File.OpenRead(await GetImagePath().ConfigureAwait(false));
+ _sourceEntity =
+ await Kernel.Instance.ItemController.GetPerson(QueryString["personname"]).ConfigureAwait(false);
}
- catch (FileNotFoundException ex)
+
+ else if (!string.IsNullOrEmpty(QueryString["genre"]))
{
- StatusCode = 404;
- Logger.LogException(ex);
+ _sourceEntity =
+ await Kernel.Instance.ItemController.GetGenre(QueryString["genre"]).ConfigureAwait(false);
}
- catch (DirectoryNotFoundException ex)
+
+ else if (!string.IsNullOrEmpty(QueryString["year"]))
{
- StatusCode = 404;
- Logger.LogException(ex);
+ _sourceEntity =
+ await
+ Kernel.Instance.ItemController.GetYear(int.Parse(QueryString["year"])).ConfigureAwait(false);
}
- catch (UnauthorizedAccessException ex)
+
+ else if (!string.IsNullOrEmpty(QueryString["studio"]))
{
- StatusCode = 403;
- Logger.LogException(ex);
+ _sourceEntity =
+ await Kernel.Instance.ItemController.GetStudio(QueryString["studio"]).ConfigureAwait(false);
}
- finally
+
+ else if (!string.IsNullOrEmpty(QueryString["userid"]))
{
- _sourceStreamEnsured = true;
+ _sourceEntity = ApiService.GetUserById(QueryString["userid"], false);
+ }
+
+ else
+ {
+ _sourceEntity = ApiService.GetItemById(QueryString["id"]);
}
}
+
+ return _sourceEntity;
}
- public async override Task<string> GetContentType()
+ private async Task<string> DiscoverImagePath()
{
- await EnsureSourceStream().ConfigureAwait(false);
+ var entity = await GetSourceEntity().ConfigureAwait(false);
+
+ return ImageProcessor.GetImagePath(entity, ImageType, ImageIndex);
+ }
+
+ protected async override Task<ResponseInfo> GetResponseInfo()
+ {
+ string path = await GetImagePath().ConfigureAwait(false);
+
+ ResponseInfo info = new ResponseInfo
+ {
+ CacheDuration = TimeSpan.FromDays(365),
+ ContentType = MimeTypes.GetMimeType(path)
+ };
- if (await GetSourceStream().ConfigureAwait(false) == null)
+ DateTime? date = File.GetLastWriteTimeUtc(path);
+
+ // If the file does not exist it will return jan 1, 1601
+ // http://msdn.microsoft.com/en-us/library/system.io.file.getlastwritetimeutc.aspx
+ if (date.Value.Year == 1601)
{
- return null;
+ if (!File.Exists(path))
+ {
+ info.StatusCode = 404;
+ date = null;
+ }
}
- return MimeTypes.GetMimeType(await GetImagePath().ConfigureAwait(false));
+ info.DateLastModified = date;
+
+ return info;
}
- public override TimeSpan CacheDuration
+ private int ImageIndex
{
get
{
- return TimeSpan.FromDays(365);
- }
- }
+ string val = QueryString["index"];
- protected async override Task<DateTime?> GetLastDateModified()
- {
- await EnsureSourceStream().ConfigureAwait(false);
+ if (string.IsNullOrEmpty(val))
+ {
+ return 0;
+ }
- if (await GetSourceStream().ConfigureAwait(false) == null)
- {
- return null;
+ return int.Parse(val);
}
-
- return File.GetLastWriteTimeUtc(await GetImagePath().ConfigureAwait(false));
}
private int? Height
@@ -236,33 +216,9 @@ namespace MediaBrowser.Api.HttpHandlers
protected override async Task WriteResponseToOutputStream(Stream stream)
{
- ImageProcessor.ProcessImage(await GetSourceStream().ConfigureAwait(false), stream, Width, Height, MaxWidth, MaxHeight, Quality);
- }
-
- private string GetImagePathFromTypes(BaseItem item, ImageType imageType, int imageIndex)
- {
- if (imageType == ImageType.Logo)
- {
- return item.LogoImagePath;
- }
- if (imageType == ImageType.Backdrop)
- {
- return item.BackdropImagePaths.ElementAt(imageIndex);
- }
- if (imageType == ImageType.Banner)
- {
- return item.BannerImagePath;
- }
- if (imageType == ImageType.Art)
- {
- return item.ArtImagePath;
- }
- if (imageType == ImageType.Thumbnail)
- {
- return item.ThumbnailImagePath;
- }
+ var entity = await GetSourceEntity().ConfigureAwait(false);
- return item.PrimaryImagePath;
+ ImageProcessor.ProcessImage(entity, ImageType, ImageIndex, stream, Width, Height, MaxWidth, MaxHeight, Quality);
}
}
}
diff --git a/MediaBrowser.Api/HttpHandlers/PluginAssemblyHandler.cs b/MediaBrowser.Api/HttpHandlers/PluginAssemblyHandler.cs
index 88161c1140..47f08c8c32 100644
--- a/MediaBrowser.Api/HttpHandlers/PluginAssemblyHandler.cs
+++ b/MediaBrowser.Api/HttpHandlers/PluginAssemblyHandler.cs
@@ -15,8 +15,8 @@ namespace MediaBrowser.Api.HttpHandlers
{
return ApiService.IsApiUrlMatch("pluginassembly", request);
}
-
- public override Task<string> GetContentType()
+
+ protected override Task<ResponseInfo> GetResponseInfo()
{
throw new NotImplementedException();
}
diff --git a/MediaBrowser.Api/HttpHandlers/PluginConfigurationHandler.cs b/MediaBrowser.Api/HttpHandlers/PluginConfigurationHandler.cs
index 95af9a3442..dc363956fd 100644
--- a/MediaBrowser.Api/HttpHandlers/PluginConfigurationHandler.cs
+++ b/MediaBrowser.Api/HttpHandlers/PluginConfigurationHandler.cs
@@ -17,7 +17,7 @@ namespace MediaBrowser.Api.HttpHandlers
{
return ApiService.IsApiUrlMatch("pluginconfiguration", request);
}
-
+
private BasePlugin _plugin;
private BasePlugin Plugin
{
@@ -39,18 +39,15 @@ namespace MediaBrowser.Api.HttpHandlers
return Task.FromResult(Plugin.Configuration);
}
- public override TimeSpan CacheDuration
+ protected override async Task<ResponseInfo> GetResponseInfo()
{
- get
- {
- return TimeSpan.FromDays(7);
- }
- }
+ var info = await base.GetResponseInfo().ConfigureAwait(false);
- protected override Task<DateTime?> GetLastDateModified()
- {
- return Task.FromResult<DateTime?>(Plugin.ConfigurationDateLastModified);
- }
+ info.DateLastModified = Plugin.ConfigurationDateLastModified;
+ info.CacheDuration = TimeSpan.FromDays(7);
+
+ return info;
+ }
}
}
diff --git a/MediaBrowser.Api/HttpHandlers/ServerConfigurationHandler.cs b/MediaBrowser.Api/HttpHandlers/ServerConfigurationHandler.cs
index 64ba44ec2f..48c6761b16 100644
--- a/MediaBrowser.Api/HttpHandlers/ServerConfigurationHandler.cs
+++ b/MediaBrowser.Api/HttpHandlers/ServerConfigurationHandler.cs
@@ -16,23 +16,22 @@ namespace MediaBrowser.Api.HttpHandlers
{
return ApiService.IsApiUrlMatch("serverconfiguration", request);
}
-
+
protected override Task<ServerConfiguration> GetObjectToSerialize()
{
return Task.FromResult(Kernel.Instance.Configuration);
}
- public override TimeSpan CacheDuration
+ protected override async Task<ResponseInfo> GetResponseInfo()
{
- get
- {
- return TimeSpan.FromDays(7);
- }
- }
+ var info = await base.GetResponseInfo().ConfigureAwait(false);
- protected override Task<DateTime?> GetLastDateModified()
- {
- return Task.FromResult<DateTime?>(File.GetLastWriteTimeUtc(Kernel.Instance.ApplicationPaths.SystemConfigurationFilePath));
+ info.DateLastModified =
+ File.GetLastWriteTimeUtc(Kernel.Instance.ApplicationPaths.SystemConfigurationFilePath);
+
+ info.CacheDuration = TimeSpan.FromDays(7);
+
+ return info;
}
}
}
diff --git a/MediaBrowser.Api/HttpHandlers/VideoHandler.cs b/MediaBrowser.Api/HttpHandlers/VideoHandler.cs
index 9d52136f0a..e34a1b41f7 100644
--- a/MediaBrowser.Api/HttpHandlers/VideoHandler.cs
+++ b/MediaBrowser.Api/HttpHandlers/VideoHandler.cs
@@ -1,5 +1,5 @@
-using MediaBrowser.Common.Drawing;
-using MediaBrowser.Common.Net.Handlers;
+using MediaBrowser.Common.Net.Handlers;
+using MediaBrowser.Controller.Drawing;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Model.DTO;
using MediaBrowser.Model.Entities;
@@ -7,7 +7,6 @@ using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Drawing;
-using System.IO;
using System.Linq;
using System.Net;
diff --git a/MediaBrowser.Api/HttpHandlers/WeatherHandler.cs b/MediaBrowser.Api/HttpHandlers/WeatherHandler.cs
index 93d4c8877e..378e89067d 100644
--- a/MediaBrowser.Api/HttpHandlers/WeatherHandler.cs
+++ b/MediaBrowser.Api/HttpHandlers/WeatherHandler.cs
@@ -3,6 +3,7 @@ using MediaBrowser.Controller;
using MediaBrowser.Model.Weather;
using System;
using System.ComponentModel.Composition;
+using System.Linq;
using System.Net;
using System.Threading.Tasks;
@@ -15,7 +16,7 @@ namespace MediaBrowser.Api.HttpHandlers
{
return ApiService.IsApiUrlMatch("weather", request);
}
-
+
protected override Task<WeatherInfo> GetObjectToSerialize()
{
// If a specific zip code was requested on the query string, use that. Otherwise use the value from configuration
@@ -27,18 +28,16 @@ namespace MediaBrowser.Api.HttpHandlers
zipCode = Kernel.Instance.Configuration.WeatherZipCode;
}
- return Kernel.Instance.WeatherClient.GetWeatherInfoAsync(zipCode);
+ return Kernel.Instance.WeatherProviders.First().GetWeatherInfoAsync(zipCode);
}
- /// <summary>
- /// Tell the client to cache the weather info for 15 minutes
- /// </summary>
- public override TimeSpan CacheDuration
+ protected override async Task<ResponseInfo> GetResponseInfo()
{
- get
- {
- return TimeSpan.FromMinutes(15);
- }
+ var info = await base.GetResponseInfo().ConfigureAwait(false);
+
+ info.CacheDuration = TimeSpan.FromMinutes(15);
+
+ return info;
}
}
}