aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Server.Implementations
diff options
context:
space:
mode:
Diffstat (limited to 'MediaBrowser.Server.Implementations')
-rw-r--r--MediaBrowser.Server.Implementations/FileOrganization/FileOrganizationService.cs95
-rw-r--r--MediaBrowser.Server.Implementations/FileOrganization/TvFileSorter.cs32
-rw-r--r--MediaBrowser.Server.Implementations/Library/Resolvers/Movies/BoxSetResolver.cs6
-rw-r--r--MediaBrowser.Server.Implementations/Library/Resolvers/TV/EpisodeResolver.cs14
-rw-r--r--MediaBrowser.Server.Implementations/Library/Resolvers/TV/SeriesResolver.cs1
-rw-r--r--MediaBrowser.Server.Implementations/Persistence/SqliteFileOrganizationRepository.cs259
6 files changed, 385 insertions, 22 deletions
diff --git a/MediaBrowser.Server.Implementations/FileOrganization/FileOrganizationService.cs b/MediaBrowser.Server.Implementations/FileOrganization/FileOrganizationService.cs
index bcbff74885..49037ef96d 100644
--- a/MediaBrowser.Server.Implementations/FileOrganization/FileOrganizationService.cs
+++ b/MediaBrowser.Server.Implementations/FileOrganization/FileOrganizationService.cs
@@ -1,8 +1,14 @@
-using MediaBrowser.Common.ScheduledTasks;
+using MediaBrowser.Common.Extensions;
+using MediaBrowser.Common.ScheduledTasks;
using MediaBrowser.Controller.FileOrganization;
+using MediaBrowser.Controller.IO;
+using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Model.FileOrganization;
+using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Querying;
+using System;
+using System.IO;
using System.Threading;
using System.Threading.Tasks;
@@ -12,21 +18,33 @@ namespace MediaBrowser.Server.Implementations.FileOrganization
{
private readonly ITaskManager _taskManager;
private readonly IFileOrganizationRepository _repo;
+ private readonly ILogger _logger;
+ private readonly IDirectoryWatchers _directoryWatchers;
+ private readonly ILibraryManager _libraryManager;
- public FileOrganizationService(ITaskManager taskManager, IFileOrganizationRepository repo)
+ public FileOrganizationService(ITaskManager taskManager, IFileOrganizationRepository repo, ILogger logger, IDirectoryWatchers directoryWatchers, ILibraryManager libraryManager)
{
_taskManager = taskManager;
_repo = repo;
+ _logger = logger;
+ _directoryWatchers = directoryWatchers;
+ _libraryManager = libraryManager;
}
public void BeginProcessNewFiles()
{
- _taskManager.CancelIfRunningAndQueue<OrganizerScheduledTask>();
+ _taskManager.CancelIfRunningAndQueue<OrganizerScheduledTask>();
}
-
public Task SaveResult(FileOrganizationResult result, CancellationToken cancellationToken)
{
+ if (result == null || string.IsNullOrEmpty(result.OriginalPath))
+ {
+ throw new ArgumentNullException("result");
+ }
+
+ result.Id = (result.OriginalPath + (result.TargetPath ?? string.Empty)).GetMD5().ToString("N");
+
return _repo.SaveResult(result, cancellationToken);
}
@@ -34,5 +52,74 @@ namespace MediaBrowser.Server.Implementations.FileOrganization
{
return _repo.GetResults(query);
}
+
+ public Task DeleteOriginalFile(string resultId)
+ {
+ var result = _repo.GetResult(resultId);
+
+ _logger.Info("Requested to delete {0}", result.OriginalPath);
+ try
+ {
+ File.Delete(result.OriginalPath);
+ }
+ catch (Exception ex)
+ {
+ _logger.ErrorException("Error deleting {0}", ex, result.OriginalPath);
+ }
+
+ return _repo.Delete(resultId);
+ }
+
+ public async Task PerformOrganization(string resultId)
+ {
+ var result = _repo.GetResult(resultId);
+
+ if (string.IsNullOrEmpty(result.TargetPath))
+ {
+ throw new ArgumentException("No target path available.");
+ }
+
+ _logger.Info("Moving {0} to {1}", result.OriginalPath, result.TargetPath);
+
+ _directoryWatchers.TemporarilyIgnore(result.TargetPath);
+
+ var copy = File.Exists(result.TargetPath);
+
+ try
+ {
+ if (copy)
+ {
+ File.Copy(result.OriginalPath, result.TargetPath, true);
+ }
+ else
+ {
+ File.Move(result.OriginalPath, result.TargetPath);
+ }
+ }
+ finally
+ {
+ _directoryWatchers.RemoveTempIgnore(result.TargetPath);
+ }
+
+ if (copy)
+ {
+ try
+ {
+ File.Delete(result.OriginalPath);
+ }
+ catch (Exception ex)
+ {
+ _logger.ErrorException("Error deleting {0}", ex, result.OriginalPath);
+ }
+ }
+
+ result.Status = FileSortingStatus.Success;
+ result.StatusMessage = string.Empty;
+
+ await SaveResult(result, CancellationToken.None).ConfigureAwait(false);
+
+ await _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None)
+ .ConfigureAwait(false);
+ }
}
}
diff --git a/MediaBrowser.Server.Implementations/FileOrganization/TvFileSorter.cs b/MediaBrowser.Server.Implementations/FileOrganization/TvFileSorter.cs
index e0efa0c3f2..5aaad2ad97 100644
--- a/MediaBrowser.Server.Implementations/FileOrganization/TvFileSorter.cs
+++ b/MediaBrowser.Server.Implementations/FileOrganization/TvFileSorter.cs
@@ -1,5 +1,4 @@
-using System.Text;
-using MediaBrowser.Common.IO;
+using MediaBrowser.Common.IO;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.FileOrganization;
using MediaBrowser.Controller.IO;
@@ -15,6 +14,7 @@ using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
+using System.Text;
using System.Threading;
using System.Threading.Tasks;
@@ -22,11 +22,11 @@ namespace MediaBrowser.Server.Implementations.FileOrganization
{
public class TvFileSorter
{
+ private readonly IDirectoryWatchers _directoryWatchers;
private readonly ILibraryManager _libraryManager;
private readonly ILogger _logger;
private readonly IFileSystem _fileSystem;
private readonly IFileOrganizationService _iFileSortingRepository;
- private readonly IDirectoryWatchers _directoryWatchers;
private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
@@ -67,7 +67,7 @@ namespace MediaBrowser.Server.Implementations.FileOrganization
{
var result = await SortFile(file.FullName, options, allSeries).ConfigureAwait(false);
- if (result.Status == FileSortingStatus.Success)
+ if (result.Status == FileSortingStatus.Success && !options.EnableTrialMode)
{
scanLibrary = true;
}
@@ -142,7 +142,9 @@ namespace MediaBrowser.Server.Implementations.FileOrganization
var result = new FileOrganizationResult
{
Date = DateTime.UtcNow,
- OriginalPath = path
+ OriginalPath = path,
+ OriginalFileName = Path.GetFileName(path),
+ Type = FileOrganizerType.Episode
};
var seriesName = TVUtils.GetSeriesNameFromEpisodeFile(path);
@@ -166,7 +168,7 @@ namespace MediaBrowser.Server.Implementations.FileOrganization
{
var msg = string.Format("Unable to determine episode number from {0}", path);
result.Status = FileSortingStatus.Failure;
- result.ErrorMessage = msg;
+ result.StatusMessage = msg;
_logger.Warn(msg);
}
}
@@ -174,7 +176,7 @@ namespace MediaBrowser.Server.Implementations.FileOrganization
{
var msg = string.Format("Unable to determine season number from {0}", path);
result.Status = FileSortingStatus.Failure;
- result.ErrorMessage = msg;
+ result.StatusMessage = msg;
_logger.Warn(msg);
}
}
@@ -182,7 +184,7 @@ namespace MediaBrowser.Server.Implementations.FileOrganization
{
var msg = string.Format("Unable to determine series name from {0}", path);
result.Status = FileSortingStatus.Failure;
- result.ErrorMessage = msg;
+ result.StatusMessage = msg;
_logger.Warn(msg);
}
@@ -203,13 +205,13 @@ namespace MediaBrowser.Server.Implementations.FileOrganization
/// <param name="result">The result.</param>
private void SortFile(string path, string seriesName, int seasonNumber, int episodeNumber, TvFileOrganizationOptions options, IEnumerable<Series> allSeries, FileOrganizationResult result)
{
- var series = GetMatchingSeries(seriesName, allSeries);
+ var series = GetMatchingSeries(seriesName, allSeries, result);
if (series == null)
{
var msg = string.Format("Unable to find series in library matching name {0}", seriesName);
result.Status = FileSortingStatus.Failure;
- result.ErrorMessage = msg;
+ result.StatusMessage = msg;
_logger.Warn(msg);
return;
}
@@ -223,7 +225,7 @@ namespace MediaBrowser.Server.Implementations.FileOrganization
{
var msg = string.Format("Unable to sort {0} because target path could not be determined.", path);
result.Status = FileSortingStatus.Failure;
- result.ErrorMessage = msg;
+ result.StatusMessage = msg;
_logger.Warn(msg);
return;
}
@@ -273,7 +275,7 @@ namespace MediaBrowser.Server.Implementations.FileOrganization
var errorMsg = string.Format("Failed to move file from {0} to {1}", result.OriginalPath, result.TargetPath);
result.Status = FileSortingStatus.Failure;
- result.ErrorMessage = errorMsg;
+ result.StatusMessage = errorMsg;
_logger.ErrorException(errorMsg, ex);
return;
@@ -413,12 +415,15 @@ namespace MediaBrowser.Server.Implementations.FileOrganization
/// <param name="seriesName">Name of the series.</param>
/// <param name="allSeries">All series.</param>
/// <returns>Series.</returns>
- private Series GetMatchingSeries(string seriesName, IEnumerable<Series> allSeries)
+ private Series GetMatchingSeries(string seriesName, IEnumerable<Series> allSeries, FileOrganizationResult result)
{
int? yearInName;
var nameWithoutYear = seriesName;
NameParser.ParseName(nameWithoutYear, out nameWithoutYear, out yearInName);
+ result.ExtractedName = nameWithoutYear;
+ result.ExtractedYear = yearInName;
+
return allSeries.Select(i => GetMatchScore(nameWithoutYear, yearInName, i))
.Where(i => i.Item2 > 0)
.OrderByDescending(i => i.Item2)
@@ -473,6 +478,7 @@ namespace MediaBrowser.Server.Implementations.FileOrganization
.Replace("&", " ")
.Replace("!", " ")
.Replace(",", " ")
+ .Replace("-", " ")
.Replace(" a ", string.Empty)
.Replace(" the ", string.Empty)
.Replace(" ", string.Empty);
diff --git a/MediaBrowser.Server.Implementations/Library/Resolvers/Movies/BoxSetResolver.cs b/MediaBrowser.Server.Implementations/Library/Resolvers/Movies/BoxSetResolver.cs
index 9afeb8eb9b..1416bd04ea 100644
--- a/MediaBrowser.Server.Implementations/Library/Resolvers/Movies/BoxSetResolver.cs
+++ b/MediaBrowser.Server.Implementations/Library/Resolvers/Movies/BoxSetResolver.cs
@@ -32,6 +32,12 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.Movies
return null;
}
+ // This is a bit of a one-off but it's here to combat MCM's over-aggressive placement of collection.xml files where they don't belong, including in series folders.
+ if (args.ContainsMetaFileByName("series.xml"))
+ {
+ return null;
+ }
+
if (filename.IndexOf("[boxset]", StringComparison.OrdinalIgnoreCase) != -1 || args.ContainsFileSystemEntryByName("collection.xml"))
{
return new BoxSet { Path = args.Path };
diff --git a/MediaBrowser.Server.Implementations/Library/Resolvers/TV/EpisodeResolver.cs b/MediaBrowser.Server.Implementations/Library/Resolvers/TV/EpisodeResolver.cs
index 693594a20b..420c9f583f 100644
--- a/MediaBrowser.Server.Implementations/Library/Resolvers/TV/EpisodeResolver.cs
+++ b/MediaBrowser.Server.Implementations/Library/Resolvers/TV/EpisodeResolver.cs
@@ -2,6 +2,7 @@
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Resolvers;
using MediaBrowser.Model.Entities;
+using System.Linq;
namespace MediaBrowser.Server.Implementations.Library.Resolvers.TV
{
@@ -17,7 +18,18 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers.TV
/// <returns>Episode.</returns>
protected override Episode Resolve(ItemResolveArgs args)
{
- var season = args.Parent as Season;
+ var parent = args.Parent;
+ var season = parent as Season;
+
+ // Just in case the user decided to nest episodes.
+ // Not officially supported but in some cases we can handle it.
+ if (season == null)
+ {
+ if (parent != null)
+ {
+ season = parent.Parents.OfType<Season>().FirstOrDefault();
+ }
+ }
// If the parent is a Season or Series, then this is an Episode if the VideoResolver returns something
if (season != null || args.Parent is Series)
diff --git a/MediaBrowser.Server.Implementations/Library/Resolvers/TV/SeriesResolver.cs b/MediaBrowser.Server.Implementations/Library/Resolvers/TV/SeriesResolver.cs
index 55a485318d..1bd9759443 100644
--- a/MediaBrowser.Server.Implementations/Library/Resolvers/TV/SeriesResolver.cs
+++ b/MediaBrowser.Server.Implementations/Library/Resolvers/TV/SeriesResolver.cs
@@ -1,6 +1,5 @@
using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Entities.Audio;
-using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Resolvers;
diff --git a/MediaBrowser.Server.Implementations/Persistence/SqliteFileOrganizationRepository.cs b/MediaBrowser.Server.Implementations/Persistence/SqliteFileOrganizationRepository.cs
index e397cc280a..75de131944 100644
--- a/MediaBrowser.Server.Implementations/Persistence/SqliteFileOrganizationRepository.cs
+++ b/MediaBrowser.Server.Implementations/Persistence/SqliteFileOrganizationRepository.cs
@@ -4,7 +4,9 @@ using MediaBrowser.Model.FileOrganization;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Querying;
using System;
+using System.Collections.Generic;
using System.Data;
+using System.Globalization;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
@@ -21,6 +23,11 @@ namespace MediaBrowser.Server.Implementations.Persistence
private SqliteShrinkMemoryTimer _shrinkMemoryTimer;
private readonly IServerApplicationPaths _appPaths;
+ private readonly CultureInfo _usCulture = new CultureInfo("en-US");
+
+ private IDbCommand _saveResultCommand;
+ private IDbCommand _deleteResultCommand;
+
public SqliteFileOrganizationRepository(ILogManager logManager, IServerApplicationPaths appPaths)
{
_appPaths = appPaths;
@@ -40,6 +47,9 @@ namespace MediaBrowser.Server.Implementations.Persistence
string[] queries = {
+ "create table if not exists organizationresults (ResultId GUID PRIMARY KEY, OriginalPath TEXT, TargetPath TEXT, OrganizationDate datetime, Status TEXT, OrganizationType TEXT, StatusMessage TEXT, ExtractedName TEXT, ExtractedYear int null)",
+ "create index if not exists idx_organizationresults on organizationresults(ResultId)",
+
//pragmas
"pragma temp_store = memory",
@@ -55,16 +65,259 @@ namespace MediaBrowser.Server.Implementations.Persistence
private void PrepareStatements()
{
+ _saveResultCommand = _connection.CreateCommand();
+ _saveResultCommand.CommandText = "replace into organizationresults (ResultId, OriginalPath, TargetPath, OrganizationDate, Status, OrganizationType, StatusMessage, ExtractedName, ExtractedYear) values (@ResultId, @OriginalPath, @TargetPath, @OrganizationDate, @Status, @OrganizationType, @StatusMessage, @ExtractedName, @ExtractedYear)";
+
+ _saveResultCommand.Parameters.Add(_saveResultCommand, "@ResultId");
+ _saveResultCommand.Parameters.Add(_saveResultCommand, "@OriginalPath");
+ _saveResultCommand.Parameters.Add(_saveResultCommand, "@TargetPath");
+ _saveResultCommand.Parameters.Add(_saveResultCommand, "@OrganizationDate");
+ _saveResultCommand.Parameters.Add(_saveResultCommand, "@Status");
+ _saveResultCommand.Parameters.Add(_saveResultCommand, "@OrganizationType");
+ _saveResultCommand.Parameters.Add(_saveResultCommand, "@StatusMessage");
+ _saveResultCommand.Parameters.Add(_saveResultCommand, "@ExtractedName");
+ _saveResultCommand.Parameters.Add(_saveResultCommand, "@ExtractedYear");
+
+ _deleteResultCommand = _connection.CreateCommand();
+ _deleteResultCommand.CommandText = "delete from organizationresults where ResultId = @ResultId";
+
+ _deleteResultCommand.Parameters.Add(_saveResultCommand, "@ResultId");
}
- public Task SaveResult(FileOrganizationResult result, CancellationToken cancellationToken)
+ public async Task SaveResult(FileOrganizationResult result, CancellationToken cancellationToken)
{
- return Task.FromResult(true);
+ if (result == null)
+ {
+ throw new ArgumentNullException("result");
+ }
+
+ cancellationToken.ThrowIfCancellationRequested();
+
+ await _writeLock.WaitAsync(cancellationToken).ConfigureAwait(false);
+
+ IDbTransaction transaction = null;
+
+ try
+ {
+ transaction = _connection.BeginTransaction();
+
+ _saveResultCommand.GetParameter(0).Value = new Guid(result.Id);
+ _saveResultCommand.GetParameter(1).Value = result.OriginalPath;
+ _saveResultCommand.GetParameter(2).Value = result.TargetPath;
+ _saveResultCommand.GetParameter(3).Value = result.Date;
+ _saveResultCommand.GetParameter(4).Value = result.Status.ToString();
+ _saveResultCommand.GetParameter(5).Value = result.Type.ToString();
+ _saveResultCommand.GetParameter(6).Value = result.StatusMessage;
+ _saveResultCommand.GetParameter(7).Value = result.ExtractedName;
+ _saveResultCommand.GetParameter(8).Value = result.ExtractedYear;
+
+ _saveResultCommand.Transaction = transaction;
+
+ _saveResultCommand.ExecuteNonQuery();
+
+ transaction.Commit();
+ }
+ catch (OperationCanceledException)
+ {
+ if (transaction != null)
+ {
+ transaction.Rollback();
+ }
+
+ throw;
+ }
+ catch (Exception e)
+ {
+ _logger.ErrorException("Failed to save FileOrganizationResult:", e);
+
+ if (transaction != null)
+ {
+ transaction.Rollback();
+ }
+
+ throw;
+ }
+ finally
+ {
+ if (transaction != null)
+ {
+ transaction.Dispose();
+ }
+
+ _writeLock.Release();
+ }
+ }
+
+ public async Task Delete(string id)
+ {
+ if (string.IsNullOrEmpty(id))
+ {
+ throw new ArgumentNullException("id");
+ }
+
+ await _writeLock.WaitAsync().ConfigureAwait(false);
+
+ IDbTransaction transaction = null;
+
+ try
+ {
+ transaction = _connection.BeginTransaction();
+
+ _deleteResultCommand.GetParameter(0).Value = new Guid(id);
+
+ _deleteResultCommand.Transaction = transaction;
+
+ _deleteResultCommand.ExecuteNonQuery();
+
+ transaction.Commit();
+ }
+ catch (OperationCanceledException)
+ {
+ if (transaction != null)
+ {
+ transaction.Rollback();
+ }
+
+ throw;
+ }
+ catch (Exception e)
+ {
+ _logger.ErrorException("Failed to save FileOrganizationResult:", e);
+
+ if (transaction != null)
+ {
+ transaction.Rollback();
+ }
+
+ throw;
+ }
+ finally
+ {
+ if (transaction != null)
+ {
+ transaction.Dispose();
+ }
+
+ _writeLock.Release();
+ }
}
public QueryResult<FileOrganizationResult> GetResults(FileOrganizationResultQuery query)
{
- return new QueryResult<FileOrganizationResult>();
+ if (query == null)
+ {
+ throw new ArgumentNullException("query");
+ }
+
+ using (var cmd = _connection.CreateCommand())
+ {
+ cmd.CommandText = "SELECT ResultId, OriginalPath, TargetPath, OrganizationDate, Status, OrganizationType, StatusMessage, ExtractedName, ExtractedYear from organizationresults";
+
+ if (query.StartIndex.HasValue && query.StartIndex.Value > 0)
+ {
+ cmd.CommandText += string.Format(" WHERE ResultId NOT IN (SELECT ResultId FROM organizationresults ORDER BY OrganizationDate desc LIMIT {0})",
+ query.StartIndex.Value.ToString(_usCulture));
+ }
+
+ cmd.CommandText += " ORDER BY OrganizationDate desc";
+
+ if (query.Limit.HasValue)
+ {
+ cmd.CommandText += " LIMIT " + query.Limit.Value.ToString(_usCulture);
+ }
+
+ cmd.CommandText += "; select count (ResultId) from organizationresults";
+
+ var list = new List<FileOrganizationResult>();
+ var count = 0;
+
+ using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
+ {
+ while (reader.Read())
+ {
+ list.Add(GetResult(reader));
+ }
+
+ if (reader.NextResult() && reader.Read())
+ {
+ count = reader.GetInt32(0);
+ }
+ }
+
+ return new QueryResult<FileOrganizationResult>()
+ {
+ Items = list.ToArray(),
+ TotalRecordCount = count
+ };
+ }
+ }
+
+ public FileOrganizationResult GetResult(string id)
+ {
+ if (string.IsNullOrEmpty(id))
+ {
+ throw new ArgumentNullException("id");
+ }
+
+ var guid = new Guid(id);
+
+ using (var cmd = _connection.CreateCommand())
+ {
+ cmd.CommandText = "select ResultId, OriginalPath, TargetPath, OrganizationDate, Status, OrganizationType, StatusMessage, ExtractedName, ExtractedYear from organizationresults where ResultId=@Id";
+
+ cmd.Parameters.Add(cmd, "@Id", DbType.Guid).Value = guid;
+
+ using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult | CommandBehavior.SingleRow))
+ {
+ if (reader.Read())
+ {
+ return GetResult(reader);
+ }
+ }
+ }
+
+ return null;
+ }
+
+ public FileOrganizationResult GetResult(IDataReader reader)
+ {
+ var result = new FileOrganizationResult
+ {
+ Id = reader.GetGuid(0).ToString("N")
+ };
+
+ if (!reader.IsDBNull(1))
+ {
+ result.OriginalPath = reader.GetString(1);
+ }
+
+ if (!reader.IsDBNull(2))
+ {
+ result.TargetPath = reader.GetString(2);
+ }
+
+ result.Date = reader.GetDateTime(3).ToUniversalTime();
+ result.Status = (FileSortingStatus)Enum.Parse(typeof(FileSortingStatus), reader.GetString(4), true);
+ result.Type = (FileOrganizerType)Enum.Parse(typeof(FileOrganizerType), reader.GetString(5), true);
+
+ if (!reader.IsDBNull(6))
+ {
+ result.StatusMessage = reader.GetString(6);
+ }
+
+ result.OriginalFileName = Path.GetFileName(result.OriginalPath);
+
+ if (!reader.IsDBNull(7))
+ {
+ result.ExtractedName = reader.GetString(7);
+ }
+
+ if (!reader.IsDBNull(8))
+ {
+ result.ExtractedYear = reader.GetInt32(8);
+ }
+
+ return result;
}
/// <summary>