FileOrganizationService.cs 6.5 KB

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