Przeglądaj źródła

#680 - added auto organize page

Luke Pulverenti 11 lat temu
rodzic
commit
1235283279

+ 41 - 2
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<QueryResult<FileOrganizationResult>>
     {
@@ -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<QueryResult<FileOrganizationResult>>
+    {
+        /// <summary>
+        /// Gets or sets the id.
+        /// </summary>
+        /// <value>The id.</value>
+        [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<QueryResult<FileOrganizationResult>>
+    {
+        /// <summary>
+        /// Gets or sets the id.
+        /// </summary>
+        /// <value>The id.</value>
+        [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);
+        }
     }
 }

+ 14 - 0
MediaBrowser.Controller/FileOrganization/IFileOrganizationService.cs

@@ -20,6 +20,20 @@ namespace MediaBrowser.Controller.FileOrganization
         /// <returns>Task.</returns>
         Task SaveResult(FileOrganizationResult result, CancellationToken cancellationToken);
 
+        /// <summary>
+        /// Deletes the original file.
+        /// </summary>
+        /// <param name="resultId">The result identifier.</param>
+        /// <returns>Task.</returns>
+        Task DeleteOriginalFile(string resultId);
+
+        /// <summary>
+        /// Performs the organization.
+        /// </summary>
+        /// <param name="resultId">The result identifier.</param>
+        /// <returns>Task.</returns>
+        Task PerformOrganization(string resultId);
+        
         /// <summary>
         /// Gets the results.
         /// </summary>

+ 14 - 0
MediaBrowser.Controller/Persistence/IFileOrganizationRepository.cs

@@ -15,6 +15,20 @@ namespace MediaBrowser.Controller.Persistence
         /// <returns>Task.</returns>
         Task SaveResult(FileOrganizationResult result, CancellationToken cancellationToken);
 
+        /// <summary>
+        /// Deletes the specified identifier.
+        /// </summary>
+        /// <param name="id">The identifier.</param>
+        /// <returns>Task.</returns>
+        Task Delete(string id);
+        
+        /// <summary>
+        /// Gets the result.
+        /// </summary>
+        /// <param name="id">The identifier.</param>
+        /// <returns>FileOrganizationResult.</returns>
+        FileOrganizationResult GetResult(string id);
+
         /// <summary>
         /// Gets the results.
         /// </summary>

+ 38 - 1
MediaBrowser.Model/FileOrganization/FileOrganizationResult.cs

@@ -4,12 +4,36 @@ namespace MediaBrowser.Model.FileOrganization
 {
     public class FileOrganizationResult
     {
+        /// <summary>
+        /// Gets or sets the result identifier.
+        /// </summary>
+        /// <value>The result identifier.</value>
+        public string Id { get; set; }
+        
         /// <summary>
         /// Gets or sets the original path.
         /// </summary>
         /// <value>The original path.</value>
         public string OriginalPath { get; set; }
 
+        /// <summary>
+        /// Gets or sets the name of the original file.
+        /// </summary>
+        /// <value>The name of the original file.</value>
+        public string OriginalFileName { get; set; }
+
+        /// <summary>
+        /// Gets or sets the name of the extracted.
+        /// </summary>
+        /// <value>The name of the extracted.</value>
+        public string ExtractedName { get; set; }
+
+        /// <summary>
+        /// Gets or sets the extracted year.
+        /// </summary>
+        /// <value>The extracted year.</value>
+        public int? ExtractedYear { get; set; }
+        
         /// <summary>
         /// Gets or sets the target path.
         /// </summary>
@@ -26,13 +50,19 @@ namespace MediaBrowser.Model.FileOrganization
         /// Gets or sets the error message.
         /// </summary>
         /// <value>The error message.</value>
-        public string ErrorMessage { get; set; }
+        public string StatusMessage { get; set; }
 
         /// <summary>
         /// Gets or sets the status.
         /// </summary>
         /// <value>The status.</value>
         public FileSortingStatus Status { get; set; }
+
+        /// <summary>
+        /// Gets or sets the type.
+        /// </summary>
+        /// <value>The type.</value>
+        public FileOrganizerType Type { get; set; }
     }
 
     public enum FileSortingStatus
@@ -42,4 +72,11 @@ namespace MediaBrowser.Model.FileOrganization
         SkippedExisting,
         SkippedTrial
     }
+
+    public enum FileOrganizerType
+    {
+        Movie,
+        Episode,
+        Song
+    }
 }

+ 91 - 4
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);
+        }
     }
 }

+ 19 - 13
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);

+ 6 - 0
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 };

+ 13 - 1
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)

+ 0 - 1
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;

+ 256 - 3
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>

+ 1 - 1
MediaBrowser.ServerApplication/ApplicationHost.cs

@@ -294,7 +294,7 @@ namespace MediaBrowser.ServerApplication
             var newsService = new Server.Implementations.News.NewsService(ApplicationPaths, JsonSerializer);
             RegisterSingleInstance<INewsService>(newsService);
 
-            var fileOrganizationService = new FileOrganizationService(TaskManager, FileOrganizationRepository);
+            var fileOrganizationService = new FileOrganizationService(TaskManager, FileOrganizationRepository, Logger, DirectoryWatchers, LibraryManager);
             RegisterSingleInstance<IFileOrganizationService>(fileOrganizationService);
 
             progress.Report(15);

+ 6 - 2
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);
                 }
             }
 

+ 1 - 0
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",

+ 34 - 3
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;
     }

+ 6 - 0
MediaBrowser.WebDashboard/MediaBrowser.WebDashboard.csproj

@@ -172,6 +172,9 @@
     <Content Include="dashboard-ui\libraryfileorganizer.html">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     </Content>
+    <Content Include="dashboard-ui\libraryfileorganizerlog.html">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </Content>
     <Content Include="dashboard-ui\livetvchannel.html">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     </Content>
@@ -424,6 +427,9 @@
     <Content Include="dashboard-ui\scripts\libraryfileorganizer.js">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     </Content>
+    <Content Include="dashboard-ui\scripts\libraryfileorganizerlog.js">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </Content>
     <Content Include="dashboard-ui\scripts\librarymenu.js">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     </Content>

+ 1 - 1
MediaBrowser.WebDashboard/packages.config

@@ -1,4 +1,4 @@
 <?xml version="1.0" encoding="utf-8"?>
 <packages>
-  <package id="MediaBrowser.ApiClient.Javascript" version="3.0.238" targetFramework="net45" />
+  <package id="MediaBrowser.ApiClient.Javascript" version="3.0.240" targetFramework="net45" />
 </packages>