Ejemplo n.º 1
0
        private async Task DownloadChannelItem(IChannelMediaItem item,
                                               ChannelOptions channelOptions,
                                               CancellationToken cancellationToken,
                                               string path)
        {
            var itemId  = item.Id.ToString("N");
            var sources = await _manager.GetStaticMediaSources(item, true, cancellationToken)
                          .ConfigureAwait(false);

            var cachedVersions = sources.Where(i => i.Protocol == MediaProtocol.File).ToList();

            if (cachedVersions.Count > 0)
            {
                await RefreshMediaSourceItems(cachedVersions, cancellationToken).ConfigureAwait(false);

                return;
            }

            var limit = GetDownloadLimit(channelOptions);

            if (limit.HasValue)
            {
                if (IsSizeLimitReached(path, limit.Value))
                {
                    return;
                }
            }

            var destination = Path.Combine(path, item.ChannelId, itemId);

            await _manager.DownloadChannelItem(item, destination, new Progress <double>(), cancellationToken)
            .ConfigureAwait(false);

            await RefreshMediaSourceItem(destination, cancellationToken).ConfigureAwait(false);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Downloads the trailer for item.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="contentType">Type of the content.</param>
        /// <param name="providersToMatch">The providers to match.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>Task.</returns>
        public async Task DownloadTrailerForItem(BaseItem item, ChannelMediaContentType contentType, List <MetadataProviders> providersToMatch, CancellationToken cancellationToken)
        {
            var providerValues = providersToMatch.Select(item.GetProviderId)
                                 .ToList();

            if (providerValues.All(string.IsNullOrWhiteSpace))
            {
                return;
            }

            var channelTrailers = await _channelManager.GetAllMediaInternal(new AllChannelMediaQuery
            {
                ContentTypes = new[] { contentType },
                ExtraTypes   = new[] { ExtraType.Trailer }
            }, CancellationToken.None);

            var channelItem = channelTrailers
                              .Items
                              .OfType <IChannelMediaItem>()
                              .FirstOrDefault(i =>
            {
                var currentProviderValues = providersToMatch.Select(i.GetProviderId).ToList();

                var index = 0;
                foreach (var val in providerValues)
                {
                    if (!string.IsNullOrWhiteSpace(val) && string.Equals(currentProviderValues[index], val, StringComparison.OrdinalIgnoreCase))
                    {
                        return(true);
                    }
                    index++;
                }

                return(false);
            });

            if (channelItem == null)
            {
                return;
            }

            var destination = Directory.Exists(item.Path) ?
                              Path.Combine(item.Path, Path.GetFileName(item.Path) + "-trailer") :
                              Path.Combine(Path.GetDirectoryName(item.Path), Path.GetFileNameWithoutExtension(item.Path) + "-trailer");

            _libraryMonitor.ReportFileSystemChangeBeginning(Path.GetDirectoryName(destination));

            try
            {
                await _channelManager.DownloadChannelItem(channelItem, destination, new Progress <double>(), cancellationToken)
                .ConfigureAwait(false);
            }
            catch (OperationCanceledException)
            {
            }
            catch (ChannelDownloadException)
            {
                // Logged at lower levels
            }
            catch (Exception ex)
            {
                _logger.ErrorException("Error downloading channel content for {0}", ex, item.Name);
            }
            finally
            {
                _libraryMonitor.ReportFileSystemChangeComplete(Path.GetDirectoryName(destination), true);
            }
        }