mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-07-03 12:52:56 +01:00
Moved discovery of loggers and weather providers to MEF. Also added support for third-party image processors, also discovered through MEF.
This commit is contained in:
parent
01a25c48a0
commit
8b7effd6ff
@@ -2,6 +2,7 @@
|
||||
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;
|
||||
@@ -20,7 +21,7 @@ namespace MediaBrowser.Api.HttpHandlers
|
||||
{
|
||||
return ApiService.IsApiUrlMatch("image", request);
|
||||
}
|
||||
|
||||
|
||||
private string _imagePath;
|
||||
private async Task<string> GetImagePath()
|
||||
{
|
||||
@@ -29,49 +30,57 @@ namespace MediaBrowser.Api.HttpHandlers
|
||||
return _imagePath;
|
||||
}
|
||||
|
||||
private BaseEntity _sourceEntity;
|
||||
private async Task<BaseEntity> GetSourceEntity()
|
||||
{
|
||||
if (_sourceEntity == null)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(QueryString["personname"]))
|
||||
{
|
||||
_sourceEntity = await Kernel.Instance.ItemController.GetPerson(QueryString["personname"]).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
else if (!string.IsNullOrEmpty(QueryString["genre"]))
|
||||
{
|
||||
_sourceEntity = await Kernel.Instance.ItemController.GetGenre(QueryString["genre"]).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
else if (!string.IsNullOrEmpty(QueryString["year"]))
|
||||
{
|
||||
_sourceEntity = await Kernel.Instance.ItemController.GetYear(int.Parse(QueryString["year"])).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
else if (!string.IsNullOrEmpty(QueryString["studio"]))
|
||||
{
|
||||
_sourceEntity = await Kernel.Instance.ItemController.GetStudio(QueryString["studio"]).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
else if (!string.IsNullOrEmpty(QueryString["userid"]))
|
||||
{
|
||||
_sourceEntity = ApiService.GetUserById(QueryString["userid"], false);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
_sourceEntity = ApiService.GetItemById(QueryString["id"]);
|
||||
}
|
||||
}
|
||||
|
||||
return _sourceEntity;
|
||||
}
|
||||
|
||||
private async Task<string> DiscoverImagePath()
|
||||
{
|
||||
string personName = QueryString["personname"];
|
||||
var entity = await GetSourceEntity().ConfigureAwait(false);
|
||||
|
||||
if (!string.IsNullOrEmpty(personName))
|
||||
var item = entity as BaseItem;
|
||||
|
||||
if (item != null)
|
||||
{
|
||||
return (await Kernel.Instance.ItemController.GetPerson(personName).ConfigureAwait(false)).PrimaryImagePath;
|
||||
return GetImagePathFromTypes(item, ImageType, ImageIndex);
|
||||
}
|
||||
|
||||
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);
|
||||
return entity.PrimaryImagePath;
|
||||
}
|
||||
|
||||
private Stream _sourceStream;
|
||||
@@ -114,8 +123,6 @@ namespace MediaBrowser.Api.HttpHandlers
|
||||
|
||||
public async override Task<string> GetContentType()
|
||||
{
|
||||
await EnsureSourceStream().ConfigureAwait(false);
|
||||
|
||||
if (await GetSourceStream().ConfigureAwait(false) == null)
|
||||
{
|
||||
return null;
|
||||
@@ -134,8 +141,6 @@ namespace MediaBrowser.Api.HttpHandlers
|
||||
|
||||
protected async override Task<DateTime?> GetLastDateModified()
|
||||
{
|
||||
await EnsureSourceStream().ConfigureAwait(false);
|
||||
|
||||
if (await GetSourceStream().ConfigureAwait(false) == null)
|
||||
{
|
||||
return null;
|
||||
@@ -144,6 +149,21 @@ namespace MediaBrowser.Api.HttpHandlers
|
||||
return File.GetLastWriteTimeUtc(await GetImagePath().ConfigureAwait(false));
|
||||
}
|
||||
|
||||
private int ImageIndex
|
||||
{
|
||||
get
|
||||
{
|
||||
string val = QueryString["index"];
|
||||
|
||||
if (string.IsNullOrEmpty(val))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return int.Parse(val);
|
||||
}
|
||||
}
|
||||
|
||||
private int? Height
|
||||
{
|
||||
get
|
||||
@@ -236,7 +256,11 @@ namespace MediaBrowser.Api.HttpHandlers
|
||||
|
||||
protected override async Task WriteResponseToOutputStream(Stream stream)
|
||||
{
|
||||
ImageProcessor.ProcessImage(await GetSourceStream().ConfigureAwait(false), stream, Width, Height, MaxWidth, MaxHeight, Quality);
|
||||
Stream sourceStream = await GetSourceStream().ConfigureAwait(false);
|
||||
|
||||
var entity = await GetSourceEntity().ConfigureAwait(false);
|
||||
|
||||
ImageProcessor.ProcessImage(sourceStream, stream, Width, Height, MaxWidth, MaxHeight, Quality, entity, ImageType, ImageIndex);
|
||||
}
|
||||
|
||||
private string GetImagePathFromTypes(BaseItem item, ImageType imageType, int imageIndex)
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -27,7 +28,7 @@ namespace MediaBrowser.Api.HttpHandlers
|
||||
zipCode = Kernel.Instance.Configuration.WeatherZipCode;
|
||||
}
|
||||
|
||||
return Kernel.Instance.WeatherClient.GetWeatherInfoAsync(zipCode);
|
||||
return Kernel.Instance.WeatherProviders.First().GetWeatherInfoAsync(zipCode);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -1,104 +0,0 @@
|
||||
using MediaBrowser.Common.Drawing;
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Drawing2D;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
|
||||
namespace MediaBrowser.Api
|
||||
{
|
||||
public static class ImageProcessor
|
||||
{
|
||||
/// <summary>
|
||||
/// Resizes an image from a source stream and saves the result to an output stream
|
||||
/// </summary>
|
||||
/// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
|
||||
/// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
|
||||
/// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
|
||||
/// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
|
||||
/// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
|
||||
public static void ProcessImage(Stream sourceImageStream, Stream toStream, int? width, int? height, int? maxWidth, int? maxHeight, int? quality)
|
||||
{
|
||||
Image originalImage = Image.FromStream(sourceImageStream);
|
||||
|
||||
Size newSize = DrawingUtils.Resize(originalImage.Size, width, height, maxWidth, maxHeight);
|
||||
|
||||
Bitmap thumbnail;
|
||||
|
||||
// Graphics.FromImage will throw an exception if the PixelFormat is Indexed, so we need to handle that here
|
||||
if (originalImage.PixelFormat.HasFlag(PixelFormat.Indexed))
|
||||
{
|
||||
thumbnail = new Bitmap(originalImage, newSize.Width, newSize.Height);
|
||||
}
|
||||
else
|
||||
{
|
||||
thumbnail = new Bitmap(newSize.Width, newSize.Height, originalImage.PixelFormat);
|
||||
}
|
||||
|
||||
thumbnail.SetResolution(originalImage.HorizontalResolution, originalImage.VerticalResolution);
|
||||
|
||||
Graphics thumbnailGraph = Graphics.FromImage(thumbnail);
|
||||
|
||||
thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
|
||||
thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
|
||||
thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
||||
thumbnailGraph.PixelOffsetMode = PixelOffsetMode.HighQuality;
|
||||
thumbnailGraph.CompositingMode = CompositingMode.SourceOver;
|
||||
|
||||
thumbnailGraph.DrawImage(originalImage, 0, 0, newSize.Width, newSize.Height);
|
||||
|
||||
Write(originalImage, thumbnail, toStream, quality);
|
||||
|
||||
thumbnailGraph.Dispose();
|
||||
thumbnail.Dispose();
|
||||
originalImage.Dispose();
|
||||
}
|
||||
|
||||
private static void Write(Image originalImage, Image newImage, Stream toStream, int? quality)
|
||||
{
|
||||
// Use special save methods for jpeg and png that will result in a much higher quality image
|
||||
// All other formats use the generic Image.Save
|
||||
if (ImageFormat.Jpeg.Equals(originalImage.RawFormat))
|
||||
{
|
||||
SaveJpeg(newImage, toStream, quality);
|
||||
}
|
||||
else if (ImageFormat.Png.Equals(originalImage.RawFormat))
|
||||
{
|
||||
newImage.Save(toStream, ImageFormat.Png);
|
||||
}
|
||||
else
|
||||
{
|
||||
newImage.Save(toStream, originalImage.RawFormat);
|
||||
}
|
||||
}
|
||||
|
||||
private static void SaveJpeg(Image newImage, Stream target, int? quality)
|
||||
{
|
||||
if (!quality.HasValue)
|
||||
{
|
||||
quality = 90;
|
||||
}
|
||||
|
||||
using (var encoderParameters = new EncoderParameters(1))
|
||||
{
|
||||
encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, quality.Value);
|
||||
newImage.Save(target, GetImageCodeInfo("image/jpeg"), encoderParameters);
|
||||
}
|
||||
}
|
||||
|
||||
private static ImageCodecInfo GetImageCodeInfo(string mimeType)
|
||||
{
|
||||
ImageCodecInfo[] info = ImageCodecInfo.GetImageEncoders();
|
||||
|
||||
for (int i = 0; i < info.Length; i++)
|
||||
{
|
||||
ImageCodecInfo ici = info[i];
|
||||
if (ici.MimeType.Equals(mimeType, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return ici;
|
||||
}
|
||||
}
|
||||
return info[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -83,7 +83,6 @@
|
||||
<Compile Include="HttpHandlers\WeatherHandler.cs" />
|
||||
<Compile Include="HttpHandlers\YearHandler.cs" />
|
||||
<Compile Include="HttpHandlers\YearsHandler.cs" />
|
||||
<Compile Include="ImageProcessor.cs" />
|
||||
<Compile Include="Plugin.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using MediaBrowser.Common.Plugins;
|
||||
using MediaBrowser.Model.Plugins;
|
||||
using System.ComponentModel.Composition;
|
||||
|
||||
namespace MediaBrowser.Api
|
||||
|
||||
Reference in New Issue
Block a user