[Thumbnail providers] Cover errors and edge cases (#23947)

* [Thumbnail providers] Cover errors and edge cases

* Add TODO comment

* Fix tests
This commit is contained in:
Stefan Markovic
2023-02-07 21:11:31 +01:00
committed by GitHub
parent c180150d54
commit 53f0b00328
16 changed files with 104 additions and 73 deletions

View File

@@ -1,14 +1,8 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using Common.ComInterlop;
using Common.Utilities;
using Windows.Data.Pdf;
using Windows.Storage;
using Windows.Storage.Streams;
namespace Microsoft.PowerToys.ThumbnailHandler.Pdf
@@ -21,7 +15,6 @@ namespace Microsoft.PowerToys.ThumbnailHandler.Pdf
public PdfThumbnailProvider(string filePath)
{
FilePath = filePath;
Stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
}
/// <summary>
@@ -29,11 +22,6 @@ namespace Microsoft.PowerToys.ThumbnailHandler.Pdf
/// </summary>
public string FilePath { get; private set; }
/// <summary>
/// Gets the stream object to access file.
/// </summary>
public Stream Stream { get; private set; }
/// <summary>
/// The maximum dimension (width or height) thumbnail we will generate.
/// </summary>
@@ -45,6 +33,16 @@ namespace Microsoft.PowerToys.ThumbnailHandler.Pdf
/// <param name="cx">Maximum thumbnail size, in pixels.</param>
/// <returns>Generated bitmap</returns>
public Bitmap GetThumbnail(uint cx)
{
return DoGetThumbnail(cx).Result;
}
/// <summary>
/// Generate thumbnail bitmap for provided Pdf file/stream.
/// </summary>
/// <param name="cx">Maximum thumbnail size, in pixels.</param>
/// <returns>Generated bitmap</returns>
private async Task<Bitmap> DoGetThumbnail(uint cx)
{
if (cx == 0 || cx > MaxThumbnailSize)
{
@@ -57,26 +55,27 @@ namespace Microsoft.PowerToys.ThumbnailHandler.Pdf
return null;
}
using var memStream = new MemoryStream();
this.Stream.CopyTo(memStream);
memStream.Position = 0;
// AsRandomAccessStream() extension method from System.Runtime.WindowsRuntime
var pdf = PdfDocument.LoadFromStreamAsync(memStream.AsRandomAccessStream()).GetAwaiter().GetResult();
if (pdf.PageCount > 0)
Bitmap thumbnail = null;
try
{
using var page = pdf.GetPage(0);
var file = await StorageFile.GetFileFromPathAsync(FilePath);
var pdf = await PdfDocument.LoadFromFileAsync(file);
var image = PageToImage(page, cx);
if (pdf.PageCount > 0)
{
using var page = pdf.GetPage(0);
using Bitmap thumbnail = new Bitmap(image);
var image = PageToImage(page, cx);
return (Bitmap)thumbnail.Clone();
thumbnail = new Bitmap(image);
}
}
catch (Exception)
{
// TODO: add logger
}
return null;
return thumbnail;
}
/// <summary>

View File

@@ -26,8 +26,11 @@ namespace Microsoft.PowerToys.ThumbnailHandler.Pdf
_thumbnailProvider = new PdfThumbnailProvider(filePath);
Bitmap thumbnail = _thumbnailProvider.GetThumbnail(cx);
filePath = filePath.Replace(".pdf", ".bmp");
thumbnail.Save(filePath, System.Drawing.Imaging.ImageFormat.Bmp);
if (thumbnail != null)
{
filePath = filePath.Replace(".pdf", ".bmp");
thumbnail.Save(filePath, System.Drawing.Imaging.ImageFormat.Bmp);
}
}
else
{