Skip to content

Commit

Permalink
Added string pdf to constant.Ensured split ignore empty entries.
Browse files Browse the repository at this point in the history
  • Loading branch information
Henry Feahn committed Sep 14, 2023
1 parent 1636305 commit e095cca
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
7 changes: 1 addition & 6 deletions Benny-Scraper.BusinessLogic/EpubGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void CreateEpub(Novel novel, IEnumerable<Chapter> 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 += $"<dc:subject>{tag}</dc:subject>";
}
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 6 additions & 5 deletions Benny-Scraper.BusinessLogic/NovelProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -278,7 +279,7 @@ private void CreatePdfByChapter(Novel novel, IEnumerable<ChapterDataBuffer> 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);
}
}
Expand Down Expand Up @@ -324,7 +325,7 @@ private void CreateSinglePdf(Novel novel, IEnumerable<ChapterDataBuffer> 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}");
Expand All @@ -333,12 +334,12 @@ private void CreateSinglePdf(Novel novel, IEnumerable<ChapterDataBuffer> chapter
private void UpdatePdf(Novel novel, IEnumerable<ChapterDataBuffer> 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"
Expand Down

0 comments on commit e095cca

Please sign in to comment.