From e095cca750bb09395ee3c7206045d97505bcf920 Mon Sep 17 00:00:00 2001 From: Henry Feahn Date: Wed, 13 Sep 2023 20:51:59 -0500 Subject: [PATCH] Added string pdf to constant.Ensured split ignore empty entries. --- Benny-Scraper.BusinessLogic/EpubGenerator.cs | 7 +------ Benny-Scraper.BusinessLogic/NovelProcessor.cs | 11 ++++++----- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/Benny-Scraper.BusinessLogic/EpubGenerator.cs b/Benny-Scraper.BusinessLogic/EpubGenerator.cs index 385acd0..3ad0823 100644 --- a/Benny-Scraper.BusinessLogic/EpubGenerator.cs +++ b/Benny-Scraper.BusinessLogic/EpubGenerator.cs @@ -71,7 +71,7 @@ public void CreateEpub(Novel novel, IEnumerable chapters, string output string spineItems = string.Empty; string subjectItems = string.Empty; - foreach (var tag in novel.Genre.Split(",")) + foreach (var tag in novel.Genre.Split(",", StringSplitOptions.RemoveEmptyEntries)) { subjectItems += $"{tag}"; } @@ -249,11 +249,6 @@ static void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine) Console.WriteLine(outLine.Data); } - private void AddDirectoryToZip(ZipOutputStream zip, string directoryPath, string entryPath) - { - AddDirectoryToZip(zip, directoryPath, entryPath); - } - private void AddDirectoryToZip(ZipOutputStream zipStream, string sourceDirectory, string targetDirectory, string baseDirectory) { DirectoryInfo diSource = new DirectoryInfo(sourceDirectory); diff --git a/Benny-Scraper.BusinessLogic/NovelProcessor.cs b/Benny-Scraper.BusinessLogic/NovelProcessor.cs index 3fc2639..086eac6 100644 --- a/Benny-Scraper.BusinessLogic/NovelProcessor.cs +++ b/Benny-Scraper.BusinessLogic/NovelProcessor.cs @@ -17,6 +17,7 @@ namespace Benny_Scraper.BusinessLogic { public class NovelProcessor : INovelProcessor { + private const string PdfFileExtension = ".pdf"; private static readonly ILogger Logger = LogManager.GetCurrentClassLogger(); private readonly INovelService _novelService; private readonly IChapterService _chapterService; @@ -160,7 +161,7 @@ private async Task UpdateExistingNovelAsync(Novel novel, Uri novelTableOfContent if (newChapters.Any(chapter => chapter?.Pages != null)) { if (string.IsNullOrEmpty(novel.SaveLocation)) - novel.SaveLocation = Path.Combine(documentsFolder, SanitizeFileName(novel.Title) + ".pdf"); + novel.SaveLocation = Path.Combine(documentsFolder, SanitizeFileName(novel.Title) + PdfFileExtension); UpdatePdf(novel, chapterDataBuffers); foreach (var chapterDataBuffer in chapterDataBuffers) { @@ -278,7 +279,7 @@ private void CreatePdfByChapter(Novel novel, IEnumerable chap //https://stackoverflow.com/questions/50858209/system-notsupportedexception-no-data-is-available-for-encoding-1252 var sanitizedTitle = SanitizeFileName($"{novel.Title} - {chapter.Title}"); - var pdfFilePath = Path.Combine(pdfDirectoryPath, $"{sanitizedTitle}.pdf"); + var pdfFilePath = Path.Combine(pdfDirectoryPath, sanitizedTitle + PdfFileExtension); document.Save(pdfFilePath); } } @@ -324,7 +325,7 @@ private void CreateSinglePdf(Novel novel, IEnumerable chapter var sanitizedTitle = SanitizeFileName($"{novel.Title}"); Logger.Info($"Saving PDF to {pdfDirectoryPath}"); - var pdfFilePath = Path.Combine(pdfDirectoryPath, $"{sanitizedTitle}.pdf"); + var pdfFilePath = Path.Combine(pdfDirectoryPath, sanitizedTitle + PdfFileExtension); document.Save(pdfFilePath); Logger.Info($"PDF saved to {pdfFilePath}"); Console.WriteLine($"PDF saved to {pdfFilePath}"); @@ -333,12 +334,12 @@ private void CreateSinglePdf(Novel novel, IEnumerable chapter private void UpdatePdf(Novel novel, IEnumerable chapterDataBuffer) { var pdfFilePath = novel.SaveLocation; - if (Path.GetExtension(pdfFilePath) != ".pdf") + if (Path.GetExtension(pdfFilePath) != PdfFileExtension) throw new ArgumentException("The path to the pdf file is not a pdf file. " + pdfFilePath); if (!File.Exists(pdfFilePath)) throw new ArgumentException("The path to the pdf file does not exist. " + pdfFilePath); - var tempPdfFilePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + ".pdf"); + var tempPdfFilePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + PdfFileExtension); Logger.Info("Updating PDF file: " + pdfFilePath); using (FileStream pdfFile = File.OpenRead(pdfFilePath)) // dispose the filestream after use to avoid the error "The process cannot access the file because it is being used by another process"