aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations
diff options
context:
space:
mode:
authorLuke Pulverenti <luke.pulverenti@gmail.com>2016-11-02 16:53:50 -0400
committerLuke Pulverenti <luke.pulverenti@gmail.com>2016-11-02 16:53:50 -0400
commitd71d2a5d02cf31b67420b54160868247f23546bb (patch)
tree264b96d3ac4ca7cf2040344ffa57e8418b2b5554 /Emby.Server.Implementations
parent8f64a5555b055f42d4c3814725a4e961c2fc980d (diff)
move classes to portable server project
Diffstat (limited to 'Emby.Server.Implementations')
-rw-r--r--Emby.Server.Implementations/Channels/ChannelConfigurations.cs29
-rw-r--r--Emby.Server.Implementations/Channels/ChannelDynamicMediaSourceProvider.cs43
-rw-r--r--Emby.Server.Implementations/Channels/ChannelImageProvider.cs55
-rw-r--r--Emby.Server.Implementations/Channels/ChannelManager.cs1620
-rw-r--r--Emby.Server.Implementations/Channels/ChannelPostScanTask.cs257
-rw-r--r--Emby.Server.Implementations/Channels/RefreshChannelsScheduledTask.cs78
-rw-r--r--Emby.Server.Implementations/Emby.Server.Implementations.csproj74
-rw-r--r--Emby.Server.Implementations/Intros/DefaultIntroProvider.cs384
-rw-r--r--Emby.Server.Implementations/News/NewsService.cs77
-rw-r--r--Emby.Server.Implementations/Properties/AssemblyInfo.cs28
-rw-r--r--Emby.Server.Implementations/Updates/InstallationManager.cs687
11 files changed, 3332 insertions, 0 deletions
diff --git a/Emby.Server.Implementations/Channels/ChannelConfigurations.cs b/Emby.Server.Implementations/Channels/ChannelConfigurations.cs
new file mode 100644
index 000000000..ef0973e7f
--- /dev/null
+++ b/Emby.Server.Implementations/Channels/ChannelConfigurations.cs
@@ -0,0 +1,29 @@
+using MediaBrowser.Common.Configuration;
+using MediaBrowser.Model.Configuration;
+using System.Collections.Generic;
+
+namespace Emby.Server.Implementations.Channels
+{
+ public static class ChannelConfigurationExtension
+ {
+ public static ChannelOptions GetChannelsConfiguration(this IConfigurationManager manager)
+ {
+ return manager.GetConfiguration<ChannelOptions>("channels");
+ }
+ }
+
+ public class ChannelConfigurationFactory : IConfigurationFactory
+ {
+ public IEnumerable<ConfigurationStore> GetConfigurations()
+ {
+ return new List<ConfigurationStore>
+ {
+ new ConfigurationStore
+ {
+ Key = "channels",
+ ConfigurationType = typeof (ChannelOptions)
+ }
+ };
+ }
+ }
+}
diff --git a/Emby.Server.Implementations/Channels/ChannelDynamicMediaSourceProvider.cs b/Emby.Server.Implementations/Channels/ChannelDynamicMediaSourceProvider.cs
new file mode 100644
index 000000000..98011ddd4
--- /dev/null
+++ b/Emby.Server.Implementations/Channels/ChannelDynamicMediaSourceProvider.cs
@@ -0,0 +1,43 @@
+using MediaBrowser.Controller.Channels;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Controller.Library;
+using MediaBrowser.Model.Dto;
+using System;
+using System.Collections.Generic;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace Emby.Server.Implementations.Channels
+{
+ public class ChannelDynamicMediaSourceProvider : IMediaSourceProvider
+ {
+ private readonly ChannelManager _channelManager;
+
+ public ChannelDynamicMediaSourceProvider(IChannelManager channelManager)
+ {
+ _channelManager = (ChannelManager)channelManager;
+ }
+
+ public Task<IEnumerable<MediaSourceInfo>> GetMediaSources(IHasMediaSources item, CancellationToken cancellationToken)
+ {
+ var baseItem = (BaseItem) item;
+
+ if (baseItem.SourceType == SourceType.Channel)
+ {
+ return _channelManager.GetDynamicMediaSources(baseItem, cancellationToken);
+ }
+
+ return Task.FromResult<IEnumerable<MediaSourceInfo>>(new List<MediaSourceInfo>());
+ }
+
+ public Task<Tuple<MediaSourceInfo, IDirectStreamProvider>> OpenMediaSource(string openToken, CancellationToken cancellationToken)
+ {
+ throw new NotImplementedException();
+ }
+
+ public Task CloseMediaSource(string liveStreamId)
+ {
+ throw new NotImplementedException();
+ }
+ }
+}
diff --git a/Emby.Server.Implementations/Channels/ChannelImageProvider.cs b/Emby.Server.Implementations/Channels/ChannelImageProvider.cs
new file mode 100644
index 000000000..f892b1e62
--- /dev/null
+++ b/Emby.Server.Implementations/Channels/ChannelImageProvider.cs
@@ -0,0 +1,55 @@
+using MediaBrowser.Controller.Channels;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Controller.Providers;
+using MediaBrowser.Model.Entities;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace Emby.Server.Implementations.Channels
+{
+ public class ChannelImageProvider : IDynamicImageProvider, IHasItemChangeMonitor
+ {
+ private readonly IChannelManager _channelManager;
+
+ public ChannelImageProvider(IChannelManager channelManager)
+ {
+ _channelManager = channelManager;
+ }
+
+ public IEnumerable<ImageType> GetSupportedImages(IHasImages item)
+ {
+ return GetChannel(item).GetSupportedChannelImages();
+ }
+
+ public Task<DynamicImageResponse> GetImage(IHasImages item, ImageType type, CancellationToken cancellationToken)
+ {
+ var channel = GetChannel(item);
+
+ return channel.GetChannelImage(type, cancellationToken);
+ }
+
+ public string Name
+ {
+ get { return "Channel Image Provider"; }
+ }
+
+ public bool Supports(IHasImages item)
+ {
+ return item is Channel;
+ }
+
+ private IChannel GetChannel(IHasImages item)
+ {
+ var channel = (Channel)item;
+
+ return ((ChannelManager)_channelManager).GetChannelProvider(channel);
+ }
+
+ public bool HasChanged(IHasMetadata item, IDirectoryService directoryService)
+ {
+ return GetSupportedImages(item).Any(i => !item.HasImage(i));
+ }
+ }
+}
diff --git a/Emby.Server.Implementations/Channels/ChannelManager.cs b/Emby.Server.Implementations/Channels/ChannelManager.cs
new file mode 100644
index 000000000..2ce880c93
--- /dev/null
+++ b/Emby.Server.Implementations/Channels/ChannelManager.cs
@@ -0,0 +1,1620 @@
+using MediaBrowser.Common.Extensions;
+using MediaBrowser.Common.Net;
+using MediaBrowser.Controller.Channels;
+using MediaBrowser.Controller.Configuration;
+using MediaBrowser.Controller.Dto;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Controller.Library;
+using MediaBrowser.Controller.Providers;
+using MediaBrowser.Model.Channels;
+using MediaBrowser.Model.Dto;
+using MediaBrowser.Model.Entities;
+using MediaBrowser.Model.Extensions;
+using MediaBrowser.Model.Logging;
+using MediaBrowser.Model.MediaInfo;
+using MediaBrowser.Model.Net;
+using MediaBrowser.Model.Querying;
+using MediaBrowser.Model.Serialization;
+using System;
+using System.Collections.Concurrent;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Net;
+using System.Threading;
+using System.Threading.Tasks;
+using MediaBrowser.Common.IO;
+using MediaBrowser.Model.IO;
+using MediaBrowser.Controller.Entities.Audio;
+using MediaBrowser.Controller.Entities.Movies;
+using MediaBrowser.Controller.Entities.TV;
+using MediaBrowser.Controller.IO;
+using MediaBrowser.Model.Globalization;
+
+namespace Emby.Server.Implementations.Channels
+{
+ public class ChannelManager : IChannelManager
+ {
+ private IChannel[] _channels;
+
+ private readonly IUserManager _userManager;
+ private readonly IUserDataManager _userDataManager;
+ private readonly IDtoService _dtoService;
+ private readonly ILibraryManager _libraryManager;
+ private readonly ILogger _logger;
+ private readonly IServerConfigurationManager _config;
+ private readonly IFileSystem _fileSystem;
+ private readonly IJsonSerializer _jsonSerializer;
+ private readonly IHttpClient _httpClient;
+ private readonly IProviderManager _providerManager;
+
+ private readonly ILocalizationManager _localization;
+ private readonly ConcurrentDictionary<Guid, bool> _refreshedItems = new ConcurrentDictionary<Guid, bool>();
+
+ public ChannelManager(IUserManager userManager, IDtoService dtoService, ILibraryManager libraryManager, ILogger logger, IServerConfigurationManager config, IFileSystem fileSystem, IUserDataManager userDataManager, IJsonSerializer jsonSerializer, ILocalizationManager localization, IHttpClient httpClient, IProviderManager providerManager)
+ {
+ _userManager = userManager;
+ _dtoService = dtoService;
+ _libraryManager = libraryManager;
+ _logger = logger;
+ _config = config;
+ _fileSystem = fileSystem;
+ _userDataManager = userDataManager;
+ _jsonSerializer = jsonSerializer;
+ _localization = localization;
+ _httpClient = httpClient;
+ _providerManager = providerManager;
+ }
+
+ private TimeSpan CacheLength
+ {
+ get
+ {
+ return TimeSpan.FromHours(6);
+ }
+ }
+
+ public void AddParts(IEnumerable<IChannel> channels)
+ {
+ _channels = channels.ToArray();
+ }
+
+ public string ChannelDownloadPath
+ {
+ get
+ {
+ var options = _config.GetChannelsConfiguration();
+
+ if (!string.IsNullOrWhiteSpace(options.DownloadPath))
+ {
+ return options.DownloadPath;
+ }
+
+ return Path.Combine(_config.ApplicationPaths.ProgramDataPath, "channels");
+ }
+ }
+
+ private IEnumerable<IChannel> GetAllChannels()
+ {
+ return _channels
+ .OrderBy(i => i.Name);
+ }
+
+ public IEnumerable<Guid> GetInstalledChannelIds()
+ {
+ return GetAllChannels().Select(i => GetInternalChannelId(i.Name));
+ }
+
+ public Task<QueryResult<Channel>> GetChannelsInternal(ChannelQuery query, CancellationToken cancellationToken)
+ {
+ var user = string.IsNullOrWhiteSpace(query.UserId)
+ ? null
+ : _userManager.GetUserById(query.UserId);
+
+ var channels = GetAllChannels()
+ .Select(GetChannelEntity)
+ .OrderBy(i => i.SortName)
+ .ToList();
+
+ if (query.SupportsLatestItems.HasValue)
+ {
+ var val = query.SupportsLatestItems.Value;
+ channels = channels.Where(i =>
+ {
+ try
+ {
+ return GetChannelProvider(i) is ISupportsLatestMedia == val;
+ }
+ catch
+ {
+ return false;
+ }
+
+ }).ToList();
+ }
+ if (query.IsFavorite.HasValue)
+ {
+ var val = query.IsFavorite.Value;
+ channels = channels.Where(i => _userDataManager.GetUserData(user, i).IsFavorite == val)
+ .ToList();
+ }
+
+ if (user != null)
+ {
+ channels = channels.Where(i =>
+ {
+ if (!i.IsVisible(user))
+ {
+ return false;
+ }
+
+ try
+ {
+ return GetChannelProvider(i).IsEnabledFor(user.Id.ToString("N"));
+ }
+ catch
+ {
+ return false;
+ }
+
+ }).ToList();
+ }
+
+ 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();
+ }
+
+ var returnItems = all.ToArray();
+
+ var result = new QueryResult<Channel>
+ {
+ Items = returnItems,
+ TotalRecordCount = totalCount
+ };
+
+ return Task.FromResult(result);
+ }
+
+ public async Task<QueryResult<BaseItemDto>> GetChannels(ChannelQuery query, CancellationToken cancellationToken)
+ {
+ var user = string.IsNullOrWhiteSpace(query.UserId)
+ ? null
+ : _userManager.GetUserById(query.UserId);
+
+ var internalResult = await GetChannelsInternal(query, cancellationToken).ConfigureAwait(false);
+
+ var dtoOptions = new DtoOptions();
+
+ var returnItems = (await _dtoService.GetBaseItemDtos(internalResult.Items, dtoOptions, user).ConfigureAwait(false))
+ .ToArray();
+
+ var result = new QueryResult<BaseItemDto>
+ {
+ Items = returnItems,
+ TotalRecordCount = internalResult.TotalRecordCount
+ };
+
+ return result;
+ }
+
+ public async Task RefreshChannels(IProgress<double> progress, CancellationToken cancellationToken)
+ {
+ _refreshedItems.Clear();
+
+ var allChannelsList = GetAllChannels().ToList();
+
+ var numComplete = 0;
+
+ foreach (var channelInfo in allChannelsList)
+ {
+ cancellationToken.ThrowIfCancellationRequested();
+
+ try
+ {
+ await GetChannel(channelInfo, cancellationToken).ConfigureAwait(false);
+ }
+ 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);
+ }
+
+ progress.Report(100);
+ }
+
+ private Channel GetChannelEntity(IChannel channel)
+ {
+ var item = GetChannel(GetInternalChannelId(channel.Name).ToString("N"));
+
+ if (item == null)
+ {
+ item = GetChannel(channel, CancellationToken.None).Result;
+ }
+
+ return item;
+ }
+
+ private List<ChannelMediaInfo> GetSavedMediaSources(BaseItem item)
+ {
+ var path = Path.Combine(item.GetInternalMetadataPath(), "channelmediasources.json");
+
+ try
+ {
+ return _jsonSerializer.DeserializeFromFile<List<ChannelMediaInfo>>(path) ?? new List<ChannelMediaInfo>();
+ }
+ catch
+ {
+ return new List<ChannelMediaInfo>();
+ }
+ }
+
+ private void SaveMediaSources(BaseItem item, List<ChannelMediaInfo> mediaSources)
+ {
+ var path = Path.Combine(item.GetInternalMetadataPath(), "channelmediasources.json");
+
+ if (mediaSources == null || mediaSources.Count == 0)
+ {
+ try
+ {
+ _fileSystem.DeleteFile(path);
+ }
+ catch
+ {
+
+ }
+ return;
+ }
+
+ _fileSystem.CreateDirectory(Path.GetDirectoryName(path));
+
+ _jsonSerializer.SerializeToFile(mediaSources, path);
+ }
+
+ public async Task<IEnumerable<MediaSourceInfo>> GetStaticMediaSources(BaseItem item, bool includeCachedVersions, CancellationToken cancellationToken)
+ {
+ IEnumerable<ChannelMediaInfo> results = new List<ChannelMediaInfo>();
+ var video = item as Video;
+ if (video != null)
+ {
+ results = video.ChannelMediaSources;
+ }
+ var audio = item as Audio;
+ if (audio != null)
+ {
+ results = audio.ChannelMediaSources ?? GetSavedMediaSources(audio);
+ }
+
+ var sources = SortMediaInfoResults(results)
+ .Select(i => GetMediaSource(item, i))
+ .ToList();
+
+ if (includeCachedVersions)
+ {
+ var cachedVersions = GetCachedChannelItemMediaSources(item);
+ sources.InsertRange(0, cachedVersions);
+ }
+
+ return sources;
+ }
+
+ public async Task<IEnumerable<MediaSourceInfo>> GetDynamicMediaSources(BaseItem item, CancellationToken cancellationToken)
+ {
+ var channel = GetChannel(item.ChannelId);
+ var channelPlugin = GetChannelProvider(channel);
+
+ var requiresCallback = channelPlugin as IRequiresMediaInfoCallback;
+
+ IEnumerable<ChannelMediaInfo> results;
+
+ if (requiresCallback != null)
+ {
+ results = await GetChannelItemMediaSourcesInternal(requiresCallback, item.ExternalId, cancellationToken)
+ .ConfigureAwait(false);
+ }
+ else
+ {
+ results = new List<ChannelMediaInfo>();
+ }
+
+ var list = SortMediaInfoResults(results)
+ .Select(i => GetMediaSource(item, i))
+ .ToList();
+
+ var cachedVersions = GetCachedChannelItemMediaSources(item);
+ list.InsertRange(0, cachedVersions);
+
+ return list;
+ }
+
+ 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;
+ }
+
+ private IEnumerable<MediaSourceInfo> GetCachedChannelItemMediaSources(BaseItem item)
+ {
+ var filenamePrefix = item.Id.ToString("N");
+ var parentPath = Path.Combine(ChannelDownloadPath, item.ChannelId);
+
+ try
+ {
+ var files = _fileSystem.GetFiles(parentPath);
+
+ if (string.Equals(item.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase))
+ {
+ files = files.Where(i => _libraryManager.IsVideoFile(i.FullName));
+ }
+ else
+ {
+ files = files.Where(i => _libraryManager.IsAudioFile(i.FullName));
+ }
+
+ var file = files
+ .FirstOrDefault(i => i.Name.StartsWith(filenamePrefix, StringComparison.OrdinalIgnoreCase));
+
+ if (file != null)
+ {
+ var cachedItem = _libraryManager.ResolvePath(file);
+
+ if (cachedItem != null)
+ {
+ var hasMediaSources = _libraryManager.GetItemById(cachedItem.Id) as IHasMediaSources;
+
+ if (hasMediaSources != null)
+ {
+ var source = hasMediaSources.GetMediaSources(true).FirstOrDefault();
+
+ if (source != null)
+ {
+ return new[] { source };
+ }
+ }
+ }
+ }
+ }
+ catch (IOException)
+ {
+
+ }
+
+ return new List<MediaSourceInfo>();
+ }
+
+ private MediaSourceInfo GetMediaSource(BaseItem item, ChannelMediaInfo info)
+ {
+ var source = info.ToMediaSource();
+
+ source.RunTimeTicks = source.RunTimeTicks ?? item.RunTimeTicks;
+
+ return source;
+ }
+
+ private IEnumerable<ChannelMediaInfo> SortMediaInfoResults(IEnumerable<ChannelMediaInfo> channelMediaSources)
+ {
+ var list = channelMediaSources.ToList();
+
+ var options = _config.GetChannelsConfiguration();
+
+ var width = options.PreferredStreamingWidth;
+
+ if (width.HasValue)
+ {
+ var val = width.Value;
+
+ var res = list
+ .OrderBy(i => i.Width.HasValue && i.Width.Value <= val ? 0 : 1)
+ .ThenBy(i => Math.Abs((i.Width ?? 0) - val))
+ .ThenByDescending(i => i.Width ?? 0)
+ .ThenBy(list.IndexOf)
+ .ToList();
+
+
+ return res;
+ }
+
+ return list
+ .OrderByDescending(i => i.Width ?? 0)
+ .ThenBy(list.IndexOf);
+ }
+
+ private async Task<Channel> GetChannel(IChannel channelInfo, CancellationToken cancellationToken)
+ {
+ var parentFolder = await GetInternalChannelFolder(cancellationToken).ConfigureAwait(false);
+ var parentFolderId = parentFolder.Id;
+
+ var id = GetInternalChannelId(channelInfo.Name);
+ var idString = id.ToString("N");
+
+ var path = Channel.GetInternalMetadataPath(_config.ApplicationPaths.InternalMetadataPath, id);
+
+ var isNew = false;
+ var forceUpdate = false;
+
+ var item = _libraryManager.GetItemById(id) as Channel;
+
+ if (item == null)
+ {
+ item = new Channel
+ {
+ Name = channelInfo.Name,
+ Id = id,
+ DateCreated = _fileSystem.GetCreationTimeUtc(path),
+ DateModified = _fileSystem.GetLastWriteTimeUtc(path)
+ };
+
+ isNew = true;
+ }
+
+ if (!string.Equals(item.Path, path, StringComparison.OrdinalIgnoreCase))
+ {
+ isNew = true;
+ }
+ item.Path = path;
+
+ if (!string.Equals(item.ChannelId, idString, StringComparison.OrdinalIgnoreCase))
+ {
+ forceUpdate = true;
+ }
+ item.ChannelId = idString;
+
+ if (item.ParentId != parentFolderId)
+ {
+ forceUpdate = true;
+ }
+ item.ParentId = parentFolderId;
+
+ item.OfficialRating = GetOfficialRating(channelInfo.ParentalRating);
+ item.Overview = channelInfo.Description;
+ item.HomePageUrl = channelInfo.HomePageUrl;
+
+ if (string.IsNullOrWhiteSpace(item.Name))
+ {
+ item.Name = channelInfo.Name;
+ }
+
+ if (isNew)
+ {
+ await _libraryManager.CreateItem(item, cancellationToken).ConfigureAwait(false);
+ }
+ else if (forceUpdate)
+ {
+ await item.UpdateToRepository(ItemUpdateType.None, cancellationToken).ConfigureAwait(false);
+ }
+
+ await item.RefreshMetadata(new MetadataRefreshOptions(_fileSystem), cancellationToken);
+ return item;
+ }
+
+ 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;
+ }
+ }
+
+ public Channel GetChannel(string id)
+ {
+ return _libraryManager.GetItemById(id) as Channel;
+ }
+
+ public IEnumerable<ChannelFeatures> GetAllChannelFeatures()
+ {
+ return _libraryManager.GetItemList(new InternalItemsQuery
+ {
+ IncludeItemTypes = new[] { typeof(Channel).Name },
+ SortBy = new[] { ItemSortBy.SortName }
+
+ }).Select(i => GetChannelFeatures(i.Id.ToString("N")));
+ }
+
+ public ChannelFeatures GetChannelFeatures(string id)
+ {
+ if (string.IsNullOrWhiteSpace(id))
+ {
+ throw new ArgumentNullException("id");
+ }
+
+ var channel = GetChannel(id);
+ var channelProvider = GetChannelProvider(channel);
+
+ return GetChannelFeaturesDto(channel, channelProvider, channelProvider.GetChannelFeatures());
+ }
+
+ public bool SupportsSync(string channelId)
+ {
+ if (string.IsNullOrWhiteSpace(channelId))
+ {
+ throw new ArgumentNullException("channelId");
+ }
+
+ //var channel = GetChannel(channelId);
+ var channelProvider = GetChannelProvider(channelId);
+
+ return channelProvider.GetChannelFeatures().SupportsContentDownloading;
+ }
+
+ public ChannelFeatures GetChannelFeaturesDto(Channel channel,
+ IChannel provider,
+ InternalChannelFeatures features)
+ {
+ var isIndexable = provider is IIndexableChannel;
+ var supportsLatest = provider is ISupportsLatestMedia;
+
+ return new ChannelFeatures
+ {
+ CanFilter = !features.MaxPageSize.HasValue,
+ CanSearch = provider is ISearchableChannel,
+ ContentTypes = features.ContentTypes,
+ DefaultSortFields = features.DefaultSortFields,
+ MaxPageSize = features.MaxPageSize,
+ MediaTypes = features.MediaTypes,
+ SupportsSortOrderToggle = features.SupportsSortOrderToggle,
+ SupportsLatestMedia = supportsLatest,
+ Name = channel.Name,
+ Id = channel.Id.ToString("N"),
+ SupportsContentDownloading = features.SupportsContentDownloading && (isIndexable || supportsLatest),
+ AutoRefreshLevels = features.AutoRefreshLevels
+ };
+ }
+
+ private Guid GetInternalChannelId(string name)
+ {
+ if (string.IsNullOrWhiteSpace(name))
+ {
+ throw new ArgumentNullException("name");
+ }
+ return _libraryManager.GetNewItemId("Channel " + name, typeof(Channel));
+ }
+
+ public async Task<QueryResult<BaseItemDto>> GetLatestChannelItems(AllChannelMediaQuery query, CancellationToken cancellationToken)
+ {
+ var user = string.IsNullOrWhiteSpace(query.UserId)
+ ? null
+ : _userManager.GetUserById(query.UserId);
+
+ var limit = query.Limit;
+
+ // See below about parental control
+ if (user != null)
+ {
+ query.StartIndex = null;
+ query.Limit = null;
+ }
+
+ var internalResult = await GetLatestChannelItemsInternal(query, cancellationToken).ConfigureAwait(false);
+
+ var items = internalResult.Items;
+ var totalRecordCount = internalResult.TotalRecordCount;
+
+ // Supporting parental control is a hack because it has to be done after querying the remote data source
+ // This will get screwy if apps try to page, so limit to 10 results in an attempt to always keep them on the first page
+ if (user != null)
+ {
+ items = items.Where(i => i.IsVisible(user))
+ .Take(limit ?? 10)
+ .ToArray();
+
+ totalRecordCount = items.Length;
+ }
+
+ var dtoOptions = new DtoOptions();
+
+ var returnItems = (await _dtoService.GetBaseItemDtos(items, dtoOptions, user).ConfigureAwait(false))
+ .ToArray();
+
+ var result = new QueryResult<BaseItemDto>
+ {
+ Items = returnItems,
+ TotalRecordCount = totalRecordCount
+ };
+
+ return result;
+ }
+
+ public async Task<QueryResult<BaseItem>> GetLatestChannelItemsInternal(AllChannelMediaQuery query, CancellationToken cancellationToken)
+ {
+ var user = string.IsNullOrWhiteSpace(query.UserId)
+ ? null
+ : _userManager.GetUserById(query.UserId);
+
+ if (!string.IsNullOrWhiteSpace(query.UserId) && user == null)
+ {
+ throw new ArgumentException("User not found.");
+ }
+
+ var channels = GetAllChannels();
+
+ 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
+ {
+ var result = await GetLatestItems(indexable, i, userId, cancellationToken).ConfigureAwait(false);
+
+ 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
+ .SelectMany(i => i.Item2.Items.Select(m => new Tuple<IChannel, ChannelItemInfo>(i.Item1, m)));
+
+ if (query.ContentTypes.Length > 0)
+ {
+ // Avoid implicitly captured closure
+ var contentTypes = query.ContentTypes;
+
+ items = items.Where(i => contentTypes.Contains(i.Item2.ContentType));
+ }
+ if (query.ExtraTypes.Length > 0)
+ {
+ // Avoid implicitly captured closure
+ var contentTypes = query.ExtraTypes;
+
+ items = items.Where(i => contentTypes.Contains(i.Item2.ExtraType));
+ }
+
+ // Avoid implicitly captured closure
+ var token = cancellationToken;
+ var itemTasks = items.Select(i =>
+ {
+ var channelProvider = i.Item1;
+ var internalChannelId = GetInternalChannelId(channelProvider.Name);
+ return GetChannelItemEntity(i.Item2, channelProvider, internalChannelId, token);
+ });
+
+ var internalItems = await Task.WhenAll(itemTasks).ConfigureAwait(false);
+
+ internalItems = ApplyFilters(internalItems, query.Filters, user).ToArray();
+ RefreshIfNeeded(internalItems);
+
+ if (query.StartIndex.HasValue)
+ {
+ internalItems = internalItems.Skip(query.StartIndex.Value).ToArray();
+ }
+ if (query.Limit.HasValue)
+ {
+ internalItems = internalItems.Take(query.Limit.Value).ToArray();
+ }
+
+ var returnItemArray = internalItems.ToArray();
+
+ return new QueryResult<BaseItem>
+ {
+ TotalRecordCount = totalCount,
+ Items = returnItemArray
+ };
+ }
+
+ private async Task<IEnumerable<ChannelItemInfo>> GetLatestItems(ISupportsLatestMedia indexable, IChannel channel, string userId, CancellationToken cancellationToken)
+ {
+ var cacheLength = CacheLength;
+ 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 (IOException)
+ {
+
+ }
+
+ 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 (IOException)
+ {
+
+ }
+
+ var result = await indexable.GetLatestMedia(new ChannelLatestMediaSearch
+ {
+ UserId = userId
+
+ }, cancellationToken).ConfigureAwait(false);
+
+ var resultItems = result.ToList();
+
+ CacheResponse(resultItems, cachePath);
+
+ return resultItems;
+ }
+ finally
+ {
+ _resourcePool.Release();
+ }
+ }
+
+ public async Task<QueryResult<BaseItem>> GetAllMediaInternal(AllChannelMediaQuery query, CancellationToken cancellationToken)
+ {
+ var channels = GetAllChannels();
+
+ 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();
+ }
+
+ var tasks = channels
+ .Select(async i =>
+ {
+ var indexable = i as IIndexableChannel;
+
+ if (indexable != null)
+ {
+ try
+ {
+ var result = await GetAllItems(indexable, i, new InternalAllChannelMediaQuery
+ {
+ UserId = query.UserId,
+ ContentTypes = query.ContentTypes,
+ ExtraTypes = query.ExtraTypes,
+ TrailerTypes = query.TrailerTypes
+
+ }, cancellationToken).ConfigureAwait(false);
+
+ return new Tuple<IChannel, ChannelItemResult>(i, result);
+ }
+ 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
+ .SelectMany(i => i.Item2.Items.Select(m => new Tuple<IChannel, ChannelItemInfo>(i.Item1, m)))
+ .OrderBy(i => i.Item2.Name);
+
+ 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 internalChannelId = GetInternalChannelId(channelProvider.Name);
+ return GetChannelItemEntity(i.Item2, channelProvider, internalChannelId, token);
+ });
+
+ var internalItems = await Task.WhenAll(itemTasks).ConfigureAwait(false);
+
+ var returnItemArray = internalItems.ToArray();
+
+ return new QueryResult<BaseItem>
+ {
+ TotalRecordCount = totalCount,
+ Items = returnItemArray
+ };
+ }
+
+ public async Task<QueryResult<BaseItemDto>> GetAllMedia(AllChannelMediaQuery query, CancellationToken cancellationToken)
+ {
+ var user = string.IsNullOrWhiteSpace(query.UserId)
+ ? null
+ : _userManager.GetUserById(query.UserId);
+
+ var internalResult = await GetAllMediaInternal(query, cancellationToken).ConfigureAwait(false);
+
+ RefreshIfNeeded(internalResult.Items);
+
+ var dtoOptions = new DtoOptions();
+
+ var returnItems = (await _dtoService.GetBaseItemDtos(internalResult.Items, dtoOptions, user).ConfigureAwait(false))
+ .ToArray();
+
+ var result = new QueryResult<BaseItemDto>
+ {
+ Items = returnItems,
+ TotalRecordCount = internalResult.TotalRecordCount
+ };
+
+ return result;
+ }
+
+ private async Task<ChannelItemResult> GetAllItems(IIndexableChannel indexable, IChannel channel, InternalAllChannelMediaQuery query, CancellationToken cancellationToken)
+ {
+ var cacheLength = CacheLength;
+ var folderId = _jsonSerializer.SerializeToString(query).GetMD5().ToString("N");
+ var cachePath = GetChannelDataCachePath(channel, query.UserId, folderId, null, false);
+
+ try
+ {
+ if (_fileSystem.GetLastWriteTimeUtc(cachePath).Add(cacheLength) > DateTime.UtcNow)
+ {
+ return _jsonSerializer.DeserializeFromFile<ChannelItemResult>(cachePath);
+ }
+ }
+ catch (FileNotFoundException)
+ {
+
+ }
+ catch (IOException)
+ {
+
+ }
+
+ await _resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
+
+ try
+ {
+ try
+ {
+ if (_fileSystem.GetLastWriteTimeUtc(cachePath).Add(cacheLength) > DateTime.UtcNow)
+ {
+ return _jsonSerializer.DeserializeFromFile<ChannelItemResult>(cachePath);
+ }
+ }
+ catch (FileNotFoundException)
+ {
+
+ }
+ catch (IOException)
+ {
+
+ }
+
+ var result = await indexable.GetAllMedia(query, cancellationToken).ConfigureAwait(false);
+
+ CacheResponse(result, cachePath);
+
+ return result;
+ }
+ finally
+ {
+ _resourcePool.Release();
+ }
+ }
+
+ public async Task<QueryResult<BaseItem>> GetChannelItemsInternal(ChannelItemQuery query, IProgress<double> progress, CancellationToken cancellationToken)
+ {
+ // Get the internal channel entity
+ var channel = GetChannel(query.ChannelId);
+
+ // Find the corresponding channel provider plugin
+ var channelProvider = GetChannelProvider(channel);
+
+ var channelInfo = channelProvider.GetChannelFeatures();
+
+ int? providerStartIndex = null;
+ int? providerLimit = null;
+
+ if (channelInfo.MaxPageSize.HasValue)
+ {
+ providerStartIndex = query.StartIndex;
+
+ if (query.Limit.HasValue && query.Limit.Value > channelInfo.MaxPageSize.Value)
+ {
+ query.Limit = Math.Min(query.Limit.Value, channelInfo.MaxPageSize.Value);
+ }
+ providerLimit = query.Limit;
+
+ // This will cause some providers to fail
+ if (providerLimit == 0)
+ {
+ providerLimit = 1;
+ }
+ }
+
+ var user = string.IsNullOrWhiteSpace(query.UserId)
+ ? null
+ : _userManager.GetUserById(query.UserId);
+
+ ChannelItemSortField? sortField = null;
+ ChannelItemSortField parsedField;
+ if (query.SortBy.Length == 1 &&
+ Enum.TryParse(query.SortBy[0], true, out parsedField))
+ {
+ sortField = parsedField;
+ }
+
+ var sortDescending = query.SortOrder.HasValue && query.SortOrder.Value == SortOrder.Descending;
+
+ var itemsResult = await GetChannelItems(channelProvider,
+ user,
+ query.FolderId,
+ providerStartIndex,
+ providerLimit,
+ sortField,
+ sortDescending,
+ cancellationToken)
+ .ConfigureAwait(false);
+
+ var providerTotalRecordCount = providerLimit.HasValue ? itemsResult.TotalRecordCount : null;
+
+ var tasks = itemsResult.Items.Select(i => GetChannelItemEntity(i, channelProvider, channel.Id, cancellationToken));
+
+ var internalItems = await Task.WhenAll(tasks).ConfigureAwait(false);
+
+ if (user != null)
+ {
+ internalItems = internalItems.Where(i => i.IsVisible(user)).ToArray();
+
+ if (providerTotalRecordCount.HasValue)
+ {
+ providerTotalRecordCount = providerTotalRecordCount.Value;
+ }
+ }
+
+ return await GetReturnItems(internalItems, providerTotalRecordCount, user, query).ConfigureAwait(false);
+ }
+
+ public async Task<QueryResult<BaseItemDto>> GetChannelItems(ChannelItemQuery query, CancellationToken cancellationToken)
+ {
+ var user = string.IsNullOrWhiteSpace(query.UserId)
+ ? null
+ : _userManager.GetUserById(query.UserId);
+
+ var internalResult = await GetChannelItemsInternal(query, new Progress<double>(), cancellationToken).ConfigureAwait(false);
+
+ var dtoOptions = new DtoOptions();
+
+ var returnItems = (await _dtoService.GetBaseItemDtos(internalResult.Items, dtoOptions, user).ConfigureAwait(false))
+ .ToArray();
+
+ var result = new QueryResult<BaseItemDto>
+ {
+ Items = returnItems,
+ TotalRecordCount = internalResult.TotalRecordCount
+ };
+
+ return result;
+ }
+
+ private readonly SemaphoreSlim _resourcePool = new SemaphoreSlim(1, 1);
+ private async Task<ChannelItemResult> GetChannelItems(IChannel channel,
+ User user,
+ string folderId,
+ int? startIndex,
+ int? limit,
+ ChannelItemSortField? sortField,
+ bool sortDescending,
+ CancellationToken cancellationToken)
+ {
+ var userId = user.Id.ToString("N");
+
+ var cacheLength = CacheLength;
+ var cachePath = GetChannelDataCachePath(channel, userId, folderId, sortField, sortDescending);
+
+ try
+ {
+ if (!startIndex.HasValue && !limit.HasValue)
+ {
+ if (_fileSystem.GetLastWriteTimeUtc(cachePath).Add(cacheLength) > DateTime.UtcNow)
+ {
+ return _jsonSerializer.DeserializeFromFile<ChannelItemResult>(cachePath);
+ }
+ }
+ }
+ catch (FileNotFoundException)
+ {
+
+ }
+ catch (IOException)
+ {
+
+ }
+
+ await _resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
+
+ try
+ {
+ try
+ {
+ if (!startIndex.HasValue && !limit.HasValue)
+ {
+ if (_fileSystem.GetLastWriteTimeUtc(cachePath).Add(cacheLength) > DateTime.UtcNow)
+ {
+ return _jsonSerializer.DeserializeFromFile<ChannelItemResult>(cachePath);
+ }
+ }
+ }
+ catch (FileNotFoundException)
+ {
+
+ }
+ catch (IOException)
+ {
+
+ }
+
+ var query = new InternalChannelItemQuery
+ {
+ UserId = userId,
+ StartIndex = startIndex,
+ Limit = limit,
+ SortBy = sortField,
+ SortDescending = sortDescending
+ };
+
+ if (!string.IsNullOrWhiteSpace(folderId))
+ {
+ var categoryItem = _libraryManager.GetItemById(new Guid(folderId));
+
+ query.FolderId = categoryItem.ExternalId;
+ }
+
+ var result = await channel.GetChannelItems(query, cancellationToken).ConfigureAwait(false);
+
+ if (!startIndex.HasValue && !limit.HasValue)
+ {
+ CacheResponse(result, cachePath);
+ }
+
+ return result;
+ }
+ finally
+ {
+ _resourcePool.Release();
+ }
+ }
+
+ private void CacheResponse(object result, string path)
+ {
+ try
+ {
+ _fileSystem.CreateDirectory(Path.GetDirectoryName(path));
+
+ _jsonSerializer.SerializeToFile(result, path);
+ }
+ catch (Exception ex)
+ {
+ _logger.ErrorException("Error writing to channel cache file: {0}", ex, path);
+ }
+ }
+
+ private string GetChannelDataCachePath(IChannel channel,
+ string userId,
+ string folderId,
+ ChannelItemSortField? sortField,
+ bool sortDescending)
+ {
+ var channelId = GetInternalChannelId(channel.Name).ToString("N");
+
+ var userCacheKey = string.Empty;
+
+ var hasCacheKey = channel as IHasCacheKey;
+ if (hasCacheKey != null)
+ {
+ userCacheKey = hasCacheKey.GetCacheKey(userId) ?? string.Empty;
+ }
+
+ var filename = string.IsNullOrWhiteSpace(folderId) ? "root" : folderId;
+ filename += userCacheKey;
+
+ var version = (channel.DataVersion ?? string.Empty).GetMD5().ToString("N");
+
+ if (sortField.HasValue)
+ {
+ filename += "-sortField-" + sortField.Value;
+ }
+ if (sortDescending)
+ {
+ filename += "-sortDescending";
+ }
+
+ filename = filename.GetMD5().ToString("N");
+
+ return Path.Combine(_config.ApplicationPaths.CachePath,
+ "channels",
+ channelId,
+ version,
+ filename + ".json");
+ }
+
+ private async Task<QueryResult<BaseItem>> GetReturnItems(IEnumerable<BaseItem> items,
+ int? totalCountFromProvider,
+ User user,
+ ChannelItemQuery query)
+ {
+ items = ApplyFilters(items, query.Filters, user);
+
+ items = _libraryManager.Sort(items, user, query.SortBy, query.SortOrder ?? SortOrder.Ascending);
+
+ var all = items.ToList();
+ var totalCount = totalCountFromProvider ?? all.Count;
+
+ if (!totalCountFromProvider.HasValue)
+ {
+ if (query.StartIndex.HasValue)
+ {
+ all = all.Skip(query.StartIndex.Value).ToList();
+ }
+ if (query.Limit.HasValue)
+ {
+ all = all.Take(query.Limit.Value).ToList();
+ }
+ }
+
+ var returnItemArray = all.ToArray();
+ RefreshIfNeeded(returnItemArray);
+
+ return new QueryResult<BaseItem>
+ {
+ Items = returnItemArray,
+ TotalRecordCount = totalCount
+ };
+ }
+
+ private string GetIdToHash(string externalId, string channelName)
+ {
+ // Increment this as needed to force new downloads
+ // Incorporate Name because it's being used to convert channel entity to provider
+ return externalId + (channelName ?? string.Empty) + "16";
+ }
+
+ private T GetItemById<T>(string idString, string channelName, string channnelDataVersion, out bool isNew)
+ where T : BaseItem, new()
+ {
+ var id = GetIdToHash(idString, channelName).GetMBId(typeof(T));
+
+ T item = null;
+
+ try
+ {
+ item = _libraryManager.GetItemById(id) as T;
+ }
+ catch (Exception ex)
+ {
+ _logger.ErrorException("Error retrieving channel item from database", ex);
+ }
+
+ if (item == null || !string.Equals(item.ExternalEtag, channnelDataVersion, StringComparison.Ordinal))
+ {
+ item = new T();
+ isNew = true;
+ }
+ else
+ {
+ isNew = false;
+ }
+
+ item.ExternalEtag = channnelDataVersion;
+ item.Id = id;
+ return item;
+ }
+
+ private async Task<BaseItem> GetChannelItemEntity(ChannelItemInfo info, IChannel channelProvider, Guid internalChannelId, CancellationToken cancellationToken)
+ {
+ BaseItem item;
+ bool isNew;
+ bool forceUpdate = false;
+
+ if (info.Type == ChannelItemType.Folder)
+ {
+ if (info.FolderType == ChannelFolderType.MusicAlbum)
+ {
+ item = GetItemById<MusicAlbum>(info.Id, channelProvider.Name, channelProvider.DataVersion, out isNew);
+ }
+ else if (info.FolderType == ChannelFolderType.MusicArtist)
+ {
+ item = GetItemById<MusicArtist>(info.Id, channelProvider.Name, channelProvider.DataVersion, out isNew);
+ }
+ else if (info.FolderType == ChannelFolderType.PhotoAlbum)
+ {
+ item = GetItemById<PhotoAlbum>(info.Id, channelProvider.Name, channelProvider.DataVersion, out isNew);
+ }
+ else if (info.FolderType == ChannelFolderType.Series)
+ {
+ item = GetItemById<Series>(info.Id, channelProvider.Name, channelProvider.DataVersion, out isNew);
+ }
+ else if (info.FolderType == ChannelFolderType.Season)
+ {
+ item = GetItemById<Season>(info.Id, channelProvider.Name, channelProvider.DataVersion, out isNew);
+ }
+ else
+ {
+ item = GetItemById<Folder>(info.Id, channelProvider.Name, channelProvider.DataVersion, out isNew);
+ }
+ }
+ else if (info.MediaType == ChannelMediaType.Audio)
+ {
+ if (info.ContentType == ChannelMediaContentType.Podcast)
+ {
+ item = GetItemById<AudioPodcast>(info.Id, channelProvider.Name, channelProvider.DataVersion, out isNew);
+ }
+ else
+ {
+ item = GetItemById<Audio>(info.Id, channelProvider.Name, channelProvider.DataVersion, out isNew);
+ }
+ }
+ else
+ {
+ if (info.ContentType == ChannelMediaContentType.Episode)
+ {
+ item = GetItemById<Episode>(info.Id, channelProvider.Name, channelProvider.DataVersion, out isNew);
+ }
+ else if (info.ContentType == ChannelMediaContentType.Movie)
+ {
+ item = GetItemById<Movie>(info.Id, channelProvider.Name, channelProvider.DataVersion, out isNew);
+ }
+ else if (info.ContentType == ChannelMediaContentType.Trailer || info.ExtraType == ExtraType.Trailer)
+ {
+ item = GetItemById<Trailer>(info.Id, channelProvider.Name, channelProvider.DataVersion, out isNew);
+ }
+ else
+ {
+ item = GetItemById<Video>(info.Id, channelProvider.Name, channelProvider.DataVersion, out isNew);
+ }
+ }
+
+ item.RunTimeTicks = info.RunTimeTicks;
+
+ if (isNew)
+ {
+ item.Name = info.Name;
+ item.Genres = info.Genres;
+ item.Studios = info.Studios;
+ item.CommunityRating = info.CommunityRating;
+ item.Overview = info.Overview;
+ item.IndexNumber = info.IndexNumber;
+ item.ParentIndexNumber = info.ParentIndexNumber;
+ item.PremiereDate = info.PremiereDate;
+ item.ProductionYear = info.ProductionYear;
+ item.ProviderIds = info.ProviderIds;
+ item.OfficialRating = info.OfficialRating;
+ item.DateCreated = info.DateCreated ?? DateTime.UtcNow;
+ item.Tags = info.Tags;
+ item.HomePageUrl = info.HomePageUrl;
+ }
+ else if (info.Type == ChannelItemType.Folder && info.FolderType == ChannelFolderType.Container)
+ {
+ // At least update names of container folders
+ if (item.Name != info.Name)
+ {
+ item.Name = info.Name;
+ forceUpdate = true;
+ }
+ }
+
+ var hasArtists = item as IHasArtist;
+ if (hasArtists != null)
+ {
+ hasArtists.Artists = info.Artists;
+ }
+
+ var hasAlbumArtists = item as IHasAlbumArtist;
+ if (hasAlbumArtists != null)
+ {
+ hasAlbumArtists.AlbumArtists = info.AlbumArtists;
+ }
+
+ var trailer = item as Trailer;
+ if (trailer != null)
+ {
+ if (!info.TrailerTypes.SequenceEqual(trailer.TrailerTypes))
+ {
+ forceUpdate = true;
+ }
+ trailer.TrailerTypes = info.TrailerTypes;
+ }
+
+ item.ChannelId = internalChannelId.ToString("N");
+
+ if (item.ParentId != internalChannelId)
+ {
+ forceUpdate = true;
+ }
+ item.ParentId = internalChannelId;
+
+ if (!string.Equals(item.ExternalId, info.Id, StringComparison.OrdinalIgnoreCase))
+ {
+ forceUpdate = true;
+ }
+ item.ExternalId = info.Id;
+
+ var channelAudioItem = item as Audio;
+ if (channelAudioItem != null)
+ {
+ channelAudioItem.ExtraType = info.ExtraType;
+
+ var mediaSource = info.MediaSources.FirstOrDefault();
+ item.Path = mediaSource == null ? null : mediaSource.Path;
+ }
+
+ var channelVideoItem = item as Video;
+ if (channelVideoItem != null)
+ {
+ channelVideoItem.ExtraType = info.ExtraType;
+ channelVideoItem.ChannelMediaSources = info.MediaSources;
+
+ var mediaSource = info.MediaSources.FirstOrDefault();
+ item.Path = mediaSource == null ? null : mediaSource.Path;
+ }
+
+ if (!string.IsNullOrWhiteSpace(info.ImageUrl) && !item.HasImage(ImageType.Primary))
+ {
+ item.SetImagePath(ImageType.Primary, info.ImageUrl);
+ }
+
+ if (item.SourceType != SourceType.Channel)
+ {
+ item.SourceType = SourceType.Channel;
+ forceUpdate = true;
+ }
+
+ if (isNew)
+ {
+ await _libraryManager.CreateItem(item, cancellationToken).ConfigureAwait(false);
+
+ if (info.People != null && info.People.Count > 0)
+ {
+ await _libraryManager.UpdatePeople(item, info.People ?? new List<PersonInfo>()).ConfigureAwait(false);
+ }
+ }
+ else if (forceUpdate)
+ {
+ await item.UpdateToRepository(ItemUpdateType.None, cancellationToken).ConfigureAwait(false);
+ }
+
+ SaveMediaSources(item, info.MediaSources);
+
+ return item;
+ }
+
+ private void RefreshIfNeeded(BaseItem[] programs)
+ {
+ foreach (var program in programs)
+ {
+ RefreshIfNeeded(program);
+ }
+ }
+
+ private void RefreshIfNeeded(BaseItem program)
+ {
+ if (!_refreshedItems.ContainsKey(program.Id))
+ {
+ _refreshedItems.TryAdd(program.Id, true);
+ _providerManager.QueueRefresh(program.Id, new MetadataRefreshOptions(_fileSystem));
+ }
+
+ }
+
+ internal IChannel GetChannelProvider(Channel channel)
+ {
+ if (channel == null)
+ {
+ throw new ArgumentNullException("channel");
+ }
+
+ var result = GetAllChannels()
+ .FirstOrDefault(i => string.Equals(GetInternalChannelId(i.Name).ToString("N"), channel.ChannelId, StringComparison.OrdinalIgnoreCase) || string.Equals(i.Name, channel.Name, StringComparison.OrdinalIgnoreCase));
+
+ if (result == null)
+ {
+ throw new ResourceNotFoundException("No channel provider found for channel " + channel.Name);
+ }
+
+ return result;
+ }
+
+ internal IChannel GetChannelProvider(string internalChannelId)
+ {
+ if (internalChannelId == null)
+ {
+ throw new ArgumentNullException("internalChannelId");
+ }
+
+ var result = GetAllChannels()
+ .FirstOrDefault(i => string.Equals(GetInternalChannelId(i.Name).ToString("N"), internalChannelId, StringComparison.OrdinalIgnoreCase));
+
+ if (result == null)
+ {
+ throw new ResourceNotFoundException("No channel provider found for channel id " + internalChannelId);
+ }
+
+ return result;
+ }
+
+ 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, item);
+
+ 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, item);
+
+ return userdata != null && userdata.Likes.HasValue && userdata.Likes.Value;
+ });
+
+ case ItemFilter.Dislikes:
+ return items.Where(item =>
+ {
+ var userdata = _userDataManager.GetUserData(user, item);
+
+ return userdata != null && userdata.Likes.HasValue && !userdata.Likes.Value;
+ });
+
+ case ItemFilter.IsFavorite:
+ return items.Where(item =>
+ {
+ var userdata = _userDataManager.GetUserData(user, item);
+
+ return userdata != null && userdata.IsFavorite;
+ });
+
+ case ItemFilter.IsResumable:
+ return items.Where(item =>
+ {
+ var userdata = _userDataManager.GetUserData(user, item);
+
+ 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;
+ }
+
+ public async Task<BaseItemDto> GetChannelFolder(string userId, CancellationToken cancellationToken)
+ {
+ var user = string.IsNullOrEmpty(userId) ? null : _userManager.GetUserById(userId);
+
+ var folder = await GetInternalChannelFolder(cancellationToken).ConfigureAwait(false);
+
+ return _dtoService.GetBaseItemDto(folder, new DtoOptions(), user);
+ }
+
+ public async Task<Folder> GetInternalChannelFolder(CancellationToken cancellationToken)
+ {
+ var name = _localization.GetLocalizedString("ViewTypeChannels");
+
+ return await _libraryManager.GetNamedView(name, "channels", "zz_" + name, cancellationToken).ConfigureAwait(false);
+ }
+ }
+} \ No newline at end of file
diff --git a/Emby.Server.Implementations/Channels/ChannelPostScanTask.cs b/Emby.Server.Implementations/Channels/ChannelPostScanTask.cs
new file mode 100644
index 000000000..aef06bd1d
--- /dev/null
+++ b/Emby.Server.Implementations/Channels/ChannelPostScanTask.cs
@@ -0,0 +1,257 @@
+using MediaBrowser.Common.Progress;
+using MediaBrowser.Controller.Channels;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Controller.Library;
+using MediaBrowser.Model.Channels;
+using MediaBrowser.Model.Logging;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading;
+using System.Threading.Tasks;
+using MediaBrowser.Model.Extensions;
+
+namespace Emby.Server.Implementations.Channels
+{
+ public class ChannelPostScanTask
+ {
+ private readonly IChannelManager _channelManager;
+ private readonly IUserManager _userManager;
+ private readonly ILogger _logger;
+ private readonly ILibraryManager _libraryManager;
+
+ public ChannelPostScanTask(IChannelManager channelManager, IUserManager userManager, ILogger logger, ILibraryManager libraryManager)
+ {
+ _channelManager = channelManager;
+ _userManager = userManager;
+ _logger = logger;
+ _libraryManager = libraryManager;
+ }
+
+ public async Task Run(IProgress<double> progress, CancellationToken cancellationToken)
+ {
+ var users = _userManager.Users
+ .DistinctBy(GetUserDistinctValue)
+ .Select(i => i.Id.ToString("N"))
+ .ToList();
+
+ var numComplete = 0;
+
+ foreach (var user in users)
+ {
+ double percentPerUser = 1;
+ percentPerUser /= users.Count;
+ var startingPercent = numComplete * percentPerUser * 100;
+
+ var innerProgress = new ActionableProgress<double>();
+ innerProgress.RegisterAction(p => progress.Report(startingPercent + percentPerUser * p));
+
+ await DownloadContent(user, cancellationToken, innerProgress).ConfigureAwait(false);
+
+ numComplete++;
+ double percent = numComplete;
+ percent /= users.Count;
+ progress.Report(percent * 100);
+ }
+
+ await CleanDatabase(cancellationToken).ConfigureAwait(false);
+
+ progress.Report(100);
+ }
+
+ public static string GetUserDistinctValue(User user)
+ {
+ var channels = user.Policy.EnabledChannels
+ .OrderBy(i => i)
+ .ToList();
+
+ return string.Join("|", channels.ToArray());
+ }
+
+ private async Task DownloadContent(string user, CancellationToken cancellationToken, IProgress<double> progress)
+ {
+ var channels = await _channelManager.GetChannelsInternal(new ChannelQuery
+ {
+ UserId = user
+
+ }, cancellationToken);
+
+ var numComplete = 0;
+ var numItems = channels.Items.Length;
+
+ foreach (var channel in channels.Items)
+ {
+ var channelId = channel.Id.ToString("N");
+
+ var features = _channelManager.GetChannelFeatures(channelId);
+
+ const int currentRefreshLevel = 1;
+ var maxRefreshLevel = features.AutoRefreshLevels ?? 0;
+ maxRefreshLevel = Math.Max(maxRefreshLevel, 2);
+
+ if (maxRefreshLevel > 0)
+ {
+ var innerProgress = new ActionableProgress<double>();
+
+ var startingNumberComplete = numComplete;
+ innerProgress.RegisterAction(p =>
+ {
+ double innerPercent = startingNumberComplete;
+ innerPercent += p / 100;
+ innerPercent /= numItems;
+ progress.Report(innerPercent * 100);
+ });
+
+ try
+ {
+ await GetAllItems(user, channelId, null, currentRefreshLevel, maxRefreshLevel, innerProgress, cancellationToken).ConfigureAwait(false);
+ }
+ catch (Exception ex)
+ {
+ _logger.ErrorException("Error getting channel content", ex);
+ }
+ }
+
+ numComplete++;
+ double percent = numComplete;
+ percent /= numItems;
+ progress.Report(percent * 100);
+ }
+
+ progress.Report(100);
+ }
+
+ private async Task CleanDatabase(CancellationToken cancellationToken)
+ {
+ var installedChannelIds = ((ChannelManager)_channelManager).GetInstalledChannelIds();
+
+ var databaseIds = _libraryManager.GetItemIds(new InternalItemsQuery
+ {
+ IncludeItemTypes = new[] { typeof(Channel).Name }
+ });
+
+ var invalidIds = databaseIds
+ .Except(installedChannelIds)
+ .ToList();
+
+ foreach (var id in invalidIds)
+ {
+ cancellationToken.ThrowIfCancellationRequested();
+
+ await CleanChannel(id, cancellationToken).ConfigureAwait(false);
+ }
+ }
+
+ private async Task CleanChannel(Guid id, CancellationToken cancellationToken)
+ {
+ _logger.Info("Cleaning channel {0} from database", id);
+
+ // Delete all channel items
+ var allIds = _libraryManager.GetItemIds(new InternalItemsQuery
+ {
+ ChannelIds = new[] { id.ToString("N") }
+ });
+
+ foreach (var deleteId in allIds)
+ {
+ cancellationToken.ThrowIfCancellationRequested();
+
+ await DeleteItem(deleteId).ConfigureAwait(false);
+ }
+
+ // Finally, delete the channel itself
+ await DeleteItem(id).ConfigureAwait(false);
+ }
+
+ private Task DeleteItem(Guid id)
+ {
+ var item = _libraryManager.GetItemById(id);
+
+ if (item == null)
+ {
+ return Task.FromResult(true);
+ }
+
+ return _libraryManager.DeleteItem(item, new DeleteOptions
+ {
+ DeleteFileLocation = false
+ });
+ }
+
+ private async Task GetAllItems(string user, string channelId, string folderId, int currentRefreshLevel, int maxRefreshLevel, IProgress<double> progress, CancellationToken cancellationToken)
+ {
+ var folderItems = new List<string>();
+
+ var innerProgress = new ActionableProgress<double>();
+ innerProgress.RegisterAction(p => progress.Report(p / 2));
+
+ var result = await _channelManager.GetChannelItemsInternal(new ChannelItemQuery
+ {
+ ChannelId = channelId,
+ UserId = user,
+ FolderId = folderId
+
+ }, innerProgress, cancellationToken);
+
+ folderItems.AddRange(result.Items.Where(i => i.IsFolder).Select(i => i.Id.ToString("N")));
+
+ var totalRetrieved = result.Items.Length;
+ var totalCount = result.TotalRecordCount;
+
+ while (totalRetrieved < totalCount)
+ {
+ result = await _channelManager.GetChannelItemsInternal(new ChannelItemQuery
+ {
+ ChannelId = channelId,
+ UserId = user,
+ StartIndex = totalRetrieved,
+ FolderId = folderId
+
+ }, new Progress<double>(), cancellationToken);
+
+ folderItems.AddRange(result.Items.Where(i => i.IsFolder).Select(i => i.Id.ToString("N")));
+
+ totalRetrieved += result.Items.Length;
+ totalCount = result.TotalRecordCount;
+ }
+
+ progress.Report(50);
+
+ if (currentRefreshLevel < maxRefreshLevel)
+ {
+ var numComplete = 0;
+ var numItems = folderItems.Count;
+
+ foreach (var folder in folderItems)
+ {
+ try
+ {
+ innerProgress = new ActionableProgress<double>();
+
+ var startingNumberComplete = numComplete;
+ innerProgress.RegisterAction(p =>
+ {
+ double innerPercent = startingNumberComplete;
+ innerPercent += p / 100;
+ innerPercent /= numItems;
+ progress.Report(innerPercent * 50 + 50);
+ });
+
+ await GetAllItems(user, channelId, folder, currentRefreshLevel + 1, maxRefreshLevel, innerProgress, cancellationToken).ConfigureAwait(false);
+ }
+ catch (Exception ex)
+ {
+ _logger.ErrorException("Error getting channel content", ex);
+ }
+
+ numComplete++;
+ double percent = numComplete;
+ percent /= numItems;
+ progress.Report(percent * 50 + 50);
+ }
+ }
+
+ progress.Report(100);
+ }
+ }
+}
diff --git a/Emby.Server.Implementations/Channels/RefreshChannelsScheduledTask.cs b/Emby.Server.Implementations/Channels/RefreshChannelsScheduledTask.cs
new file mode 100644
index 000000000..d5ec86445
--- /dev/null
+++ b/Emby.Server.Implementations/Channels/RefreshChannelsScheduledTask.cs
@@ -0,0 +1,78 @@
+using MediaBrowser.Controller.Channels;
+using MediaBrowser.Controller.Library;
+using MediaBrowser.Model.Logging;
+using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using MediaBrowser.Model.Tasks;
+
+namespace Emby.Server.Implementations.Channels
+{
+ class RefreshChannelsScheduledTask : IScheduledTask
+ {
+ private readonly IChannelManager _channelManager;
+ private readonly IUserManager _userManager;
+ private readonly ILogger _logger;
+ private readonly ILibraryManager _libraryManager;
+
+ public RefreshChannelsScheduledTask(IChannelManager channelManager, IUserManager userManager, ILogger logger, ILibraryManager libraryManager)
+ {
+ _channelManager = channelManager;
+ _userManager = userManager;
+ _logger = logger;
+ _libraryManager = libraryManager;
+ }
+
+ public string Name
+ {
+ get { return "Refresh Channels"; }
+ }
+
+ public string Description
+ {
+ get { return "Refreshes internet channel information."; }
+ }
+
+ public string Category
+ {
+ get { return "Internet Channels"; }
+ }
+
+ public async Task Execute(System.Threading.CancellationToken cancellationToken, IProgress<double> progress)
+ {
+ var manager = (ChannelManager)_channelManager;
+
+ await manager.RefreshChannels(new Progress<double>(), cancellationToken).ConfigureAwait(false);
+
+ await new ChannelPostScanTask(_channelManager, _userManager, _logger, _libraryManager).Run(progress, cancellationToken)
+ .ConfigureAwait(false);
+ }
+
+ /// <summary>
+ /// Creates the triggers that define when the task will run
+ /// </summary>
+ public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
+ {
+ return new[] {
+
+ // Every so often
+ new TaskTriggerInfo { Type = TaskTriggerInfo.TriggerInterval, IntervalTicks = TimeSpan.FromHours(24).Ticks}
+ };
+ }
+
+ public string Key
+ {
+ get { return "RefreshInternetChannels"; }
+ }
+
+ public bool IsHidden
+ {
+ get { return false; }
+ }
+
+ public bool IsEnabled
+ {
+ get { return true; }
+ }
+ }
+}
diff --git a/Emby.Server.Implementations/Emby.Server.Implementations.csproj b/Emby.Server.Implementations/Emby.Server.Implementations.csproj
new file mode 100644
index 000000000..317b1b9b8
--- /dev/null
+++ b/Emby.Server.Implementations/Emby.Server.Implementations.csproj
@@ -0,0 +1,74 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
+ <PropertyGroup>
+ <MinimumVisualStudioVersion>11.0</MinimumVisualStudioVersion>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProjectGuid>{D08B8079-08B3-48F2-83C4-E9CCCE48AFF1}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>Emby.Server.Implementations</RootNamespace>
+ <AssemblyName>Emby.Server.Implementations</AssemblyName>
+ <DefaultLanguage>en-US</DefaultLanguage>
+ <FileAlignment>512</FileAlignment>
+ <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+ <TargetFrameworkProfile>Profile75</TargetFrameworkProfile>
+ <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <!-- A reference to the entire .NET Framework is automatically included -->
+ <ProjectReference Include="..\MediaBrowser.Common\MediaBrowser.Common.csproj">
+ <Project>{9142eefa-7570-41e1-bfcc-468bb571af2f}</Project>
+ <Name>MediaBrowser.Common</Name>
+ </ProjectReference>
+ <ProjectReference Include="..\MediaBrowser.Controller\MediaBrowser.Controller.csproj">
+ <Project>{17e1f4e6-8abd-4fe5-9ecf-43d4b6087ba2}</Project>
+ <Name>MediaBrowser.Controller</Name>
+ </ProjectReference>
+ <ProjectReference Include="..\MediaBrowser.Model\MediaBrowser.Model.csproj">
+ <Project>{7eeeb4bb-f3e8-48fc-b4c5-70f0fff8329b}</Project>
+ <Name>MediaBrowser.Model</Name>
+ </ProjectReference>
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="..\SharedVersion.cs">
+ <Link>Properties\SharedVersion.cs</Link>
+ </Compile>
+ <Compile Include="Channels\ChannelConfigurations.cs" />
+ <Compile Include="Channels\ChannelDynamicMediaSourceProvider.cs" />
+ <Compile Include="Channels\ChannelImageProvider.cs" />
+ <Compile Include="Channels\ChannelManager.cs" />
+ <Compile Include="Channels\ChannelPostScanTask.cs" />
+ <Compile Include="Channels\RefreshChannelsScheduledTask.cs" />
+ <Compile Include="Intros\DefaultIntroProvider.cs" />
+ <Compile Include="News\NewsService.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="Updates\InstallationManager.cs" />
+ </ItemGroup>
+ <Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+</Project> \ No newline at end of file
diff --git a/Emby.Server.Implementations/Intros/DefaultIntroProvider.cs b/Emby.Server.Implementations/Intros/DefaultIntroProvider.cs
new file mode 100644
index 000000000..180f6aba7
--- /dev/null
+++ b/Emby.Server.Implementations/Intros/DefaultIntroProvider.cs
@@ -0,0 +1,384 @@
+using MediaBrowser.Common.Configuration;
+using MediaBrowser.Common.Security;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Controller.Entities.Movies;
+using MediaBrowser.Controller.Entities.TV;
+using MediaBrowser.Controller.Library;
+using MediaBrowser.Model.Configuration;
+using MediaBrowser.Model.Entities;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Threading.Tasks;
+using MediaBrowser.Model.IO;
+using MediaBrowser.Model.Extensions;
+using MediaBrowser.Model.Globalization;
+
+namespace Emby.Server.Implementations.Intros
+{
+ public class DefaultIntroProvider : IIntroProvider
+ {
+ private readonly ISecurityManager _security;
+ private readonly ILocalizationManager _localization;
+ private readonly IConfigurationManager _serverConfig;
+ private readonly ILibraryManager _libraryManager;
+ private readonly IFileSystem _fileSystem;
+ private readonly IMediaSourceManager _mediaSourceManager;
+
+ public DefaultIntroProvider(ISecurityManager security, ILocalizationManager localization, IConfigurationManager serverConfig, ILibraryManager libraryManager, IFileSystem fileSystem, IMediaSourceManager mediaSourceManager)
+ {
+ _security = security;
+ _localization = localization;
+ _serverConfig = serverConfig;
+ _libraryManager = libraryManager;
+ _fileSystem = fileSystem;
+ _mediaSourceManager = mediaSourceManager;
+ }
+
+ public async Task<IEnumerable<IntroInfo>> GetIntros(BaseItem item, User user)
+ {
+ var config = GetOptions();
+
+ if (item is Movie)
+ {
+ if (!config.EnableIntrosForMovies)
+ {
+ return new List<IntroInfo>();
+ }
+ }
+ else if (item is Episode)
+ {
+ if (!config.EnableIntrosForEpisodes)
+ {
+ return new List<IntroInfo>();
+ }
+ }
+ else
+ {
+ return new List<IntroInfo>();
+ }
+
+ var ratingLevel = string.IsNullOrWhiteSpace(item.OfficialRating)
+ ? null
+ : _localization.GetRatingLevel(item.OfficialRating);
+
+ var candidates = new List<ItemWithTrailer>();
+
+ var trailerTypes = new List<TrailerType>();
+ var sourceTypes = new List<SourceType>();
+
+ if (config.EnableIntrosFromMoviesInLibrary)
+ {
+ trailerTypes.Add(TrailerType.LocalTrailer);
+ sourceTypes.Add(SourceType.Library);
+ }
+
+ if (IsSupporter)
+ {
+ if (config.EnableIntrosFromUpcomingTrailers)
+ {
+ trailerTypes.Add(TrailerType.ComingSoonToTheaters);
+ sourceTypes.Clear();
+ }
+ if (config.EnableIntrosFromUpcomingDvdMovies)
+ {
+ trailerTypes.Add(TrailerType.ComingSoonToDvd);
+ sourceTypes.Clear();
+ }
+ if (config.EnableIntrosFromUpcomingStreamingMovies)
+ {
+ trailerTypes.Add(TrailerType.ComingSoonToStreaming);
+ sourceTypes.Clear();
+ }
+ if (config.EnableIntrosFromSimilarMovies)
+ {
+ trailerTypes.Add(TrailerType.Archive);
+ sourceTypes.Clear();
+ }
+ }
+
+ if (trailerTypes.Count > 0)
+ {
+ var trailerResult = _libraryManager.GetItemList(new InternalItemsQuery
+ {
+ IncludeItemTypes = new[] { typeof(Trailer).Name },
+ TrailerTypes = trailerTypes.ToArray(),
+ SimilarTo = item,
+ IsPlayed = config.EnableIntrosForWatchedContent ? (bool?)null : false,
+ MaxParentalRating = config.EnableIntrosParentalControl ? ratingLevel : null,
+ BlockUnratedItems = config.EnableIntrosParentalControl ? new[] { UnratedItem.Trailer } : new UnratedItem[] { },
+
+ // Account for duplicates by imdb id, since the database doesn't support this yet
+ Limit = config.TrailerLimit * 2,
+ SourceTypes = sourceTypes.ToArray()
+
+ }).Where(i => string.IsNullOrWhiteSpace(i.GetProviderId(MetadataProviders.Imdb)) || !string.Equals(i.GetProviderId(MetadataProviders.Imdb), item.GetProviderId(MetadataProviders.Imdb), StringComparison.OrdinalIgnoreCase)).Take(config.TrailerLimit);
+
+ candidates.AddRange(trailerResult.Select(i => new ItemWithTrailer
+ {
+ Item = i,
+ Type = i.SourceType == SourceType.Channel ? ItemWithTrailerType.ChannelTrailer : ItemWithTrailerType.ItemWithTrailer,
+ LibraryManager = _libraryManager
+ }));
+ }
+
+ return GetResult(item, candidates, config);
+ }
+
+ private IEnumerable<IntroInfo> GetResult(BaseItem item, IEnumerable<ItemWithTrailer> candidates, CinemaModeConfiguration config)
+ {
+ var customIntros = !string.IsNullOrWhiteSpace(config.CustomIntroPath) ?
+ GetCustomIntros(config) :
+ new List<IntroInfo>();
+
+ var mediaInfoIntros = !string.IsNullOrWhiteSpace(config.MediaInfoIntroPath) ?
+ GetMediaInfoIntros(config, item) :
+ new List<IntroInfo>();
+
+ // Avoid implicitly captured closure
+ return candidates.Select(i => i.IntroInfo)
+ .Concat(customIntros.Take(1))
+ .Concat(mediaInfoIntros);
+ }
+
+ private CinemaModeConfiguration GetOptions()
+ {
+ return _serverConfig.GetConfiguration<CinemaModeConfiguration>("cinemamode");
+ }
+
+ private List<IntroInfo> GetCustomIntros(CinemaModeConfiguration options)
+ {
+ try
+ {
+ return GetCustomIntroFiles(options, true, false)
+ .OrderBy(i => Guid.NewGuid())
+ .Select(i => new IntroInfo
+ {
+ Path = i
+
+ }).ToList();
+ }
+ catch (IOException)
+ {
+ return new List<IntroInfo>();
+ }
+ }
+
+ private IEnumerable<IntroInfo> GetMediaInfoIntros(CinemaModeConfiguration options, BaseItem item)
+ {
+ try
+ {
+ var hasMediaSources = item as IHasMediaSources;
+
+ if (hasMediaSources == null)
+ {
+ return new List<IntroInfo>();
+ }
+
+ var mediaSource = _mediaSourceManager.GetStaticMediaSources(hasMediaSources, false)
+ .FirstOrDefault();
+
+ if (mediaSource == null)
+ {
+ return new List<IntroInfo>();
+ }
+
+ var videoStream = mediaSource.MediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Video);
+ var audioStream = mediaSource.MediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Audio);
+
+ var allIntros = GetCustomIntroFiles(options, false, true)
+ .OrderBy(i => Guid.NewGuid())
+ .Select(i => new IntroInfo
+ {
+ Path = i
+
+ }).ToList();
+
+ var returnResult = new List<IntroInfo>();
+
+ if (videoStream != null)
+ {
+ returnResult.AddRange(GetMediaInfoIntrosByVideoStream(allIntros, videoStream).Take(1));
+ }
+
+ if (audioStream != null)
+ {
+ returnResult.AddRange(GetMediaInfoIntrosByAudioStream(allIntros, audioStream).Take(1));
+ }
+
+ returnResult.AddRange(GetMediaInfoIntrosByTags(allIntros, item.Tags).Take(1));
+
+ return returnResult.DistinctBy(i => i.Path, StringComparer.OrdinalIgnoreCase);
+ }
+ catch (IOException)
+ {
+ return new List<IntroInfo>();
+ }
+ }
+
+ private IEnumerable<IntroInfo> GetMediaInfoIntrosByVideoStream(List<IntroInfo> allIntros, MediaStream stream)
+ {
+ var codec = stream.Codec;
+
+ if (string.IsNullOrWhiteSpace(codec))
+ {
+ return new List<IntroInfo>();
+ }
+
+ return allIntros
+ .Where(i => IsMatch(i.Path, codec))
+ .OrderBy(i => Guid.NewGuid());
+ }
+
+ private IEnumerable<IntroInfo> GetMediaInfoIntrosByAudioStream(List<IntroInfo> allIntros, MediaStream stream)
+ {
+ var codec = stream.Codec;
+
+ if (string.IsNullOrWhiteSpace(codec))
+ {
+ return new List<IntroInfo>();
+ }
+
+ return allIntros
+ .Where(i => IsAudioMatch(i.Path, stream))
+ .OrderBy(i => Guid.NewGuid());
+ }
+
+ private IEnumerable<IntroInfo> GetMediaInfoIntrosByTags(List<IntroInfo> allIntros, List<string> tags)
+ {
+ return allIntros
+ .Where(i => tags.Any(t => IsMatch(i.Path, t)))
+ .OrderBy(i => Guid.NewGuid());
+ }
+
+ private bool IsMatch(string file, string attribute)
+ {
+ var filename = Path.GetFileNameWithoutExtension(file) ?? string.Empty;
+ filename = Normalize(filename);
+
+ if (string.IsNullOrWhiteSpace(filename))
+ {
+ return false;
+ }
+
+ attribute = Normalize(attribute);
+ if (string.IsNullOrWhiteSpace(attribute))
+ {
+ return false;
+ }
+
+ return string.Equals(filename, attribute, StringComparison.OrdinalIgnoreCase);
+ }
+
+ private string Normalize(string value)
+ {
+ return value;
+ }
+
+ private bool IsAudioMatch(string path, MediaStream stream)
+ {
+ if (!string.IsNullOrWhiteSpace(stream.Codec))
+ {
+ if (IsMatch(path, stream.Codec))
+ {
+ return true;
+ }
+ }
+ if (!string.IsNullOrWhiteSpace(stream.Profile))
+ {
+ if (IsMatch(path, stream.Profile))
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ private IEnumerable<string> GetCustomIntroFiles(CinemaModeConfiguration options, bool enableCustomIntros, bool enableMediaInfoIntros)
+ {
+ var list = new List<string>();
+
+ if (enableCustomIntros && !string.IsNullOrWhiteSpace(options.CustomIntroPath))
+ {
+ list.AddRange(_fileSystem.GetFilePaths(options.CustomIntroPath, true)
+ .Where(_libraryManager.IsVideoFile));
+ }
+
+ if (enableMediaInfoIntros && !string.IsNullOrWhiteSpace(options.MediaInfoIntroPath))
+ {
+ list.AddRange(_fileSystem.GetFilePaths(options.MediaInfoIntroPath, true)
+ .Where(_libraryManager.IsVideoFile));
+ }
+
+ return list.Distinct(StringComparer.OrdinalIgnoreCase);
+ }
+
+ public IEnumerable<string> GetAllIntroFiles()
+ {
+ return GetCustomIntroFiles(GetOptions(), true, true);
+ }
+
+ private bool IsSupporter
+ {
+ get { return _security.IsMBSupporter; }
+ }
+
+ public string Name
+ {
+ get { return "Default"; }
+ }
+
+ internal class ItemWithTrailer
+ {
+ internal BaseItem Item;
+ internal ItemWithTrailerType Type;
+ internal ILibraryManager LibraryManager;
+
+ public IntroInfo IntroInfo
+ {
+ get
+ {
+ var id = Item.Id;
+
+ if (Type == ItemWithTrailerType.ItemWithTrailer)
+ {
+ var hasTrailers = Item as IHasTrailers;
+
+ if (hasTrailers != null)
+ {
+ id = hasTrailers.LocalTrailerIds.FirstOrDefault();
+ }
+ }
+ return new IntroInfo
+ {
+ ItemId = id
+ };
+ }
+ }
+ }
+
+ internal enum ItemWithTrailerType
+ {
+ ChannelTrailer,
+ ItemWithTrailer
+ }
+ }
+
+ public class CinemaModeConfigurationFactory : IConfigurationFactory
+ {
+ public IEnumerable<ConfigurationStore> GetConfigurations()
+ {
+ return new[]
+ {
+ new ConfigurationStore
+ {
+ ConfigurationType = typeof(CinemaModeConfiguration),
+ Key = "cinemamode"
+ }
+ };
+ }
+ }
+
+}
diff --git a/Emby.Server.Implementations/News/NewsService.cs b/Emby.Server.Implementations/News/NewsService.cs
new file mode 100644
index 000000000..80e799634
--- /dev/null
+++ b/Emby.Server.Implementations/News/NewsService.cs
@@ -0,0 +1,77 @@
+using MediaBrowser.Common.Configuration;
+using MediaBrowser.Model.News;
+using MediaBrowser.Model.Querying;
+using MediaBrowser.Model.Serialization;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+
+namespace Emby.Server.Implementations.News
+{
+ public class NewsService : INewsService
+ {
+ private readonly IApplicationPaths _appPaths;
+ private readonly IJsonSerializer _json;
+
+ public NewsService(IApplicationPaths appPaths, IJsonSerializer json)
+ {
+ _appPaths = appPaths;
+ _json = json;
+ }
+
+ public QueryResult<NewsItem> GetProductNews(NewsQuery query)
+ {
+ try
+ {
+ return GetProductNewsInternal(query);
+ }
+ catch (FileNotFoundException)
+ {
+ // No biggie
+ return new QueryResult<NewsItem>
+ {
+ Items = new NewsItem[] { }
+ };
+ }
+ catch (IOException)
+ {
+ // No biggie
+ return new QueryResult<NewsItem>
+ {
+ Items = new NewsItem[] { }
+ };
+ }
+ }
+
+ private QueryResult<NewsItem> GetProductNewsInternal(NewsQuery query)
+ {
+ var path = Path.Combine(_appPaths.CachePath, "news.json");
+
+ var items = GetNewsItems(path).OrderByDescending(i => i.Date);
+
+ var itemsArray = items.ToArray();
+ var count = itemsArray.Length;
+
+ if (query.StartIndex.HasValue)
+ {
+ itemsArray = itemsArray.Skip(query.StartIndex.Value).ToArray();
+ }
+
+ if (query.Limit.HasValue)
+ {
+ itemsArray = itemsArray.Take(query.Limit.Value).ToArray();
+ }
+
+ return new QueryResult<NewsItem>
+ {
+ Items = itemsArray,
+ TotalRecordCount = count
+ };
+ }
+
+ private IEnumerable<NewsItem> GetNewsItems(string path)
+ {
+ return _json.DeserializeFromFile<List<NewsItem>>(path);
+ }
+ }
+}
diff --git a/Emby.Server.Implementations/Properties/AssemblyInfo.cs b/Emby.Server.Implementations/Properties/AssemblyInfo.cs
new file mode 100644
index 000000000..28ffcbac6
--- /dev/null
+++ b/Emby.Server.Implementations/Properties/AssemblyInfo.cs
@@ -0,0 +1,28 @@
+using System.Resources;
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("Emby.Server.Implementations")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("Emby.Server.Implementations")]
+[assembly: AssemblyCopyright("Copyright © 2016")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+[assembly: NeutralResourcesLanguage("en")]
+
+// Version information for an assembly consists of the following four values:
+//
+// Major Version
+// Minor Version
+// Build Number
+// Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")] \ No newline at end of file
diff --git a/Emby.Server.Implementations/Updates/InstallationManager.cs b/Emby.Server.Implementations/Updates/InstallationManager.cs
new file mode 100644
index 000000000..943977a28
--- /dev/null
+++ b/Emby.Server.Implementations/Updates/InstallationManager.cs
@@ -0,0 +1,687 @@
+using System;
+using System.Collections.Concurrent;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Threading;
+using System.Threading.Tasks;
+using MediaBrowser.Common;
+using MediaBrowser.Common.Configuration;
+using MediaBrowser.Common.Events;
+using MediaBrowser.Common.Net;
+using MediaBrowser.Common.Plugins;
+using MediaBrowser.Common.Progress;
+using MediaBrowser.Common.Security;
+using MediaBrowser.Common.Updates;
+using MediaBrowser.Model.Cryptography;
+using MediaBrowser.Model.Events;
+using MediaBrowser.Model.IO;
+using MediaBrowser.Model.Logging;
+using MediaBrowser.Model.Serialization;
+using MediaBrowser.Model.Updates;
+
+namespace Emby.Server.Implementations.Updates
+{
+ /// <summary>
+ /// Manages all install, uninstall and update operations (both plugins and system)
+ /// </summary>
+ public class InstallationManager : IInstallationManager
+ {
+ public event EventHandler<InstallationEventArgs> PackageInstalling;
+ public event EventHandler<InstallationEventArgs> PackageInstallationCompleted;
+ public event EventHandler<InstallationFailedEventArgs> PackageInstallationFailed;
+ public event EventHandler<InstallationEventArgs> PackageInstallationCancelled;
+
+ /// <summary>
+ /// The current installations
+ /// </summary>
+ public List<Tuple<InstallationInfo, CancellationTokenSource>> CurrentInstallations { get; set; }
+
+ /// <summary>
+ /// The completed installations
+ /// </summary>
+ public ConcurrentBag<InstallationInfo> CompletedInstallations { get; set; }
+
+ #region PluginUninstalled Event
+ /// <summary>
+ /// Occurs when [plugin uninstalled].
+ /// </summary>
+ public event EventHandler<GenericEventArgs<IPlugin>> PluginUninstalled;
+
+ /// <summary>
+ /// Called when [plugin uninstalled].
+ /// </summary>
+ /// <param name="plugin">The plugin.</param>
+ private void OnPluginUninstalled(IPlugin plugin)
+ {
+ EventHelper.FireEventIfNotNull(PluginUninstalled, this, new GenericEventArgs<IPlugin> { Argument = plugin }, _logger);
+ }
+ #endregion
+
+ #region PluginUpdated Event
+ /// <summary>
+ /// Occurs when [plugin updated].
+ /// </summary>
+ public event EventHandler<GenericEventArgs<Tuple<IPlugin, PackageVersionInfo>>> PluginUpdated;
+ /// <summary>
+ /// Called when [plugin updated].
+ /// </summary>
+ /// <param name="plugin">The plugin.</param>
+ /// <param name="newVersion">The new version.</param>
+ private void OnPluginUpdated(IPlugin plugin, PackageVersionInfo newVersion)
+ {
+ _logger.Info("Plugin updated: {0} {1} {2}", newVersion.name, newVersion.versionStr ?? string.Empty, newVersion.classification);
+
+ EventHelper.FireEventIfNotNull(PluginUpdated, this, new GenericEventArgs<Tuple<IPlugin, PackageVersionInfo>> { Argument = new Tuple<IPlugin, PackageVersionInfo>(plugin, newVersion) }, _logger);
+
+ _applicationHost.NotifyPendingRestart();
+ }
+ #endregion
+
+ #region PluginInstalled Event
+ /// <summary>
+ /// Occurs when [plugin updated].
+ /// </summary>
+ public event EventHandler<GenericEventArgs<PackageVersionInfo>> PluginInstalled;
+ /// <summary>
+ /// Called when [plugin installed].
+ /// </summary>
+ /// <param name="package">The package.</param>
+ private void OnPluginInstalled(PackageVersionInfo package)
+ {
+ _logger.Info("New plugin installed: {0} {1} {2}", package.name, package.versionStr ?? string.Empty, package.classification);
+
+ EventHelper.FireEventIfNotNull(PluginInstalled, this, new GenericEventArgs<PackageVersionInfo> { Argument = package }, _logger);
+
+ _applicationHost.NotifyPendingRestart();
+ }
+ #endregion
+
+ /// <summary>
+ /// The _logger
+ /// </summary>
+ private readonly ILogger _logger;
+
+ private readonly IApplicationPaths _appPaths;
+ private readonly IHttpClient _httpClient;
+ private readonly IJsonSerializer _jsonSerializer;
+ private readonly ISecurityManager _securityManager;
+ private readonly IConfigurationManager _config;
+ private readonly IFileSystem _fileSystem;
+
+ /// <summary>
+ /// Gets the application host.
+ /// </summary>
+ /// <value>The application host.</value>
+ private readonly IApplicationHost _applicationHost;
+
+ private readonly ICryptographyProvider _cryptographyProvider;
+
+ public InstallationManager(ILogger logger, IApplicationHost appHost, IApplicationPaths appPaths, IHttpClient httpClient, IJsonSerializer jsonSerializer, ISecurityManager securityManager, IConfigurationManager config, IFileSystem fileSystem, ICryptographyProvider cryptographyProvider)
+ {
+ if (logger == null)
+ {
+ throw new ArgumentNullException("logger");
+ }
+
+ CurrentInstallations = new List<Tuple<InstallationInfo, CancellationTokenSource>>();
+ CompletedInstallations = new ConcurrentBag<InstallationInfo>();
+
+ _applicationHost = appHost;
+ _appPaths = appPaths;
+ _httpClient = httpClient;
+ _jsonSerializer = jsonSerializer;
+ _securityManager = securityManager;
+ _config = config;
+ _fileSystem = fileSystem;
+ _cryptographyProvider = cryptographyProvider;
+ _logger = logger;
+ }
+
+ private Version GetPackageVersion(PackageVersionInfo version)
+ {
+ return new Version(ValueOrDefault(version.versionStr, "0.0.0.1"));
+ }
+
+ private static string ValueOrDefault(string str, string def)
+ {
+ return string.IsNullOrEmpty(str) ? def : str;
+ }
+
+ /// <summary>
+ /// Gets all available packages.
+ /// </summary>
+ /// <returns>Task{List{PackageInfo}}.</returns>
+ public async Task<IEnumerable<PackageInfo>> GetAvailablePackages(CancellationToken cancellationToken,
+ bool withRegistration = true,
+ string packageType = null,
+ Version applicationVersion = null)
+ {
+ var data = new Dictionary<string, string>
+ {
+ { "key", _securityManager.SupporterKey },
+ { "mac", _applicationHost.SystemId },
+ { "systemid", _applicationHost.SystemId }
+ };
+
+ if (withRegistration)
+ {
+ using (var json = await _httpClient.Post("https://www.mb3admin.com/admin/service/package/retrieveall", data, cancellationToken).ConfigureAwait(false))
+ {
+ cancellationToken.ThrowIfCancellationRequested();
+
+ var packages = _jsonSerializer.DeserializeFromStream<List<PackageInfo>>(json).ToList();
+
+ return FilterPackages(packages, packageType, applicationVersion);
+ }
+ }
+ else
+ {
+ var packages = await GetAvailablePackagesWithoutRegistrationInfo(cancellationToken).ConfigureAwait(false);
+
+ return FilterPackages(packages.ToList(), packageType, applicationVersion);
+ }
+ }
+
+ private DateTime _lastPackageUpdateTime;
+
+ /// <summary>
+ /// Gets all available packages.
+ /// </summary>
+ /// <param name="cancellationToken">The cancellation token.</param>
+ /// <returns>Task{List{PackageInfo}}.</returns>
+ public async Task<IEnumerable<PackageInfo>> GetAvailablePackagesWithoutRegistrationInfo(CancellationToken cancellationToken)
+ {
+ _logger.Info("Opening {0}", PackageCachePath);
+ try
+ {
+ using (var stream = _fileSystem.OpenRead(PackageCachePath))
+ {
+ var packages = _jsonSerializer.DeserializeFromStream<List<PackageInfo>>(stream).ToList();
+
+ if (DateTime.UtcNow - _lastPackageUpdateTime > GetCacheLength())
+ {
+ UpdateCachedPackages(CancellationToken.None, false);
+ }
+
+ return packages;
+ }
+ }
+ catch (Exception)
+ {
+
+ }
+
+ _lastPackageUpdateTime = DateTime.MinValue;
+ await UpdateCachedPackages(cancellationToken, true).ConfigureAwait(false);
+ using (var stream = _fileSystem.OpenRead(PackageCachePath))
+ {
+ return _jsonSerializer.DeserializeFromStream<List<PackageInfo>>(stream).ToList();
+ }
+ }
+
+ private string PackageCachePath
+ {
+ get { return Path.Combine(_appPaths.CachePath, "serverpackages.json"); }
+ }
+
+ private readonly SemaphoreSlim _updateSemaphore = new SemaphoreSlim(1, 1);
+ private async Task UpdateCachedPackages(CancellationToken cancellationToken, bool throwErrors)
+ {
+ await _updateSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
+
+ try
+ {
+ if (DateTime.UtcNow - _lastPackageUpdateTime < GetCacheLength())
+ {
+ return;
+ }
+
+ var tempFile = await _httpClient.GetTempFile(new HttpRequestOptions
+ {
+ Url = "https://www.mb3admin.com/admin/service/MB3Packages.json",
+ CancellationToken = cancellationToken,
+ Progress = new Progress<Double>()
+
+ }).ConfigureAwait(false);
+
+ _fileSystem.CreateDirectory(Path.GetDirectoryName(PackageCachePath));
+
+ _fileSystem.CopyFile(tempFile, PackageCachePath, true);
+ _lastPackageUpdateTime = DateTime.UtcNow;
+ }
+ catch (Exception ex)
+ {
+ _logger.ErrorException("Error updating package cache", ex);
+
+ if (throwErrors)
+ {
+ throw;
+ }
+ }
+ finally
+ {
+ _updateSemaphore.Release();
+ }
+ }
+
+ private TimeSpan GetCacheLength()
+ {
+ switch (_config.CommonConfiguration.SystemUpdateLevel)
+ {
+ case PackageVersionClass.Beta:
+ return TimeSpan.FromMinutes(30);
+ case PackageVersionClass.Dev:
+ return TimeSpan.FromMinutes(3);
+ default:
+ return TimeSpan.FromHours(24);
+ }
+ }
+
+ protected IEnumerable<PackageInfo> FilterPackages(List<PackageInfo> packages)
+ {
+ foreach (var package in packages)
+ {
+ package.versions = package.versions.Where(v => !string.IsNullOrWhiteSpace(v.sourceUrl))
+ .OrderByDescending(GetPackageVersion).ToList();
+ }
+
+ // Remove packages with no versions
+ packages = packages.Where(p => p.versions.Any()).ToList();
+
+ return packages;
+ }
+
+ protected IEnumerable<PackageInfo> FilterPackages(List<PackageInfo> packages, string packageType, Version applicationVersion)
+ {
+ foreach (var package in packages)
+ {
+ package.versions = package.versions.Where(v => !string.IsNullOrWhiteSpace(v.sourceUrl))
+ .OrderByDescending(GetPackageVersion).ToList();
+ }
+
+ if (!string.IsNullOrWhiteSpace(packageType))
+ {
+ packages = packages.Where(p => string.Equals(p.type, packageType, StringComparison.OrdinalIgnoreCase)).ToList();
+ }
+
+ // If an app version was supplied, filter the versions for each package to only include supported versions
+ if (applicationVersion != null)
+ {
+ foreach (var package in packages)
+ {
+ package.versions = package.versions.Where(v => IsPackageVersionUpToDate(v, applicationVersion)).ToList();
+ }
+ }
+
+ // Remove packages with no versions
+ packages = packages.Where(p => p.versions.Any()).ToList();
+
+ return packages;
+ }
+
+ /// <summary>
+ /// Determines whether [is package version up to date] [the specified package version info].
+ /// </summary>
+ /// <param name="packageVersionInfo">The package version info.</param>
+ /// <param name="currentServerVersion">The current server version.</param>
+ /// <returns><c>true</c> if [is package version up to date] [the specified package version info]; otherwise, <c>false</c>.</returns>
+ private bool IsPackageVersionUpToDate(PackageVersionInfo packageVersionInfo, Version currentServerVersion)
+ {
+ if (string.IsNullOrEmpty(packageVersionInfo.requiredVersionStr))
+ {
+ return true;
+ }
+
+ Version requiredVersion;
+
+ return Version.TryParse(packageVersionInfo.requiredVersionStr, out requiredVersion) && currentServerVersion >= requiredVersion;
+ }
+
+ /// <summary>
+ /// Gets the package.
+ /// </summary>
+ /// <param name="name">The name.</param>
+ /// <param name="guid">The assembly guid</param>
+ /// <param name="classification">The classification.</param>
+ /// <param name="version">The version.</param>
+ /// <returns>Task{PackageVersionInfo}.</returns>
+ public async Task<PackageVersionInfo> GetPackage(string name, string guid, PackageVersionClass classification, Version version)
+ {
+ var packages = await GetAvailablePackages(CancellationToken.None).ConfigureAwait(false);
+
+ var package = packages.FirstOrDefault(p => string.Equals(p.guid, guid ?? "none", StringComparison.OrdinalIgnoreCase))
+ ?? packages.FirstOrDefault(p => p.name.Equals(name, StringComparison.OrdinalIgnoreCase));
+
+ if (package == null)
+ {
+ return null;
+ }
+
+ return package.versions.FirstOrDefault(v => GetPackageVersion(v).Equals(version) && v.classification == classification);
+ }
+
+ /// <summary>
+ /// Gets the latest compatible version.
+ /// </summary>
+ /// <param name="name">The name.</param>
+ /// <param name="guid">The assembly guid if this is a plug-in</param>
+ /// <param name="currentServerVersion">The current server version.</param>
+ /// <param name="classification">The classification.</param>
+ /// <returns>Task{PackageVersionInfo}.</returns>
+ public async Task<PackageVersionInfo> GetLatestCompatibleVersion(string name, string guid, Version currentServerVersion, PackageVersionClass classification = PackageVersionClass.Release)
+ {
+ var packages = await GetAvailablePackages(CancellationToken.None).ConfigureAwait(false);
+
+ return GetLatestCompatibleVersion(packages, name, guid, currentServerVersion, classification);
+ }
+
+ /// <summary>
+ /// Gets the latest compatible version.
+ /// </summary>
+ /// <param name="availablePackages">The available packages.</param>
+ /// <param name="name">The name.</param>
+ /// <param name="currentServerVersion">The current server version.</param>
+ /// <param name="classification">The classification.</param>
+ /// <returns>PackageVersionInfo.</returns>
+ public PackageVersionInfo GetLatestCompatibleVersion(IEnumerable<PackageInfo> availablePackages, string name, string guid, Version currentServerVersion, PackageVersionClass classification = PackageVersionClass.Release)
+ {
+ var package = availablePackages.FirstOrDefault(p => string.Equals(p.guid, guid ?? "none", StringComparison.OrdinalIgnoreCase))
+ ?? availablePackages.FirstOrDefault(p => p.name.Equals(name, StringComparison.OrdinalIgnoreCase));
+
+ if (package == null)
+ {
+ return null;
+ }
+
+ return package.versions
+ .OrderByDescending(GetPackageVersion)
+ .FirstOrDefault(v => v.classification <= classification && IsPackageVersionUpToDate(v, currentServerVersion));
+ }
+
+ /// <summary>
+ /// Gets the available plugin updates.
+ /// </summary>
+ /// <param name="applicationVersion">The current server version.</param>
+ /// <param name="withAutoUpdateEnabled">if set to <c>true</c> [with auto update enabled].</param>
+ /// <param name="cancellationToken">The cancellation token.</param>
+ /// <returns>Task{IEnumerable{PackageVersionInfo}}.</returns>
+ public async Task<IEnumerable<PackageVersionInfo>> GetAvailablePluginUpdates(Version applicationVersion, bool withAutoUpdateEnabled, CancellationToken cancellationToken)
+ {
+ var catalog = await GetAvailablePackagesWithoutRegistrationInfo(cancellationToken).ConfigureAwait(false);
+
+ var plugins = _applicationHost.Plugins.ToList();
+
+ if (withAutoUpdateEnabled)
+ {
+ plugins = plugins
+ .Where(p => _config.CommonConfiguration.EnableAutoUpdate)
+ .ToList();
+ }
+
+ // Figure out what needs to be installed
+ var packages = plugins.Select(p =>
+ {
+ var latestPluginInfo = GetLatestCompatibleVersion(catalog, p.Name, p.Id.ToString(), applicationVersion, _config.CommonConfiguration.SystemUpdateLevel);
+
+ return latestPluginInfo != null && GetPackageVersion(latestPluginInfo) > p.Version ? latestPluginInfo : null;
+
+ }).Where(i => i != null).ToList();
+
+ return packages
+ .Where(p => !string.IsNullOrWhiteSpace(p.sourceUrl) && !CompletedInstallations.Any(i => string.Equals(i.AssemblyGuid, p.guid, StringComparison.OrdinalIgnoreCase)));
+ }
+
+ /// <summary>
+ /// Installs the package.
+ /// </summary>
+ /// <param name="package">The package.</param>
+ /// <param name="isPlugin">if set to <c>true</c> [is plugin].</param>
+ /// <param name="progress">The progress.</param>
+ /// <param name="cancellationToken">The cancellation token.</param>
+ /// <returns>Task.</returns>
+ /// <exception cref="System.ArgumentNullException">package</exception>
+ public async Task InstallPackage(PackageVersionInfo package, bool isPlugin, IProgress<double> progress, CancellationToken cancellationToken)
+ {
+ if (package == null)
+ {
+ throw new ArgumentNullException("package");
+ }
+
+ if (progress == null)
+ {
+ throw new ArgumentNullException("progress");
+ }
+
+ var installationInfo = new InstallationInfo
+ {
+ Id = Guid.NewGuid().ToString("N"),
+ Name = package.name,
+ AssemblyGuid = package.guid,
+ UpdateClass = package.classification,
+ Version = package.versionStr
+ };
+
+ var innerCancellationTokenSource = new CancellationTokenSource();
+
+ var tuple = new Tuple<InstallationInfo, CancellationTokenSource>(installationInfo, innerCancellationTokenSource);
+
+ // Add it to the in-progress list
+ lock (CurrentInstallations)
+ {
+ CurrentInstallations.Add(tuple);
+ }
+
+ var innerProgress = new ActionableProgress<double>();
+
+ // Whenever the progress updates, update the outer progress object and InstallationInfo
+ innerProgress.RegisterAction(percent =>
+ {
+ progress.Report(percent);
+
+ installationInfo.PercentComplete = percent;
+ });
+
+ var linkedToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, innerCancellationTokenSource.Token).Token;
+
+ var installationEventArgs = new InstallationEventArgs
+ {
+ InstallationInfo = installationInfo,
+ PackageVersionInfo = package
+ };
+
+ EventHelper.FireEventIfNotNull(PackageInstalling, this, installationEventArgs, _logger);
+
+ try
+ {
+ await InstallPackageInternal(package, isPlugin, innerProgress, linkedToken).ConfigureAwait(false);
+
+ lock (CurrentInstallations)
+ {
+ CurrentInstallations.Remove(tuple);
+ }
+
+ progress.Report(100);
+
+ CompletedInstallations.Add(installationInfo);
+
+ EventHelper.FireEventIfNotNull(PackageInstallationCompleted, this, installationEventArgs, _logger);
+ }
+ catch (OperationCanceledException)
+ {
+ lock (CurrentInstallations)
+ {
+ CurrentInstallations.Remove(tuple);
+ }
+
+ _logger.Info("Package installation cancelled: {0} {1}", package.name, package.versionStr);
+
+ EventHelper.FireEventIfNotNull(PackageInstallationCancelled, this, installationEventArgs, _logger);
+
+ throw;
+ }
+ catch (Exception ex)
+ {
+ _logger.ErrorException("Package installation failed", ex);
+
+ lock (CurrentInstallations)
+ {
+ CurrentInstallations.Remove(tuple);
+ }
+
+ EventHelper.FireEventIfNotNull(PackageInstallationFailed, this, new InstallationFailedEventArgs
+ {
+ InstallationInfo = installationInfo,
+ Exception = ex
+
+ }, _logger);
+
+ throw;
+ }
+ finally
+ {
+ // Dispose the progress object and remove the installation from the in-progress list
+ innerProgress.Dispose();
+ tuple.Item2.Dispose();
+ }
+ }
+
+ /// <summary>
+ /// Installs the package internal.
+ /// </summary>
+ /// <param name="package">The package.</param>
+ /// <param name="isPlugin">if set to <c>true</c> [is plugin].</param>
+ /// <param name="progress">The progress.</param>
+ /// <param name="cancellationToken">The cancellation token.</param>
+ /// <returns>Task.</returns>
+ private async Task InstallPackageInternal(PackageVersionInfo package, bool isPlugin, IProgress<double> progress, CancellationToken cancellationToken)
+ {
+ // Do the install
+ await PerformPackageInstallation(progress, package, cancellationToken).ConfigureAwait(false);
+
+ // Do plugin-specific processing
+ if (isPlugin)
+ {
+ // Set last update time if we were installed before
+ var plugin = _applicationHost.Plugins.FirstOrDefault(p => string.Equals(p.Id.ToString(), package.guid, StringComparison.OrdinalIgnoreCase))
+ ?? _applicationHost.Plugins.FirstOrDefault(p => p.Name.Equals(package.name, StringComparison.OrdinalIgnoreCase));
+
+ if (plugin != null)
+ {
+ OnPluginUpdated(plugin, package);
+ }
+ else
+ {
+ OnPluginInstalled(package);
+ }
+ }
+ }
+
+ private async Task PerformPackageInstallation(IProgress<double> progress, PackageVersionInfo package, CancellationToken cancellationToken)
+ {
+ // Target based on if it is an archive or single assembly
+ // zip archives are assumed to contain directory structures relative to our ProgramDataPath
+ var extension = Path.GetExtension(package.targetFilename);
+ var isArchive = string.Equals(extension, ".zip", StringComparison.OrdinalIgnoreCase) || string.Equals(extension, ".rar", StringComparison.OrdinalIgnoreCase) || string.Equals(extension, ".7z", StringComparison.OrdinalIgnoreCase);
+ var target = Path.Combine(isArchive ? _appPaths.TempUpdatePath : _appPaths.PluginsPath, package.targetFilename);
+
+ // Download to temporary file so that, if interrupted, it won't destroy the existing installation
+ var tempFile = await _httpClient.GetTempFile(new HttpRequestOptions
+ {
+ Url = package.sourceUrl,
+ CancellationToken = cancellationToken,
+ Progress = progress
+
+ }).ConfigureAwait(false);
+
+ cancellationToken.ThrowIfCancellationRequested();
+
+ // Validate with a checksum
+ var packageChecksum = string.IsNullOrWhiteSpace(package.checksum) ? Guid.Empty : new Guid(package.checksum);
+ if (packageChecksum != Guid.Empty) // support for legacy uploads for now
+ {
+ using (var stream = _fileSystem.OpenRead(tempFile))
+ {
+ var check = Guid.Parse(BitConverter.ToString(_cryptographyProvider.GetMD5Bytes(stream)).Replace("-", String.Empty));
+ if (check != packageChecksum)
+ {
+ throw new Exception(string.Format("Download validation failed for {0}. Probably corrupted during transfer.", package.name));
+ }
+ }
+ }
+
+ cancellationToken.ThrowIfCancellationRequested();
+
+ // Success - move it to the real target
+ try
+ {
+ _fileSystem.CreateDirectory(Path.GetDirectoryName(target));
+ _fileSystem.CopyFile(tempFile, target, true);
+ //If it is an archive - write out a version file so we know what it is
+ if (isArchive)
+ {
+ _fileSystem.WriteAllText(target + ".ver", package.versionStr);
+ }
+ }
+ catch (IOException e)
+ {
+ _logger.ErrorException("Error attempting to move file from {0} to {1}", e, tempFile, target);
+ throw;
+ }
+
+ try
+ {
+ _fileSystem.DeleteFile(tempFile);
+ }
+ catch (IOException e)
+ {
+ // Don't fail because of this
+ _logger.ErrorException("Error deleting temp file {0]", e, tempFile);
+ }
+ }
+
+ /// <summary>
+ /// Uninstalls a plugin
+ /// </summary>
+ /// <param name="plugin">The plugin.</param>
+ /// <exception cref="System.ArgumentException"></exception>
+ public void UninstallPlugin(IPlugin plugin)
+ {
+ plugin.OnUninstalling();
+
+ // Remove it the quick way for now
+ _applicationHost.RemovePlugin(plugin);
+
+ _fileSystem.DeleteFile(plugin.AssemblyFilePath);
+
+ OnPluginUninstalled(plugin);
+
+ _applicationHost.NotifyPendingRestart();
+ }
+
+ /// <summary>
+ /// Releases unmanaged and - optionally - managed resources.
+ /// </summary>
+ /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
+ protected virtual void Dispose(bool dispose)
+ {
+ if (dispose)
+ {
+ lock (CurrentInstallations)
+ {
+ foreach (var tuple in CurrentInstallations)
+ {
+ tuple.Item2.Dispose();
+ }
+
+ CurrentInstallations.Clear();
+ }
+ }
+ }
+
+ public void Dispose()
+ {
+ Dispose(true);
+ }
+ }
+}