diff options
Diffstat (limited to 'MediaBrowser.Controller')
| -rw-r--r-- | MediaBrowser.Controller/Entities/Audio/Audio.cs | 2 | ||||
| -rw-r--r-- | MediaBrowser.Controller/LiveTv/ILiveTvManager.cs | 12 | ||||
| -rw-r--r-- | MediaBrowser.Controller/LiveTv/ILiveTvService.cs | 30 | ||||
| -rw-r--r-- | MediaBrowser.Controller/LiveTv/LiveTvProgram.cs | 21 | ||||
| -rw-r--r-- | MediaBrowser.Controller/LiveTv/RecordingInfo.cs | 6 | ||||
| -rw-r--r-- | MediaBrowser.Controller/MediaBrowser.Controller.csproj | 3 | ||||
| -rw-r--r-- | MediaBrowser.Controller/MediaInfo/FFMpegManager.cs | 3 | ||||
| -rw-r--r-- | MediaBrowser.Controller/MediaInfo/IMediaEncoder.cs | 106 | ||||
| -rw-r--r-- | MediaBrowser.Controller/MediaInfo/InternalMediaInfoResult.cs | 311 | ||||
| -rw-r--r-- | MediaBrowser.Controller/MediaInfo/MediaEncoderHelpers.cs | 21 | ||||
| -rw-r--r-- | MediaBrowser.Controller/Sorting/SortExtensions.cs | 143 |
11 files changed, 638 insertions, 20 deletions
diff --git a/MediaBrowser.Controller/Entities/Audio/Audio.cs b/MediaBrowser.Controller/Entities/Audio/Audio.cs index 028fc964d..9e4129cd1 100644 --- a/MediaBrowser.Controller/Entities/Audio/Audio.cs +++ b/MediaBrowser.Controller/Entities/Audio/Audio.cs @@ -109,7 +109,7 @@ namespace MediaBrowser.Controller.Entities.Audio /// <returns>System.String.</returns> public override string GetUserDataKey() { - var parent = Parent as MusicAlbum; + var parent = FindParent<MusicAlbum>(); if (parent != null) { diff --git a/MediaBrowser.Controller/LiveTv/ILiveTvManager.cs b/MediaBrowser.Controller/LiveTv/ILiveTvManager.cs index d23c8c555..31c336932 100644 --- a/MediaBrowser.Controller/LiveTv/ILiveTvManager.cs +++ b/MediaBrowser.Controller/LiveTv/ILiveTvManager.cs @@ -1,5 +1,4 @@ -using System.IO; -using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Entities; using MediaBrowser.Model.LiveTv; using MediaBrowser.Model.Querying; using System.Collections.Generic; @@ -241,5 +240,14 @@ namespace MediaBrowser.Controller.LiveTv /// </summary> /// <returns>GuideInfo.</returns> GuideInfo GetGuideInfo(); + + /// <summary> + /// Gets the recommended programs. + /// </summary> + /// <param name="query">The query.</param> + /// <param name="cancellationToken">The cancellation token.</param> + /// <returns>Task{QueryResult{ProgramInfoDto}}.</returns> + Task<QueryResult<ProgramInfoDto>> GetRecommendedPrograms(RecommendedProgramQuery query, + CancellationToken cancellationToken); } } diff --git a/MediaBrowser.Controller/LiveTv/ILiveTvService.cs b/MediaBrowser.Controller/LiveTv/ILiveTvService.cs index 1e535139c..004f0b452 100644 --- a/MediaBrowser.Controller/LiveTv/ILiveTvService.cs +++ b/MediaBrowser.Controller/LiveTv/ILiveTvService.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; @@ -37,7 +38,7 @@ namespace MediaBrowser.Controller.LiveTv /// <param name="cancellationToken">The cancellation token.</param> /// <returns>Task.</returns> Task CancelSeriesTimerAsync(string timerId, CancellationToken cancellationToken); - + /// <summary> /// Deletes the recording asynchronous. /// </summary> @@ -77,7 +78,7 @@ namespace MediaBrowser.Controller.LiveTv /// <param name="cancellationToken">The cancellation token.</param> /// <returns>Task.</returns> Task UpdateSeriesTimerAsync(SeriesTimerInfo info, CancellationToken cancellationToken); - + /// <summary> /// Gets the channel image asynchronous. This only needs to be implemented if an image path or url cannot be supplied to ChannelInfo /// </summary> @@ -102,7 +103,7 @@ namespace MediaBrowser.Controller.LiveTv /// <param name="cancellationToken">The cancellation token.</param> /// <returns>Task{ImageResponseInfo}.</returns> Task<StreamResponseInfo> GetProgramImageAsync(string programId, string channelId, CancellationToken cancellationToken); - + /// <summary> /// Gets the recordings asynchronous. /// </summary> @@ -118,11 +119,12 @@ namespace MediaBrowser.Controller.LiveTv Task<IEnumerable<TimerInfo>> GetTimersAsync(CancellationToken cancellationToken); /// <summary> - /// Gets the timer defaults asynchronous. + /// Gets the new timer defaults asynchronous. /// </summary> /// <param name="cancellationToken">The cancellation token.</param> - /// <returns>Task{TimerInfo}.</returns> - Task<SeriesTimerInfo> GetNewTimerDefaultsAsync(CancellationToken cancellationToken); + /// <param name="program">The program.</param> + /// <returns>Task{SeriesTimerInfo}.</returns> + Task<SeriesTimerInfo> GetNewTimerDefaultsAsync(CancellationToken cancellationToken, ProgramInfo program = null); /// <summary> /// Gets the series timers asynchronous. @@ -130,14 +132,16 @@ namespace MediaBrowser.Controller.LiveTv /// <param name="cancellationToken">The cancellation token.</param> /// <returns>Task{IEnumerable{SeriesTimerInfo}}.</returns> Task<IEnumerable<SeriesTimerInfo>> GetSeriesTimersAsync(CancellationToken cancellationToken); - + /// <summary> /// Gets the programs asynchronous. /// </summary> /// <param name="channelId">The channel identifier.</param> + /// <param name="startDateUtc">The start date UTC.</param> + /// <param name="endDateUtc">The end date UTC.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>Task{IEnumerable{ProgramInfo}}.</returns> - Task<IEnumerable<ProgramInfo>> GetProgramsAsync(string channelId, CancellationToken cancellationToken); + Task<IEnumerable<ProgramInfo>> GetProgramsAsync(string channelId, DateTime startDateUtc, DateTime endDateUtc, CancellationToken cancellationToken); /// <summary> /// Gets the recording stream. @@ -162,5 +166,13 @@ namespace MediaBrowser.Controller.LiveTv /// <param name="cancellationToken">The cancellation token.</param> /// <returns>Task.</returns> Task CloseLiveStream(string id, CancellationToken cancellationToken); + + /// <summary> + /// Records the live stream. + /// </summary> + /// <param name="id">The identifier.</param> + /// <param name="cancellationToken">The cancellation token.</param> + /// <returns>Task.</returns> + Task RecordLiveStream(string id, CancellationToken cancellationToken); } } diff --git a/MediaBrowser.Controller/LiveTv/LiveTvProgram.cs b/MediaBrowser.Controller/LiveTv/LiveTvProgram.cs index abacc0c18..aceb32885 100644 --- a/MediaBrowser.Controller/LiveTv/LiveTvProgram.cs +++ b/MediaBrowser.Controller/LiveTv/LiveTvProgram.cs @@ -1,5 +1,6 @@ using MediaBrowser.Controller.Entities; using MediaBrowser.Model.LiveTv; +using System; namespace MediaBrowser.Controller.LiveTv { @@ -28,6 +29,26 @@ namespace MediaBrowser.Controller.LiveTv } } + public bool IsAiring + { + get + { + var now = DateTime.UtcNow; + + return now >= ProgramInfo.StartDate && now < ProgramInfo.EndDate; + } + } + + public bool HasAired + { + get + { + var now = DateTime.UtcNow; + + return now >= ProgramInfo.EndDate; + } + } + public override string GetClientTypeName() { return "Program"; diff --git a/MediaBrowser.Controller/LiveTv/RecordingInfo.cs b/MediaBrowser.Controller/LiveTv/RecordingInfo.cs index 6a0d135c8..bf453ccf4 100644 --- a/MediaBrowser.Controller/LiveTv/RecordingInfo.cs +++ b/MediaBrowser.Controller/LiveTv/RecordingInfo.cs @@ -103,6 +103,12 @@ namespace MediaBrowser.Controller.LiveTv public ProgramAudio? Audio { get; set; } /// <summary> + /// Gets or sets the original air date. + /// </summary> + /// <value>The original air date.</value> + public DateTime? OriginalAirDate { get; set; } + + /// <summary> /// Gets or sets a value indicating whether this instance is movie. /// </summary> /// <value><c>true</c> if this instance is movie; otherwise, <c>false</c>.</value> diff --git a/MediaBrowser.Controller/MediaBrowser.Controller.csproj b/MediaBrowser.Controller/MediaBrowser.Controller.csproj index 61de32e41..14205e668 100644 --- a/MediaBrowser.Controller/MediaBrowser.Controller.csproj +++ b/MediaBrowser.Controller/MediaBrowser.Controller.csproj @@ -125,6 +125,8 @@ <Compile Include="LiveTv\SeriesTimerInfo.cs" /> <Compile Include="LiveTv\TimerInfo.cs" /> <Compile Include="Localization\ILocalizationManager.cs" /> + <Compile Include="MediaInfo\IMediaEncoder.cs" /> + <Compile Include="MediaInfo\InternalMediaInfoResult.cs" /> <Compile Include="Net\IHasResultFactory.cs" /> <Compile Include="Net\IHttpResultFactory.cs" /> <Compile Include="Net\IHttpServer.cs" /> @@ -208,6 +210,7 @@ <Compile Include="Sorting\IBaseItemComparer.cs" /> <Compile Include="Sorting\IUserBaseItemComparer.cs" /> <Compile Include="Providers\BaseItemXmlParser.cs" /> + <Compile Include="Sorting\SortExtensions.cs" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\MediaBrowser.Common\MediaBrowser.Common.csproj"> diff --git a/MediaBrowser.Controller/MediaInfo/FFMpegManager.cs b/MediaBrowser.Controller/MediaInfo/FFMpegManager.cs index 644222949..c1951038c 100644 --- a/MediaBrowser.Controller/MediaInfo/FFMpegManager.cs +++ b/MediaBrowser.Controller/MediaInfo/FFMpegManager.cs @@ -1,6 +1,5 @@ using MediaBrowser.Common.Extensions; using MediaBrowser.Common.IO; -using MediaBrowser.Common.MediaInfo; using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities.Movies; @@ -178,7 +177,7 @@ namespace MediaBrowser.Controller.MediaInfo Directory.CreateDirectory(parentPath); - await _encoder.ExtractImage(inputPath, type, video.Video3DFormat, time, path, cancellationToken).ConfigureAwait(false); + await _encoder.ExtractImage(inputPath, type, false, video.Video3DFormat, time, path, cancellationToken).ConfigureAwait(false); chapter.ImagePath = path; changesMade = true; } diff --git a/MediaBrowser.Controller/MediaInfo/IMediaEncoder.cs b/MediaBrowser.Controller/MediaInfo/IMediaEncoder.cs new file mode 100644 index 000000000..8e0d696c9 --- /dev/null +++ b/MediaBrowser.Controller/MediaInfo/IMediaEncoder.cs @@ -0,0 +1,106 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using MediaBrowser.Model.Entities; + +namespace MediaBrowser.Controller.MediaInfo +{ + /// <summary> + /// Interface IMediaEncoder + /// </summary> + public interface IMediaEncoder + { + /// <summary> + /// Gets the encoder path. + /// </summary> + /// <value>The encoder path.</value> + string EncoderPath { get; } + + /// <summary> + /// Gets the version. + /// </summary> + /// <value>The version.</value> + string Version { get; } + + /// <summary> + /// Extracts the image. + /// </summary> + /// <param name="inputFiles">The input files.</param> + /// <param name="type">The type.</param> + /// <param name="isAudio">if set to <c>true</c> [is audio].</param> + /// <param name="threedFormat">The threed format.</param> + /// <param name="offset">The offset.</param> + /// <param name="outputPath">The output path.</param> + /// <param name="cancellationToken">The cancellation token.</param> + /// <returns>Task.</returns> + Task ExtractImage(string[] inputFiles, InputType type, bool isAudio, Video3DFormat? threedFormat, TimeSpan? offset, string outputPath, CancellationToken cancellationToken); + + /// <summary> + /// Extracts the text subtitle. + /// </summary> + /// <param name="inputFiles">The input files.</param> + /// <param name="type">The type.</param> + /// <param name="subtitleStreamIndex">Index of the subtitle stream.</param> + /// <param name="outputPath">The output path.</param> + /// <param name="cancellationToken">The cancellation token.</param> + /// <returns>Task.</returns> + Task ExtractTextSubtitle(string[] inputFiles, InputType type, int subtitleStreamIndex, string outputPath, CancellationToken cancellationToken); + + /// <summary> + /// Converts the text subtitle to ass. + /// </summary> + /// <param name="inputPath">The input path.</param> + /// <param name="outputPath">The output path.</param> + /// <param name="language">The language.</param> + /// <param name="cancellationToken">The cancellation token.</param> + /// <returns>Task.</returns> + Task ConvertTextSubtitleToAss(string inputPath, string outputPath, string language, CancellationToken cancellationToken); + + /// <summary> + /// Gets the media info. + /// </summary> + /// <param name="inputFiles">The input files.</param> + /// <param name="type">The type.</param> + /// <param name="cancellationToken">The cancellation token.</param> + /// <returns>Task.</returns> + Task<InternalMediaInfoResult> GetMediaInfo(string[] inputFiles, InputType type, CancellationToken cancellationToken); + + /// <summary> + /// Gets the probe size argument. + /// </summary> + /// <param name="type">The type.</param> + /// <returns>System.String.</returns> + string GetProbeSizeArgument(InputType type); + + /// <summary> + /// Gets the input argument. + /// </summary> + /// <param name="inputFiles">The input files.</param> + /// <param name="type">The type.</param> + /// <returns>System.String.</returns> + string GetInputArgument(string[] inputFiles, InputType type); + } + + /// <summary> + /// Enum InputType + /// </summary> + public enum InputType + { + /// <summary> + /// The file + /// </summary> + File, + /// <summary> + /// The bluray + /// </summary> + Bluray, + /// <summary> + /// The DVD + /// </summary> + Dvd, + /// <summary> + /// The URL + /// </summary> + Url + } +} diff --git a/MediaBrowser.Controller/MediaInfo/InternalMediaInfoResult.cs b/MediaBrowser.Controller/MediaInfo/InternalMediaInfoResult.cs new file mode 100644 index 000000000..3ceec1b90 --- /dev/null +++ b/MediaBrowser.Controller/MediaInfo/InternalMediaInfoResult.cs @@ -0,0 +1,311 @@ +using System.Collections.Generic; +using MediaBrowser.Model.Entities; + +namespace MediaBrowser.Controller.MediaInfo +{ + /// <summary> + /// Class MediaInfoResult + /// </summary> + public class InternalMediaInfoResult + { + /// <summary> + /// Gets or sets the streams. + /// </summary> + /// <value>The streams.</value> + public MediaStreamInfo[] streams { get; set; } + + /// <summary> + /// Gets or sets the format. + /// </summary> + /// <value>The format.</value> + public MediaFormatInfo format { get; set; } + + /// <summary> + /// Gets or sets the chapters. + /// </summary> + /// <value>The chapters.</value> + public List<ChapterInfo> Chapters { get; set; } + } + + /// <summary> + /// Represents a stream within the output + /// </summary> + public class MediaStreamInfo + { + /// <summary> + /// Gets or sets the index. + /// </summary> + /// <value>The index.</value> + public int index { get; set; } + + /// <summary> + /// Gets or sets the profile. + /// </summary> + /// <value>The profile.</value> + public string profile { get; set; } + + /// <summary> + /// Gets or sets the codec_name. + /// </summary> + /// <value>The codec_name.</value> + public string codec_name { get; set; } + + /// <summary> + /// Gets or sets the codec_long_name. + /// </summary> + /// <value>The codec_long_name.</value> + public string codec_long_name { get; set; } + + /// <summary> + /// Gets or sets the codec_type. + /// </summary> + /// <value>The codec_type.</value> + public string codec_type { get; set; } + + /// <summary> + /// Gets or sets the sample_rate. + /// </summary> + /// <value>The sample_rate.</value> + public string sample_rate { get; set; } + + /// <summary> + /// Gets or sets the channels. + /// </summary> + /// <value>The channels.</value> + public int channels { get; set; } + + /// <summary> + /// Gets or sets the channel_layout. + /// </summary> + /// <value>The channel_layout.</value> + public string channel_layout { get; set; } + + /// <summary> + /// Gets or sets the avg_frame_rate. + /// </summary> + /// <value>The avg_frame_rate.</value> + public string avg_frame_rate { get; set; } + + /// <summary> + /// Gets or sets the duration. + /// </summary> + /// <value>The duration.</value> + public string duration { get; set; } + + /// <summary> + /// Gets or sets the bit_rate. + /// </summary> + /// <value>The bit_rate.</value> + public string bit_rate { get; set; } + + /// <summary> + /// Gets or sets the width. + /// </summary> + /// <value>The width.</value> + public int width { get; set; } + + /// <summary> + /// Gets or sets the height. + /// </summary> + /// <value>The height.</value> + public int height { get; set; } + + /// <summary> + /// Gets or sets the display_aspect_ratio. + /// </summary> + /// <value>The display_aspect_ratio.</value> + public string display_aspect_ratio { get; set; } + + /// <summary> + /// Gets or sets the tags. + /// </summary> + /// <value>The tags.</value> + public Dictionary<string, string> tags { get; set; } + + /// <summary> + /// Gets or sets the bits_per_sample. + /// </summary> + /// <value>The bits_per_sample.</value> + public int bits_per_sample { get; set; } + + /// <summary> + /// Gets or sets the r_frame_rate. + /// </summary> + /// <value>The r_frame_rate.</value> + public string r_frame_rate { get; set; } + + /// <summary> + /// Gets or sets the has_b_frames. + /// </summary> + /// <value>The has_b_frames.</value> + public int has_b_frames { get; set; } + + /// <summary> + /// Gets or sets the sample_aspect_ratio. + /// </summary> + /// <value>The sample_aspect_ratio.</value> + public string sample_aspect_ratio { get; set; } + + /// <summary> + /// Gets or sets the pix_fmt. + /// </summary> + /// <value>The pix_fmt.</value> + public string pix_fmt { get; set; } + + /// <summary> + /// Gets or sets the level. + /// </summary> + /// <value>The level.</value> + public int level { get; set; } + + /// <summary> + /// Gets or sets the time_base. + /// </summary> + /// <value>The time_base.</value> + public string time_base { get; set; } + + /// <summary> + /// Gets or sets the start_time. + /// </summary> + /// <value>The start_time.</value> + public string start_time { get; set; } + + /// <summary> + /// Gets or sets the codec_time_base. + /// </summary> + /// <value>The codec_time_base.</value> + public string codec_time_base { get; set; } + + /// <summary> + /// Gets or sets the codec_tag. + /// </summary> + /// <value>The codec_tag.</value> + public string codec_tag { get; set; } + + /// <summary> + /// Gets or sets the codec_tag_string. + /// </summary> + /// <value>The codec_tag_string.</value> + public string codec_tag_string { get; set; } + + /// <summary> + /// Gets or sets the sample_fmt. + /// </summary> + /// <value>The sample_fmt.</value> + public string sample_fmt { get; set; } + + /// <summary> + /// Gets or sets the dmix_mode. + /// </summary> + /// <value>The dmix_mode.</value> + public string dmix_mode { get; set; } + + /// <summary> + /// Gets or sets the start_pts. + /// </summary> + /// <value>The start_pts.</value> + public string start_pts { get; set; } + + /// <summary> + /// Gets or sets the is_avc. + /// </summary> + /// <value>The is_avc.</value> + public string is_avc { get; set; } + + /// <summary> + /// Gets or sets the nal_length_size. + /// </summary> + /// <value>The nal_length_size.</value> + public string nal_length_size { get; set; } + + /// <summary> + /// Gets or sets the ltrt_cmixlev. + /// </summary> + /// <value>The ltrt_cmixlev.</value> + public string ltrt_cmixlev { get; set; } + + /// <summary> + /// Gets or sets the ltrt_surmixlev. + /// </summary> + /// <value>The ltrt_surmixlev.</value> + public string ltrt_surmixlev { get; set; } + + /// <summary> + /// Gets or sets the loro_cmixlev. + /// </summary> + /// <value>The loro_cmixlev.</value> + public string loro_cmixlev { get; set; } + + /// <summary> + /// Gets or sets the loro_surmixlev. + /// </summary> + /// <value>The loro_surmixlev.</value> + public string loro_surmixlev { get; set; } + + /// <summary> + /// Gets or sets the disposition. + /// </summary> + /// <value>The disposition.</value> + public Dictionary<string, string> disposition { get; set; } + } + + /// <summary> + /// Class MediaFormat + /// </summary> + public class MediaFormatInfo + { + /// <summary> + /// Gets or sets the filename. + /// </summary> + /// <value>The filename.</value> + public string filename { get; set; } + + /// <summary> + /// Gets or sets the nb_streams. + /// </summary> + /// <value>The nb_streams.</value> + public int nb_streams { get; set; } + + /// <summary> + /// Gets or sets the format_name. + /// </summary> + /// <value>The format_name.</value> + public string format_name { get; set; } + + /// <summary> + /// Gets or sets the format_long_name. + /// </summary> + /// <value>The format_long_name.</value> + public string format_long_name { get; set; } + + /// <summary> + /// Gets or sets the start_time. + /// </summary> + /// <value>The start_time.</value> + public string start_time { get; set; } + + /// <summary> + /// Gets or sets the duration. + /// </summary> + /// <value>The duration.</value> + public string duration { get; set; } + + /// <summary> + /// Gets or sets the size. + /// </summary> + /// <value>The size.</value> + public string size { get; set; } + + /// <summary> + /// Gets or sets the bit_rate. + /// </summary> + /// <value>The bit_rate.</value> + public string bit_rate { get; set; } + + /// <summary> + /// Gets or sets the tags. + /// </summary> + /// <value>The tags.</value> + public Dictionary<string, string> tags { get; set; } + } +} diff --git a/MediaBrowser.Controller/MediaInfo/MediaEncoderHelpers.cs b/MediaBrowser.Controller/MediaInfo/MediaEncoderHelpers.cs index 261454f6d..300071b7b 100644 --- a/MediaBrowser.Controller/MediaInfo/MediaEncoderHelpers.cs +++ b/MediaBrowser.Controller/MediaInfo/MediaEncoderHelpers.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; -using MediaBrowser.Common.MediaInfo; using MediaBrowser.Model.Entities; using MediaBrowser.Model.IO; @@ -29,7 +28,7 @@ namespace MediaBrowser.Controller.MediaInfo { var inputPath = isoMount == null ? new[] { videoPath } : new[] { isoMount.MountedPath }; - type = InputType.VideoFile; + type = InputType.File; switch (videoType) { @@ -87,7 +86,7 @@ namespace MediaBrowser.Controller.MediaInfo /// <returns>InputType.</returns> public static InputType GetInputType(VideoType? videoType, IsoType? isoType) { - var type = InputType.AudioFile; + var type = InputType.File; if (videoType.HasValue) { @@ -119,12 +118,22 @@ namespace MediaBrowser.Controller.MediaInfo return type; } - public static IEnumerable<MediaStream> GetMediaStreams(MediaInfoResult data) + public static Model.Entities.MediaInfo GetMediaInfo(InternalMediaInfoResult data) { var internalStreams = data.streams ?? new MediaStreamInfo[] { }; - return internalStreams.Select(s => GetMediaStream(s, data.format)) - .Where(i => i != null); + var info = new Model.Entities.MediaInfo(); + + info.MediaStreams = internalStreams.Select(s => GetMediaStream(s, data.format)) + .Where(i => i != null) + .ToList(); + + if (data.format != null) + { + info.Format = data.format.format_name; + } + + return info; } private static readonly CultureInfo UsCulture = new CultureInfo("en-US"); diff --git a/MediaBrowser.Controller/Sorting/SortExtensions.cs b/MediaBrowser.Controller/Sorting/SortExtensions.cs new file mode 100644 index 000000000..ec8ee5a11 --- /dev/null +++ b/MediaBrowser.Controller/Sorting/SortExtensions.cs @@ -0,0 +1,143 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace MediaBrowser.Controller.Sorting +{ + public static class SortExtensions + { + public static IEnumerable<T> OrderByString<T>(this IEnumerable<T> list, Func<T, string> getName) + { + return list.OrderBy(getName, new AlphanumComparator()); + } + + public static IEnumerable<T> OrderByStringDescending<T>(this IEnumerable<T> list, Func<T, string> getName) + { + return list.OrderByDescending(getName, new AlphanumComparator()); + } + + public static IOrderedEnumerable<T> ThenByString<T>(this IOrderedEnumerable<T> list, Func<T, string> getName) + { + return list.ThenBy(getName, new AlphanumComparator()); + } + + public static IOrderedEnumerable<T> ThenByStringDescending<T>(this IOrderedEnumerable<T> list, Func<T, string> getName) + { + return list.ThenByDescending(getName, new AlphanumComparator()); + } + + private class AlphanumComparator : IComparer<string> + { + private enum ChunkType { Alphanumeric, Numeric }; + + private static bool InChunk(char ch, char otherCh) + { + var type = ChunkType.Alphanumeric; + + if (char.IsDigit(otherCh)) + { + type = ChunkType.Numeric; + } + + if ((type == ChunkType.Alphanumeric && char.IsDigit(ch)) + || (type == ChunkType.Numeric && !char.IsDigit(ch))) + { + return false; + } + + return true; + } + + public static int CompareValues(string s1, string s2) + { + if (s1 == null || s2 == null) + { + return 0; + } + + int thisMarker = 0, thisNumericChunk = 0; + int thatMarker = 0, thatNumericChunk = 0; + + while ((thisMarker < s1.Length) || (thatMarker < s2.Length)) + { + if (thisMarker >= s1.Length) + { + return -1; + } + else if (thatMarker >= s2.Length) + { + return 1; + } + char thisCh = s1[thisMarker]; + char thatCh = s2[thatMarker]; + + StringBuilder thisChunk = new StringBuilder(); + StringBuilder thatChunk = new StringBuilder(); + + while ((thisMarker < s1.Length) && (thisChunk.Length == 0 || InChunk(thisCh, thisChunk[0]))) + { + thisChunk.Append(thisCh); + thisMarker++; + + if (thisMarker < s1.Length) + { + thisCh = s1[thisMarker]; + } + } + + while ((thatMarker < s2.Length) && (thatChunk.Length == 0 || InChunk(thatCh, thatChunk[0]))) + { + thatChunk.Append(thatCh); + thatMarker++; + + if (thatMarker < s2.Length) + { + thatCh = s2[thatMarker]; + } + } + + int result = 0; + // If both chunks contain numeric characters, sort them numerically + if (char.IsDigit(thisChunk[0]) && char.IsDigit(thatChunk[0])) + { + if (!int.TryParse(thisChunk.ToString(), out thisNumericChunk)) + { + return 0; + } + if (!int.TryParse(thatChunk.ToString(), out thatNumericChunk)) + { + return 0; + } + + if (thisNumericChunk < thatNumericChunk) + { + result = -1; + } + + if (thisNumericChunk > thatNumericChunk) + { + result = 1; + } + } + else + { + result = thisChunk.ToString().CompareTo(thatChunk.ToString()); + } + + if (result != 0) + { + return result; + } + } + + return 0; + } + + public int Compare(string x, string y) + { + return CompareValues(x, y); + } + } + } +} |
