private double? GetDownloadLimit(ChannelOptions channelOptions)
 {
     return channelOptions.DownloadSizeLimit;
 }
        private async Task DownloadChannelItem(BaseItem item,
            ChannelOptions channelOptions,
            CancellationToken cancellationToken,
            string path)
        {
            var limit = GetDownloadLimit(channelOptions);

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

            var itemId = item.Id.ToString("N");
            var sources = await _manager.GetChannelItemMediaSources(itemId, false, 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 channelItem = (IChannelMediaItem)item;

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

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

            await RefreshMediaSourceItem(destination, cancellationToken).ConfigureAwait(false);
        }
        private async Task DownloadChannelItem(BaseItemDto item,
            ChannelOptions channelOptions,
            CancellationToken cancellationToken,
            string path)
        {
            if (channelOptions.DownloadSizeLimit.HasValue)
            {
                if (IsSizeLimitReached(path, channelOptions.DownloadSizeLimit.Value))
                {
                    return;
                }    
            }

            var sources = await _manager.GetChannelItemMediaSources(item.Id, cancellationToken)
                .ConfigureAwait(false);

            var list = sources.ToList();

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

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

            var source = list.FirstOrDefault(i => i.Protocol == MediaProtocol.Http);

            if (source == null)
            {
                return;
            }

            var options = new HttpRequestOptions
            {
                CancellationToken = cancellationToken,
                Url = source.Path,
                Progress = new Progress<double>()
            };

            foreach (var header in source.RequiredHttpHeaders)
            {
                options.RequestHeaders[header.Key] = header.Value;
            }

            var destination = Path.Combine(path, item.ChannelId, item.Id);
            Directory.CreateDirectory(Path.GetDirectoryName(destination));

            // Determine output extension
            var response = await _httpClient.GetTempFileResponse(options).ConfigureAwait(false);

            if (item.IsVideo && response.ContentType.StartsWith("video/", StringComparison.OrdinalIgnoreCase))
            {
                var extension = response.ContentType.Split('/')
                        .Last()
                        .Replace("quicktime", "mov", StringComparison.OrdinalIgnoreCase);

                destination += "." + extension;
            }
            else if (item.IsAudio && response.ContentType.StartsWith("audio/", StringComparison.OrdinalIgnoreCase))
            {
                var extension = response.ContentType.Replace("audio/mpeg", "audio/mp3", StringComparison.OrdinalIgnoreCase)
                        .Split('/')
                        .Last();

                destination += "." + extension;
            }
            else
            {
                File.Delete(response.TempFilePath);

                throw new ApplicationException("Unexpected response type encountered: " + response.ContentType);
            }

            File.Copy(response.TempFilePath, destination, true);

            await RefreshMediaSourceItem(destination, cancellationToken).ConfigureAwait(false);

            try
            {
                File.Delete(response.TempFilePath);
            }
            catch
            {
                
            }
        }