diff --git a/MediaBrowser.Model/Configuration/ServerConfiguration.cs b/MediaBrowser.Model/Configuration/ServerConfiguration.cs
index 180d0292f0..9c13c86457 100644
--- a/MediaBrowser.Model/Configuration/ServerConfiguration.cs
+++ b/MediaBrowser.Model/Configuration/ServerConfiguration.cs
@@ -224,7 +224,7 @@ namespace MediaBrowser.Model.Configuration
public bool EnableAutomaticRestart { get; set; }
- public FileSortingOptions FileSortingOptions { get; set; }
+ public TvFileSortingOptions TvFileSortingOptions { get; set; }
public LiveTvOptions LiveTvOptions { get; set; }
///
@@ -293,7 +293,7 @@ namespace MediaBrowser.Model.Configuration
LiveTvOptions = new LiveTvOptions();
- FileSortingOptions = new FileSortingOptions();
+ TvFileSortingOptions = new TvFileSortingOptions();
}
}
@@ -316,12 +316,12 @@ namespace MediaBrowser.Model.Configuration
public int? GuideDays { get; set; }
}
- public class FileSortingOptions
+ public class TvFileSortingOptions
{
public bool IsEnabled { get; set; }
public int MinFileSizeMb { get; set; }
public string[] LeftOverFileExtensionsToDelete { get; set; }
- public string[] TvWatchLocations { get; set; }
+ public string[] WatchLocations { get; set; }
public string SeasonFolderPattern { get; set; }
@@ -336,7 +336,7 @@ namespace MediaBrowser.Model.Configuration
///
public bool EnableTrialMode { get; set; }
- public FileSortingOptions()
+ public TvFileSortingOptions()
{
MinFileSizeMb = 50;
@@ -345,7 +345,7 @@ namespace MediaBrowser.Model.Configuration
".txt"
};
- TvWatchLocations = new string[] { };
+ WatchLocations = new string[] { };
SeasonFolderPattern = "Season %s";
SeasonZeroFolderName = "Season 0";
diff --git a/MediaBrowser.Server.Implementations/FileSorting/SortingScheduledTask.cs b/MediaBrowser.Server.Implementations/FileSorting/SortingScheduledTask.cs
index 622546794e..85d172d361 100644
--- a/MediaBrowser.Server.Implementations/FileSorting/SortingScheduledTask.cs
+++ b/MediaBrowser.Server.Implementations/FileSorting/SortingScheduledTask.cs
@@ -45,7 +45,7 @@ namespace MediaBrowser.Server.Implementations.FileSorting
public Task Execute(CancellationToken cancellationToken, IProgress progress)
{
- return new TvFileSorter(_libraryManager, _logger, _fileSystem, _iFileSortingRepository).Sort(_config.Configuration.FileSortingOptions, cancellationToken, progress);
+ return new TvFileSorter(_libraryManager, _logger, _fileSystem, _iFileSortingRepository).Sort(_config.Configuration.TvFileSortingOptions, cancellationToken, progress);
}
public IEnumerable GetDefaultTriggers()
@@ -58,12 +58,12 @@ namespace MediaBrowser.Server.Implementations.FileSorting
public bool IsHidden
{
- get { return !_config.Configuration.FileSortingOptions.IsEnabled; }
+ get { return !_config.Configuration.TvFileSortingOptions.IsEnabled; }
}
public bool IsEnabled
{
- get { return _config.Configuration.FileSortingOptions.IsEnabled; }
+ get { return _config.Configuration.TvFileSortingOptions.IsEnabled; }
}
}
}
diff --git a/MediaBrowser.Server.Implementations/FileSorting/TvFileSorter.cs b/MediaBrowser.Server.Implementations/FileSorting/TvFileSorter.cs
index 679ab2c2d2..093a1dba66 100644
--- a/MediaBrowser.Server.Implementations/FileSorting/TvFileSorter.cs
+++ b/MediaBrowser.Server.Implementations/FileSorting/TvFileSorter.cs
@@ -35,11 +35,13 @@ namespace MediaBrowser.Server.Implementations.FileSorting
_iFileSortingRepository = iFileSortingRepository;
}
- public async Task Sort(FileSortingOptions options, CancellationToken cancellationToken, IProgress progress)
+ public async Task Sort(TvFileSortingOptions options, CancellationToken cancellationToken, IProgress progress)
{
var minFileBytes = options.MinFileSizeMb * 1024 * 1024;
- var eligibleFiles = options.TvWatchLocations.SelectMany(GetFilesToSort)
+ var watchLocations = options.WatchLocations.ToList();
+
+ var eligibleFiles = watchLocations.SelectMany(GetFilesToSort)
.OrderBy(_fileSystem.GetCreationTimeUtc)
.Where(i => EntityResolutionHelper.IsVideoFile(i.FullName) && i.Length >= minFileBytes)
.ToList();
@@ -72,7 +74,7 @@ namespace MediaBrowser.Server.Implementations.FileSorting
if (!options.EnableTrialMode)
{
- foreach (var path in options.TvWatchLocations)
+ foreach (var path in watchLocations)
{
if (options.LeftOverFileExtensionsToDelete.Length > 0)
{
@@ -116,7 +118,7 @@ namespace MediaBrowser.Server.Implementations.FileSorting
/// The path.
/// The options.
/// All series.
- private Task SortFile(string path, FileSortingOptions options, IEnumerable allSeries)
+ private Task SortFile(string path, TvFileSortingOptions options, IEnumerable allSeries)
{
_logger.Info("Sorting file {0}", path);
@@ -180,7 +182,7 @@ namespace MediaBrowser.Server.Implementations.FileSorting
/// The options.
/// All series.
/// The result.
- private void SortFile(string path, string seriesName, int seasonNumber, int episodeNumber, FileSortingOptions options, IEnumerable allSeries, FileSortingResult result)
+ private void SortFile(string path, string seriesName, int seasonNumber, int episodeNumber, TvFileSortingOptions options, IEnumerable allSeries, FileSortingResult result)
{
var series = GetMatchingSeries(seriesName, allSeries);
@@ -216,13 +218,14 @@ namespace MediaBrowser.Server.Implementations.FileSorting
return;
}
- if (!options.OverwriteExistingEpisodes && File.Exists(result.TargetPath))
+ var targetExists = File.Exists(result.TargetPath);
+ if (!options.OverwriteExistingEpisodes && targetExists)
{
result.Status = FileSortingStatus.SkippedExisting;
return;
}
- PerformFileSorting(options, result);
+ PerformFileSorting(options, result, targetExists);
}
///
@@ -230,8 +233,40 @@ namespace MediaBrowser.Server.Implementations.FileSorting
///
/// The options.
/// The result.
- private void PerformFileSorting(FileSortingOptions options, FileSortingResult result)
+ /// if set to true [copy].
+ private void PerformFileSorting(TvFileSortingOptions options, FileSortingResult result, bool copy)
{
+ try
+ {
+ if (copy)
+ {
+ File.Copy(result.OriginalPath, result.TargetPath, true);
+ }
+ else
+ {
+ File.Move(result.OriginalPath, result.TargetPath);
+ }
+ }
+ catch (Exception ex)
+ {
+ var errorMsg = string.Format("Failed to move file from {0} to {1}", result.OriginalPath, result.TargetPath);
+ result.Status = FileSortingStatus.Failure;
+ result.ErrorMessage = errorMsg;
+ _logger.ErrorException(errorMsg, ex);
+ return;
+ }
+
+ if (copy)
+ {
+ try
+ {
+ File.Delete(result.OriginalPath);
+ }
+ catch (Exception ex)
+ {
+ _logger.ErrorException("Error deleting {0}", ex, result.OriginalPath);
+ }
+ }
}
///
@@ -252,7 +287,7 @@ namespace MediaBrowser.Server.Implementations.FileSorting
/// The episode number.
/// The options.
/// System.String.
- private string GetNewPath(Series series, int seasonNumber, int episodeNumber, FileSortingOptions options)
+ private string GetNewPath(Series series, int seasonNumber, int episodeNumber, TvFileSortingOptions options)
{
var currentEpisodes = series.RecursiveChildren.OfType()
.Where(i => i.IndexNumber.HasValue && i.IndexNumber.Value == episodeNumber && i.ParentIndexNumber.HasValue && i.ParentIndexNumber.Value == seasonNumber)
@@ -295,7 +330,7 @@ namespace MediaBrowser.Server.Implementations.FileSorting
/// The season number.
/// The options.
/// System.String.
- private string GetSeasonFolderPath(Series series, int seasonNumber, FileSortingOptions options)
+ private string GetSeasonFolderPath(Series series, int seasonNumber, TvFileSortingOptions options)
{
// If there's already a season folder, use that
var season = series