2010-10-17 17:22:48 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using NLog;
|
2011-03-10 07:49:59 +00:00
|
|
|
|
using NzbDrone.Core.Model;
|
2010-10-21 01:49:23 +00:00
|
|
|
|
using NzbDrone.Core.Model.Notification;
|
2010-10-17 17:22:48 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
|
{
|
|
|
|
|
public class SyncProvider : ISyncProvider
|
|
|
|
|
{
|
|
|
|
|
private readonly ISeriesProvider _seriesProvider;
|
|
|
|
|
private readonly IEpisodeProvider _episodeProvider;
|
2010-10-30 02:46:32 +00:00
|
|
|
|
private readonly IMediaFileProvider _mediaFileProvider;
|
2010-10-17 17:22:48 +00:00
|
|
|
|
private readonly INotificationProvider _notificationProvider;
|
2011-03-09 07:40:48 +00:00
|
|
|
|
private readonly IDiskProvider _diskProvider;
|
2010-10-17 17:22:48 +00:00
|
|
|
|
|
|
|
|
|
private ProgressNotification _seriesSyncNotification;
|
|
|
|
|
private Thread _seriesSyncThread;
|
2011-03-10 07:49:59 +00:00
|
|
|
|
private List<SeriesMappingModel> _syncList;
|
2010-10-17 17:22:48 +00:00
|
|
|
|
|
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
2011-03-09 07:40:48 +00:00
|
|
|
|
public SyncProvider(ISeriesProvider seriesProvider, IEpisodeProvider episodeProvider,
|
|
|
|
|
IMediaFileProvider mediaFileProvider, INotificationProvider notificationProvider,
|
|
|
|
|
IDiskProvider diskProvider)
|
2010-10-17 17:22:48 +00:00
|
|
|
|
{
|
|
|
|
|
_seriesProvider = seriesProvider;
|
|
|
|
|
_episodeProvider = episodeProvider;
|
2010-10-30 02:46:32 +00:00
|
|
|
|
_mediaFileProvider = mediaFileProvider;
|
2010-10-17 17:22:48 +00:00
|
|
|
|
_notificationProvider = notificationProvider;
|
2011-03-09 07:40:48 +00:00
|
|
|
|
_diskProvider = diskProvider;
|
2010-10-17 17:22:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-09 07:40:48 +00:00
|
|
|
|
#region ISyncProvider Members
|
|
|
|
|
|
|
|
|
|
public List<String> GetUnmappedFolders(string path)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Generating list of unmapped folders");
|
|
|
|
|
if (String.IsNullOrEmpty(path))
|
|
|
|
|
throw new InvalidOperationException("Invalid path provided");
|
|
|
|
|
|
|
|
|
|
if (!_diskProvider.FolderExists(path))
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Path supplied does not exist: {0}", path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var results = new List<String>();
|
|
|
|
|
foreach (string seriesFolder in _diskProvider.GetDirectories(path))
|
|
|
|
|
{
|
|
|
|
|
var cleanPath = Parser.NormalizePath(new DirectoryInfo(seriesFolder).FullName);
|
|
|
|
|
|
|
|
|
|
if (!_seriesProvider.SeriesPathExists(cleanPath))
|
|
|
|
|
results.Add(cleanPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Logger.Debug("{0} unmapped folders detected.", results.Count);
|
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2011-03-10 07:49:59 +00:00
|
|
|
|
public bool BeginSyncUnmappedFolders(List<SeriesMappingModel> unmapped)
|
2010-10-17 17:22:48 +00:00
|
|
|
|
{
|
2011-03-15 15:29:14 +00:00
|
|
|
|
Logger.Debug("User has requested series folder scan");
|
2010-10-17 17:22:48 +00:00
|
|
|
|
if (_seriesSyncThread == null || !_seriesSyncThread.IsAlive)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Initializing background scan of series folder.");
|
|
|
|
|
_seriesSyncThread = new Thread(SyncUnmappedFolders)
|
|
|
|
|
{
|
|
|
|
|
Name = "SyncUnmappedFolders",
|
2010-10-18 06:06:16 +00:00
|
|
|
|
Priority = ThreadPriority.Lowest
|
2010-10-17 17:22:48 +00:00
|
|
|
|
};
|
|
|
|
|
|
2011-03-10 07:49:59 +00:00
|
|
|
|
_syncList = unmapped;
|
2010-10-17 17:22:48 +00:00
|
|
|
|
_seriesSyncThread.Start();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Logger.Warn("Series folder scan already in progress. Ignoring request.");
|
2011-03-09 07:40:48 +00:00
|
|
|
|
|
|
|
|
|
//return false if sync was already running, then we can tell the user to try again later
|
|
|
|
|
return false;
|
2010-10-17 17:22:48 +00:00
|
|
|
|
}
|
2011-03-09 07:40:48 +00:00
|
|
|
|
|
|
|
|
|
//return true if sync has started
|
|
|
|
|
return true;
|
2010-10-17 17:22:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-15 15:29:14 +00:00
|
|
|
|
public bool BeginAddNewSeries(string dir, int seriesId, string seriesName)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("User has requested adding of new series");
|
|
|
|
|
if (_seriesSyncThread == null || !_seriesSyncThread.IsAlive)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Initializing background add of of series folder.");
|
|
|
|
|
_seriesSyncThread = new Thread(SyncUnmappedFolders)
|
|
|
|
|
{
|
|
|
|
|
Name = "SyncUnmappedFolders",
|
|
|
|
|
Priority = ThreadPriority.Lowest
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_syncList = new List<SeriesMappingModel>();
|
|
|
|
|
|
|
|
|
|
var path = dir + Path.DirectorySeparatorChar + seriesName;
|
|
|
|
|
|
|
|
|
|
//Create a directory for this new series
|
|
|
|
|
_diskProvider.CreateDirectory(path);
|
|
|
|
|
|
|
|
|
|
//Add it to the list so it will be processed
|
|
|
|
|
_syncList.Add(new SeriesMappingModel { Path = path, TvDbId = seriesId });
|
|
|
|
|
|
|
|
|
|
_seriesSyncThread.Start();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Logger.Warn("Series folder scan already in progress. Ignoring request.");
|
|
|
|
|
|
|
|
|
|
//return false if sync was already running, then we can tell the user to try again later
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//return true if sync has started
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-17 07:40:23 +00:00
|
|
|
|
public bool BeginAddExistingSeries(string path, int seriesId)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("User has requested adding of new series");
|
|
|
|
|
if (_seriesSyncThread == null || !_seriesSyncThread.IsAlive)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Initializing background add of of series folder.");
|
|
|
|
|
_seriesSyncThread = new Thread(SyncUnmappedFolders)
|
|
|
|
|
{
|
|
|
|
|
Name = "SyncUnmappedFolders",
|
|
|
|
|
Priority = ThreadPriority.Lowest
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_syncList = new List<SeriesMappingModel>();
|
|
|
|
|
|
|
|
|
|
//Add it to the list so it will be processed
|
|
|
|
|
_syncList.Add(new SeriesMappingModel { Path = path, TvDbId = seriesId });
|
|
|
|
|
|
|
|
|
|
_seriesSyncThread.Start();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Logger.Warn("Series folder scan already in progress. Ignoring request.");
|
|
|
|
|
|
|
|
|
|
//return false if sync was already running, then we can tell the user to try again later
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//return true if sync has started
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-09 07:40:48 +00:00
|
|
|
|
private void SyncUnmappedFolders()
|
2010-10-17 17:22:48 +00:00
|
|
|
|
{
|
|
|
|
|
Logger.Info("Starting Series folder scan");
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2010-10-18 06:06:16 +00:00
|
|
|
|
using (_seriesSyncNotification = new ProgressNotification("Series Scan"))
|
2010-10-17 17:22:48 +00:00
|
|
|
|
{
|
|
|
|
|
_notificationProvider.Register(_seriesSyncNotification);
|
2010-10-18 06:06:16 +00:00
|
|
|
|
_seriesSyncNotification.CurrentStatus = "Analysing Folder";
|
2011-03-09 07:40:48 +00:00
|
|
|
|
_seriesSyncNotification.ProgressMax = _syncList.Count;
|
2010-10-17 17:22:48 +00:00
|
|
|
|
|
2011-03-09 07:40:48 +00:00
|
|
|
|
foreach (var seriesFolder in _syncList)
|
2010-10-17 17:22:48 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2011-03-10 07:49:59 +00:00
|
|
|
|
_seriesSyncNotification.CurrentStatus = String.Format("Searching For: {0}", new DirectoryInfo(seriesFolder.Path).Name);
|
2010-10-17 17:22:48 +00:00
|
|
|
|
|
2011-03-10 07:49:59 +00:00
|
|
|
|
if (_seriesProvider.SeriesPathExists(Parser.NormalizePath(seriesFolder.Path)))
|
2011-03-09 07:40:48 +00:00
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Folder '{0}' is mapped in the database. Skipping.'", seriesFolder);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-24 17:35:58 +00:00
|
|
|
|
Logger.Debug("Folder '{0}' isn't mapped in the database. Trying to map it.'", seriesFolder);
|
2011-03-10 07:49:59 +00:00
|
|
|
|
var mappedSeries = _seriesProvider.MapPathToSeries(seriesFolder.TvDbId);
|
2010-10-17 17:22:48 +00:00
|
|
|
|
|
|
|
|
|
if (mappedSeries == null)
|
|
|
|
|
{
|
2011-03-10 07:49:59 +00:00
|
|
|
|
Logger.Warn("Invalid TVDB ID '{0}' Unable to map: '{1}'", seriesFolder.TvDbId, seriesFolder.Path);
|
2010-10-17 17:22:48 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//Check if series is mapped to another folder
|
|
|
|
|
if (_seriesProvider.GetSeries(mappedSeries.Id) == null)
|
|
|
|
|
{
|
2010-10-30 02:46:32 +00:00
|
|
|
|
_seriesSyncNotification.CurrentStatus = String.Format("{0}: downloading series info...", mappedSeries.SeriesName);
|
2011-03-10 07:49:59 +00:00
|
|
|
|
_seriesProvider.AddSeries(seriesFolder.Path, mappedSeries);
|
2010-10-17 17:22:48 +00:00
|
|
|
|
_episodeProvider.RefreshEpisodeInfo(mappedSeries.Id);
|
2010-10-30 02:46:32 +00:00
|
|
|
|
_seriesSyncNotification.CurrentStatus = String.Format("{0}: finding episodes on disk...", mappedSeries.SeriesName);
|
|
|
|
|
_mediaFileProvider.Scan(_seriesProvider.GetSeries(mappedSeries.Id));
|
2011-03-17 07:40:23 +00:00
|
|
|
|
//Todo: Launch Backlog search for this series _backlogProvider.StartSearch(mappedSeries.Id);
|
2010-10-17 17:22:48 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Logger.Warn("Folder '{0}' mapped to '{1}' which is already another folder assigned to it.'", seriesFolder, mappedSeries.SeriesName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2010-10-24 17:35:58 +00:00
|
|
|
|
Logger.ErrorException(e.Message, e);
|
2010-10-17 17:22:48 +00:00
|
|
|
|
}
|
|
|
|
|
_seriesSyncNotification.ProgressValue++;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-18 06:06:16 +00:00
|
|
|
|
_seriesSyncNotification.CurrentStatus = "Series Scan Completed";
|
|
|
|
|
Logger.Info("Series folders scan has successfully completed.");
|
|
|
|
|
Thread.Sleep(3000);
|
2010-10-21 01:49:23 +00:00
|
|
|
|
_seriesSyncNotification.Status = ProgressNotificationStatus.Completed;
|
2010-10-17 17:22:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2010-10-24 17:35:58 +00:00
|
|
|
|
Logger.ErrorException(e.Message, e);
|
2010-10-17 17:22:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|