From 123528327974d9291f5868bb87a0d63437fa1ae5 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Tue, 21 Jan 2014 01:10:58 -0500 Subject: [PATCH] #680 - added auto organize page --- .../Library/FileOrganizationService.cs | 43 ++- .../IFileOrganizationService.cs | 14 + .../IFileOrganizationRepository.cs | 14 + .../FileOrganizationResult.cs | 39 ++- .../FileOrganizationService.cs | 95 ++++++- .../FileOrganization/TvFileSorter.cs | 32 ++- .../Resolvers/Movies/BoxSetResolver.cs | 6 + .../Library/Resolvers/TV/EpisodeResolver.cs | 14 +- .../Library/Resolvers/TV/SeriesResolver.cs | 1 - .../SqliteFileOrganizationRepository.cs | 259 +++++++++++++++++- .../ApplicationHost.cs | 2 +- .../FFMpeg/FFMpegDownloader.cs | 8 +- .../Api/DashboardService.cs | 1 + MediaBrowser.WebDashboard/ApiClient.js | 37 ++- .../MediaBrowser.WebDashboard.csproj | 6 + MediaBrowser.WebDashboard/packages.config | 2 +- 16 files changed, 541 insertions(+), 32 deletions(-) diff --git a/MediaBrowser.Api/Library/FileOrganizationService.cs b/MediaBrowser.Api/Library/FileOrganizationService.cs index 529a755061..1244a16f1b 100644 --- a/MediaBrowser.Api/Library/FileOrganizationService.cs +++ b/MediaBrowser.Api/Library/FileOrganizationService.cs @@ -2,10 +2,11 @@ using MediaBrowser.Model.FileOrganization; using MediaBrowser.Model.Querying; using ServiceStack; +using System.Threading.Tasks; namespace MediaBrowser.Api.Library { - [Route("/Library/FileOrganization/Results", "GET")] + [Route("/Library/FileOrganization", "GET")] [Api(Description = "Gets file organization results")] public class GetFileOrganizationActivity : IReturn> { @@ -24,6 +25,30 @@ namespace MediaBrowser.Api.Library public int? Limit { get; set; } } + [Route("/Library/FileOrganizations/{Id}/File", "DELETE")] + [Api(Description = "Deletes the original file of a organizer result")] + public class DeleteOriginalFile : IReturn> + { + /// + /// Gets or sets the id. + /// + /// The id. + [ApiMember(Name = "Id", Description = "Result Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")] + public string Id { get; set; } + } + + [Route("/Library/FileOrganizations/{Id}/Organize", "POST")] + [Api(Description = "Performs an organization")] + public class PerformOrganization : IReturn> + { + /// + /// Gets or sets the id. + /// + /// The id. + [ApiMember(Name = "Id", Description = "Result Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")] + public string Id { get; set; } + } + public class FileOrganizationService : BaseApiService { private readonly IFileOrganizationService _iFileOrganizationService; @@ -38,10 +63,24 @@ namespace MediaBrowser.Api.Library var result = _iFileOrganizationService.GetResults(new FileOrganizationResultQuery { Limit = request.Limit, - StartIndex = request.Limit + StartIndex = request.StartIndex }); return ToOptimizedResult(result); } + + public void Delete(DeleteOriginalFile request) + { + var task = _iFileOrganizationService.DeleteOriginalFile(request.Id); + + Task.WaitAll(task); + } + + public void Post(PerformOrganization request) + { + var task = _iFileOrganizationService.PerformOrganization(request.Id); + + Task.WaitAll(task); + } } } diff --git a/MediaBrowser.Controller/FileOrganization/IFileOrganizationService.cs b/MediaBrowser.Controller/FileOrganization/IFileOrganizationService.cs index 58a6125087..1978402144 100644 --- a/MediaBrowser.Controller/FileOrganization/IFileOrganizationService.cs +++ b/MediaBrowser.Controller/FileOrganization/IFileOrganizationService.cs @@ -20,6 +20,20 @@ namespace MediaBrowser.Controller.FileOrganization /// Task. Task SaveResult(FileOrganizationResult result, CancellationToken cancellationToken); + /// + /// Deletes the original file. + /// + /// The result identifier. + /// Task. + Task DeleteOriginalFile(string resultId); + + /// + /// Performs the organization. + /// + /// The result identifier. + /// Task. + Task PerformOrganization(string resultId); + /// /// Gets the results. /// diff --git a/MediaBrowser.Controller/Persistence/IFileOrganizationRepository.cs b/MediaBrowser.Controller/Persistence/IFileOrganizationRepository.cs index 47cfcb56eb..14d2081bb1 100644 --- a/MediaBrowser.Controller/Persistence/IFileOrganizationRepository.cs +++ b/MediaBrowser.Controller/Persistence/IFileOrganizationRepository.cs @@ -15,6 +15,20 @@ namespace MediaBrowser.Controller.Persistence /// Task. Task SaveResult(FileOrganizationResult result, CancellationToken cancellationToken); + /// + /// Deletes the specified identifier. + /// + /// The identifier. + /// Task. + Task Delete(string id); + + /// + /// Gets the result. + /// + /// The identifier. + /// FileOrganizationResult. + FileOrganizationResult GetResult(string id); + /// /// Gets the results. /// diff --git a/MediaBrowser.Model/FileOrganization/FileOrganizationResult.cs b/MediaBrowser.Model/FileOrganization/FileOrganizationResult.cs index 505f0e2da5..ca912ed634 100644 --- a/MediaBrowser.Model/FileOrganization/FileOrganizationResult.cs +++ b/MediaBrowser.Model/FileOrganization/FileOrganizationResult.cs @@ -4,12 +4,36 @@ namespace MediaBrowser.Model.FileOrganization { public class FileOrganizationResult { + /// + /// Gets or sets the result identifier. + /// + /// The result identifier. + public string Id { get; set; } + /// /// Gets or sets the original path. /// /// The original path. public string OriginalPath { get; set; } + /// + /// Gets or sets the name of the original file. + /// + /// The name of the original file. + public string OriginalFileName { get; set; } + + /// + /// Gets or sets the name of the extracted. + /// + /// The name of the extracted. + public string ExtractedName { get; set; } + + /// + /// Gets or sets the extracted year. + /// + /// The extracted year. + public int? ExtractedYear { get; set; } + /// /// Gets or sets the target path. /// @@ -26,13 +50,19 @@ namespace MediaBrowser.Model.FileOrganization /// Gets or sets the error message. /// /// The error message. - public string ErrorMessage { get; set; } + public string StatusMessage { get; set; } /// /// Gets or sets the status. /// /// The status. public FileSortingStatus Status { get; set; } + + /// + /// Gets or sets the type. + /// + /// The type. + public FileOrganizerType Type { get; set; } } public enum FileSortingStatus @@ -42,4 +72,11 @@ namespace MediaBrowser.Model.FileOrganization SkippedExisting, SkippedTrial } + + public enum FileOrganizerType + { + Movie, + Episode, + Song + } } 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(); + _taskManager.CancelIfRunningAndQueue(); } - 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(), 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 /// The result. private void SortFile(string path, string seriesName, int seasonNumber, int episodeNumber, TvFileOrganizationOptions options, IEnumerable 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 /// Name of the series. /// All series. /// Series. - private Series GetMatchingSeries(string seriesName, IEnumerable allSeries) + private Series GetMatchingSeries(string seriesName, IEnumerable 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 /// Episode. 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().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 GetResults(FileOrganizationResultQuery query) { - return new QueryResult(); + 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(); + 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() + { + 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; } /// diff --git a/MediaBrowser.ServerApplication/ApplicationHost.cs b/MediaBrowser.ServerApplication/ApplicationHost.cs index c1d36c79a2..ef77da8b84 100644 --- a/MediaBrowser.ServerApplication/ApplicationHost.cs +++ b/MediaBrowser.ServerApplication/ApplicationHost.cs @@ -294,7 +294,7 @@ namespace MediaBrowser.ServerApplication var newsService = new Server.Implementations.News.NewsService(ApplicationPaths, JsonSerializer); RegisterSingleInstance(newsService); - var fileOrganizationService = new FileOrganizationService(TaskManager, FileOrganizationRepository); + var fileOrganizationService = new FileOrganizationService(TaskManager, FileOrganizationRepository, Logger, DirectoryWatchers, LibraryManager); RegisterSingleInstance(fileOrganizationService); progress.Report(15); diff --git a/MediaBrowser.ServerApplication/FFMpeg/FFMpegDownloader.cs b/MediaBrowser.ServerApplication/FFMpeg/FFMpegDownloader.cs index 1e99c4eb0b..b9c45e0d9b 100644 --- a/MediaBrowser.ServerApplication/FFMpeg/FFMpegDownloader.cs +++ b/MediaBrowser.ServerApplication/FFMpeg/FFMpegDownloader.cs @@ -117,9 +117,13 @@ namespace MediaBrowser.ServerApplication.FFMpeg ExtractFFMpeg(tempFile, Path.GetDirectoryName(info.Path)); return; } - catch (HttpException) + catch (HttpException ex) { - + _logger.ErrorException("Error downloading {0}", ex, url); + } + catch (Exception ex) + { + _logger.ErrorException("Error unpacking {0}", ex, url); } } diff --git a/MediaBrowser.WebDashboard/Api/DashboardService.cs b/MediaBrowser.WebDashboard/Api/DashboardService.cs index 83043dd43f..28b3970d3e 100644 --- a/MediaBrowser.WebDashboard/Api/DashboardService.cs +++ b/MediaBrowser.WebDashboard/Api/DashboardService.cs @@ -495,6 +495,7 @@ namespace MediaBrowser.WebDashboard.Api "itemlistpage.js", "librarysettings.js", "libraryfileorganizer.js", + "libraryfileorganizerlog.js", "livetvchannel.js", "livetvchannels.js", "livetvguide.js", diff --git a/MediaBrowser.WebDashboard/ApiClient.js b/MediaBrowser.WebDashboard/ApiClient.js index 2b282e5e42..49ce5fd959 100644 --- a/MediaBrowser.WebDashboard/ApiClient.js +++ b/MediaBrowser.WebDashboard/ApiClient.js @@ -441,7 +441,7 @@ MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout, wi self.getLiveTvPrograms = function (options) { options = options || {}; - + if (options.channelIds && options.channelIds.length > 1800) { return self.ajax({ @@ -453,7 +453,7 @@ MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout, wi }); } else { - + return self.ajax({ type: "GET", url: self.getUrl("LiveTv/Programs", options), @@ -666,6 +666,37 @@ MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout, wi }); }; + self.getFileOrganizationResults = function (options) { + + var url = self.getUrl("Library/FileOrganization", options || {}); + + return self.ajax({ + type: "GET", + url: url, + dataType: "json" + }); + }; + + self.deleteOriginalFileFromOrganizationResult = function (id) { + + var url = self.getUrl("Library/FileOrganizations/" + id + "/File"); + + return self.ajax({ + type: "DELETE", + url: url + }); + }; + + self.performOrganization = function (id) { + + var url = self.getUrl("Library/FileOrganizations/" + id + "/Organize"); + + return self.ajax({ + type: "POST", + url: url + }); + }; + self.getLiveTvSeriesTimer = function (id) { if (!id) { @@ -4003,7 +4034,7 @@ MediaBrowser.ApiClient.create = function (clientName, applicationVersion) { var loc = window.location; var address = loc.protocol + '//' + loc.hostname; - + if (loc.port) { address += ':' + loc.port; } diff --git a/MediaBrowser.WebDashboard/MediaBrowser.WebDashboard.csproj b/MediaBrowser.WebDashboard/MediaBrowser.WebDashboard.csproj index d3b0285d0d..b60c533b3e 100644 --- a/MediaBrowser.WebDashboard/MediaBrowser.WebDashboard.csproj +++ b/MediaBrowser.WebDashboard/MediaBrowser.WebDashboard.csproj @@ -172,6 +172,9 @@ PreserveNewest + + PreserveNewest + PreserveNewest @@ -424,6 +427,9 @@ PreserveNewest + + PreserveNewest + PreserveNewest diff --git a/MediaBrowser.WebDashboard/packages.config b/MediaBrowser.WebDashboard/packages.config index 70c1bd6e29..3f77d9541e 100644 --- a/MediaBrowser.WebDashboard/packages.config +++ b/MediaBrowser.WebDashboard/packages.config @@ -1,4 +1,4 @@  - + \ No newline at end of file