Beispiel #1
0
        private void Worker_DoWork(object sender, DoWorkEventArgs e)
        {
            for (var i = 0; i < _allFiles.Length; i++)
            {
                if (Worker.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }
                var bookfile = new BookFile {
                    FullPathAndFileName = _allFiles[i]
                };
                var book = new Book {
                    Title = Path.GetFileNameWithoutExtension(_allFiles[i])
                };

                var pluginForBook =
                    _supportedFormats.LoadedPlugins.FirstOrDefault(x => x.Plugin.FileExtension == bookfile.FileExtension);
                if (pluginForBook != null)
                {
                    try
                    {
                        var coverimage = new CoverImage();

                        var cover = pluginForBook.Plugin.ExtractCover(bookfile.FullPathAndFileName);
                        if (cover != null)
                        {
                            var coverFileName = Path.Combine(_settings.ImageCoversPath,
                                                             Path.GetFileNameWithoutExtension(bookfile.FileName) + ".png");
                            cover.Save(coverFileName);
                            coverimage.FullPathAndFileName = coverFileName;
                        }
                        coverimage.AddBook(book);
                    }
                    catch (BookieException ex)
                    {
                        _log.Error("Error extracting cover image from file " + bookfile.FullPathAndFileName, ex);
                    }

                    try
                    {
                        //Supported format so process
                        var metadata = pluginForBook.Plugin.ExtractMetadata(bookfile.FullPathAndFileName);
                        book.Title         = metadata.Title;
                        book.Isbn10        = metadata.Isbn;
                        book.Abstract      = metadata.Abstract.Truncate(3500);
                        book.DatePublished = metadata.PublishedDate;
                        book.Pages         = metadata.PageCount;
                        if (metadata.Authors != null && metadata.Authors.Count > 0)
                        {
                            foreach (var author in metadata.Authors)
                            {
                                var existing =
                                    _ctx.Authors.FirstOrDefault(
                                        x => x.FirstName == author.FirstName && x.LastName == author.LastName);
                                if (existing == null)
                                {
                                    book.Authors.Add(author);
                                    _ctx.Authors.Add(author);
                                }
                                else
                                {
                                    existing.Books.Add(book);
                                    _ctx.Entry(existing).State = EntityState.Modified;
                                }
                            }
                        }
                    }
                    catch (BookieException ex)
                    {
                        _log.Error("Error extracting metadata from file " + bookfile.FullPathAndFileName, ex);
                    }
                }
                else
                {
                    _log.Info($"{bookfile.FullPathAndFileName} : ({bookfile.FileExtension}) is not a supported format");
                    continue;
                }

                book.Rating = 5;
                book.BookFiles.Add(bookfile);
                _bookCore.Persist(book);

                var percentage = Utils.CalculatePercentage(i, 1, _allFiles.Length);
                Worker.ReportProgress(percentage, book);
            }
        }
 private void Save(object obj)
 {
     _bookCore.Persist(Book);
 }
Beispiel #3
0
 private void ChooseAuthor(object obj)
 {
     Book.Authors.Add(SelectedAuthor);
     _bookCore.Persist(Book);
 }