@ -30,11 +30,14 @@ namespace NzbDrone.Core.MediaFiles.EpisodeImport.Manual
private readonly IDiskScanService _diskScanService ;
private readonly IMakeImportDecision _importDecisionMaker ;
private readonly ISeriesService _seriesService ;
private readonly IMovieService _movieService ;
private readonly IEpisodeService _episodeService ;
private readonly IVideoFileInfoReader _videoFileInfoReader ;
private readonly IImportApprovedEpisodes _importApprovedEpisodes ;
private readonly IImportApprovedMovie _importApprovedMovie ;
private readonly ITrackedDownloadService _trackedDownloadService ;
private readonly IDownloadedEpisodesImportService _downloadedEpisodesImportService ;
private readonly IDownloadedMovieImportService _downloadedMovieImportService ;
private readonly IEventAggregator _eventAggregator ;
private readonly Logger _logger ;
@ -43,11 +46,14 @@ namespace NzbDrone.Core.MediaFiles.EpisodeImport.Manual
IDiskScanService diskScanService ,
IMakeImportDecision importDecisionMaker ,
ISeriesService seriesService ,
IMovieService movieService ,
IEpisodeService episodeService ,
IVideoFileInfoReader videoFileInfoReader ,
IImportApprovedEpisodes importApprovedEpisodes ,
IImportApprovedMovie importApprovedMovie ,
ITrackedDownloadService trackedDownloadService ,
IDownloadedEpisodesImportService downloadedEpisodesImportService ,
IDownloadedMovieImportService downloadedMovieImportService ,
IEventAggregator eventAggregator ,
Logger logger )
{
@ -56,11 +62,14 @@ namespace NzbDrone.Core.MediaFiles.EpisodeImport.Manual
_diskScanService = diskScanService ;
_importDecisionMaker = importDecisionMaker ;
_seriesService = seriesService ;
_movieService = movieService ;
_episodeService = episodeService ;
_videoFileInfoReader = videoFileInfoReader ;
_importApprovedEpisodes = importApprovedEpisodes ;
_importApprovedMovie = importApprovedMovie ;
_trackedDownloadService = trackedDownloadService ;
_downloadedEpisodesImportService = downloadedEpisodesImportService ;
_downloadedMovieImportService = downloadedMovieImportService ;
_eventAggregator = eventAggregator ;
_logger = logger ;
}
@ -126,62 +135,128 @@ namespace NzbDrone.Core.MediaFiles.EpisodeImport.Manual
var relativeFile = folder . GetRelativePath ( file ) ;
var series = _parsingService . GetSeries ( relativeFile . Split ( '\\' , '/' ) [ 0 ] ) ;
var movie = _parsingService . GetMovie ( relativeFile . Split ( '\\' , '/' ) [ 0 ] ) ;
if ( series = = null )
if ( movie = = null )
{
series = _parsingService . GetSeries ( relativeFile ) ;
movie = _parsingService . GetMovie ( relativeFile ) ;
}
if ( series = = null & & downloadId . IsNotNullOrWhiteSpace ( ) )
if ( movie = = null & & downloadId . IsNotNullOrWhiteSpace ( ) )
{
var trackedDownload = _trackedDownloadService . Find ( downloadId ) ;
series = trackedDownload . RemoteEpisode . Series ;
movie = trackedDownload . RemoteMovie . Movie ;
}
if ( series = = null )
if ( movie = = null )
{
var localEpisode = new LocalEpisode ( ) ;
localEpisode . Path = file ;
localEpisode . Quality = QualityParser . ParseQuality ( file ) ;
localEpisode . Size = _diskProvider . GetFileSize ( file ) ;
var localMovie = new LocalMovie ( )
{
Path = file ,
Quality = QualityParser . ParseQuality ( file ) ,
Size = _diskProvider . GetFileSize ( file )
} ;
return MapItem ( new ImportDecision ( localEpisode , new Rejection ( "Unknown Series" ) ) , folder , downloadId ) ;
return MapItem ( new ImportDecision ( local Movie, new Rejection ( "Unknown Movie ") ) , folder , downloadId ) ;
}
var importDecisions = _importDecisionMaker . GetImportDecisions ( new List < string > { file } ,
series, null , SceneSource ( series , folder ) ) ;
var importDecisions = _importDecisionMaker . GetImportDecisions ( new List < string > { file } ,
movie, null , SceneSource ( movie , folder ) ) ;
return importDecisions . Any ( ) ? MapItem ( importDecisions . First ( ) , folder , downloadId ) : null ;
}
//private ManualImportItem ProcessFile(string file, string downloadId, string folder = null)
//{
// if (folder.IsNullOrWhiteSpace())
// {
// folder = new FileInfo(file).Directory.FullName;
// }
// var relativeFile = folder.GetRelativePath(file);
// var series = _parsingService.GetSeries(relativeFile.Split('\\', '/')[0]);
// if (series == null)
// {
// series = _parsingService.GetSeries(relativeFile);
// }
// if (series == null && downloadId.IsNotNullOrWhiteSpace())
// {
// var trackedDownload = _trackedDownloadService.Find(downloadId);
// series = trackedDownload.RemoteEpisode.Series;
// }
// if (series == null)
// {
// var localEpisode = new LocalEpisode();
// localEpisode.Path = file;
// localEpisode.Quality = QualityParser.ParseQuality(file);
// localEpisode.Size = _diskProvider.GetFileSize(file);
// return MapItem(new ImportDecision(localEpisode, new Rejection("Unknown Series")), folder, downloadId);
// }
// var importDecisions = _importDecisionMaker.GetImportDecisions(new List<string> {file},
// series, null, SceneSource(series, folder));
// return importDecisions.Any() ? MapItem(importDecisions.First(), folder, downloadId) : null;
//}
private bool SceneSource ( Series series , string folder )
{
return ! ( series . Path . PathEquals ( folder ) | | series . Path . IsParentPath ( folder ) ) ;
}
private bool SceneSource ( Movie movie , string folder )
{
return ! ( movie . Path . PathEquals ( folder ) | | movie . Path . IsParentPath ( folder ) ) ;
}
//private ManualImportItem MapItem(ImportDecision decision, string folder, string downloadId)
//{
// var item = new ManualImportItem();
// item.Path = decision.LocalEpisode.Path;
// item.RelativePath = folder.GetRelativePath(decision.LocalEpisode.Path);
// item.Name = Path.GetFileNameWithoutExtension(decision.LocalEpisode.Path);
// item.DownloadId = downloadId;
// if (decision.LocalEpisode.Series != null)
// {
// item.Series = decision.LocalEpisode.Series;
// }
// if (decision.LocalEpisode.Episodes.Any())
// {
// item.SeasonNumber = decision.LocalEpisode.SeasonNumber;
// item.Episodes = decision.LocalEpisode.Episodes;
// }
// item.Quality = decision.LocalEpisode.Quality;
// item.Size = _diskProvider.GetFileSize(decision.LocalEpisode.Path);
// item.Rejections = decision.Rejections;
// return item;
//}
private ManualImportItem MapItem ( ImportDecision decision , string folder , string downloadId )
{
var item = new ManualImportItem ( ) ;
item . Path = decision . LocalEpisode . Path ;
item . RelativePath = folder . GetRelativePath ( decision . LocalEpisode . Path ) ;
item . Name = Path . GetFileNameWithoutExtension ( decision . LocalEpisode . Path ) ;
item . Path = decision . Local Movi e. Path ;
item . RelativePath = folder . GetRelativePath ( decision . Local Movi e. Path ) ;
item . Name = Path . GetFileNameWithoutExtension ( decision . Local Movi e. Path ) ;
item . DownloadId = downloadId ;
if ( decision . LocalEpisode . Series ! = null )
if ( decision . Local Movie. Movie ! = null )
{
item . Series = decision . LocalEpisode . Series ;
item . Movie = decision . LocalMovie . Movie ;
}
if ( decision . LocalEpisode . Episodes . Any ( ) )
{
item . SeasonNumber = decision . LocalEpisode . SeasonNumber ;
item . Episodes = decision . LocalEpisode . Episodes ;
}
item . Quality = decision . LocalEpisode . Quality ;
item . Size = _diskProvider . GetFileSize ( decision . LocalEpisode . Path ) ;
item . Quality = decision . LocalMovie . Quality ;
item . Size = _diskProvider . GetFileSize ( decision . LocalMovie . Path ) ;
item . Rejections = decision . Rejections ;
return item ;
@ -199,45 +274,43 @@ namespace NzbDrone.Core.MediaFiles.EpisodeImport.Manual
_logger . ProgressTrace ( "Processing file {0} of {1}" , i + 1 , message . Files . Count ) ;
var file = message . Files [ i ] ;
var series = _seriesService . GetSeries ( file . SeriesId ) ;
var episodes = _episodeService . GetEpisodes ( file . EpisodeIds ) ;
var parsedEpisodeInfo = Parser . Parser . ParsePath ( file . Path ) ? ? new ParsedEpisodeInfo ( ) ;
var movie = _movieService . GetMovie ( file . MovieId ) ;
var parsedMovieInfo = Parser . Parser . ParseMoviePath ( file . Path ) ? ? new ParsedMovieInfo ( ) ;
var mediaInfo = _videoFileInfoReader . GetMediaInfo ( file . Path ) ;
var existingFile = series . Path . IsParentPath ( file . Path ) ;
var existingFile = movie . Path . IsParentPath ( file . Path ) ;
var local Episode = new LocalEpisod e
var local Movie = new LocalMovi e
{
ExistingFile = false ,
Episodes = episodes ,
MediaInfo = mediaInfo ,
Parsed EpisodeInfo = parsedEpisod eInfo,
Parsed MovieInfo = parsedMovi eInfo,
Path = file . Path ,
Quality = file . Quality ,
Series = series ,
Movie = movie ,
Size = 0
} ;
//TODO: Cleanup non-tracked downloads
var importDecision = new ImportDecision ( local Episod e) ;
var importDecision = new ImportDecision ( local Movi e) ;
if ( file . DownloadId . IsNullOrWhiteSpace ( ) )
{
imported . AddRange ( _importApproved Episodes . Import ( new List < ImportDecision > { importDecision } , ! existingFile , null , message . ImportMode ) ) ;
imported . AddRange ( _importApproved Movie . Import ( new List < ImportDecision > { importDecision } , ! existingFile , null , message . ImportMode ) ) ;
}
else
{
var trackedDownload = _trackedDownloadService . Find ( file . DownloadId ) ;
var importResult = _importApproved Episodes . Import ( new List < ImportDecision > { importDecision } , true , trackedDownload . DownloadItem , message . ImportMode ) . First ( ) ;
var importResult = _importApproved Movie . Import ( new List < ImportDecision > { importDecision } , true , trackedDownload . DownloadItem , message . ImportMode ) . First ( ) ;
imported . Add ( importResult ) ;
importedTrackedDownload . Add ( new ManuallyImportedFile
{
TrackedDownload = trackedDownload ,
ImportResult = importResult
} ) ;
{
TrackedDownload = trackedDownload ,
ImportResult = importResult
} ) ;
}
}
@ -249,20 +322,98 @@ namespace NzbDrone.Core.MediaFiles.EpisodeImport.Manual
if ( _diskProvider . FolderExists ( trackedDownload . DownloadItem . OutputPath . FullPath ) )
{
if ( _downloaded Episodes ImportService. ShouldDeleteFolder (
if ( _downloaded Movie ImportService. ShouldDeleteFolder (
new DirectoryInfo ( trackedDownload . DownloadItem . OutputPath . FullPath ) ,
trackedDownload . Remote Episode. Series ) & & ! trackedDownload . DownloadItem . IsReadOnly )
trackedDownload . Remote Movie. Movie ) & & ! trackedDownload . DownloadItem . IsReadOnly )
{
_diskProvider . DeleteFolder ( trackedDownload . DownloadItem . OutputPath . FullPath , true ) ;
}
}
if ( groupedTrackedDownload . Select ( c = > c . ImportResult ) . Count ( c = > c . Result = = ImportResultType . Imported ) > = Math . Max ( 1 , trackedDownload . RemoteEpisode . Episodes . Count ) )
if ( groupedTrackedDownload . Select ( c = > c . ImportResult ) . Count ( c = > c . Result = = ImportResultType . Imported ) > = Math . Max ( 1 , 1 ) ) //TODO: trackedDownload.RemoteMovie.Movie.Count is always 1?
{
trackedDownload . State = TrackedDownloadStage . Imported ;
_eventAggregator . PublishEvent ( new DownloadCompletedEvent ( trackedDownload ) ) ;
}
}
}
//public void Execute(ManualImportCommand message)
//{
// _logger.ProgressTrace("Manually importing {0} files using mode {1}", message.Files.Count, message.ImportMode);
// var imported = new List<ImportResult>();
// var importedTrackedDownload = new List<ManuallyImportedFile>();
// for (int i = 0; i < message.Files.Count; i++)
// {
// _logger.ProgressTrace("Processing file {0} of {1}", i + 1, message.Files.Count);
// var file = message.Files[i];
// var series = _seriesService.GetSeries(file.SeriesId);
// var episodes = _episodeService.GetEpisodes(file.EpisodeIds);
// var parsedEpisodeInfo = Parser.Parser.ParsePath(file.Path) ?? new ParsedEpisodeInfo();
// var mediaInfo = _videoFileInfoReader.GetMediaInfo(file.Path);
// var existingFile = series.Path.IsParentPath(file.Path);
// var localEpisode = new LocalEpisode
// {
// ExistingFile = false,
// Episodes = episodes,
// MediaInfo = mediaInfo,
// ParsedEpisodeInfo = parsedEpisodeInfo,
// Path = file.Path,
// Quality = file.Quality,
// Series = series,
// Size = 0
// };
// //TODO: Cleanup non-tracked downloads
// var importDecision = new ImportDecision(localEpisode);
// if (file.DownloadId.IsNullOrWhiteSpace())
// {
// imported.AddRange(_importApprovedEpisodes.Import(new List<ImportDecision> { importDecision }, !existingFile, null, message.ImportMode));
// }
// else
// {
// var trackedDownload = _trackedDownloadService.Find(file.DownloadId);
// var importResult = _importApprovedEpisodes.Import(new List<ImportDecision> { importDecision }, true, trackedDownload.DownloadItem, message.ImportMode).First();
// imported.Add(importResult);
// importedTrackedDownload.Add(new ManuallyImportedFile
// {
// TrackedDownload = trackedDownload,
// ImportResult = importResult
// });
// }
// }
// _logger.ProgressTrace("Manually imported {0} files", imported.Count);
// foreach (var groupedTrackedDownload in importedTrackedDownload.GroupBy(i => i.TrackedDownload.DownloadItem.DownloadId).ToList())
// {
// var trackedDownload = groupedTrackedDownload.First().TrackedDownload;
// if (_diskProvider.FolderExists(trackedDownload.DownloadItem.OutputPath.FullPath))
// {
// if (_downloadedEpisodesImportService.ShouldDeleteFolder(
// new DirectoryInfo(trackedDownload.DownloadItem.OutputPath.FullPath),
// trackedDownload.RemoteEpisode.Series) && !trackedDownload.DownloadItem.IsReadOnly)
// {
// _diskProvider.DeleteFolder(trackedDownload.DownloadItem.OutputPath.FullPath, true);
// }
// }
// if (groupedTrackedDownload.Select(c => c.ImportResult).Count(c => c.Result == ImportResultType.Imported) >= Math.Max(1, trackedDownload.RemoteEpisode.Episodes.Count))
// {
// trackedDownload.State = TrackedDownloadStage.Imported;
// _eventAggregator.PublishEvent(new DownloadCompletedEvent(trackedDownload));
// }
// }
//}
}
}