2014-07-08 17:46:11 -07:00
|
|
|
|
using MediaBrowser.Common.Extensions;
|
2014-03-17 18:45:41 -07:00
|
|
|
|
using MediaBrowser.Common.IO;
|
|
|
|
|
using MediaBrowser.Controller.Channels;
|
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
|
using MediaBrowser.Controller.Dto;
|
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
2014-06-07 12:46:24 -07:00
|
|
|
|
using MediaBrowser.Controller.Localization;
|
2014-03-17 18:45:41 -07:00
|
|
|
|
using MediaBrowser.Controller.Providers;
|
2014-06-02 19:01:30 -07:00
|
|
|
|
using MediaBrowser.Controller.Resolvers;
|
2014-03-17 18:45:41 -07:00
|
|
|
|
using MediaBrowser.Model.Channels;
|
|
|
|
|
using MediaBrowser.Model.Dto;
|
2014-05-02 21:20:04 -07:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2014-03-17 18:45:41 -07:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using MediaBrowser.Model.Querying;
|
2014-05-04 07:19:46 -07:00
|
|
|
|
using MediaBrowser.Model.Serialization;
|
2014-03-17 18:45:41 -07:00
|
|
|
|
using System;
|
2014-07-08 17:46:11 -07:00
|
|
|
|
using System.Collections.Concurrent;
|
2014-03-17 18:45:41 -07:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Implementations.Channels
|
|
|
|
|
{
|
2014-06-19 21:50:30 -07:00
|
|
|
|
public class ChannelManager : IChannelManager, IDisposable
|
2014-03-17 18:45:41 -07:00
|
|
|
|
{
|
|
|
|
|
private IChannel[] _channels;
|
2014-05-03 16:38:23 -07:00
|
|
|
|
private IChannelFactory[] _factories;
|
2014-03-17 18:45:41 -07:00
|
|
|
|
private List<Channel> _channelEntities = new List<Channel>();
|
|
|
|
|
|
|
|
|
|
private readonly IUserManager _userManager;
|
2014-05-02 21:20:04 -07:00
|
|
|
|
private readonly IUserDataManager _userDataManager;
|
2014-03-17 18:45:41 -07:00
|
|
|
|
private readonly IDtoService _dtoService;
|
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IServerConfigurationManager _config;
|
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2014-05-04 07:19:46 -07:00
|
|
|
|
private readonly IJsonSerializer _jsonSerializer;
|
2014-03-17 18:45:41 -07:00
|
|
|
|
|
2014-06-07 12:46:24 -07:00
|
|
|
|
private readonly ILocalizationManager _localization;
|
2014-06-19 21:50:30 -07:00
|
|
|
|
private readonly ConcurrentDictionary<Guid, bool> _refreshedItems = new ConcurrentDictionary<Guid, bool>();
|
|
|
|
|
|
|
|
|
|
private Timer _refreshTimer;
|
2014-06-07 12:46:24 -07:00
|
|
|
|
|
|
|
|
|
public ChannelManager(IUserManager userManager, IDtoService dtoService, ILibraryManager libraryManager, ILogger logger, IServerConfigurationManager config, IFileSystem fileSystem, IUserDataManager userDataManager, IJsonSerializer jsonSerializer, ILocalizationManager localization)
|
2014-03-17 18:45:41 -07:00
|
|
|
|
{
|
|
|
|
|
_userManager = userManager;
|
|
|
|
|
_dtoService = dtoService;
|
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_config = config;
|
|
|
|
|
_fileSystem = fileSystem;
|
2014-05-02 21:20:04 -07:00
|
|
|
|
_userDataManager = userDataManager;
|
2014-05-04 07:19:46 -07:00
|
|
|
|
_jsonSerializer = jsonSerializer;
|
2014-06-07 12:46:24 -07:00
|
|
|
|
_localization = localization;
|
2014-06-19 21:50:30 -07:00
|
|
|
|
|
|
|
|
|
_refreshTimer = new Timer(s => _refreshedItems.Clear(), null, TimeSpan.FromHours(3), TimeSpan.FromHours(3));
|
2014-03-17 18:45:41 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-15 16:30:04 -07:00
|
|
|
|
private TimeSpan CacheLength
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return TimeSpan.FromDays(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-03 16:38:23 -07:00
|
|
|
|
public void AddParts(IEnumerable<IChannel> channels, IEnumerable<IChannelFactory> factories)
|
2014-03-17 18:45:41 -07:00
|
|
|
|
{
|
|
|
|
|
_channels = channels.ToArray();
|
2014-05-03 16:38:23 -07:00
|
|
|
|
_factories = factories.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-02 12:32:41 -07:00
|
|
|
|
public string ChannelDownloadPath
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2014-06-30 10:40:46 -07:00
|
|
|
|
var options = _config.GetChannelsConfiguration();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(options.DownloadPath))
|
2014-06-02 12:32:41 -07:00
|
|
|
|
{
|
2014-06-30 10:40:46 -07:00
|
|
|
|
return options.DownloadPath;
|
2014-06-02 12:32:41 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Path.Combine(_config.ApplicationPaths.ProgramDataPath, "channels");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-03 16:38:23 -07:00
|
|
|
|
private IEnumerable<IChannel> GetAllChannels()
|
|
|
|
|
{
|
|
|
|
|
return _factories
|
|
|
|
|
.SelectMany(i =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return i.GetChannels().ToList();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error getting channel list", ex);
|
|
|
|
|
return new List<IChannel>();
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.Concat(_channels)
|
|
|
|
|
.OrderBy(i => i.Name);
|
2014-03-17 18:45:41 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task<QueryResult<BaseItemDto>> GetChannels(ChannelQuery query, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var user = string.IsNullOrWhiteSpace(query.UserId)
|
|
|
|
|
? null
|
|
|
|
|
: _userManager.GetUserById(new Guid(query.UserId));
|
|
|
|
|
|
|
|
|
|
var channels = _channelEntities.OrderBy(i => i.SortName).ToList();
|
|
|
|
|
|
2014-06-17 09:03:14 -07:00
|
|
|
|
if (query.SupportsLatestItems.HasValue)
|
|
|
|
|
{
|
|
|
|
|
var val = query.SupportsLatestItems.Value;
|
|
|
|
|
channels = channels.Where(i => (GetChannelProvider(i) is ISupportsLatestMedia) == val)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
2014-06-23 09:05:19 -07:00
|
|
|
|
if (query.IsFavorite.HasValue)
|
|
|
|
|
{
|
|
|
|
|
var val = query.IsFavorite.Value;
|
|
|
|
|
channels = channels.Where(i => _userDataManager.GetUserData(user.Id, i.GetUserDataKey()).IsFavorite == val)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
2014-06-17 09:03:14 -07:00
|
|
|
|
|
2014-03-17 18:45:41 -07:00
|
|
|
|
if (user != null)
|
|
|
|
|
{
|
2014-06-06 10:14:02 -07:00
|
|
|
|
channels = channels.Where(i => GetChannelProvider(i).IsEnabledFor(user.Id.ToString("N")) && i.IsVisible(user))
|
2014-03-17 18:45:41 -07:00
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-14 11:04:26 -07:00
|
|
|
|
var all = channels;
|
|
|
|
|
var totalCount = all.Count;
|
|
|
|
|
|
|
|
|
|
if (query.StartIndex.HasValue)
|
|
|
|
|
{
|
|
|
|
|
all = all.Skip(query.StartIndex.Value).ToList();
|
|
|
|
|
}
|
|
|
|
|
if (query.Limit.HasValue)
|
|
|
|
|
{
|
|
|
|
|
all = all.Take(query.Limit.Value).ToList();
|
|
|
|
|
}
|
2014-05-18 12:58:42 -07:00
|
|
|
|
|
2014-03-17 18:45:41 -07:00
|
|
|
|
// Get everything
|
|
|
|
|
var fields = Enum.GetNames(typeof(ItemFields))
|
|
|
|
|
.Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true))
|
|
|
|
|
.ToList();
|
|
|
|
|
|
2014-05-14 11:04:26 -07:00
|
|
|
|
var returnItems = all.Select(i => _dtoService.GetBaseItemDto(i, fields, user))
|
2014-03-17 18:45:41 -07:00
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
|
|
var result = new QueryResult<BaseItemDto>
|
|
|
|
|
{
|
|
|
|
|
Items = returnItems,
|
2014-05-14 11:04:26 -07:00
|
|
|
|
TotalRecordCount = totalCount
|
2014-03-17 18:45:41 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return Task.FromResult(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task RefreshChannels(IProgress<double> progress, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2014-05-03 16:38:23 -07:00
|
|
|
|
var allChannelsList = GetAllChannels().ToList();
|
2014-03-17 18:45:41 -07:00
|
|
|
|
|
|
|
|
|
var list = new List<Channel>();
|
|
|
|
|
|
|
|
|
|
var numComplete = 0;
|
|
|
|
|
|
|
|
|
|
foreach (var channelInfo in allChannelsList)
|
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var item = await GetChannel(channelInfo, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
list.Add(item);
|
|
|
|
|
|
|
|
|
|
_libraryManager.RegisterItem(item);
|
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error getting channel information for {0}", ex, channelInfo.Name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
numComplete++;
|
|
|
|
|
double percent = numComplete;
|
|
|
|
|
percent /= allChannelsList.Count;
|
|
|
|
|
|
|
|
|
|
progress.Report(100 * percent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_channelEntities = list.ToList();
|
|
|
|
|
progress.Report(100);
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-02 12:32:41 -07:00
|
|
|
|
public async Task<IEnumerable<MediaSourceInfo>> GetChannelItemMediaSources(string id, CancellationToken cancellationToken)
|
2014-05-18 12:58:42 -07:00
|
|
|
|
{
|
|
|
|
|
var item = (IChannelMediaItem)_libraryManager.GetItemById(id);
|
|
|
|
|
|
|
|
|
|
var channelGuid = new Guid(item.ChannelId);
|
|
|
|
|
var channel = _channelEntities.First(i => i.Id == channelGuid);
|
2014-05-18 15:26:06 -07:00
|
|
|
|
var channelPlugin = GetChannelProvider(channel);
|
2014-05-18 12:58:42 -07:00
|
|
|
|
|
2014-05-18 15:26:06 -07:00
|
|
|
|
var requiresCallback = channelPlugin as IRequiresMediaInfoCallback;
|
2014-05-18 12:58:42 -07:00
|
|
|
|
|
2014-06-02 12:32:41 -07:00
|
|
|
|
IEnumerable<ChannelMediaInfo> results;
|
|
|
|
|
|
2014-05-18 12:58:42 -07:00
|
|
|
|
if (requiresCallback != null)
|
|
|
|
|
{
|
2014-06-19 21:50:30 -07:00
|
|
|
|
results = await GetChannelItemMediaSourcesInternal(requiresCallback, item.ExternalId, cancellationToken)
|
|
|
|
|
.ConfigureAwait(false);
|
2014-06-02 12:32:41 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
results = item.ChannelMediaSources;
|
2014-05-18 12:58:42 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-02 12:32:41 -07:00
|
|
|
|
var sources = SortMediaInfoResults(results).Select(i => GetMediaSource(item, i))
|
|
|
|
|
.ToList();
|
|
|
|
|
|
2014-06-02 19:01:30 -07:00
|
|
|
|
var cachedVersions = GetCachedChannelItemMediaSources(item);
|
2014-06-02 12:32:41 -07:00
|
|
|
|
|
2014-06-02 19:01:30 -07:00
|
|
|
|
sources.InsertRange(0, cachedVersions);
|
2014-06-02 12:32:41 -07:00
|
|
|
|
|
|
|
|
|
return sources;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-19 21:50:30 -07:00
|
|
|
|
private readonly ConcurrentDictionary<string, Tuple<DateTime, List<ChannelMediaInfo>>> _channelItemMediaInfo =
|
|
|
|
|
new ConcurrentDictionary<string, Tuple<DateTime, List<ChannelMediaInfo>>>();
|
|
|
|
|
|
|
|
|
|
private async Task<IEnumerable<ChannelMediaInfo>> GetChannelItemMediaSourcesInternal(IRequiresMediaInfoCallback channel, string id, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
Tuple<DateTime, List<ChannelMediaInfo>> cachedInfo;
|
|
|
|
|
|
|
|
|
|
if (_channelItemMediaInfo.TryGetValue(id, out cachedInfo))
|
|
|
|
|
{
|
|
|
|
|
if ((DateTime.UtcNow - cachedInfo.Item1).TotalMinutes < 5)
|
|
|
|
|
{
|
|
|
|
|
return cachedInfo.Item2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var mediaInfo = await channel.GetChannelItemMediaInfo(id, cancellationToken)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
var list = mediaInfo.ToList();
|
|
|
|
|
|
|
|
|
|
var item2 = new Tuple<DateTime, List<ChannelMediaInfo>>(DateTime.UtcNow, list);
|
|
|
|
|
_channelItemMediaInfo.AddOrUpdate(id, item2, (key, oldValue) => item2);
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-02 19:01:30 -07:00
|
|
|
|
public IEnumerable<MediaSourceInfo> GetCachedChannelItemMediaSources(string id)
|
2014-06-02 12:32:41 -07:00
|
|
|
|
{
|
2014-06-02 19:01:30 -07:00
|
|
|
|
var item = (IChannelMediaItem)_libraryManager.GetItemById(id);
|
2014-06-02 12:32:41 -07:00
|
|
|
|
|
2014-06-02 19:01:30 -07:00
|
|
|
|
return GetCachedChannelItemMediaSources(item);
|
2014-06-02 12:32:41 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-02 19:01:30 -07:00
|
|
|
|
public IEnumerable<MediaSourceInfo> GetCachedChannelItemMediaSources(IChannelMediaItem item)
|
2014-06-02 12:32:41 -07:00
|
|
|
|
{
|
2014-06-02 19:01:30 -07:00
|
|
|
|
var filenamePrefix = item.Id.ToString("N");
|
|
|
|
|
var parentPath = Path.Combine(ChannelDownloadPath, item.ChannelId);
|
2014-06-02 12:32:41 -07:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2014-06-02 19:01:30 -07:00
|
|
|
|
var files = new DirectoryInfo(parentPath).EnumerateFiles("*", SearchOption.TopDirectoryOnly);
|
2014-06-02 12:32:41 -07:00
|
|
|
|
|
2014-06-02 19:01:30 -07:00
|
|
|
|
if (string.Equals(item.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase))
|
2014-06-02 12:32:41 -07:00
|
|
|
|
{
|
2014-06-02 19:01:30 -07:00
|
|
|
|
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));
|
2014-06-02 12:32:41 -07:00
|
|
|
|
|
2014-06-02 19:01:30 -07:00
|
|
|
|
if (file != null)
|
|
|
|
|
{
|
|
|
|
|
var cachedItem = _libraryManager.ResolvePath(file);
|
|
|
|
|
|
|
|
|
|
if (cachedItem != null)
|
2014-06-02 12:32:41 -07:00
|
|
|
|
{
|
2014-06-02 19:01:30 -07:00
|
|
|
|
var hasMediaSources = _libraryManager.GetItemById(cachedItem.Id) as IHasMediaSources;
|
|
|
|
|
|
|
|
|
|
if (hasMediaSources != null)
|
|
|
|
|
{
|
|
|
|
|
var source = hasMediaSources.GetMediaSources(true).FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
if (source != null)
|
|
|
|
|
{
|
|
|
|
|
source.Type = MediaSourceType.Cache;
|
|
|
|
|
return new[] { source };
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-06-02 12:32:41 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (DirectoryNotFoundException)
|
|
|
|
|
{
|
2014-06-02 19:01:30 -07:00
|
|
|
|
|
2014-06-02 12:32:41 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-02 19:01:30 -07:00
|
|
|
|
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,
|
2014-06-16 18:56:23 -07:00
|
|
|
|
Protocol = info.Protocol,
|
2014-06-02 19:01:30 -07:00
|
|
|
|
Path = info.Path,
|
|
|
|
|
RequiredHttpHeaders = info.RequiredHttpHeaders,
|
|
|
|
|
RunTimeTicks = item.RunTimeTicks,
|
|
|
|
|
Name = id,
|
|
|
|
|
Id = id
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return source;
|
2014-06-02 12:32:41 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerable<MediaStream> GetMediaStreams(ChannelMediaInfo info)
|
|
|
|
|
{
|
|
|
|
|
var list = new List<MediaStream>();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(info.VideoCodec) &&
|
|
|
|
|
!string.IsNullOrWhiteSpace(info.AudioCodec))
|
|
|
|
|
{
|
|
|
|
|
list.Add(new MediaStream
|
|
|
|
|
{
|
|
|
|
|
Type = MediaStreamType.Video,
|
|
|
|
|
Width = info.Width,
|
|
|
|
|
RealFrameRate = info.Framerate,
|
|
|
|
|
Profile = info.VideoProfile,
|
|
|
|
|
Level = info.VideoLevel,
|
|
|
|
|
Index = -1,
|
|
|
|
|
Height = info.Height,
|
|
|
|
|
Codec = info.VideoCodec,
|
|
|
|
|
BitRate = info.VideoBitrate,
|
|
|
|
|
AverageFrameRate = info.Framerate
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
list.Add(new MediaStream
|
|
|
|
|
{
|
|
|
|
|
Type = MediaStreamType.Audio,
|
|
|
|
|
Index = -1,
|
|
|
|
|
Codec = info.AudioCodec,
|
|
|
|
|
BitRate = info.AudioBitrate,
|
|
|
|
|
Channels = info.AudioChannels,
|
|
|
|
|
SampleRate = info.AudioSampleRate
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerable<ChannelMediaInfo> SortMediaInfoResults(IEnumerable<ChannelMediaInfo> channelMediaSources)
|
|
|
|
|
{
|
|
|
|
|
var list = channelMediaSources.ToList();
|
|
|
|
|
|
2014-06-30 10:40:46 -07:00
|
|
|
|
var options = _config.GetChannelsConfiguration();
|
|
|
|
|
|
|
|
|
|
var width = options.PreferredStreamingWidth;
|
2014-06-02 12:32:41 -07:00
|
|
|
|
|
|
|
|
|
if (width.HasValue)
|
|
|
|
|
{
|
|
|
|
|
var val = width.Value;
|
|
|
|
|
|
2014-07-10 21:27:46 -07:00
|
|
|
|
var res = list
|
2014-07-15 12:57:27 -07:00
|
|
|
|
.OrderBy(i => (i.Width.HasValue && i.Width.Value <= val ? 0 : 1))
|
2014-07-10 21:27:46 -07:00
|
|
|
|
.ThenBy(i => Math.Abs((i.Width ?? 0) - val))
|
2014-06-02 12:32:41 -07:00
|
|
|
|
.ThenByDescending(i => i.Width ?? 0)
|
2014-07-10 21:27:46 -07:00
|
|
|
|
.ThenBy(list.IndexOf)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return res;
|
2014-06-02 12:32:41 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return list
|
|
|
|
|
.OrderByDescending(i => i.Width ?? 0)
|
|
|
|
|
.ThenBy(list.IndexOf);
|
2014-05-18 12:58:42 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-17 18:45:41 -07:00
|
|
|
|
private async Task<Channel> GetChannel(IChannel channelInfo, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var path = Path.Combine(_config.ApplicationPaths.ItemsByNamePath, "channels", _fileSystem.GetValidFilename(channelInfo.Name));
|
|
|
|
|
|
|
|
|
|
var fileInfo = new DirectoryInfo(path);
|
|
|
|
|
|
|
|
|
|
var isNew = false;
|
|
|
|
|
|
|
|
|
|
if (!fileInfo.Exists)
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Creating directory {0}", path);
|
|
|
|
|
|
|
|
|
|
Directory.CreateDirectory(path);
|
|
|
|
|
fileInfo = new DirectoryInfo(path);
|
|
|
|
|
|
|
|
|
|
if (!fileInfo.Exists)
|
|
|
|
|
{
|
|
|
|
|
throw new IOException("Path not created: " + path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isNew = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var id = GetInternalChannelId(channelInfo.Name);
|
|
|
|
|
|
|
|
|
|
var item = _libraryManager.GetItemById(id) as Channel;
|
|
|
|
|
|
|
|
|
|
if (item == null)
|
|
|
|
|
{
|
|
|
|
|
item = new Channel
|
|
|
|
|
{
|
|
|
|
|
Name = channelInfo.Name,
|
|
|
|
|
Id = id,
|
|
|
|
|
DateCreated = _fileSystem.GetCreationTimeUtc(fileInfo),
|
|
|
|
|
DateModified = _fileSystem.GetLastWriteTimeUtc(fileInfo),
|
2014-06-07 12:46:24 -07:00
|
|
|
|
Path = path
|
2014-03-17 18:45:41 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
isNew = true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-07 12:46:24 -07:00
|
|
|
|
item.OfficialRating = GetOfficialRating(channelInfo.ParentalRating);
|
|
|
|
|
item.Overview = channelInfo.Description;
|
2014-05-23 16:58:28 -07:00
|
|
|
|
item.HomePageUrl = channelInfo.HomePageUrl;
|
2014-03-17 18:45:41 -07:00
|
|
|
|
item.OriginalChannelName = channelInfo.Name;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(item.Name))
|
|
|
|
|
{
|
|
|
|
|
item.Name = channelInfo.Name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await item.RefreshMetadata(new MetadataRefreshOptions
|
|
|
|
|
{
|
|
|
|
|
ForceSave = isNew
|
|
|
|
|
|
|
|
|
|
}, cancellationToken);
|
|
|
|
|
|
|
|
|
|
return item;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-06 10:14:02 -07:00
|
|
|
|
private string GetOfficialRating(ChannelParentalRating rating)
|
|
|
|
|
{
|
|
|
|
|
switch (rating)
|
|
|
|
|
{
|
|
|
|
|
case ChannelParentalRating.Adult:
|
|
|
|
|
return "XXX";
|
|
|
|
|
case ChannelParentalRating.UsR:
|
|
|
|
|
return "R";
|
|
|
|
|
case ChannelParentalRating.UsPG13:
|
|
|
|
|
return "PG-13";
|
|
|
|
|
case ChannelParentalRating.UsPG:
|
|
|
|
|
return "PG";
|
|
|
|
|
default:
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-19 12:51:56 -07:00
|
|
|
|
public Channel GetChannel(string id)
|
|
|
|
|
{
|
|
|
|
|
return (Channel)_libraryManager.GetItemById(new Guid(id));
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-02 12:32:41 -07:00
|
|
|
|
public IEnumerable<ChannelFeatures> GetAllChannelFeatures()
|
|
|
|
|
{
|
|
|
|
|
return _channelEntities
|
|
|
|
|
.OrderBy(i => i.SortName)
|
|
|
|
|
.Select(i => GetChannelFeatures(i.Id.ToString("N")));
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-27 10:09:48 -07:00
|
|
|
|
public ChannelFeatures GetChannelFeatures(string id)
|
|
|
|
|
{
|
|
|
|
|
var channel = GetChannel(id);
|
|
|
|
|
|
|
|
|
|
var channelProvider = GetChannelProvider(channel);
|
|
|
|
|
|
2014-06-06 10:14:02 -07:00
|
|
|
|
return GetChannelFeaturesDto(channel, channelProvider, channelProvider.GetChannelFeatures());
|
2014-05-27 10:57:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-06 10:14:02 -07:00
|
|
|
|
public ChannelFeatures GetChannelFeaturesDto(Channel channel,
|
|
|
|
|
IChannel provider,
|
|
|
|
|
InternalChannelFeatures features)
|
2014-05-27 10:57:29 -07:00
|
|
|
|
{
|
2014-06-06 10:14:02 -07:00
|
|
|
|
var isIndexable = provider is IIndexableChannel;
|
2014-06-15 16:30:04 -07:00
|
|
|
|
var supportsLatest = provider is ISupportsLatestMedia;
|
2014-06-06 10:14:02 -07:00
|
|
|
|
|
2014-05-27 10:57:29 -07:00
|
|
|
|
return new ChannelFeatures
|
|
|
|
|
{
|
|
|
|
|
CanFilter = !features.MaxPageSize.HasValue,
|
2014-06-06 10:14:02 -07:00
|
|
|
|
CanSearch = provider is ISearchableChannel,
|
2014-05-27 10:57:29 -07:00
|
|
|
|
ContentTypes = features.ContentTypes,
|
|
|
|
|
DefaultSortFields = features.DefaultSortFields,
|
|
|
|
|
MaxPageSize = features.MaxPageSize,
|
|
|
|
|
MediaTypes = features.MediaTypes,
|
2014-06-02 12:32:41 -07:00
|
|
|
|
SupportsSortOrderToggle = features.SupportsSortOrderToggle,
|
2014-06-15 16:30:04 -07:00
|
|
|
|
SupportsLatestMedia = supportsLatest,
|
2014-06-02 12:32:41 -07:00
|
|
|
|
Name = channel.Name,
|
|
|
|
|
Id = channel.Id.ToString("N"),
|
2014-06-15 16:30:04 -07:00
|
|
|
|
SupportsContentDownloading = isIndexable || supportsLatest
|
2014-05-27 10:57:29 -07:00
|
|
|
|
};
|
2014-05-27 10:09:48 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-17 18:45:41 -07:00
|
|
|
|
private Guid GetInternalChannelId(string name)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(name))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("name");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ("Channel " + name).GetMBId(typeof(Channel));
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-15 16:30:04 -07:00
|
|
|
|
public async Task<QueryResult<BaseItemDto>> GetLatestChannelItems(AllChannelMediaQuery query, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var user = string.IsNullOrWhiteSpace(query.UserId)
|
|
|
|
|
? null
|
|
|
|
|
: _userManager.GetUserById(new Guid(query.UserId));
|
|
|
|
|
|
2014-07-10 21:27:46 -07:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(query.UserId) && user == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("User not found.");
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-15 16:30:04 -07:00
|
|
|
|
var channels = _channels;
|
|
|
|
|
|
|
|
|
|
if (query.ChannelIds.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
// Avoid implicitly captured closure
|
|
|
|
|
var ids = query.ChannelIds;
|
|
|
|
|
channels = channels
|
|
|
|
|
.Where(i => ids.Contains(GetInternalChannelId(i.Name).ToString("N")))
|
|
|
|
|
.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Avoid implicitly captured closure
|
|
|
|
|
var userId = query.UserId;
|
|
|
|
|
|
|
|
|
|
var tasks = channels
|
|
|
|
|
.Select(async i =>
|
|
|
|
|
{
|
|
|
|
|
var indexable = i as ISupportsLatestMedia;
|
|
|
|
|
|
|
|
|
|
if (indexable != null)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2014-06-19 21:50:30 -07:00
|
|
|
|
var result = await GetLatestItems(indexable, i, userId, cancellationToken).ConfigureAwait(false);
|
2014-06-15 16:30:04 -07:00
|
|
|
|
|
|
|
|
|
var resultItems = result.ToList();
|
|
|
|
|
|
|
|
|
|
return new Tuple<IChannel, ChannelItemResult>(i, new ChannelItemResult
|
|
|
|
|
{
|
|
|
|
|
Items = resultItems,
|
|
|
|
|
TotalRecordCount = resultItems.Count
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error getting all media from {0}", ex, i.Name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return new Tuple<IChannel, ChannelItemResult>(i, new ChannelItemResult { });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var results = await Task.WhenAll(tasks).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var totalCount = results.Length;
|
|
|
|
|
|
|
|
|
|
IEnumerable<Tuple<IChannel, ChannelItemInfo>> items = results
|
2014-06-17 22:16:00 -07:00
|
|
|
|
.SelectMany(i => i.Item2.Items.Select(m => new Tuple<IChannel, ChannelItemInfo>(i.Item1, m)));
|
2014-06-15 16:30:04 -07:00
|
|
|
|
|
|
|
|
|
if (query.ContentTypes.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
// Avoid implicitly captured closure
|
|
|
|
|
var contentTypes = query.ContentTypes;
|
|
|
|
|
|
|
|
|
|
items = items.Where(i => contentTypes.Contains(i.Item2.ContentType));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Avoid implicitly captured closure
|
|
|
|
|
var token = cancellationToken;
|
|
|
|
|
var itemTasks = items.Select(i =>
|
|
|
|
|
{
|
|
|
|
|
var channelProvider = i.Item1;
|
|
|
|
|
var channel = GetChannel(GetInternalChannelId(channelProvider.Name).ToString("N"));
|
|
|
|
|
return GetChannelItemEntity(i.Item2, channelProvider, channel, token);
|
|
|
|
|
});
|
|
|
|
|
|
2014-06-16 18:56:23 -07:00
|
|
|
|
var internalItems = await Task.WhenAll(itemTasks).ConfigureAwait(false);
|
2014-06-15 16:30:04 -07:00
|
|
|
|
|
2014-06-16 18:56:23 -07:00
|
|
|
|
internalItems = ApplyFilters(internalItems, query.Filters, user).ToArray();
|
|
|
|
|
await RefreshIfNeeded(internalItems, cancellationToken).ConfigureAwait(false);
|
2014-06-15 16:30:04 -07:00
|
|
|
|
|
|
|
|
|
if (query.StartIndex.HasValue)
|
|
|
|
|
{
|
2014-06-16 18:56:23 -07:00
|
|
|
|
internalItems = internalItems.Skip(query.StartIndex.Value).ToArray();
|
2014-06-15 16:30:04 -07:00
|
|
|
|
}
|
|
|
|
|
if (query.Limit.HasValue)
|
|
|
|
|
{
|
2014-06-16 18:56:23 -07:00
|
|
|
|
internalItems = internalItems.Take(query.Limit.Value).ToArray();
|
2014-06-15 16:30:04 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var returnItemArray = internalItems.Select(i => _dtoService.GetBaseItemDto(i, query.Fields, user))
|
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
|
|
return new QueryResult<BaseItemDto>
|
|
|
|
|
{
|
|
|
|
|
TotalRecordCount = totalCount,
|
|
|
|
|
Items = returnItemArray
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-19 21:50:30 -07:00
|
|
|
|
private async Task<IEnumerable<ChannelItemInfo>> GetLatestItems(ISupportsLatestMedia indexable, IChannel channel, string userId, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var cacheLength = TimeSpan.FromHours(12);
|
|
|
|
|
var cachePath = GetChannelDataCachePath(channel, userId, "channelmanager-latest", null, false);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (_fileSystem.GetLastWriteTimeUtc(cachePath).Add(cacheLength) > DateTime.UtcNow)
|
|
|
|
|
{
|
|
|
|
|
return _jsonSerializer.DeserializeFromFile<List<ChannelItemInfo>>(cachePath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (DirectoryNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await _resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (_fileSystem.GetLastWriteTimeUtc(cachePath).Add(cacheLength) > DateTime.UtcNow)
|
|
|
|
|
{
|
|
|
|
|
return _jsonSerializer.DeserializeFromFile<List<ChannelItemInfo>>(cachePath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (DirectoryNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = await indexable.GetLatestMedia(new ChannelLatestMediaSearch
|
|
|
|
|
{
|
|
|
|
|
UserId = userId
|
|
|
|
|
|
|
|
|
|
}, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var resultItems = result.ToList();
|
|
|
|
|
|
|
|
|
|
CacheResponse(resultItems, cachePath);
|
|
|
|
|
|
|
|
|
|
return resultItems;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
_resourcePool.Release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-02 12:32:41 -07:00
|
|
|
|
public async Task<QueryResult<BaseItemDto>> GetAllMedia(AllChannelMediaQuery query, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var user = string.IsNullOrWhiteSpace(query.UserId)
|
|
|
|
|
? null
|
|
|
|
|
: _userManager.GetUserById(new Guid(query.UserId));
|
|
|
|
|
|
|
|
|
|
var channels = _channels;
|
|
|
|
|
|
|
|
|
|
if (query.ChannelIds.Length > 0)
|
|
|
|
|
{
|
2014-06-15 16:30:04 -07:00
|
|
|
|
// Avoid implicitly captured closure
|
|
|
|
|
var ids = query.ChannelIds;
|
2014-06-02 12:32:41 -07:00
|
|
|
|
channels = channels
|
2014-06-15 16:30:04 -07:00
|
|
|
|
.Where(i => ids.Contains(GetInternalChannelId(i.Name).ToString("N")))
|
2014-06-02 12:32:41 -07:00
|
|
|
|
.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-15 16:30:04 -07:00
|
|
|
|
// Avoid implicitly captured closure
|
|
|
|
|
var userId = query.UserId;
|
|
|
|
|
|
2014-06-02 12:32:41 -07:00
|
|
|
|
var tasks = channels
|
|
|
|
|
.Select(async i =>
|
|
|
|
|
{
|
2014-06-06 10:14:02 -07:00
|
|
|
|
var indexable = i as IIndexableChannel;
|
|
|
|
|
|
|
|
|
|
if (indexable != null)
|
2014-06-02 12:32:41 -07:00
|
|
|
|
{
|
2014-06-06 10:14:02 -07:00
|
|
|
|
try
|
2014-06-02 12:32:41 -07:00
|
|
|
|
{
|
2014-06-19 21:50:30 -07:00
|
|
|
|
var result = await GetAllItems(indexable, i, userId, cancellationToken).ConfigureAwait(false);
|
2014-06-02 12:32:41 -07:00
|
|
|
|
|
2014-06-06 10:14:02 -07:00
|
|
|
|
return new Tuple<IChannel, ChannelItemResult>(i, result);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error getting all media from {0}", ex, i.Name);
|
|
|
|
|
}
|
2014-06-02 12:32:41 -07:00
|
|
|
|
}
|
2014-06-06 10:14:02 -07:00
|
|
|
|
return new Tuple<IChannel, ChannelItemResult>(i, new ChannelItemResult { });
|
2014-06-02 12:32:41 -07:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var results = await Task.WhenAll(tasks).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var totalCount = results.Length;
|
|
|
|
|
|
|
|
|
|
IEnumerable<Tuple<IChannel, ChannelItemInfo>> items = results
|
|
|
|
|
.SelectMany(i => i.Item2.Items.Select(m => new Tuple<IChannel, ChannelItemInfo>(i.Item1, m)))
|
|
|
|
|
.OrderBy(i => i.Item2.Name);
|
|
|
|
|
|
2014-06-07 12:46:24 -07:00
|
|
|
|
if (query.ContentTypes.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
// Avoid implicitly captured closure
|
|
|
|
|
var contentTypes = query.ContentTypes;
|
|
|
|
|
|
|
|
|
|
items = items.Where(i => contentTypes.Contains(i.Item2.ContentType));
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-02 12:32:41 -07:00
|
|
|
|
if (query.StartIndex.HasValue)
|
|
|
|
|
{
|
|
|
|
|
items = items.Skip(query.StartIndex.Value);
|
|
|
|
|
}
|
|
|
|
|
if (query.Limit.HasValue)
|
|
|
|
|
{
|
|
|
|
|
items = items.Take(query.Limit.Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Avoid implicitly captured closure
|
|
|
|
|
var token = cancellationToken;
|
|
|
|
|
var itemTasks = items.Select(i =>
|
|
|
|
|
{
|
|
|
|
|
var channelProvider = i.Item1;
|
|
|
|
|
var channel = GetChannel(GetInternalChannelId(channelProvider.Name).ToString("N"));
|
|
|
|
|
return GetChannelItemEntity(i.Item2, channelProvider, channel, token);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var internalItems = await Task.WhenAll(itemTasks).ConfigureAwait(false);
|
2014-06-16 18:56:23 -07:00
|
|
|
|
await RefreshIfNeeded(internalItems, cancellationToken).ConfigureAwait(false);
|
2014-06-02 12:32:41 -07:00
|
|
|
|
|
2014-06-15 16:30:04 -07:00
|
|
|
|
var returnItemArray = internalItems.Select(i => _dtoService.GetBaseItemDto(i, query.Fields, user))
|
2014-06-02 12:32:41 -07:00
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
|
|
return new QueryResult<BaseItemDto>
|
|
|
|
|
{
|
|
|
|
|
TotalRecordCount = totalCount,
|
|
|
|
|
Items = returnItemArray
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-19 21:50:30 -07:00
|
|
|
|
private async Task<ChannelItemResult> GetAllItems(IIndexableChannel indexable, IChannel channel, string userId, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var cacheLength = TimeSpan.FromHours(12);
|
|
|
|
|
var cachePath = GetChannelDataCachePath(channel, userId, "channelmanager-allitems", null, false);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (_fileSystem.GetLastWriteTimeUtc(cachePath).Add(cacheLength) > DateTime.UtcNow)
|
|
|
|
|
{
|
|
|
|
|
return _jsonSerializer.DeserializeFromFile<ChannelItemResult>(cachePath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (DirectoryNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await _resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (_fileSystem.GetLastWriteTimeUtc(cachePath).Add(cacheLength) > DateTime.UtcNow)
|
|
|
|
|
{
|
|
|
|
|
return _jsonSerializer.DeserializeFromFile<ChannelItemResult>(cachePath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (DirectoryNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = await indexable.GetAllMedia(new InternalAllChannelMediaQuery
|
|
|
|
|
{
|
|
|
|
|
UserId = userId
|
|
|
|
|
|
|
|
|
|
}, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
CacheResponse(result, cachePath);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
_resourcePool.Release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-17 18:45:41 -07:00
|
|
|
|
public async Task<QueryResult<BaseItemDto>> GetChannelItems(ChannelItemQuery query, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2014-05-04 17:46:52 -07:00
|
|
|
|
var queryChannelId = query.ChannelId;
|
2014-05-19 12:51:56 -07:00
|
|
|
|
// Get the internal channel entity
|
|
|
|
|
var channel = _channelEntities.First(i => i.Id == new Guid(queryChannelId));
|
2014-03-17 18:45:41 -07:00
|
|
|
|
|
2014-05-19 12:51:56 -07:00
|
|
|
|
// Find the corresponding channel provider plugin
|
|
|
|
|
var channelProvider = GetChannelProvider(channel);
|
|
|
|
|
|
2014-05-23 16:58:28 -07:00
|
|
|
|
var channelInfo = channelProvider.GetChannelFeatures();
|
2014-05-19 12:51:56 -07:00
|
|
|
|
|
|
|
|
|
int? providerStartIndex = null;
|
|
|
|
|
int? providerLimit = null;
|
|
|
|
|
|
|
|
|
|
if (channelInfo.MaxPageSize.HasValue)
|
2014-05-04 17:46:52 -07:00
|
|
|
|
{
|
2014-05-19 12:51:56 -07:00
|
|
|
|
providerStartIndex = query.StartIndex;
|
2014-05-04 17:46:52 -07:00
|
|
|
|
|
2014-05-20 17:56:24 -07:00
|
|
|
|
if (query.Limit.HasValue && query.Limit.Value > channelInfo.MaxPageSize.Value)
|
2014-05-19 12:51:56 -07:00
|
|
|
|
{
|
2014-05-27 10:09:48 -07:00
|
|
|
|
throw new ArgumentException(string.Format("{0} channel only supports a maximum of {1} records at a time.", channel.Name, channelInfo.MaxPageSize.Value));
|
2014-05-19 12:51:56 -07:00
|
|
|
|
}
|
|
|
|
|
providerLimit = query.Limit;
|
|
|
|
|
}
|
2014-05-04 17:46:52 -07:00
|
|
|
|
|
2014-05-19 12:51:56 -07:00
|
|
|
|
var user = string.IsNullOrWhiteSpace(query.UserId)
|
|
|
|
|
? null
|
|
|
|
|
: _userManager.GetUserById(new Guid(query.UserId));
|
2014-05-04 17:46:52 -07:00
|
|
|
|
|
2014-05-27 14:17:48 -07:00
|
|
|
|
ChannelItemSortField? sortField = null;
|
|
|
|
|
ChannelItemSortField parsedField;
|
2014-06-02 12:32:41 -07:00
|
|
|
|
if (query.SortBy.Length == 1 &&
|
2014-05-27 14:17:48 -07:00
|
|
|
|
Enum.TryParse(query.SortBy[0], true, out parsedField))
|
|
|
|
|
{
|
|
|
|
|
sortField = parsedField;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var sortDescending = query.SortOrder.HasValue && query.SortOrder.Value == SortOrder.Descending;
|
|
|
|
|
|
2014-06-02 12:32:41 -07:00
|
|
|
|
var itemsResult = await GetChannelItems(channelProvider,
|
|
|
|
|
user,
|
|
|
|
|
query.FolderId,
|
|
|
|
|
providerStartIndex,
|
|
|
|
|
providerLimit,
|
2014-05-27 14:17:48 -07:00
|
|
|
|
sortField,
|
|
|
|
|
sortDescending,
|
|
|
|
|
cancellationToken)
|
|
|
|
|
.ConfigureAwait(false);
|
2014-05-18 15:26:06 -07:00
|
|
|
|
|
2014-05-19 12:51:56 -07:00
|
|
|
|
var providerTotalRecordCount = providerLimit.HasValue ? itemsResult.TotalRecordCount : null;
|
2014-05-04 17:46:52 -07:00
|
|
|
|
|
2014-05-19 12:51:56 -07:00
|
|
|
|
var tasks = itemsResult.Items.Select(i => GetChannelItemEntity(i, channelProvider, channel, cancellationToken));
|
|
|
|
|
|
|
|
|
|
var internalItems = await Task.WhenAll(tasks).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (user != null)
|
|
|
|
|
{
|
|
|
|
|
internalItems = internalItems.Where(i => i.IsVisible(user)).ToArray();
|
2014-03-17 18:45:41 -07:00
|
|
|
|
|
2014-05-19 12:51:56 -07:00
|
|
|
|
if (providerTotalRecordCount.HasValue)
|
|
|
|
|
{
|
|
|
|
|
providerTotalRecordCount = providerTotalRecordCount.Value;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-05-04 17:46:52 -07:00
|
|
|
|
|
2014-05-19 12:51:56 -07:00
|
|
|
|
return await GetReturnItems(internalItems, providerTotalRecordCount, user, query, cancellationToken).ConfigureAwait(false);
|
2014-03-17 18:45:41 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-04 07:19:46 -07:00
|
|
|
|
private readonly SemaphoreSlim _resourcePool = new SemaphoreSlim(1, 1);
|
2014-05-27 14:17:48 -07:00
|
|
|
|
private async Task<ChannelItemResult> GetChannelItems(IChannel channel,
|
|
|
|
|
User user,
|
|
|
|
|
string folderId,
|
|
|
|
|
int? startIndex,
|
|
|
|
|
int? limit,
|
|
|
|
|
ChannelItemSortField? sortField,
|
|
|
|
|
bool sortDescending,
|
|
|
|
|
CancellationToken cancellationToken)
|
2014-03-17 18:45:41 -07:00
|
|
|
|
{
|
2014-06-06 10:14:02 -07:00
|
|
|
|
var userId = user.Id.ToString("N");
|
|
|
|
|
|
2014-06-15 16:30:04 -07:00
|
|
|
|
var cacheLength = CacheLength;
|
2014-06-06 10:14:02 -07:00
|
|
|
|
var cachePath = GetChannelDataCachePath(channel, userId, folderId, sortField, sortDescending);
|
2014-03-17 18:45:41 -07:00
|
|
|
|
|
2014-05-04 07:19:46 -07:00
|
|
|
|
try
|
2014-03-17 18:45:41 -07:00
|
|
|
|
{
|
2014-05-19 12:51:56 -07:00
|
|
|
|
if (!startIndex.HasValue && !limit.HasValue)
|
2014-05-04 07:19:46 -07:00
|
|
|
|
{
|
2014-06-06 10:14:02 -07:00
|
|
|
|
if (_fileSystem.GetLastWriteTimeUtc(cachePath).Add(cacheLength) > DateTime.UtcNow)
|
2014-05-19 12:51:56 -07:00
|
|
|
|
{
|
2014-06-19 21:50:30 -07:00
|
|
|
|
return _jsonSerializer.DeserializeFromFile<ChannelItemResult>(cachePath);
|
2014-05-19 12:51:56 -07:00
|
|
|
|
}
|
2014-05-04 07:19:46 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (DirectoryNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await _resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2014-05-19 12:51:56 -07:00
|
|
|
|
if (!startIndex.HasValue && !limit.HasValue)
|
2014-05-04 07:19:46 -07:00
|
|
|
|
{
|
2014-06-06 10:14:02 -07:00
|
|
|
|
if (_fileSystem.GetLastWriteTimeUtc(cachePath).Add(cacheLength) > DateTime.UtcNow)
|
2014-05-19 12:51:56 -07:00
|
|
|
|
{
|
2014-06-19 21:50:30 -07:00
|
|
|
|
return _jsonSerializer.DeserializeFromFile<ChannelItemResult>(cachePath);
|
2014-05-19 12:51:56 -07:00
|
|
|
|
}
|
2014-05-04 07:19:46 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (DirectoryNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var query = new InternalChannelItemQuery
|
|
|
|
|
{
|
2014-06-06 10:14:02 -07:00
|
|
|
|
UserId = userId,
|
2014-05-19 12:51:56 -07:00
|
|
|
|
StartIndex = startIndex,
|
2014-05-27 14:17:48 -07:00
|
|
|
|
Limit = limit,
|
|
|
|
|
SortBy = sortField,
|
|
|
|
|
SortDescending = sortDescending
|
2014-05-04 07:19:46 -07:00
|
|
|
|
};
|
|
|
|
|
|
2014-05-23 07:09:58 -07:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(folderId))
|
2014-05-18 12:58:42 -07:00
|
|
|
|
{
|
2014-05-23 07:09:58 -07:00
|
|
|
|
var categoryItem = (IChannelItem)_libraryManager.GetItemById(new Guid(folderId));
|
2014-05-18 12:58:42 -07:00
|
|
|
|
|
2014-05-23 07:09:58 -07:00
|
|
|
|
query.FolderId = categoryItem.ExternalId;
|
2014-05-18 12:58:42 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-04 07:19:46 -07:00
|
|
|
|
var result = await channel.GetChannelItems(query, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
2014-05-19 12:51:56 -07:00
|
|
|
|
if (!startIndex.HasValue && !limit.HasValue)
|
|
|
|
|
{
|
|
|
|
|
CacheResponse(result, cachePath);
|
|
|
|
|
}
|
2014-05-04 07:19:46 -07:00
|
|
|
|
|
2014-05-19 12:51:56 -07:00
|
|
|
|
return result;
|
2014-05-04 07:19:46 -07:00
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
_resourcePool.Release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-19 21:50:30 -07:00
|
|
|
|
private void CacheResponse(object result, string path)
|
2014-05-04 07:19:46 -07:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(path));
|
|
|
|
|
|
|
|
|
|
_jsonSerializer.SerializeToFile(result, path);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error writing to channel cache file: {0}", ex, path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-27 14:17:48 -07:00
|
|
|
|
private string GetChannelDataCachePath(IChannel channel,
|
2014-06-06 10:14:02 -07:00
|
|
|
|
string userId,
|
2014-05-27 14:17:48 -07:00
|
|
|
|
string folderId,
|
|
|
|
|
ChannelItemSortField? sortField,
|
|
|
|
|
bool sortDescending)
|
2014-05-04 07:19:46 -07:00
|
|
|
|
{
|
|
|
|
|
var channelId = GetInternalChannelId(channel.Name).ToString("N");
|
2014-03-17 18:45:41 -07:00
|
|
|
|
|
2014-06-06 10:14:02 -07:00
|
|
|
|
var userCacheKey = string.Empty;
|
|
|
|
|
|
|
|
|
|
var hasCacheKey = channel as IHasCacheKey;
|
|
|
|
|
if (hasCacheKey != null)
|
|
|
|
|
{
|
|
|
|
|
userCacheKey = hasCacheKey.GetCacheKey(userId) ?? string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-07 12:46:24 -07:00
|
|
|
|
var filename = string.IsNullOrWhiteSpace(folderId) ? "root" : folderId;
|
|
|
|
|
filename += userCacheKey;
|
2014-03-18 10:05:57 -07:00
|
|
|
|
|
2014-06-07 12:46:24 -07:00
|
|
|
|
var version = (channel.DataVersion ?? string.Empty).GetMD5().ToString("N");
|
2014-05-27 14:17:48 -07:00
|
|
|
|
|
|
|
|
|
if (sortField.HasValue)
|
|
|
|
|
{
|
|
|
|
|
filename += "-sortField-" + sortField.Value;
|
|
|
|
|
}
|
|
|
|
|
if (sortDescending)
|
|
|
|
|
{
|
|
|
|
|
filename += "-sortDescending";
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-07 12:46:24 -07:00
|
|
|
|
filename = filename.GetMD5().ToString("N");
|
2014-05-27 14:17:48 -07:00
|
|
|
|
|
2014-06-06 10:14:02 -07:00
|
|
|
|
return Path.Combine(_config.ApplicationPaths.CachePath,
|
|
|
|
|
"channels",
|
|
|
|
|
channelId,
|
|
|
|
|
version,
|
|
|
|
|
filename + ".json");
|
2014-03-17 18:45:41 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-19 12:51:56 -07:00
|
|
|
|
private async Task<QueryResult<BaseItemDto>> GetReturnItems(IEnumerable<BaseItem> items, int? totalCountFromProvider, User user, ChannelItemQuery query, CancellationToken cancellationToken)
|
2014-03-17 18:45:41 -07:00
|
|
|
|
{
|
2014-05-04 17:46:52 -07:00
|
|
|
|
items = ApplyFilters(items, query.Filters, user);
|
2014-05-02 21:20:04 -07:00
|
|
|
|
|
2014-05-19 12:51:56 -07:00
|
|
|
|
var sortBy = query.SortBy.Length == 0 ? new[] { ItemSortBy.SortName } : query.SortBy;
|
|
|
|
|
items = _libraryManager.Sort(items, user, sortBy, query.SortOrder ?? SortOrder.Ascending);
|
2014-03-17 18:45:41 -07:00
|
|
|
|
|
2014-05-04 17:46:52 -07:00
|
|
|
|
var all = items.ToList();
|
2014-05-19 12:51:56 -07:00
|
|
|
|
var totalCount = totalCountFromProvider ?? all.Count;
|
2014-05-02 21:20:04 -07:00
|
|
|
|
|
2014-05-19 12:51:56 -07:00
|
|
|
|
if (!totalCountFromProvider.HasValue)
|
2014-05-02 21:20:04 -07:00
|
|
|
|
{
|
2014-05-19 12:51:56 -07:00
|
|
|
|
if (query.StartIndex.HasValue)
|
|
|
|
|
{
|
|
|
|
|
all = all.Skip(query.StartIndex.Value).ToList();
|
|
|
|
|
}
|
|
|
|
|
if (query.Limit.HasValue)
|
|
|
|
|
{
|
|
|
|
|
all = all.Take(query.Limit.Value).ToList();
|
|
|
|
|
}
|
2014-05-02 21:20:04 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await RefreshIfNeeded(all, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
2014-06-07 14:06:01 -07:00
|
|
|
|
var returnItemArray = all.Select(i => _dtoService.GetBaseItemDto(i, query.Fields, user))
|
2014-03-17 18:45:41 -07:00
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
|
|
return new QueryResult<BaseItemDto>
|
|
|
|
|
{
|
|
|
|
|
Items = returnItemArray,
|
2014-05-02 21:20:04 -07:00
|
|
|
|
TotalRecordCount = totalCount
|
2014-03-17 18:45:41 -07:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-18 15:26:06 -07:00
|
|
|
|
private string GetIdToHash(string externalId, IChannel channelProvider)
|
2014-03-17 18:45:41 -07:00
|
|
|
|
{
|
2014-05-02 21:20:04 -07:00
|
|
|
|
// Increment this as needed to force new downloads
|
2014-05-18 15:26:06 -07:00
|
|
|
|
// Incorporate Name because it's being used to convert channel entity to provider
|
2014-06-06 10:14:02 -07:00
|
|
|
|
return externalId + (channelProvider.DataVersion ?? string.Empty) +
|
2014-06-07 12:46:24 -07:00
|
|
|
|
(channelProvider.Name ?? string.Empty) + "16";
|
2014-05-02 21:20:04 -07:00
|
|
|
|
}
|
2014-03-18 10:05:57 -07:00
|
|
|
|
|
2014-05-19 12:51:56 -07:00
|
|
|
|
private async Task<BaseItem> GetChannelItemEntity(ChannelItemInfo info, IChannel channelProvider, Channel internalChannel, CancellationToken cancellationToken)
|
2014-05-02 21:20:04 -07:00
|
|
|
|
{
|
|
|
|
|
BaseItem item;
|
2014-03-18 18:35:40 -07:00
|
|
|
|
Guid id;
|
2014-05-02 21:20:04 -07:00
|
|
|
|
var isNew = false;
|
2014-03-18 18:35:40 -07:00
|
|
|
|
|
2014-05-18 15:26:06 -07:00
|
|
|
|
var idToHash = GetIdToHash(info.Id, channelProvider);
|
|
|
|
|
|
2014-05-23 07:09:58 -07:00
|
|
|
|
if (info.Type == ChannelItemType.Folder)
|
2014-03-18 10:05:57 -07:00
|
|
|
|
{
|
2014-05-23 07:09:58 -07:00
|
|
|
|
id = idToHash.GetMBId(typeof(ChannelFolderItem));
|
2014-05-02 21:20:04 -07:00
|
|
|
|
|
2014-05-23 07:09:58 -07:00
|
|
|
|
item = _libraryManager.GetItemById(id) as ChannelFolderItem;
|
2014-05-02 21:20:04 -07:00
|
|
|
|
|
|
|
|
|
if (item == null)
|
|
|
|
|
{
|
|
|
|
|
isNew = true;
|
2014-05-23 07:09:58 -07:00
|
|
|
|
item = new ChannelFolderItem();
|
2014-05-02 21:20:04 -07:00
|
|
|
|
}
|
2014-03-18 10:05:57 -07:00
|
|
|
|
}
|
|
|
|
|
else if (info.MediaType == ChannelMediaType.Audio)
|
|
|
|
|
{
|
2014-05-23 16:58:28 -07:00
|
|
|
|
id = idToHash.GetMBId(typeof(ChannelAudioItem));
|
2014-05-02 21:20:04 -07:00
|
|
|
|
|
|
|
|
|
item = _libraryManager.GetItemById(id) as ChannelAudioItem;
|
|
|
|
|
|
|
|
|
|
if (item == null)
|
|
|
|
|
{
|
|
|
|
|
isNew = true;
|
|
|
|
|
item = new ChannelAudioItem();
|
|
|
|
|
}
|
2014-03-18 10:05:57 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-05-18 15:26:06 -07:00
|
|
|
|
id = idToHash.GetMBId(typeof(ChannelVideoItem));
|
2014-05-02 21:20:04 -07:00
|
|
|
|
|
|
|
|
|
item = _libraryManager.GetItemById(id) as ChannelVideoItem;
|
|
|
|
|
|
|
|
|
|
if (item == null)
|
|
|
|
|
{
|
|
|
|
|
isNew = true;
|
|
|
|
|
item = new ChannelVideoItem();
|
|
|
|
|
}
|
2014-03-18 10:05:57 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-18 18:35:40 -07:00
|
|
|
|
item.Id = id;
|
2014-03-18 10:05:57 -07:00
|
|
|
|
item.RunTimeTicks = info.RunTimeTicks;
|
2014-05-02 21:20:04 -07:00
|
|
|
|
|
|
|
|
|
if (isNew)
|
|
|
|
|
{
|
|
|
|
|
item.Name = info.Name;
|
|
|
|
|
item.Genres = info.Genres;
|
|
|
|
|
item.Studios = info.Studios;
|
|
|
|
|
item.CommunityRating = info.CommunityRating;
|
|
|
|
|
item.OfficialRating = info.OfficialRating;
|
|
|
|
|
item.Overview = info.Overview;
|
2014-07-07 18:41:03 -07:00
|
|
|
|
item.IndexNumber = info.IndexNumber;
|
|
|
|
|
item.ParentIndexNumber = info.ParentIndexNumber;
|
2014-05-02 21:20:04 -07:00
|
|
|
|
item.People = info.People;
|
|
|
|
|
item.PremiereDate = info.PremiereDate;
|
|
|
|
|
item.ProductionYear = info.ProductionYear;
|
|
|
|
|
item.ProviderIds = info.ProviderIds;
|
|
|
|
|
|
2014-06-19 21:50:30 -07:00
|
|
|
|
item.DateCreated = info.DateCreated.HasValue ?
|
|
|
|
|
info.DateCreated.Value :
|
2014-06-17 22:16:00 -07:00
|
|
|
|
DateTime.UtcNow;
|
2014-05-02 21:20:04 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var channelItem = (IChannelItem)item;
|
|
|
|
|
|
|
|
|
|
channelItem.OriginalImageUrl = info.ImageUrl;
|
|
|
|
|
channelItem.ExternalId = info.Id;
|
2014-05-19 12:51:56 -07:00
|
|
|
|
channelItem.ChannelId = internalChannel.Id.ToString("N");
|
2014-05-02 21:20:04 -07:00
|
|
|
|
channelItem.ChannelItemType = info.Type;
|
|
|
|
|
|
2014-05-18 12:58:42 -07:00
|
|
|
|
if (isNew)
|
|
|
|
|
{
|
|
|
|
|
channelItem.Tags = info.Tags;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-02 21:20:04 -07:00
|
|
|
|
var channelMediaItem = item as IChannelMediaItem;
|
|
|
|
|
|
|
|
|
|
if (channelMediaItem != null)
|
|
|
|
|
{
|
|
|
|
|
channelMediaItem.ContentType = info.ContentType;
|
2014-05-18 12:58:42 -07:00
|
|
|
|
channelMediaItem.ChannelMediaSources = info.MediaSources;
|
|
|
|
|
|
|
|
|
|
var mediaSource = info.MediaSources.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
item.Path = mediaSource == null ? null : mediaSource.Path;
|
2014-07-08 17:46:11 -07:00
|
|
|
|
|
|
|
|
|
item.DisplayMediaType = channelMediaItem.ContentType.ToString();
|
2014-05-02 21:20:04 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isNew)
|
|
|
|
|
{
|
|
|
|
|
await _libraryManager.CreateItem(item, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
_libraryManager.RegisterItem(item);
|
|
|
|
|
}
|
2014-03-18 10:05:57 -07:00
|
|
|
|
|
|
|
|
|
return item;
|
2014-03-17 18:45:41 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-02 21:20:04 -07:00
|
|
|
|
private async Task RefreshIfNeeded(IEnumerable<BaseItem> programs, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
foreach (var program in programs)
|
|
|
|
|
{
|
|
|
|
|
await RefreshIfNeeded(program, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task RefreshIfNeeded(BaseItem program, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2014-06-19 21:50:30 -07:00
|
|
|
|
if (_refreshedItems.ContainsKey(program.Id))
|
2014-05-02 21:20:04 -07:00
|
|
|
|
{
|
2014-06-19 21:50:30 -07:00
|
|
|
|
return;
|
2014-05-02 21:20:04 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await program.RefreshMetadata(cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
2014-06-19 21:50:30 -07:00
|
|
|
|
_refreshedItems.TryAdd(program.Id, true);
|
2014-05-02 21:20:04 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-18 10:05:57 -07:00
|
|
|
|
internal IChannel GetChannelProvider(Channel channel)
|
2014-03-17 18:45:41 -07:00
|
|
|
|
{
|
2014-05-03 16:38:23 -07:00
|
|
|
|
return GetAllChannels().First(i => string.Equals(i.Name, channel.OriginalChannelName, StringComparison.OrdinalIgnoreCase));
|
2014-03-17 18:45:41 -07:00
|
|
|
|
}
|
2014-05-02 21:20:04 -07:00
|
|
|
|
|
|
|
|
|
private IEnumerable<BaseItem> ApplyFilters(IEnumerable<BaseItem> items, IEnumerable<ItemFilter> filters, User user)
|
|
|
|
|
{
|
|
|
|
|
foreach (var filter in filters.OrderByDescending(f => (int)f))
|
|
|
|
|
{
|
|
|
|
|
items = ApplyFilter(items, filter, user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return items;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerable<BaseItem> ApplyFilter(IEnumerable<BaseItem> items, ItemFilter filter, User user)
|
|
|
|
|
{
|
|
|
|
|
// Avoid implicitly captured closure
|
|
|
|
|
var currentUser = user;
|
|
|
|
|
|
|
|
|
|
switch (filter)
|
|
|
|
|
{
|
|
|
|
|
case ItemFilter.IsFavoriteOrLikes:
|
|
|
|
|
return items.Where(item =>
|
|
|
|
|
{
|
|
|
|
|
var userdata = _userDataManager.GetUserData(user.Id, item.GetUserDataKey());
|
|
|
|
|
|
|
|
|
|
if (userdata == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var likes = userdata.Likes ?? false;
|
|
|
|
|
var favorite = userdata.IsFavorite;
|
|
|
|
|
|
|
|
|
|
return likes || favorite;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
case ItemFilter.Likes:
|
|
|
|
|
return items.Where(item =>
|
|
|
|
|
{
|
|
|
|
|
var userdata = _userDataManager.GetUserData(user.Id, item.GetUserDataKey());
|
|
|
|
|
|
|
|
|
|
return userdata != null && userdata.Likes.HasValue && userdata.Likes.Value;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
case ItemFilter.Dislikes:
|
|
|
|
|
return items.Where(item =>
|
|
|
|
|
{
|
|
|
|
|
var userdata = _userDataManager.GetUserData(user.Id, item.GetUserDataKey());
|
|
|
|
|
|
|
|
|
|
return userdata != null && userdata.Likes.HasValue && !userdata.Likes.Value;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
case ItemFilter.IsFavorite:
|
|
|
|
|
return items.Where(item =>
|
|
|
|
|
{
|
|
|
|
|
var userdata = _userDataManager.GetUserData(user.Id, item.GetUserDataKey());
|
|
|
|
|
|
|
|
|
|
return userdata != null && userdata.IsFavorite;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
case ItemFilter.IsResumable:
|
|
|
|
|
return items.Where(item =>
|
|
|
|
|
{
|
|
|
|
|
var userdata = _userDataManager.GetUserData(user.Id, item.GetUserDataKey());
|
|
|
|
|
|
|
|
|
|
return userdata != null && userdata.PlaybackPositionTicks > 0;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
case ItemFilter.IsPlayed:
|
|
|
|
|
return items.Where(item => item.IsPlayed(currentUser));
|
|
|
|
|
|
|
|
|
|
case ItemFilter.IsUnplayed:
|
|
|
|
|
return items.Where(item => item.IsUnplayed(currentUser));
|
|
|
|
|
|
|
|
|
|
case ItemFilter.IsFolder:
|
|
|
|
|
return items.Where(item => item.IsFolder);
|
|
|
|
|
|
|
|
|
|
case ItemFilter.IsNotFolder:
|
|
|
|
|
return items.Where(item => !item.IsFolder);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return items;
|
|
|
|
|
}
|
2014-06-07 12:46:24 -07:00
|
|
|
|
|
|
|
|
|
public async Task<BaseItemDto> GetChannelFolder(string userId, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var user = string.IsNullOrEmpty(userId) ? null : _userManager.GetUserById(new Guid(userId));
|
|
|
|
|
|
|
|
|
|
// Get everything
|
|
|
|
|
var fields = Enum.GetNames(typeof(ItemFields)).Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true)).ToList();
|
|
|
|
|
|
|
|
|
|
var folder = await GetInternalChannelFolder(userId, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
return _dtoService.GetBaseItemDto(folder, fields, user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<Folder> GetInternalChannelFolder(string userId, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var name = _localization.GetLocalizedString("ViewTypeChannels");
|
|
|
|
|
return await _libraryManager.GetNamedView(name, "channels", "zz_" + name, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
2014-06-19 21:50:30 -07:00
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if (_refreshTimer != null)
|
|
|
|
|
{
|
|
|
|
|
_refreshTimer.Dispose();
|
|
|
|
|
_refreshTimer = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-17 18:45:41 -07:00
|
|
|
|
}
|
|
|
|
|
}
|