FileOrganizationService.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Common.ScheduledTasks;
  4. using MediaBrowser.Controller.Configuration;
  5. using MediaBrowser.Controller.FileOrganization;
  6. using MediaBrowser.Controller.Library;
  7. using MediaBrowser.Controller.Persistence;
  8. using MediaBrowser.Controller.Providers;
  9. using MediaBrowser.Model.FileOrganization;
  10. using MediaBrowser.Model.Logging;
  11. using MediaBrowser.Model.Querying;
  12. using System;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Threading;
  16. using System.Threading.Tasks;
  17. using CommonIO;
  18. namespace MediaBrowser.Server.Implementations.FileOrganization
  19. {
  20. public class FileOrganizationService : IFileOrganizationService
  21. {
  22. private readonly ITaskManager _taskManager;
  23. private readonly IFileOrganizationRepository _repo;
  24. private readonly ILogger _logger;
  25. private readonly ILibraryMonitor _libraryMonitor;
  26. private readonly ILibraryManager _libraryManager;
  27. private readonly IServerConfigurationManager _config;
  28. private readonly IFileSystem _fileSystem;
  29. private readonly IProviderManager _providerManager;
  30. public FileOrganizationService(ITaskManager taskManager, IFileOrganizationRepository repo, ILogger logger, ILibraryMonitor libraryMonitor, ILibraryManager libraryManager, IServerConfigurationManager config, IFileSystem fileSystem, IProviderManager providerManager)
  31. {
  32. _taskManager = taskManager;
  33. _repo = repo;
  34. _logger = logger;
  35. _libraryMonitor = libraryMonitor;
  36. _libraryManager = libraryManager;
  37. _config = config;
  38. _fileSystem = fileSystem;
  39. _providerManager = providerManager;
  40. }
  41. public void BeginProcessNewFiles()
  42. {
  43. _taskManager.CancelIfRunningAndQueue<OrganizerScheduledTask>();
  44. }
  45. public Task SaveResult(FileOrganizationResult result, CancellationToken cancellationToken)
  46. {
  47. if (result == null || string.IsNullOrEmpty(result.OriginalPath))
  48. {
  49. throw new ArgumentNullException("result");
  50. }
  51. result.Id = result.OriginalPath.GetMD5().ToString("N");
  52. return _repo.SaveResult(result, cancellationToken);
  53. }
  54. public QueryResult<FileOrganizationResult> GetResults(FileOrganizationResultQuery query)
  55. {
  56. return _repo.GetResults(query);
  57. }
  58. public FileOrganizationResult GetResult(string id)
  59. {
  60. return _repo.GetResult(id);
  61. }
  62. public FileOrganizationResult GetResultBySourcePath(string path)
  63. {
  64. if (string.IsNullOrEmpty(path))
  65. {
  66. throw new ArgumentNullException("path");
  67. }
  68. var id = path.GetMD5().ToString("N");
  69. return GetResult(id);
  70. }
  71. public Task DeleteOriginalFile(string resultId)
  72. {
  73. var result = _repo.GetResult(resultId);
  74. _logger.Info("Requested to delete {0}", result.OriginalPath);
  75. try
  76. {
  77. _fileSystem.DeleteFile(result.OriginalPath);
  78. }
  79. catch (Exception ex)
  80. {
  81. _logger.ErrorException("Error deleting {0}", ex, result.OriginalPath);
  82. }
  83. return _repo.Delete(resultId);
  84. }
  85. private AutoOrganizeOptions GetAutoOrganizeptions()
  86. {
  87. return _config.GetAutoOrganizeOptions();
  88. }
  89. public async Task PerformOrganization(string resultId)
  90. {
  91. var result = _repo.GetResult(resultId);
  92. if (string.IsNullOrEmpty(result.TargetPath))
  93. {
  94. throw new ArgumentException("No target path available.");
  95. }
  96. var organizer = new EpisodeFileOrganizer(this, _config, _fileSystem, _logger, _libraryManager,
  97. _libraryMonitor, _providerManager);
  98. await organizer.OrganizeEpisodeFile(result.OriginalPath, GetAutoOrganizeptions(), true, CancellationToken.None)
  99. .ConfigureAwait(false);
  100. }
  101. public Task ClearLog()
  102. {
  103. return _repo.DeleteAll();
  104. }
  105. public async Task PerformEpisodeOrganization(EpisodeFileOrganizationRequest request)
  106. {
  107. var organizer = new EpisodeFileOrganizer(this, _config, _fileSystem, _logger, _libraryManager,
  108. _libraryMonitor, _providerManager);
  109. await organizer.OrganizeWithCorrection(request, GetAutoOrganizeptions(), CancellationToken.None).ConfigureAwait(false);
  110. }
  111. public QueryResult<SmartMatchInfo> GetSmartMatchInfos(FileOrganizationResultQuery query)
  112. {
  113. if (query == null)
  114. {
  115. throw new ArgumentNullException("query");
  116. }
  117. var options = GetAutoOrganizeptions();
  118. var items = options.SmartMatchInfos.Skip(query.StartIndex ?? 0).Take(query.Limit ?? Int32.MaxValue).ToArray();
  119. return new QueryResult<SmartMatchInfo>()
  120. {
  121. Items = items,
  122. TotalRecordCount = options.SmartMatchInfos.Length
  123. };
  124. }
  125. public void DeleteSmartMatchEntry(string itemName, string matchString)
  126. {
  127. if (string.IsNullOrEmpty(itemName))
  128. {
  129. throw new ArgumentNullException("itemName");
  130. }
  131. if (string.IsNullOrEmpty(matchString))
  132. {
  133. throw new ArgumentNullException("matchString");
  134. }
  135. var options = GetAutoOrganizeptions();
  136. SmartMatchInfo info = options.SmartMatchInfos.FirstOrDefault(i => string.Equals(i.ItemName, itemName));
  137. if (info != null && info.MatchStrings.Contains(matchString))
  138. {
  139. var list = info.MatchStrings.ToList();
  140. list.Remove(matchString);
  141. info.MatchStrings = list.ToArray();
  142. if (info.MatchStrings.Length == 0)
  143. {
  144. var infos = options.SmartMatchInfos.ToList();
  145. infos.Remove(info);
  146. options.SmartMatchInfos = infos.ToArray();
  147. }
  148. _config.SaveAutoOrganizeOptions(options);
  149. }
  150. }
  151. }
  152. }