Beispiel #1
0
        public async Task PerformOrganization(string resultId)
        {
            var result = _repo.GetResult(resultId);

            var options = GetAutoOrganizeOptions();

            if (string.IsNullOrEmpty(result.TargetPath))
            {
                throw new ArgumentException("No target path available.");
            }

            FileOrganizationResult organizeResult;

            switch (result.Type)
            {
            case FileOrganizerType.Episode:
                var episodeOrganizer = new EpisodeFileOrganizer(this, _config, _fileSystem, _logger, _libraryManager,
                                                                _libraryMonitor, _providerManager);

                organizeResult = await episodeOrganizer.OrganizeEpisodeFile(result.OriginalPath, options.TvOptions, CancellationToken.None)
                                 .ConfigureAwait(false);

                break;

            case FileOrganizerType.Movie:
                var movieOrganizer = new MovieFileOrganizer(this, _config, _fileSystem, _logger, _libraryManager,
                                                            _libraryMonitor, _providerManager);

                organizeResult = await movieOrganizer.OrganizeMovieFile(result.OriginalPath, options.MovieOptions, CancellationToken.None)
                                 .ConfigureAwait(false);

                break;

            default:
                throw new OrganizationException("No organizer exist for the type " + result.Type);
            }

            if (organizeResult.Status != FileSortingStatus.Success)
            {
                throw new OrganizationException(result.StatusMessage);
            }
        }
Beispiel #2
0
        public async Task Organize(MovieFileOrganizationOptions options, CancellationToken cancellationToken, IProgress <double> progress)
        {
            var libraryFolderPaths = _libraryManager.GetVirtualFolders().SelectMany(i => i.Locations).ToList();

            var watchLocations = options.WatchLocations
                                 .Where(i => IsValidWatchLocation(i, libraryFolderPaths))
                                 .ToList();

            var eligibleFiles = watchLocations.SelectMany(GetFilesToOrganize)
                                .OrderBy(_fileSystem.GetCreationTimeUtc)
                                .Where(i => EnableOrganization(i, options))
                                .ToList();

            var processedFolders = new HashSet <string>();

            progress.Report(10);

            if (eligibleFiles.Count > 0)
            {
                var numComplete = 0;

                var organizer = new MovieFileOrganizer(_organizationService, _config, _fileSystem, _logger, _libraryManager,
                                                       _libraryMonitor, _providerManager);

                foreach (var file in eligibleFiles)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    try
                    {
                        var result = await organizer.OrganizeMovieFile(file.FullName, options, cancellationToken).ConfigureAwait(false);

                        if (result.Status == FileSortingStatus.Success && !processedFolders.Contains(file.DirectoryName, StringComparer.OrdinalIgnoreCase))
                        {
                            processedFolders.Add(file.DirectoryName);
                        }
                    }
                    catch (OperationCanceledException)
                    {
                        break;
                    }
                    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);


            List <string> deleteExtensions = options.LeftOverFileExtensionsToDelete
                                             .Select(i => i.Trim().TrimStart('.'))
                                             .Where(i => !string.IsNullOrEmpty(i))
                                             .Select(i => "." + i)
                                             .ToList();


            // Normal Clean
            Clean(processedFolders, watchLocations, options.DeleteEmptyFolders, deleteExtensions);

            // Extended Clean
            if (options.ExtendedClean)
            {
                Clean(watchLocations, watchLocations, options.DeleteEmptyFolders, deleteExtensions);
            }

            progress.Report(100);
        }