public async Task PerformOrganization(string resultId) { var result = _repo.GetResult(resultId); if (string.IsNullOrEmpty(result.TargetPath)) { throw new ArgumentException("No target path available."); } var organizer = new EpisodeFileOrganizer(this, _config, _fileSystem, _logger, _libraryManager, _libraryMonitor, _providerManager); var organizeResult = await organizer.OrganizeEpisodeFile(result.OriginalPath, GetAutoOrganizeOptions(), true, CancellationToken.None) .ConfigureAwait(false); if (organizeResult.Status != FileSortingStatus.Success) { throw new Exception(result.StatusMessage); } }
public async Task Organize(AutoOrganizeOptions options, CancellationToken cancellationToken, IProgress <double> progress) { var watchLocations = options.TvOptions.WatchLocations.ToList(); var eligibleFiles = watchLocations.SelectMany(GetFilesToOrganize) .OrderBy(_fileSystem.GetCreationTimeUtc) .Where(i => EnableOrganization(i, options.TvOptions)) .ToList(); var processedFolders = new HashSet <string>(); progress.Report(10); if (eligibleFiles.Count > 0) { var numComplete = 0; foreach (var file in eligibleFiles) { var organizer = new EpisodeFileOrganizer(_organizationService, _config, _fileSystem, _logger, _libraryManager, _libraryMonitor, _providerManager); try { var result = await organizer.OrganizeEpisodeFile(file.FullName, options, options.TvOptions.OverwriteExistingEpisodes, cancellationToken).ConfigureAwait(false); if (result.Status == FileSortingStatus.Success && !processedFolders.Contains(file.DirectoryName, StringComparer.OrdinalIgnoreCase)) { processedFolders.Add(file.DirectoryName); } } catch (Exception ex) { _logger.ErrorException("Error organizing episode {0}", ex, file.FullName); } numComplete++; double percent = numComplete; percent /= eligibleFiles.Count; progress.Report(10 + 89 * percent); } } cancellationToken.ThrowIfCancellationRequested(); progress.Report(99); foreach (var path in processedFolders) { var deleteExtensions = options.TvOptions.LeftOverFileExtensionsToDelete .Select(i => i.Trim().TrimStart('.')) .Where(i => !string.IsNullOrEmpty(i)) .Select(i => "." + i) .ToList(); if (deleteExtensions.Count > 0) { DeleteLeftOverFiles(path, deleteExtensions); } if (options.TvOptions.DeleteEmptyFolders) { if (!IsWatchFolder(path, watchLocations)) { DeleteEmptyFolders(path); } } } progress.Report(100); }