Пример #1
0
        private List <ManualImportResource> UpdateImportItems(List <ManualImportResource> resources)
        {
            var items = new List <ManualImportItem>();

            foreach (var resource in resources)
            {
                items.Add(new ManualImportItem
                {
                    Id                      = resource.Id,
                    Path                    = resource.Path,
                    Name                    = resource.Name,
                    Size                    = resource.Size,
                    Author                  = resource.Author == null ? null : _authorService.GetAuthor(resource.Author.Id),
                    Book                    = resource.Book == null ? null : _bookService.GetBook(resource.Book.Id),
                    Edition                 = resource.EditionId == 0 ? null : _editionService.GetEdition(resource.EditionId),
                    Quality                 = resource.Quality,
                    DownloadId              = resource.DownloadId,
                    AdditionalFile          = resource.AdditionalFile,
                    ReplaceExistingFiles    = resource.ReplaceExistingFiles,
                    DisableReleaseSwitching = resource.DisableReleaseSwitching
                });
            }

            return(_manualImportService.UpdateItems(items).Select(x => x.ToResource()).ToList());
        }
Пример #2
0
        public void Handle(TrackFolderCreatedEvent message)
        {
            var author  = message.Author;
            var edition = _editionService.GetEdition(message.BookFile.EditionId);

            foreach (var extraFileManager in _extraFileManagers)
            {
                extraFileManager.CreateAfterBookImport(author, edition.Book.Value, message.AuthorFolder, message.BookFolder);
            }
        }
Пример #3
0
        public BookFile MoveBookFile(BookFile bookFile, Author author)
        {
            var edition     = _editionService.GetEdition(bookFile.EditionId);
            var newFileName = _buildFileNames.BuildBookFileName(author, edition, bookFile);
            var filePath    = _buildFileNames.BuildBookFilePath(author, edition, newFileName, Path.GetExtension(bookFile.Path));

            EnsureBookFolder(bookFile, author, edition.Book.Value, filePath);

            _logger.Debug("Renaming book file: {0} to {1}", bookFile, filePath);

            return(TransferFile(bookFile, author, bookFile.Edition.Value.Book.Value, filePath, TransferMode.Move));
        }
Пример #4
0
        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.GetEdition(file.EditionId);
                    var fileTrackInfo = _audioTagService.ReadTags(file.Path) ?? new ParsedTrackInfo();
                    var fileInfo      = _diskProvider.GetFileInfo(file.Path);

                    var localTrack = new LocalBook
                    {
                        ExistingFile  = author.Path.IsParentPath(file.Path),
                        FileTrackInfo = fileTrackInfo,
                        Path          = file.Path,
                        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.RemoteBook.Author) && trackedDownload.DownloadItem.CanMoveFiles)
                    {
                        _diskProvider.DeleteFolder(outputPath, true);
                    }
                }

                if (groupedTrackedDownload.Select(c => c.ImportResult).Count(c => c.Result == ImportResultType.Imported) >= Math.Max(1, trackedDownload.RemoteBook.Books.Count))
                {
                    trackedDownload.State = TrackedDownloadState.Imported;
                    _eventAggregator.PublishEvent(new DownloadCompletedEvent(trackedDownload));
                }
            }
        }