Skip to content

Commit

Permalink
Add a ContentTypes class
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanfish committed Dec 12, 2023
1 parent d41317b commit 0994327
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
8 changes: 8 additions & 0 deletions NAPS2.Sdk/Remoting/ContentTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace NAPS2.Remoting;

internal static class ContentTypes
{
public const string PDF = "application/pdf";
public const string PNG = "image/png";
public const string JPEG = "image/jpeg";
}
16 changes: 6 additions & 10 deletions NAPS2.Sdk/Remoting/Server/ScanJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ namespace NAPS2.Remoting.Server;

internal class ScanJob : IEsclScanJob
{
private const string CT_PDF = "application/pdf";
private const string CT_PNG = "image/png";
private const string CT_JPEG = "image/jpeg";

private readonly ScanningContext _scanningContext;
private readonly ScanController _controller;
private readonly CancellationTokenSource _cts = new();
Expand Down Expand Up @@ -62,8 +58,8 @@ public ScanJob(ScanningContext scanningContext, ScanController controller, ScanD
var requestedFormat = settings.DocumentFormat;
ContentType = requestedFormat switch
{
CT_PNG or CT_PDF => requestedFormat,
_ => CT_JPEG
ContentTypes.PNG or ContentTypes.PDF => requestedFormat,
_ => ContentTypes.JPEG
};
try
{
Expand Down Expand Up @@ -104,7 +100,7 @@ public void RegisterStatusTransitionCallback(Action<StatusTransition> callback)

public async Task<bool> WaitForNextDocument()
{
if (ContentType == CT_PDF)
if (ContentType == ContentTypes.PDF)
{
// For PDFs we merge all the pages into a single PDF document, so we need to wait for the full scan here
if (!await _enumerable.MoveNextAsync())
Expand All @@ -123,17 +119,17 @@ public async Task<bool> WaitForNextDocument()

public async Task WriteDocumentTo(Stream stream)
{
if (ContentType == CT_JPEG)
if (ContentType == ContentTypes.JPEG)
{
_enumerable.Current.Save(stream, ImageFileFormat.Jpeg);
_enumerable.Current.Dispose();
}
if (ContentType == CT_PNG)
if (ContentType == ContentTypes.PNG)
{
_enumerable.Current.Save(stream, ImageFileFormat.Png);
_enumerable.Current.Dispose();
}
if (ContentType == CT_PDF)
if (ContentType == ContentTypes.PDF)
{
var pdfExporter = new PdfExporter(_scanningContext);
await pdfExporter.Export(stream, _pdfImages);
Expand Down
2 changes: 1 addition & 1 deletion NAPS2.Sdk/Remoting/Server/ScanServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private EsclDeviceConfig MakeEsclDeviceConfig(SharedDevice device)
{ EsclColorMode.RGB24, EsclColorMode.Grayscale8, EsclColorMode.BlackAndWhite1 },
XResolutionRange = new ResolutionRange(100, 4800, 300),
YResolutionRange = new ResolutionRange(100, 4800, 300),
DocumentFormats = { "application/pdf", "image/jpeg" }
DocumentFormats = { ContentTypes.PDF, ContentTypes.JPEG, ContentTypes.PNG }
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions NAPS2.Sdk/Scan/Internal/Escl/EsclScanDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ private static void VerifyStatus(EsclScannerStatus status, EsclScanSettings scan

private IEnumerable<IMemoryImage> GetImagesFromRawDocument(ScanOptions options, RawDocument doc)
{
if (doc.ContentType == "application/pdf")
if (doc.ContentType == ContentTypes.PDF)
{
// TODO: For SDK some kind an error message if Pdfium isn't present
var renderer = new PdfiumPdfRenderer();
Expand Down Expand Up @@ -322,8 +322,8 @@ private EsclScanSettings GetScanSettings(ScanOptions options, EsclCapabilities c
InputSource = inputSource,
Duplex = duplex,
DocumentFormat = options.BitDepth == BitDepth.BlackAndWhite || options.MaxQuality
? "application/pdf" // TODO: Use PNG if available?
: "image/jpeg",
? ContentTypes.PDF // TODO: Use PNG if available?
: ContentTypes.JPEG,
XOffset = options.PageAlign switch
{
HorizontalAlign.Left => inputCaps.MaxWidth is > 0 ? inputCaps.MaxWidth.Value - width : 0,
Expand Down

0 comments on commit 0994327

Please sign in to comment.