Пример #1
0
        private static async Task <BookData> InsertFileIntoDatabase(BookDataContext bookdb, IFolder folder, string filename, bool getFullData = false)
        {
            BookData bookData = null;

            if (bookdb == null)
            {
                bookdb = BookDataContext.Get();
            }
            string fullfname = $"{folder.Path}\\{filename}";
            var    wd        = new WizardData()
            {
                FilePath = fullfname, FileName = filename
            };

            try
            {
                wd = await GutenbergFileWizard.GetDataAsync(wd, getFullData);

                if (!string.IsNullOrEmpty(wd.BookId))
                {
                    bookData = CommonQueries.BookGet(bookdb, wd.BookId);

                    //TODO: when I drop a book that's been added to the database because it was
                    // in a bookmark file (which should happen reasonably often!)
                    // then the bookData here is non-null, but also not really filled in well.
                    if (bookData == null || bookData.BookSource.StartsWith(BookData.BookSourceBookMarkFile))
                    {
                        if (wd.BD != null)
                        {
                            // Gotcha! Add to the main book database!
                            if (!string.IsNullOrEmpty(wd.BD.BookId))
                            {
                                wd.BD.Files.Add(new FilenameAndFormatData()
                                {
                                    FileName = filename,
                                    BookId   = wd.BookId,
                                    MimeType = "application/epub+zip"
                                });
                                wd.BD.BookSource = BookData.BookSourceUser;

                                // Add in possible data from the bookData set by the user
                                if (bookData != null)
                                {
                                    wd.BD.NavigationData = bookData.NavigationData;
                                    wd.BD.Review         = bookData.Review;
                                    wd.BD.Notes          = bookData.Notes;

                                    //TODO: now get this book into the database.
                                    // via some kind of merge??
                                }
                                else
                                {
                                    CommonQueries.BookAdd(bookdb, wd.BD, CommonQueries.ExistHandling.IfNotExists);
                                    CommonQueries.BookSaveChanges(bookdb);
                                    bookData = wd.BD;
                                }
                            }
                            else
                            {
                                App.Error($"ERROR: {filename} ({wd.BookId}) was unable to wizard read and get ID");
                            }
                        }
                        else
                        {
                            App.Error($"ERROR: {filename} ({wd.BookId}) was unable to wizard read");
                        }
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine($"{filename}({wd.BookId}) is {bookData.GetBestTitleForFilename()}");
                    }
                    var fullpath = folder.Path;
                    if (fullpath.Contains(@"AppX\Assets\PreinstalledBooks"))
                    {
                        // Whoops. The initial database might incorrectly have a developer path hard-coded.
                        // Replace with correct location.
                        fullpath = $"PreinstalledBooks:";
                    }
                    CommonQueries.DownloadedBookEnsureFileMarkedAsDownloaded(bookdb, wd.BookId, fullpath, filename);
                }
                else
                {
                    App.Error($"{filename} with id {wd.BookId} is not a known type of e-book");
                }
            }
            catch (Exception)
            {
                App.Error($"{filename} is not a readable e-book");
            }
            return(bookData);
        }