Skip to content

Commit

Permalink
SettingsHelper -> SharedSettings. Move GetPageString there.
Browse files Browse the repository at this point in the history
  • Loading branch information
ToofDerling committed Dec 31, 2022
1 parent 1feca18 commit d26d601
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 28 deletions.
4 changes: 2 additions & 2 deletions Source/AzwConverter/AzwSettings.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using CbzMage.Shared.Extensions;
using CbzMage.Shared.Helpers;
using CbzMage.Shared;

namespace AzwConverter
{
public class AzwSettings
{
public static Settings Settings => new();

private readonly SettingsHelper _settingsHelper = new();
private readonly SharedSettings _settingsHelper = new();

public void CreateSettings()
{
Expand Down
5 changes: 0 additions & 5 deletions Source/AzwConverter/CbzState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,5 @@ public sealed class CbzState
public DateTime? Checked { get; set; }

public CbzState? Changed { get; set; }

public string PageName()
{
return $"page-{Pages.ToString().PadLeft(4, '0')}.jpg";
}
}
}
5 changes: 3 additions & 2 deletions Source/AzwConverter/Engine/ConvertEngine.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MobiMetadata;
using CbzMage.Shared;
using MobiMetadata;
using System.IO.Compression;
using System.IO.MemoryMappedFiles;

Expand Down Expand Up @@ -98,7 +99,7 @@ private async Task<CbzState> ReadAndCompressPagesAsync(ZipArchive zipArchive, Pa
for (int i = 0, sz = sdImageRecords.ContentRecords.Count; i < sz; i++)
{
state.Pages++;
var pageName = state.PageName();
var pageName = SharedSettings.GetPageString(state.Pages);

if (hdImageRecords != null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;

namespace CbzMage.Shared.Helpers
namespace CbzMage.Shared
{
public class SettingsHelper
public class SharedSettings
{
private const string _mainSettings = "CbzMageSettings";

Expand Down Expand Up @@ -34,7 +34,7 @@ public int GetThreadCount(int settingsThreadCount)
if (settingsThreadCount <= 0)
{
var threadCountFraction = Environment.ProcessorCount * fraction;

var calculatedThreadCount = Convert.ToInt32(threadCountFraction);

calculatedThreadCount = Math.Min(calculatedThreadCount, maxThreads);
Expand All @@ -45,5 +45,11 @@ public int GetThreadCount(int settingsThreadCount)

return settingsThreadCount;
}

public static string GetPageString(int pageNumber)
{
var page = pageNumber.ToString().PadLeft(4, '0');
return $"page-{page}.jpg";
}
}
}
7 changes: 4 additions & 3 deletions Source/PdfConverter/PageCompressor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CbzMage.Shared.Buffers;
using CbzMage.Shared;
using CbzMage.Shared.Buffers;
using CbzMage.Shared.Helpers;
using CbzMage.Shared.Jobs;
using PdfConverter.Jobs;
Expand Down Expand Up @@ -131,7 +132,7 @@ private void OnImagesCompressed(JobEventArgs<IEnumerable<string>> eventArgs)

private bool AddCompressorJob()
{
var key = _pdf.GetPageString(_nextPageNumber);
var key = SharedSettings.GetPageString(_nextPageNumber);

var firstPage = _nextPageNumber == 1;

Expand All @@ -143,7 +144,7 @@ private bool AddCompressorJob()

_pageNumbers.TryDequeue(out _nextPageNumber);

key = _pdf.GetPageString(_nextPageNumber);
key = SharedSettings.GetPageString(_nextPageNumber);
}

if (imageList.Count > 0)
Expand Down
5 changes: 3 additions & 2 deletions Source/PdfConverter/PageConverter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CbzMage.Shared.Buffers;
using CbzMage.Shared;
using CbzMage.Shared.Buffers;
using CbzMage.Shared.Jobs;
using PdfConverter.Ghostscript;
using PdfConverter.Jobs;
Expand Down Expand Up @@ -46,7 +47,7 @@ public void HandleImageData(ArrayPoolBufferWriter<byte> bufferWriter)
return;
}
var pageNumber = _pageQueue.Dequeue();
var page = _pdf.GetPageString(pageNumber);
var page = SharedSettings.GetPageString(pageNumber);

var job = new ImageConverterJob(bufferWriter , _convertedPages, page, _resizeHeight);
_converterExecutor.AddJob(job);
Expand Down
6 changes: 0 additions & 6 deletions Source/PdfConverter/Pdf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ public Pdf(string path)

public int ImageCount { get; set; }

public string GetPageString(int pageNumber)
{
var page = pageNumber.ToString().PadLeft(4, '0');
return $"page-{page}.jpg";
}

public static List<Pdf> List(params string[] paths)
{
return new List<Pdf>(paths.Select(x => new Pdf(x)));
Expand Down
17 changes: 14 additions & 3 deletions Source/PdfConverter/PdfConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void ConvertFileOrDirectory(string path)
{
var config = new PdfSettings();
config.CreateSettings();

if (Settings.GhostscriptVersion != null)
{
Console.WriteLine($"Ghostscript version: {Settings.GhostscriptVersion}");
Expand Down Expand Up @@ -75,11 +75,22 @@ private void ConvertPdf(Pdf pdf, ConverterEngine converter)

private List<Pdf> InitializePdfPath(string path)
{
path ??= Environment.CurrentDirectory;
var scanAllDirectories = false;
if (path.EndsWith(Settings.ScanAllDirectoriesPattern))
{
scanAllDirectories = true;
path = path.Replace(Settings.ScanAllDirectoriesPattern, null);
}

if (string.IsNullOrEmpty(path))
{
path = Environment.CurrentDirectory;
}

if (Directory.Exists(path))
{
var files = Directory.GetFiles(path, "*.pdf");
var files = Directory.GetFiles(path, "*.pdf",
scanAllDirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
if (files.Length > 0)
{
return Pdf.List(files.ToArray());
Expand Down
5 changes: 3 additions & 2 deletions Source/PdfConverter/PdfSettings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CbzMage.Shared.AppVersions;
using CbzMage.Shared;
using CbzMage.Shared.AppVersions;
using CbzMage.Shared.Extensions;
using CbzMage.Shared.Helpers;
using System.Diagnostics;
Expand All @@ -9,7 +10,7 @@ public class PdfSettings
{
public static Settings Settings => new();

private readonly SettingsHelper _settingsHelper = new();
private readonly SharedSettings _settingsHelper = new();

public void CreateSettings()
{
Expand Down
2 changes: 2 additions & 0 deletions Source/PdfConverter/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,7 @@ public static void SetGhostscriptVersion(Version version)
}

public static int WriteBufferSize => 262144;

public static string ScanAllDirectoriesPattern => $"{Path.DirectorySeparatorChar}**";
}
}

0 comments on commit d26d601

Please sign in to comment.