|
|
|
@ -15,6 +15,7 @@ namespace NzbDrone.Core.Download
|
|
|
|
|
public interface ICompletedDownloadService
|
|
|
|
|
{
|
|
|
|
|
void CheckForCompletedItem(IDownloadClient downloadClient, TrackedDownload trackedDownload, List<History.History> grabbedHistory, List<History.History> importedHistory);
|
|
|
|
|
List<ImportResult> Import(TrackedDownload trackedDownload);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class CompletedDownloadService : ICompletedDownloadService
|
|
|
|
@ -69,11 +70,13 @@ namespace NzbDrone.Core.Download
|
|
|
|
|
|
|
|
|
|
UpdateStatusMessage(trackedDownload, LogLevel.Debug, "Already added to history as imported.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (trackedDownload.Status != TrackedDownloadStatus.Ok)
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Tracked download status is: {0}, skipping import.", trackedDownload.Status);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var downloadedEpisodesFolder = _configService.DownloadedEpisodesFolder;
|
|
|
|
@ -91,26 +94,93 @@ namespace NzbDrone.Core.Download
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var importResults = Import(trackedDownload);
|
|
|
|
|
|
|
|
|
|
//Only attempt to associate it with a previous import if its still in the downloading state
|
|
|
|
|
if (trackedDownload.State == TrackedDownloadState.Downloading && importResults.Empty())
|
|
|
|
|
{
|
|
|
|
|
AssociateWithPreviouslyImported(trackedDownload, grabbedItems, importedHistory);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_configService.RemoveCompletedDownloads)
|
|
|
|
|
{
|
|
|
|
|
RemoveCompleted(trackedDownload, downloadClient);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ImportResult> Import(TrackedDownload trackedDownload)
|
|
|
|
|
{
|
|
|
|
|
var importResults = new List<ImportResult>();
|
|
|
|
|
|
|
|
|
|
if (_diskProvider.FolderExists(trackedDownload.DownloadItem.OutputPath))
|
|
|
|
|
{
|
|
|
|
|
var importResults = _downloadedEpisodesImportService.ProcessFolder(new DirectoryInfo(trackedDownload.DownloadItem.OutputPath), trackedDownload.DownloadItem);
|
|
|
|
|
importResults = _downloadedEpisodesImportService.ProcessFolder(new DirectoryInfo(trackedDownload.DownloadItem.OutputPath), trackedDownload.DownloadItem);
|
|
|
|
|
|
|
|
|
|
ProcessImportResults(trackedDownload, importResults);
|
|
|
|
|
}
|
|
|
|
|
else if (_diskProvider.FileExists(trackedDownload.DownloadItem.OutputPath))
|
|
|
|
|
{
|
|
|
|
|
var importResults = _downloadedEpisodesImportService.ProcessFile(new FileInfo(trackedDownload.DownloadItem.OutputPath), trackedDownload.DownloadItem);
|
|
|
|
|
importResults = _downloadedEpisodesImportService.ProcessFile(new FileInfo(trackedDownload.DownloadItem.OutputPath), trackedDownload.DownloadItem);
|
|
|
|
|
|
|
|
|
|
ProcessImportResults(trackedDownload, importResults);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return importResults;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateStatusMessage(TrackedDownload trackedDownload, LogLevel logLevel, String message, params object[] args)
|
|
|
|
|
{
|
|
|
|
|
var statusMessage = String.Format(message, args);
|
|
|
|
|
var logMessage = String.Format("[{0}] {1}", trackedDownload.DownloadItem.Title, statusMessage);
|
|
|
|
|
|
|
|
|
|
if (trackedDownload.StatusMessage != statusMessage)
|
|
|
|
|
{
|
|
|
|
|
trackedDownload.SetStatusLevel(logLevel);
|
|
|
|
|
trackedDownload.StatusMessage = statusMessage;
|
|
|
|
|
_logger.Log(logLevel, logMessage);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug(logMessage);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ProcessImportResults(TrackedDownload trackedDownload, List<ImportResult> importResults)
|
|
|
|
|
{
|
|
|
|
|
if (importResults.Empty())
|
|
|
|
|
{
|
|
|
|
|
UpdateStatusMessage(trackedDownload, LogLevel.Error, "No files found are eligible for import in {0}", trackedDownload.DownloadItem.OutputPath);
|
|
|
|
|
}
|
|
|
|
|
else if (importResults.Any(v => v.Result == ImportResultType.Imported) && importResults.All(v => v.Result == ImportResultType.Imported || v.Result == ImportResultType.Rejected))
|
|
|
|
|
{
|
|
|
|
|
UpdateStatusMessage(trackedDownload, LogLevel.Info, "Imported {0} files.", importResults.Count(v => v.Result == ImportResultType.Imported));
|
|
|
|
|
|
|
|
|
|
trackedDownload.State = TrackedDownloadState.Imported;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var errors = importResults
|
|
|
|
|
.Where(v => v.Result == ImportResultType.Skipped || v.Result == ImportResultType.Rejected)
|
|
|
|
|
.Select(v => v.Errors.Aggregate(Path.GetFileName(v.ImportDecision.LocalEpisode.Path), (a, r) => a + "\r\n- " + r))
|
|
|
|
|
.Aggregate("Failed to import:", (a, r) => a + "\r\n" + r);
|
|
|
|
|
|
|
|
|
|
trackedDownload.StatusMessages = importResults.Where(v => v.Result == ImportResultType.Skipped || v.Result == ImportResultType.Rejected)
|
|
|
|
|
.Select(v => new TrackedDownloadStatusMessage(Path.GetFileName(v.ImportDecision.LocalEpisode.Path), v.Errors)).ToList();
|
|
|
|
|
|
|
|
|
|
UpdateStatusMessage(trackedDownload, LogLevel.Error, errors);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AssociateWithPreviouslyImported(TrackedDownload trackedDownload, List<History.History> grabbedItems, List<History.History> importedHistory)
|
|
|
|
|
{
|
|
|
|
|
if (grabbedItems.Any())
|
|
|
|
|
{
|
|
|
|
|
var episodeIds = trackedDownload.RemoteEpisode.Episodes.Select(v => v.Id).ToList();
|
|
|
|
|
|
|
|
|
|
// Check if we can associate it with a previous drone factory import.
|
|
|
|
|
importedItems = importedHistory.Where(v => v.Data.GetValueOrDefault(DownloadTrackingService.DOWNLOAD_CLIENT_ID) == null &&
|
|
|
|
|
var importedItems = importedHistory.Where(v => v.Data.GetValueOrDefault(DownloadTrackingService.DOWNLOAD_CLIENT_ID) == null &&
|
|
|
|
|
episodeIds.Contains(v.EpisodeId) &&
|
|
|
|
|
v.Data.GetValueOrDefault("droppedPath") != null &&
|
|
|
|
|
new FileInfo(v.Data["droppedPath"]).Directory.Name == grabbedItems.First().SourceTitle
|
|
|
|
@ -134,12 +204,11 @@ namespace NzbDrone.Core.Download
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UpdateStatusMessage(trackedDownload, LogLevel.Error, "Intermediate Download path does not exist: {0}", trackedDownload.DownloadItem.OutputPath);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_configService.RemoveCompletedDownloads && trackedDownload.State == TrackedDownloadState.Imported && !trackedDownload.DownloadItem.IsReadOnly)
|
|
|
|
|
private void RemoveCompleted(TrackedDownload trackedDownload, IDownloadClient downloadClient)
|
|
|
|
|
{
|
|
|
|
|
if (trackedDownload.State == TrackedDownloadState.Imported && !trackedDownload.DownloadItem.IsReadOnly)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
@ -148,7 +217,8 @@ namespace NzbDrone.Core.Download
|
|
|
|
|
|
|
|
|
|
if (_diskProvider.FolderExists(trackedDownload.DownloadItem.OutputPath))
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Removing completed download directory: {0}", trackedDownload.DownloadItem.OutputPath);
|
|
|
|
|
_logger.Debug("Removing completed download directory: {0}",
|
|
|
|
|
trackedDownload.DownloadItem.OutputPath);
|
|
|
|
|
_diskProvider.DeleteFolder(trackedDownload.DownloadItem.OutputPath, true);
|
|
|
|
|
}
|
|
|
|
|
else if (_diskProvider.FileExists(trackedDownload.DownloadItem.OutputPath))
|
|
|
|
@ -161,52 +231,11 @@ namespace NzbDrone.Core.Download
|
|
|
|
|
}
|
|
|
|
|
catch (NotSupportedException)
|
|
|
|
|
{
|
|
|
|
|
UpdateStatusMessage(trackedDownload, LogLevel.Debug, "Removing item not supported by your download client.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateStatusMessage(TrackedDownload trackedDownload, LogLevel logLevel, String message, params object[] args)
|
|
|
|
|
{
|
|
|
|
|
var statusMessage = String.Format(message, args);
|
|
|
|
|
var logMessage = String.Format("[{0}] {1}", trackedDownload.DownloadItem.Title, statusMessage);
|
|
|
|
|
|
|
|
|
|
if (trackedDownload.StatusMessage != statusMessage)
|
|
|
|
|
{
|
|
|
|
|
trackedDownload.SetStatusLevel(logLevel);
|
|
|
|
|
trackedDownload.StatusMessage = statusMessage;
|
|
|
|
|
_logger.Log(logLevel, logMessage);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug(logMessage);
|
|
|
|
|
UpdateStatusMessage(trackedDownload, LogLevel.Debug,
|
|
|
|
|
"Removing item not supported by your download client.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ProcessImportResults(TrackedDownload trackedDownload, List<ImportResult> importResults)
|
|
|
|
|
{
|
|
|
|
|
if (importResults.Empty())
|
|
|
|
|
{
|
|
|
|
|
UpdateStatusMessage(trackedDownload, LogLevel.Error, "No files found are eligible for import in {0}", trackedDownload.DownloadItem.OutputPath);
|
|
|
|
|
}
|
|
|
|
|
else if (importResults.Any(v => v.Result == ImportResultType.Imported) && importResults.All(v => v.Result == ImportResultType.Imported || v.Result == ImportResultType.Rejected))
|
|
|
|
|
{
|
|
|
|
|
UpdateStatusMessage(trackedDownload, LogLevel.Info, "Imported {0} files.", importResults.Count(v => v.Result == ImportResultType.Imported));
|
|
|
|
|
|
|
|
|
|
trackedDownload.State = TrackedDownloadState.Imported;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var errors = importResults
|
|
|
|
|
.Where(v => v.Result == ImportResultType.Skipped || v.Result == ImportResultType.Rejected)
|
|
|
|
|
.Select(v => v.Errors.Aggregate(Path.GetFileName(v.ImportDecision.LocalEpisode.Path), (a, r) => a + "\r\n- " + r))
|
|
|
|
|
.Aggregate("Failed to import:", (a, r) => a + "\r\n" + r);
|
|
|
|
|
|
|
|
|
|
trackedDownload.StatusMessages = importResults.Where(v => v.Result == ImportResultType.Skipped || v.Result == ImportResultType.Rejected)
|
|
|
|
|
.Select(v => new TrackedDownloadStatusMessage(Path.GetFileName(v.ImportDecision.LocalEpisode.Path), v.Errors)).ToList();
|
|
|
|
|
|
|
|
|
|
UpdateStatusMessage(trackedDownload, LogLevel.Error, errors);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|