diff options
Diffstat (limited to 'MediaBrowser.Server.Implementations/Channels/ChannelManager.cs')
| -rw-r--r-- | MediaBrowser.Server.Implementations/Channels/ChannelManager.cs | 106 |
1 files changed, 59 insertions, 47 deletions
diff --git a/MediaBrowser.Server.Implementations/Channels/ChannelManager.cs b/MediaBrowser.Server.Implementations/Channels/ChannelManager.cs index b9e4e73b0..9f4db175e 100644 --- a/MediaBrowser.Server.Implementations/Channels/ChannelManager.cs +++ b/MediaBrowser.Server.Implementations/Channels/ChannelManager.cs @@ -6,6 +6,7 @@ using MediaBrowser.Controller.Dto; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Providers; +using MediaBrowser.Controller.Resolvers; using MediaBrowser.Model.Channels; using MediaBrowser.Model.Dto; using MediaBrowser.Model.Entities; @@ -194,77 +195,88 @@ namespace MediaBrowser.Server.Implementations.Channels var sources = SortMediaInfoResults(results).Select(i => GetMediaSource(item, i)) .ToList(); - var channelIdString = channel.Id.ToString("N"); - var isVideo = string.Equals(item.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase); + var cachedVersions = GetCachedChannelItemMediaSources(item); - var cachedVersionTasks = sources - .Select(i => GetCachedVersion(channelIdString, i, isVideo, cancellationToken)); - - var cachedVersions = await Task.WhenAll(cachedVersionTasks).ConfigureAwait(false); - - sources.InsertRange(0, cachedVersions.Where(i => i != null)); + sources.InsertRange(0, cachedVersions); return sources; } - private MediaSourceInfo GetMediaSource(IChannelMediaItem item, ChannelMediaInfo info) + public IEnumerable<MediaSourceInfo> GetCachedChannelItemMediaSources(string id) { - var id = info.Path.GetMD5().ToString("N"); - - var source = new MediaSourceInfo - { - MediaStreams = GetMediaStreams(info).ToList(), - - Container = info.Container, - LocationType = info.IsRemote ? LocationType.Remote : LocationType.FileSystem, - Path = info.Path, - RequiredHttpHeaders = info.RequiredHttpHeaders, - RunTimeTicks = item.RunTimeTicks, - Name = id, - Id = id - }; + var item = (IChannelMediaItem)_libraryManager.GetItemById(id); - return source; + return GetCachedChannelItemMediaSources(item); } - private async Task<MediaSourceInfo> GetCachedVersion(string channelId, - MediaSourceInfo info, - bool isVideo, - CancellationToken cancellationToken) + public IEnumerable<MediaSourceInfo> GetCachedChannelItemMediaSources(IChannelMediaItem item) { - var filename = info.Path.GetMD5().ToString("N"); - - var path = Path.Combine(ChannelDownloadPath, channelId, filename); + var filenamePrefix = item.Id.ToString("N"); + var parentPath = Path.Combine(ChannelDownloadPath, item.ChannelId); try { - var file = Directory.EnumerateFiles(Path.GetDirectoryName(path), "*", SearchOption.TopDirectoryOnly) - .FirstOrDefault(i => (Path.GetFileName(i) ?? string.Empty).StartsWith(filename, StringComparison.OrdinalIgnoreCase)); + var files = new DirectoryInfo(parentPath).EnumerateFiles("*", SearchOption.TopDirectoryOnly); - if (!string.IsNullOrWhiteSpace(file)) + if (string.Equals(item.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase)) { - var source = new MediaSourceInfo - { - Path = file, - LocationType = LocationType.FileSystem, - Name = "Cached " + info.Name, - Id = file.GetMD5().ToString("N") - }; + files = files.Where(i => EntityResolutionHelper.IsVideoFile(i.FullName)); + } + else + { + files = files.Where(i => EntityResolutionHelper.IsAudioFile(i.FullName)); + } + + var file = files + .FirstOrDefault(i => i.Name.StartsWith(filenamePrefix, StringComparison.OrdinalIgnoreCase)); + + if (file != null) + { + var cachedItem = _libraryManager.ResolvePath(file); - if (isVideo) + if (cachedItem != null) { - source.VideoType = VideoType.VideoFile; - } + var hasMediaSources = _libraryManager.GetItemById(cachedItem.Id) as IHasMediaSources; + + if (hasMediaSources != null) + { + var source = hasMediaSources.GetMediaSources(true).FirstOrDefault(); - return source; + if (source != null) + { + source.Type = MediaSourceType.Cache; + return new[] { source }; + } + } + } } } catch (DirectoryNotFoundException) { - return null; + } - return null; + return new List<MediaSourceInfo>(); + } + + private MediaSourceInfo GetMediaSource(IChannelMediaItem item, ChannelMediaInfo info) + { + var id = info.Path.GetMD5().ToString("N"); + + var source = new MediaSourceInfo + { + MediaStreams = GetMediaStreams(info).ToList(), + + Container = info.Container, + LocationType = info.IsRemote ? LocationType.Remote : LocationType.FileSystem, + Path = info.Path, + RequiredHttpHeaders = info.RequiredHttpHeaders, + RunTimeTicks = item.RunTimeTicks, + Name = id, + Id = id + }; + + return source; } private IEnumerable<MediaStream> GetMediaStreams(ChannelMediaInfo info) |
