From d4bdd42acfda160e0a78ac302adc92802b237584 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sun, 19 Jan 2014 13:08:17 -0500 Subject: #680 - Support new episode file sorting added dummy results repository --- .../FileSorting/SortingScheduledTask.cs | 12 +- .../FileSorting/TvFileSorter.cs | 128 ++++++++++++++++++--- 2 files changed, 115 insertions(+), 25 deletions(-) (limited to 'MediaBrowser.Server.Implementations/FileSorting') diff --git a/MediaBrowser.Server.Implementations/FileSorting/SortingScheduledTask.cs b/MediaBrowser.Server.Implementations/FileSorting/SortingScheduledTask.cs index a9d7ada94..622546794 100644 --- a/MediaBrowser.Server.Implementations/FileSorting/SortingScheduledTask.cs +++ b/MediaBrowser.Server.Implementations/FileSorting/SortingScheduledTask.cs @@ -2,6 +2,7 @@ using MediaBrowser.Common.ScheduledTasks; using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.Persistence; using MediaBrowser.Model.Logging; using System; using System.Collections.Generic; @@ -16,13 +17,15 @@ namespace MediaBrowser.Server.Implementations.FileSorting private readonly ILogger _logger; private readonly ILibraryManager _libraryManager; private readonly IFileSystem _fileSystem; + private readonly IFileSortingRepository _iFileSortingRepository; - public SortingScheduledTask(IServerConfigurationManager config, ILogger logger, ILibraryManager libraryManager, IFileSystem fileSystem) + public SortingScheduledTask(IServerConfigurationManager config, ILogger logger, ILibraryManager libraryManager, IFileSystem fileSystem, IFileSortingRepository iFileSortingRepository) { _config = config; _logger = logger; _libraryManager = libraryManager; _fileSystem = fileSystem; + _iFileSortingRepository = iFileSortingRepository; } public string Name @@ -42,12 +45,7 @@ namespace MediaBrowser.Server.Implementations.FileSorting public Task Execute(CancellationToken cancellationToken, IProgress progress) { - return Task.Run(() => SortFiles(cancellationToken, progress), cancellationToken); - } - - private void SortFiles(CancellationToken cancellationToken, IProgress progress) - { - new TvFileSorter(_libraryManager, _logger, _fileSystem).Sort(_config.Configuration.FileSortingOptions, cancellationToken, progress); + return new TvFileSorter(_libraryManager, _logger, _fileSystem, _iFileSortingRepository).Sort(_config.Configuration.FileSortingOptions, cancellationToken, progress); } public IEnumerable GetDefaultTriggers() diff --git a/MediaBrowser.Server.Implementations/FileSorting/TvFileSorter.cs b/MediaBrowser.Server.Implementations/FileSorting/TvFileSorter.cs index cac05d426..679ab2c2d 100644 --- a/MediaBrowser.Server.Implementations/FileSorting/TvFileSorter.cs +++ b/MediaBrowser.Server.Implementations/FileSorting/TvFileSorter.cs @@ -1,10 +1,12 @@ using MediaBrowser.Common.IO; using MediaBrowser.Controller.Entities.TV; using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.Persistence; using MediaBrowser.Controller.Providers; using MediaBrowser.Controller.Resolvers; using MediaBrowser.Model.Configuration; using MediaBrowser.Model.Entities; +using MediaBrowser.Model.FileSorting; using MediaBrowser.Model.Logging; using System; using System.Collections.Generic; @@ -12,6 +14,7 @@ using System.Globalization; using System.IO; using System.Linq; using System.Threading; +using System.Threading.Tasks; namespace MediaBrowser.Server.Implementations.FileSorting { @@ -20,26 +23,29 @@ namespace MediaBrowser.Server.Implementations.FileSorting private readonly ILibraryManager _libraryManager; private readonly ILogger _logger; private readonly IFileSystem _fileSystem; + private readonly IFileSortingRepository _iFileSortingRepository; private static readonly CultureInfo UsCulture = new CultureInfo("en-US"); - - public TvFileSorter(ILibraryManager libraryManager, ILogger logger, IFileSystem fileSystem) + + public TvFileSorter(ILibraryManager libraryManager, ILogger logger, IFileSystem fileSystem, IFileSortingRepository iFileSortingRepository) { _libraryManager = libraryManager; _logger = logger; _fileSystem = fileSystem; + _iFileSortingRepository = iFileSortingRepository; } - public void Sort(FileSortingOptions options, CancellationToken cancellationToken, IProgress progress) + public async Task Sort(FileSortingOptions options, CancellationToken cancellationToken, IProgress progress) { var minFileBytes = options.MinFileSizeMb * 1024 * 1024; - var eligibleFiles = options.TvWatchLocations.SelectMany(GetEligibleFiles) + var eligibleFiles = options.TvWatchLocations.SelectMany(GetFilesToSort) + .OrderBy(_fileSystem.GetCreationTimeUtc) .Where(i => EntityResolutionHelper.IsVideoFile(i.FullName) && i.Length >= minFileBytes) .ToList(); progress.Report(10); - + if (eligibleFiles.Count > 0) { var allSeries = _libraryManager.RootFolder @@ -48,16 +54,16 @@ namespace MediaBrowser.Server.Implementations.FileSorting .ToList(); var numComplete = 0; - + foreach (var file in eligibleFiles) { - SortFile(file.FullName, options, allSeries); + await SortFile(file.FullName, options, allSeries).ConfigureAwait(false); numComplete++; double percent = numComplete; percent /= eligibleFiles.Count; - progress.Report(100 * percent); + progress.Report(10 + (89 * percent)); } } @@ -83,7 +89,12 @@ namespace MediaBrowser.Server.Implementations.FileSorting progress.Report(100); } - private IEnumerable GetEligibleFiles(string path) + /// + /// Gets the eligible files. + /// + /// The path. + /// IEnumerable{FileInfo}. + private IEnumerable GetFilesToSort(string path) { try { @@ -95,14 +106,26 @@ namespace MediaBrowser.Server.Implementations.FileSorting { _logger.ErrorException("Error getting files from {0}", ex, path); - return new List(); + return new List(); } } - private void SortFile(string path, FileSortingOptions options, IEnumerable allSeries) + /// + /// Sorts the file. + /// + /// The path. + /// The options. + /// All series. + private Task SortFile(string path, FileSortingOptions options, IEnumerable allSeries) { _logger.Info("Sorting file {0}", path); + var result = new FileSortingResult + { + Date = DateTime.UtcNow, + OriginalPath = path + }; + var seriesName = TVUtils.GetSeriesNameFromEpisodeFile(path); if (!string.IsNullOrEmpty(seriesName)) @@ -118,31 +141,55 @@ namespace MediaBrowser.Server.Implementations.FileSorting { _logger.Debug("Extracted information from {0}. Series name {1}, Season {2}, Episode {3}", path, seriesName, season, episode); - SortFile(path, seriesName, season.Value, episode.Value, options, allSeries); + SortFile(path, seriesName, season.Value, episode.Value, options, allSeries, result); } else { - _logger.Warn("Unable to determine episode number from {0}", path); + var msg = string.Format("Unable to determine episode number from {0}", path); + result.Status = FileSortingStatus.Failure; + result.ErrorMessage = msg; + _logger.Warn(msg); } } else { - _logger.Warn("Unable to determine season number from {0}", path); + var msg = string.Format("Unable to determine season number from {0}", path); + result.Status = FileSortingStatus.Failure; + result.ErrorMessage = msg; + _logger.Warn(msg); } } else { - _logger.Warn("Unable to determine series name from {0}", path); + var msg = string.Format("Unable to determine series name from {0}", path); + result.Status = FileSortingStatus.Failure; + result.ErrorMessage = msg; + _logger.Warn(msg); } + + return LogResult(result); } - private void SortFile(string path, string seriesName, int seasonNumber, int episodeNumber, FileSortingOptions options, IEnumerable allSeries) + /// + /// Sorts the file. + /// + /// The path. + /// Name of the series. + /// The season number. + /// The episode number. + /// The options. + /// All series. + /// The result. + private void SortFile(string path, string seriesName, int seasonNumber, int episodeNumber, FileSortingOptions options, IEnumerable allSeries, FileSortingResult result) { var series = GetMatchingSeries(seriesName, allSeries); if (series == null) { - _logger.Warn("Unable to find series in library matching name {0}", seriesName); + var msg = string.Format("Unable to find series in library matching name {0}", seriesName); + result.Status = FileSortingStatus.Failure; + result.ErrorMessage = msg; + _logger.Warn(msg); return; } @@ -153,13 +200,58 @@ namespace MediaBrowser.Server.Implementations.FileSorting if (string.IsNullOrEmpty(newPath)) { - _logger.Warn("Unable to sort {0} because target path could not be found.", path); + var msg = string.Format("Unable to sort {0} because target path could not be determined.", path); + result.Status = FileSortingStatus.Failure; + result.ErrorMessage = msg; + _logger.Warn(msg); return; } _logger.Info("Sorting file {0} to new path {1}", path, newPath); + result.TargetPath = newPath; + + if (options.EnableTrialMode) + { + result.Status = FileSortingStatus.SkippedTrial; + return; + } + + if (!options.OverwriteExistingEpisodes && File.Exists(result.TargetPath)) + { + result.Status = FileSortingStatus.SkippedExisting; + return; + } + + PerformFileSorting(options, result); + } + + /// + /// Performs the file sorting. + /// + /// The options. + /// The result. + private void PerformFileSorting(FileSortingOptions options, FileSortingResult result) + { } + /// + /// Logs the result. + /// + /// The result. + /// Task. + private Task LogResult(FileSortingResult result) + { + return _iFileSortingRepository.SaveResult(result, CancellationToken.None); + } + + /// + /// Gets the new path. + /// + /// The series. + /// The season number. + /// The episode number. + /// The options. + /// System.String. private string GetNewPath(Series series, int seasonNumber, int episodeNumber, FileSortingOptions options) { var currentEpisodes = series.RecursiveChildren.OfType() -- cgit v1.2.3