2014-01-22 10:05:06 -07:00
|
|
|
|
using MediaBrowser.Common.IO;
|
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
|
using MediaBrowser.Controller.FileOrganization;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
2014-02-19 21:53:15 -07:00
|
|
|
|
using MediaBrowser.Controller.Providers;
|
2014-01-22 10:05:06 -07:00
|
|
|
|
using MediaBrowser.Model.FileOrganization;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2015-10-03 21:23:11 -07:00
|
|
|
|
using CommonIO;
|
2014-01-22 10:05:06 -07:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Implementations.FileOrganization
|
|
|
|
|
{
|
|
|
|
|
public class TvFolderOrganizer
|
|
|
|
|
{
|
2014-01-28 14:25:10 -07:00
|
|
|
|
private readonly ILibraryMonitor _libraryMonitor;
|
2014-01-22 10:05:06 -07:00
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
|
private readonly IFileOrganizationService _organizationService;
|
|
|
|
|
private readonly IServerConfigurationManager _config;
|
2014-02-19 21:53:15 -07:00
|
|
|
|
private readonly IProviderManager _providerManager;
|
2014-01-22 10:05:06 -07:00
|
|
|
|
|
2014-02-19 21:53:15 -07:00
|
|
|
|
public TvFolderOrganizer(ILibraryManager libraryManager, ILogger logger, IFileSystem fileSystem, ILibraryMonitor libraryMonitor, IFileOrganizationService organizationService, IServerConfigurationManager config, IProviderManager providerManager)
|
2014-01-22 10:05:06 -07:00
|
|
|
|
{
|
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_fileSystem = fileSystem;
|
2014-01-28 14:25:10 -07:00
|
|
|
|
_libraryMonitor = libraryMonitor;
|
2014-01-22 10:05:06 -07:00
|
|
|
|
_organizationService = organizationService;
|
|
|
|
|
_config = config;
|
2014-02-19 21:53:15 -07:00
|
|
|
|
_providerManager = providerManager;
|
2014-01-22 10:05:06 -07:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-03 20:38:46 -07:00
|
|
|
|
private bool EnableOrganization(FileSystemMetadata fileInfo, TvFileOrganizationOptions options)
|
2015-10-02 16:08:01 -07:00
|
|
|
|
{
|
2015-10-03 20:38:46 -07:00
|
|
|
|
var minFileBytes = options.MinFileSizeMb * 1024 * 1024;
|
|
|
|
|
|
2015-10-02 16:08:01 -07:00
|
|
|
|
try
|
|
|
|
|
{
|
2015-10-03 20:38:46 -07:00
|
|
|
|
return _libraryManager.IsVideoFile(fileInfo.FullName) && fileInfo.Length >= minFileBytes;
|
2015-10-02 16:08:01 -07:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error organizing file {0}", ex, fileInfo.Name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-03 20:38:46 -07:00
|
|
|
|
public async Task Organize(TvFileOrganizationOptions options, CancellationToken cancellationToken, IProgress<double> progress)
|
2014-01-22 10:05:06 -07:00
|
|
|
|
{
|
|
|
|
|
var watchLocations = options.WatchLocations.ToList();
|
|
|
|
|
|
|
|
|
|
var eligibleFiles = watchLocations.SelectMany(GetFilesToOrganize)
|
|
|
|
|
.OrderBy(_fileSystem.GetCreationTimeUtc)
|
2015-10-03 20:38:46 -07:00
|
|
|
|
.Where(i => EnableOrganization(i, options))
|
2014-01-22 10:05:06 -07:00
|
|
|
|
.ToList();
|
|
|
|
|
|
2015-09-18 18:22:23 -07:00
|
|
|
|
var processedFolders = new HashSet<string>();
|
|
|
|
|
|
2014-01-22 10:05:06 -07:00
|
|
|
|
progress.Report(10);
|
|
|
|
|
|
|
|
|
|
if (eligibleFiles.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
var numComplete = 0;
|
|
|
|
|
|
|
|
|
|
foreach (var file in eligibleFiles)
|
|
|
|
|
{
|
|
|
|
|
var organizer = new EpisodeFileOrganizer(_organizationService, _config, _fileSystem, _logger, _libraryManager,
|
2014-02-19 21:53:15 -07:00
|
|
|
|
_libraryMonitor, _providerManager);
|
2014-01-22 10:05:06 -07:00
|
|
|
|
|
2015-06-08 14:32:20 -07:00
|
|
|
|
try
|
|
|
|
|
{
|
2015-09-18 18:22:23 -07:00
|
|
|
|
var result = await organizer.OrganizeEpisodeFile(file.FullName, options, options.OverwriteExistingEpisodes, cancellationToken).ConfigureAwait(false);
|
2015-09-28 20:35:50 -07:00
|
|
|
|
if (result.Status == FileSortingStatus.Success && !processedFolders.Contains(file.DirectoryName, StringComparer.OrdinalIgnoreCase))
|
2015-09-18 18:22:23 -07:00
|
|
|
|
{
|
|
|
|
|
processedFolders.Add(file.DirectoryName);
|
|
|
|
|
}
|
2015-06-08 14:32:20 -07:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
2014-01-22 10:05:06 -07:00
|
|
|
|
{
|
2015-06-08 14:32:20 -07:00
|
|
|
|
_logger.ErrorException("Error organizing episode {0}", ex, file);
|
2014-01-22 10:05:06 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
numComplete++;
|
|
|
|
|
double percent = numComplete;
|
|
|
|
|
percent /= eligibleFiles.Count;
|
|
|
|
|
|
|
|
|
|
progress.Report(10 + (89 * percent));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
progress.Report(99);
|
|
|
|
|
|
2015-09-18 18:22:23 -07:00
|
|
|
|
foreach (var path in processedFolders)
|
2014-01-22 10:05:06 -07:00
|
|
|
|
{
|
2014-01-29 13:30:26 -07:00
|
|
|
|
var deleteExtensions = options.LeftOverFileExtensionsToDelete
|
|
|
|
|
.Select(i => i.Trim().TrimStart('.'))
|
|
|
|
|
.Where(i => !string.IsNullOrEmpty(i))
|
|
|
|
|
.Select(i => "." + i)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
if (deleteExtensions.Count > 0)
|
2014-01-22 10:05:06 -07:00
|
|
|
|
{
|
2014-01-29 13:30:26 -07:00
|
|
|
|
DeleteLeftOverFiles(path, deleteExtensions);
|
2014-01-22 10:05:06 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (options.DeleteEmptyFolders)
|
|
|
|
|
{
|
2015-09-18 18:22:23 -07:00
|
|
|
|
if (!IsWatchFolder(path, watchLocations))
|
2014-01-22 10:22:04 -07:00
|
|
|
|
{
|
2015-09-18 18:22:23 -07:00
|
|
|
|
DeleteEmptyFolders(path);
|
2014-01-22 10:22:04 -07:00
|
|
|
|
}
|
2014-01-22 10:05:06 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
progress.Report(100);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the files to organize.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
|
/// <returns>IEnumerable{FileInfo}.</returns>
|
2015-10-19 03:51:20 -07:00
|
|
|
|
private List<FileSystemMetadata> GetFilesToOrganize(string path)
|
2014-01-22 10:05:06 -07:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2015-09-13 14:32:02 -07:00
|
|
|
|
return _fileSystem.GetFiles(path, true)
|
2014-01-22 10:05:06 -07:00
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
catch (IOException ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error getting files from {0}", ex, path);
|
|
|
|
|
|
2015-10-03 20:38:46 -07:00
|
|
|
|
return new List<FileSystemMetadata>();
|
2014-01-22 10:05:06 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Deletes the left over files.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
|
/// <param name="extensions">The extensions.</param>
|
|
|
|
|
private void DeleteLeftOverFiles(string path, IEnumerable<string> extensions)
|
|
|
|
|
{
|
2015-09-13 14:32:02 -07:00
|
|
|
|
var eligibleFiles = _fileSystem.GetFiles(path, true)
|
2014-01-22 10:05:06 -07:00
|
|
|
|
.Where(i => extensions.Contains(i.Extension, StringComparer.OrdinalIgnoreCase))
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
foreach (var file in eligibleFiles)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2015-01-12 20:46:44 -07:00
|
|
|
|
_fileSystem.DeleteFile(file.FullName);
|
2014-01-22 10:05:06 -07:00
|
|
|
|
}
|
2014-02-25 21:38:21 -07:00
|
|
|
|
catch (Exception ex)
|
2014-01-22 10:05:06 -07:00
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error deleting file {0}", ex, file.FullName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Deletes the empty folders.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
|
private void DeleteEmptyFolders(string path)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2015-09-24 10:50:49 -07:00
|
|
|
|
foreach (var d in _fileSystem.GetDirectoryPaths(path))
|
2014-01-22 10:05:06 -07:00
|
|
|
|
{
|
|
|
|
|
DeleteEmptyFolders(d);
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-24 10:50:49 -07:00
|
|
|
|
var entries = _fileSystem.GetFileSystemEntryPaths(path);
|
2014-01-22 10:05:06 -07:00
|
|
|
|
|
|
|
|
|
if (!entries.Any())
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2014-01-22 10:22:04 -07:00
|
|
|
|
_logger.Debug("Deleting empty directory {0}", path);
|
2015-09-13 16:07:54 -07:00
|
|
|
|
_fileSystem.DeleteDirectory(path, false);
|
2014-01-22 10:05:06 -07:00
|
|
|
|
}
|
|
|
|
|
catch (UnauthorizedAccessException) { }
|
|
|
|
|
catch (DirectoryNotFoundException) { }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (UnauthorizedAccessException) { }
|
|
|
|
|
}
|
2015-09-18 18:22:23 -07:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines if a given folder path is contained in a folder list
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">The folder path to check.</param>
|
|
|
|
|
/// <param name="watchLocations">A list of folders.</param>
|
|
|
|
|
private bool IsWatchFolder(string path, IEnumerable<string> watchLocations)
|
|
|
|
|
{
|
2015-09-28 20:35:50 -07:00
|
|
|
|
return watchLocations.Contains(path, StringComparer.OrdinalIgnoreCase);
|
2015-09-18 18:22:23 -07:00
|
|
|
|
}
|
2014-01-22 10:05:06 -07:00
|
|
|
|
}
|
|
|
|
|
}
|