Skip to content

Commit e095cca

Browse files
author
Henry Feahn
committed
Added string pdf to constant.Ensured split ignore empty entries.
1 parent 1636305 commit e095cca

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
lines changed

Benny-Scraper.BusinessLogic/EpubGenerator.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void CreateEpub(Novel novel, IEnumerable<Chapter> chapters, string output
7171
string spineItems = string.Empty;
7272
string subjectItems = string.Empty;
7373

74-
foreach (var tag in novel.Genre.Split(","))
74+
foreach (var tag in novel.Genre.Split(",", StringSplitOptions.RemoveEmptyEntries))
7575
{
7676
subjectItems += $"<dc:subject>{tag}</dc:subject>";
7777
}
@@ -249,11 +249,6 @@ static void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
249249
Console.WriteLine(outLine.Data);
250250
}
251251

252-
private void AddDirectoryToZip(ZipOutputStream zip, string directoryPath, string entryPath)
253-
{
254-
AddDirectoryToZip(zip, directoryPath, entryPath);
255-
}
256-
257252
private void AddDirectoryToZip(ZipOutputStream zipStream, string sourceDirectory, string targetDirectory, string baseDirectory)
258253
{
259254
DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);

Benny-Scraper.BusinessLogic/NovelProcessor.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace Benny_Scraper.BusinessLogic
1717
{
1818
public class NovelProcessor : INovelProcessor
1919
{
20+
private const string PdfFileExtension = ".pdf";
2021
private static readonly ILogger Logger = LogManager.GetCurrentClassLogger();
2122
private readonly INovelService _novelService;
2223
private readonly IChapterService _chapterService;
@@ -160,7 +161,7 @@ private async Task UpdateExistingNovelAsync(Novel novel, Uri novelTableOfContent
160161
if (newChapters.Any(chapter => chapter?.Pages != null))
161162
{
162163
if (string.IsNullOrEmpty(novel.SaveLocation))
163-
novel.SaveLocation = Path.Combine(documentsFolder, SanitizeFileName(novel.Title) + ".pdf");
164+
novel.SaveLocation = Path.Combine(documentsFolder, SanitizeFileName(novel.Title) + PdfFileExtension);
164165
UpdatePdf(novel, chapterDataBuffers);
165166
foreach (var chapterDataBuffer in chapterDataBuffers)
166167
{
@@ -278,7 +279,7 @@ private void CreatePdfByChapter(Novel novel, IEnumerable<ChapterDataBuffer> chap
278279
//https://stackoverflow.com/questions/50858209/system-notsupportedexception-no-data-is-available-for-encoding-1252
279280

280281
var sanitizedTitle = SanitizeFileName($"{novel.Title} - {chapter.Title}");
281-
var pdfFilePath = Path.Combine(pdfDirectoryPath, $"{sanitizedTitle}.pdf");
282+
var pdfFilePath = Path.Combine(pdfDirectoryPath, sanitizedTitle + PdfFileExtension);
282283
document.Save(pdfFilePath);
283284
}
284285
}
@@ -324,7 +325,7 @@ private void CreateSinglePdf(Novel novel, IEnumerable<ChapterDataBuffer> chapter
324325

325326
var sanitizedTitle = SanitizeFileName($"{novel.Title}");
326327
Logger.Info($"Saving PDF to {pdfDirectoryPath}");
327-
var pdfFilePath = Path.Combine(pdfDirectoryPath, $"{sanitizedTitle}.pdf");
328+
var pdfFilePath = Path.Combine(pdfDirectoryPath, sanitizedTitle + PdfFileExtension);
328329
document.Save(pdfFilePath);
329330
Logger.Info($"PDF saved to {pdfFilePath}");
330331
Console.WriteLine($"PDF saved to {pdfFilePath}");
@@ -333,12 +334,12 @@ private void CreateSinglePdf(Novel novel, IEnumerable<ChapterDataBuffer> chapter
333334
private void UpdatePdf(Novel novel, IEnumerable<ChapterDataBuffer> chapterDataBuffer)
334335
{
335336
var pdfFilePath = novel.SaveLocation;
336-
if (Path.GetExtension(pdfFilePath) != ".pdf")
337+
if (Path.GetExtension(pdfFilePath) != PdfFileExtension)
337338
throw new ArgumentException("The path to the pdf file is not a pdf file. " + pdfFilePath);
338339
if (!File.Exists(pdfFilePath))
339340
throw new ArgumentException("The path to the pdf file does not exist. " + pdfFilePath);
340341

341-
var tempPdfFilePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + ".pdf");
342+
var tempPdfFilePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + PdfFileExtension);
342343

343344
Logger.Info("Updating PDF file: " + pdfFilePath);
344345
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"

0 commit comments

Comments
 (0)