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>(); var albumIds = message.Files.GroupBy(e => e.AlbumId).ToList(); var fileCount = 0; foreach (var importAlbumId in albumIds) { var albumImportDecisions = new List <ImportDecision <LocalTrack> >(); // turn off anyReleaseOk if specified if (importAlbumId.First().DisableReleaseSwitching) { var album = _albumService.GetAlbum(importAlbumId.First().AlbumId); album.AnyReleaseOk = false; _albumService.UpdateAlbum(album); } foreach (var file in importAlbumId) { _logger.ProgressTrace("Processing file {0} of {1}", fileCount + 1, message.Files.Count); var artist = _artistService.GetArtist(file.ArtistId); var album = _albumService.GetAlbum(file.AlbumId); var release = _releaseService.GetRelease(file.AlbumReleaseId); var tracks = _trackService.GetTracks(file.TrackIds); var fileTrackInfo = _audioTagService.ReadTags(file.Path) ?? new ParsedTrackInfo(); var fileInfo = _diskProvider.GetFileInfo(file.Path); var localTrack = new LocalTrack { ExistingFile = artist.Path.IsParentPath(file.Path), Tracks = tracks, FileTrackInfo = fileTrackInfo, Path = file.Path, Size = fileInfo.Length, Modified = fileInfo.LastWriteTimeUtc, Quality = file.Quality, Artist = artist, Album = album, Release = release }; var importDecision = new ImportDecision <LocalTrack>(localTrack); if (_rootFolderService.GetBestRootFolder(artist.Path) == null) { _logger.Warn($"Destination artist folder {artist.Path} not in a Root Folder, skipping import"); importDecision.Reject(new Rejection($"Destination artist folder {artist.Path} is not in a Root Folder")); } albumImportDecisions.Add(importDecision); fileCount += 1; } var downloadId = importAlbumId.Select(x => x.DownloadId).FirstOrDefault(x => x.IsNotNullOrWhiteSpace()); if (downloadId.IsNullOrWhiteSpace()) { imported.AddRange(_importApprovedTracks.Import(albumImportDecisions, message.ReplaceExistingFiles, null, message.ImportMode)); } else { var trackedDownload = _trackedDownloadService.Find(downloadId); var importResults = _importApprovedTracks.Import(albumImportDecisions, message.ReplaceExistingFiles, trackedDownload.DownloadItem, message.ImportMode); imported.AddRange(importResults); foreach (var importResult in importResults) { 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; var importArtist = groupedTrackedDownload.First().ImportResult.ImportDecision.Item.Artist; var outputPath = trackedDownload.ImportItem.OutputPath.FullPath; if (_diskProvider.FolderExists(outputPath)) { if (_downloadedTracksImportService.ShouldDeleteFolder( _diskProvider.GetDirectoryInfo(outputPath), importArtist) && trackedDownload.DownloadItem.CanMoveFiles) { _diskProvider.DeleteFolder(outputPath, true); } } if (groupedTrackedDownload.Select(c => c.ImportResult).Count(c => c.Result == ImportResultType.Imported) >= Math.Max(1, trackedDownload.RemoteAlbum.Albums.Count)) { trackedDownload.State = TrackedDownloadState.Imported; _eventAggregator.PublishEvent(new DownloadCompletedEvent(trackedDownload, importArtist.Id)); } } }
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>(); var bookIds = message.Files.GroupBy(e => e.BookId).ToList(); var fileCount = 0; foreach (var importBookId in bookIds) { var bookImportDecisions = new List <ImportDecision <LocalBook> >(); // turn off anyReleaseOk if specified if (importBookId.First().DisableReleaseSwitching) { var book = _bookService.GetBook(importBookId.First().BookId); book.AnyEditionOk = false; _bookService.UpdateBook(book); } foreach (var file in importBookId) { _logger.ProgressTrace("Processing file {0} of {1}", fileCount + 1, message.Files.Count); var author = _authorService.GetAuthor(file.AuthorId); var book = _bookService.GetBook(file.BookId); var edition = _editionService.GetEditionByForeignEditionId(file.ForeignEditionId); if (edition == null) { var tuple = _bookInfo.GetBookInfo(book.ForeignBookId); edition = tuple.Item2.Editions.Value.SingleOrDefault(x => x.ForeignEditionId == file.ForeignEditionId); } var fileRootFolder = _rootFolderService.GetBestRootFolder(file.Path); var fileInfo = _diskProvider.GetFileInfo(file.Path); var fileTrackInfo = _metadataTagService.ReadTags(fileInfo) ?? new ParsedTrackInfo(); var localTrack = new LocalBook { ExistingFile = fileRootFolder != null, FileTrackInfo = fileTrackInfo, Path = file.Path, Part = fileTrackInfo.TrackNumbers.Any() ? fileTrackInfo.TrackNumbers.First() : 1, PartCount = importBookId.Count(), Size = fileInfo.Length, Modified = fileInfo.LastWriteTimeUtc, Quality = file.Quality, Author = author, Book = book, Edition = edition }; var importDecision = new ImportDecision <LocalBook>(localTrack); if (_rootFolderService.GetBestRootFolder(author.Path) == null) { _logger.Warn($"Destination author folder {author.Path} not in a Root Folder, skipping import"); importDecision.Reject(new Rejection($"Destination author folder {author.Path} is not in a Root Folder")); } bookImportDecisions.Add(importDecision); fileCount += 1; } var downloadId = importBookId.Select(x => x.DownloadId).FirstOrDefault(x => x.IsNotNullOrWhiteSpace()); if (downloadId.IsNullOrWhiteSpace()) { imported.AddRange(_importApprovedBooks.Import(bookImportDecisions, message.ReplaceExistingFiles, null, message.ImportMode)); } else { var trackedDownload = _trackedDownloadService.Find(downloadId); var importResults = _importApprovedBooks.Import(bookImportDecisions, message.ReplaceExistingFiles, trackedDownload.DownloadItem, message.ImportMode); imported.AddRange(importResults); foreach (var importResult in importResults) { 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; var outputPath = trackedDownload.ImportItem.OutputPath.FullPath; if (_diskProvider.FolderExists(outputPath)) { if (_downloadedTracksImportService.ShouldDeleteFolder(_diskProvider.GetDirectoryInfo(outputPath)) && trackedDownload.DownloadItem.CanMoveFiles) { _diskProvider.DeleteFolder(outputPath, true); } } var importedCount = groupedTrackedDownload.Select(c => c.ImportResult) .Count(c => c.Result == ImportResultType.Imported); var downloadItemCount = Math.Max(1, trackedDownload.RemoteBook?.Books.Count ?? 1); var allItemsImported = importedCount >= downloadItemCount; if (allItemsImported) { trackedDownload.State = TrackedDownloadState.Imported; _eventAggregator.PublishEvent(new DownloadCompletedEvent(trackedDownload, imported.First().ImportDecision.Item.Author.Id)); } } }