diff options
| author | Luke Pulverenti <luke.pulverenti@gmail.com> | 2013-09-17 22:43:34 -0400 |
|---|---|---|
| committer | Luke Pulverenti <luke.pulverenti@gmail.com> | 2013-09-17 22:43:34 -0400 |
| commit | 60780399c51165a06a04d8a01d59252bc9c82d7f (patch) | |
| tree | 53edb713a4d311234b21eebfa091a6c2bf382db5 /MediaBrowser.Controller/Drawing | |
| parent | 06c611dd50d37b786a957cea7308fb30e926a919 (diff) | |
allow request header overrides
Diffstat (limited to 'MediaBrowser.Controller/Drawing')
| -rw-r--r-- | MediaBrowser.Controller/Drawing/ImageExtensions.cs | 11 | ||||
| -rw-r--r-- | MediaBrowser.Controller/Drawing/ImageHeader.cs | 24 |
2 files changed, 23 insertions, 12 deletions
diff --git a/MediaBrowser.Controller/Drawing/ImageExtensions.cs b/MediaBrowser.Controller/Drawing/ImageExtensions.cs index 3d3b947a0..92b2ec835 100644 --- a/MediaBrowser.Controller/Drawing/ImageExtensions.cs +++ b/MediaBrowser.Controller/Drawing/ImageExtensions.cs @@ -3,7 +3,6 @@ using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.IO; -using System.Linq; namespace MediaBrowser.Controller.Drawing { @@ -61,7 +60,15 @@ namespace MediaBrowser.Controller.Drawing /// <returns>ImageCodecInfo.</returns> private static ImageCodecInfo GetImageCodecInfo(string mimeType) { - return Encoders.FirstOrDefault(i => i.MimeType.Equals(mimeType, StringComparison.OrdinalIgnoreCase)) ?? Encoders.FirstOrDefault(); + foreach (var encoder in Encoders) + { + if (string.Equals(encoder.MimeType, mimeType, StringComparison.OrdinalIgnoreCase)) + { + return encoder; + } + } + + return Encoders.Length == 0 ? null : Encoders[0]; } /// <summary> diff --git a/MediaBrowser.Controller/Drawing/ImageHeader.cs b/MediaBrowser.Controller/Drawing/ImageHeader.cs index 11072ff0c..95a753f00 100644 --- a/MediaBrowser.Controller/Drawing/ImageHeader.cs +++ b/MediaBrowser.Controller/Drawing/ImageHeader.cs @@ -18,19 +18,22 @@ namespace MediaBrowser.Controller.Drawing /// <summary> /// The error message /// </summary> - const string errorMessage = "Could not recognize image format."; + const string ErrorMessage = "Could not recognize image format."; /// <summary> /// The image format decoders /// </summary> - private static readonly Dictionary<byte[], Func<BinaryReader, Size>> imageFormatDecoders = new Dictionary<byte[], Func<BinaryReader, Size>> + private static readonly KeyValuePair<byte[], Func<BinaryReader, Size>>[] ImageFormatDecoders = new Dictionary<byte[], Func<BinaryReader, Size>> { { new byte[] { 0x42, 0x4D }, DecodeBitmap }, { new byte[] { 0x47, 0x49, 0x46, 0x38, 0x37, 0x61 }, DecodeGif }, { new byte[] { 0x47, 0x49, 0x46, 0x38, 0x39, 0x61 }, DecodeGif }, { new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }, DecodePng }, - { new byte[] { 0xff, 0xd8 }, DecodeJfif }, - }; + { new byte[] { 0xff, 0xd8 }, DecodeJfif } + + }.ToArray(); + + private static readonly int MaxMagicBytesLength = ImageFormatDecoders.Select(i => i.Key.Length).OrderByDescending(i => i).First(); /// <summary> /// Gets the dimensions of an image. @@ -81,12 +84,13 @@ namespace MediaBrowser.Controller.Drawing /// <exception cref="ArgumentException">The image was of an unrecognized format.</exception> private static Size GetDimensions(BinaryReader binaryReader) { - int maxMagicBytesLength = imageFormatDecoders.Keys.OrderByDescending(x => x.Length).First().Length; - var magicBytes = new byte[maxMagicBytesLength]; - for (int i = 0; i < maxMagicBytesLength; i += 1) + var magicBytes = new byte[MaxMagicBytesLength]; + + for (var i = 0; i < MaxMagicBytesLength; i += 1) { magicBytes[i] = binaryReader.ReadByte(); - foreach (var kvPair in imageFormatDecoders) + + foreach (var kvPair in ImageFormatDecoders) { if (StartsWith(magicBytes, kvPair.Key)) { @@ -95,7 +99,7 @@ namespace MediaBrowser.Controller.Drawing } } - throw new ArgumentException(errorMessage, "binaryReader"); + throw new ArgumentException(ErrorMessage, "binaryReader"); } /// <summary> @@ -217,7 +221,7 @@ namespace MediaBrowser.Controller.Drawing } } - throw new ArgumentException(errorMessage); + throw new ArgumentException(ErrorMessage); } } } |
