ChannelDownloadScheduledTask.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Common.Net;
  4. using MediaBrowser.Common.ScheduledTasks;
  5. using MediaBrowser.Controller.Channels;
  6. using MediaBrowser.Controller.Configuration;
  7. using MediaBrowser.Controller.Library;
  8. using MediaBrowser.Model.Channels;
  9. using MediaBrowser.Model.Dto;
  10. using MediaBrowser.Model.Entities;
  11. using MediaBrowser.Model.Logging;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Threading;
  17. using System.Threading.Tasks;
  18. namespace MediaBrowser.Server.Implementations.Channels
  19. {
  20. public class ChannelDownloadScheduledTask : IScheduledTask, IConfigurableScheduledTask
  21. {
  22. private readonly IChannelManager _manager;
  23. private readonly IServerConfigurationManager _config;
  24. private readonly ILogger _logger;
  25. private readonly IHttpClient _httpClient;
  26. private readonly IFileSystem _fileSystem;
  27. private readonly ILibraryManager _libraryManager;
  28. public ChannelDownloadScheduledTask(IChannelManager manager, IServerConfigurationManager config, ILogger logger, IHttpClient httpClient, IFileSystem fileSystem, ILibraryManager libraryManager)
  29. {
  30. _manager = manager;
  31. _config = config;
  32. _logger = logger;
  33. _httpClient = httpClient;
  34. _fileSystem = fileSystem;
  35. _libraryManager = libraryManager;
  36. }
  37. public string Name
  38. {
  39. get { return "Download channel content"; }
  40. }
  41. public string Description
  42. {
  43. get { return "Downloads channel content based on configuration."; }
  44. }
  45. public string Category
  46. {
  47. get { return "Channels"; }
  48. }
  49. public async Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
  50. {
  51. CleanChannelContent(cancellationToken);
  52. progress.Report(5);
  53. await DownloadChannelContent(cancellationToken, progress).ConfigureAwait(false);
  54. progress.Report(100);
  55. }
  56. private void CleanChannelContent(CancellationToken cancellationToken)
  57. {
  58. if (!_config.Configuration.ChannelOptions.MaxDownloadAge.HasValue)
  59. {
  60. return;
  61. }
  62. var minDateModified = DateTime.UtcNow.AddDays(0 - _config.Configuration.ChannelOptions.MaxDownloadAge.Value);
  63. var path = _manager.ChannelDownloadPath;
  64. try
  65. {
  66. DeleteCacheFilesFromDirectory(cancellationToken, path, minDateModified, new Progress<double>());
  67. }
  68. catch (DirectoryNotFoundException)
  69. {
  70. // No biggie here. Nothing to delete
  71. }
  72. }
  73. private async Task DownloadChannelContent(CancellationToken cancellationToken, IProgress<double> progress)
  74. {
  75. if (_config.Configuration.ChannelOptions.DownloadingChannels.Length == 0)
  76. {
  77. return;
  78. }
  79. var result = await _manager.GetAllMedia(new AllChannelMediaQuery
  80. {
  81. ChannelIds = _config.Configuration.ChannelOptions.DownloadingChannels
  82. }, cancellationToken).ConfigureAwait(false);
  83. var path = _manager.ChannelDownloadPath;
  84. var numComplete = 0;
  85. foreach (var item in result.Items)
  86. {
  87. try
  88. {
  89. await DownloadChannelItem(item, cancellationToken, path);
  90. }
  91. catch (OperationCanceledException)
  92. {
  93. break;
  94. }
  95. catch (Exception ex)
  96. {
  97. _logger.ErrorException("Error downloading channel content for {0}", ex, item.Name);
  98. }
  99. numComplete++;
  100. double percent = numComplete;
  101. percent /= result.Items.Length;
  102. progress.Report(percent * 95 + 5);
  103. }
  104. }
  105. private async Task DownloadChannelItem(BaseItemDto item,
  106. CancellationToken cancellationToken,
  107. string path)
  108. {
  109. var sources = await _manager.GetChannelItemMediaSources(item.Id, cancellationToken)
  110. .ConfigureAwait(false);
  111. var list = sources.ToList();
  112. var cachedVersions = list.Where(i => i.LocationType == LocationType.FileSystem).ToList();
  113. if (cachedVersions.Count > 0)
  114. {
  115. await RefreshMediaSourceItems(cachedVersions, cancellationToken).ConfigureAwait(false);
  116. return;
  117. }
  118. var source = list.First();
  119. var options = new HttpRequestOptions
  120. {
  121. CancellationToken = cancellationToken,
  122. Url = source.Path,
  123. Progress = new Progress<double>()
  124. };
  125. foreach (var header in source.RequiredHttpHeaders)
  126. {
  127. options.RequestHeaders[header.Key] = header.Value;
  128. }
  129. var destination = Path.Combine(path, item.ChannelId, item.Id);
  130. Directory.CreateDirectory(Path.GetDirectoryName(destination));
  131. // Determine output extension
  132. var response = await _httpClient.GetTempFileResponse(options).ConfigureAwait(false);
  133. if (item.IsVideo && response.ContentType.StartsWith("video/", StringComparison.OrdinalIgnoreCase))
  134. {
  135. var extension = response.ContentType.Split('/')
  136. .Last();
  137. destination += "." + extension;
  138. }
  139. else if (item.IsAudio && response.ContentType.StartsWith("audio/", StringComparison.OrdinalIgnoreCase))
  140. {
  141. var extension = response.ContentType.Replace("audio/mpeg", "audio/mp3", StringComparison.OrdinalIgnoreCase)
  142. .Split('/')
  143. .Last();
  144. destination += "." + extension;
  145. }
  146. else
  147. {
  148. throw new ApplicationException("Unexpected response type encountered: " + response.ContentType);
  149. }
  150. File.Move(response.TempFilePath, destination);
  151. await RefreshMediaSourceItem(destination, cancellationToken).ConfigureAwait(false);
  152. }
  153. private async Task RefreshMediaSourceItems(IEnumerable<MediaSourceInfo> items, CancellationToken cancellationToken)
  154. {
  155. foreach (var item in items)
  156. {
  157. await RefreshMediaSourceItem(item.Path, cancellationToken).ConfigureAwait(false);
  158. }
  159. }
  160. private async Task RefreshMediaSourceItem(string path, CancellationToken cancellationToken)
  161. {
  162. var item = _libraryManager.ResolvePath(new FileInfo(path));
  163. if (item != null)
  164. {
  165. // Get the version from the database
  166. item = _libraryManager.GetItemById(item.Id) ?? item;
  167. await item.RefreshMetadata(cancellationToken).ConfigureAwait(false);
  168. }
  169. }
  170. public IEnumerable<ITaskTrigger> GetDefaultTriggers()
  171. {
  172. return new ITaskTrigger[]
  173. {
  174. new IntervalTrigger{ Interval = TimeSpan.FromHours(4)},
  175. };
  176. }
  177. /// <summary>
  178. /// Deletes the cache files from directory with a last write time less than a given date
  179. /// </summary>
  180. /// <param name="cancellationToken">The task cancellation token.</param>
  181. /// <param name="directory">The directory.</param>
  182. /// <param name="minDateModified">The min date modified.</param>
  183. /// <param name="progress">The progress.</param>
  184. private void DeleteCacheFilesFromDirectory(CancellationToken cancellationToken, string directory, DateTime minDateModified, IProgress<double> progress)
  185. {
  186. var filesToDelete = new DirectoryInfo(directory).EnumerateFiles("*", SearchOption.AllDirectories)
  187. .Where(f => _fileSystem.GetLastWriteTimeUtc(f) < minDateModified)
  188. .ToList();
  189. var index = 0;
  190. foreach (var file in filesToDelete)
  191. {
  192. double percent = index;
  193. percent /= filesToDelete.Count;
  194. progress.Report(100 * percent);
  195. cancellationToken.ThrowIfCancellationRequested();
  196. DeleteFile(file.FullName);
  197. index++;
  198. }
  199. progress.Report(100);
  200. }
  201. /// <summary>
  202. /// Deletes the file.
  203. /// </summary>
  204. /// <param name="path">The path.</param>
  205. private void DeleteFile(string path)
  206. {
  207. try
  208. {
  209. File.Delete(path);
  210. }
  211. catch (IOException ex)
  212. {
  213. _logger.ErrorException("Error deleting file {0}", ex, path);
  214. }
  215. }
  216. public bool IsHidden
  217. {
  218. get
  219. {
  220. return !_manager.GetAllChannelFeatures()
  221. .Any(i => i.CanDownloadAllMedia && _config.Configuration.ChannelOptions.DownloadingChannels.Contains(i.Id));
  222. }
  223. }
  224. public bool IsEnabled
  225. {
  226. get
  227. {
  228. return true;
  229. }
  230. }
  231. }
  232. }