[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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 104 additions and 73 deletions

View File

@ -159,12 +159,10 @@ namespace Microsoft.PowerToys.ThumbnailHandler.Gcode
using (var reader = new StreamReader(this.Stream))
{
using (Bitmap thumbnail = GetThumbnail(reader, cx))
Bitmap thumbnail = GetThumbnail(reader, cx);
if (thumbnail != null && thumbnail.Size.Width > 0 && thumbnail.Size.Height > 0)
{
if (thumbnail != null && thumbnail.Size.Width > 0 && thumbnail.Size.Height > 0)
{
return (Bitmap)thumbnail.Clone();
}
return thumbnail;
}
}

View File

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

View File

@ -159,11 +159,19 @@ IFACEMETHODIMP GcodeThumbnailProvider::GetThumbnail(UINT cx, HBITMAP* phbmp, WTS
WaitForSingleObject(m_process, INFINITE);
std::filesystem::remove(fileName);
std::wstring fileNameBmp = filePath + guid + L".bmp";
*phbmp = (HBITMAP)LoadImage(NULL, fileNameBmp.c_str(), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
*pdwAlpha = WTS_ALPHATYPE::WTSAT_ARGB;
std::filesystem::remove(fileNameBmp);
if (std::filesystem::exists(fileNameBmp))
{
*phbmp = (HBITMAP)LoadImage(NULL, fileNameBmp.c_str(), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
*pdwAlpha = WTS_ALPHATYPE::WTSAT_ARGB;
std::filesystem::remove(fileNameBmp);
}
else
{
Logger::info(L"Bmp file not generated.");
return E_FAIL;
}
}
catch (std::exception& e)
{

View File

@ -19,7 +19,7 @@ public:
// IInitializeWithStream
IFACEMETHODIMP Initialize(IStream* pstream, DWORD grfMode);
// IPreviewHandler
// IThumbnailProvider
IFACEMETHODIMP GetThumbnail(UINT cx, HBITMAP* phbmp, WTS_ALPHATYPE* pdwAlpha);
GcodeThumbnailProvider();

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
{

View File

@ -158,10 +158,18 @@ IFACEMETHODIMP PdfThumbnailProvider::GetThumbnail(UINT cx, HBITMAP* phbmp, WTS_A
std::filesystem::remove(fileName);
std::wstring fileNameBmp = filePath + guid + L".bmp";
*phbmp = (HBITMAP)LoadImage(NULL, fileNameBmp.c_str(), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
*pdwAlpha = WTS_ALPHATYPE::WTSAT_ARGB;
if (std::filesystem::exists(fileNameBmp))
{
*phbmp = (HBITMAP)LoadImage(NULL, fileNameBmp.c_str(), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
*pdwAlpha = WTS_ALPHATYPE::WTSAT_ARGB;
std::filesystem::remove(fileNameBmp);
}
else
{
Logger::info(L"Bmp file not generated.");
return E_FAIL;
}
std::filesystem::remove(fileNameBmp);
}
catch (std::exception& e)
{

View File

@ -19,7 +19,7 @@ public:
// IInitializeWithStream
IFACEMETHODIMP Initialize(IStream* pstream, DWORD grfMode);
// IPreviewHandler
// IThumbnailProvider
IFACEMETHODIMP GetThumbnail(UINT cx, HBITMAP* phbmp, WTS_ALPHATYPE* pdwAlpha);
PdfThumbnailProvider();

View File

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

View File

@ -130,19 +130,10 @@ namespace Microsoft.PowerToys.ThumbnailHandler.Stl
return null;
}
using (var memStream = new MemoryStream())
Bitmap thumbnail = GetThumbnail(this.Stream, cx);
if (thumbnail != null && thumbnail.Size.Width > 0 && thumbnail.Size.Height > 0)
{
this.Stream.CopyTo(memStream);
memStream.Position = 0;
using (Bitmap thumbnail = GetThumbnail(memStream, cx))
{
if (thumbnail != null && thumbnail.Size.Width > 0 && thumbnail.Size.Height > 0)
{
return (Bitmap)thumbnail.Clone();
}
}
return thumbnail;
}
return null;

View File

@ -158,10 +158,17 @@ IFACEMETHODIMP StlThumbnailProvider::GetThumbnail(UINT cx, HBITMAP* phbmp, WTS_A
std::filesystem::remove(fileName);
std::wstring fileNameBmp = filePath + guid + L".bmp";
*phbmp = (HBITMAP)LoadImage(NULL, fileNameBmp.c_str(), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
*pdwAlpha = WTS_ALPHATYPE::WTSAT_ARGB;
std::filesystem::remove(fileNameBmp);
if (std::filesystem::exists(fileNameBmp))
{
*phbmp = (HBITMAP)LoadImage(NULL, fileNameBmp.c_str(), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
*pdwAlpha = WTS_ALPHATYPE::WTSAT_ARGB;
std::filesystem::remove(fileNameBmp);
}
else
{
Logger::info(L"Bmp file not generated.");
return E_FAIL;
}
}
catch (std::exception& e)
{

View File

@ -19,7 +19,7 @@ public:
// IInitializeWithStream
IFACEMETHODIMP Initialize(IStream* pstream, DWORD grfMode);
// IPreviewHandler
// IThumbnailProvider
IFACEMETHODIMP GetThumbnail(UINT cx, HBITMAP* phbmp, WTS_ALPHATYPE* pdwAlpha);
StlThumbnailProvider();

View File

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

View File

@ -158,10 +158,18 @@ IFACEMETHODIMP SvgThumbnailProvider::GetThumbnail(UINT cx, HBITMAP* phbmp, WTS_A
std::filesystem::remove(fileName);
std::wstring fileNameBmp = filePath + guid + L".bmp";
*phbmp = (HBITMAP)LoadImage(NULL, fileNameBmp.c_str(), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
*pdwAlpha = WTS_ALPHATYPE::WTSAT_ARGB;
std::filesystem::remove(fileNameBmp);
if (std::filesystem::exists(fileNameBmp))
{
*phbmp = (HBITMAP)LoadImage(NULL, fileNameBmp.c_str(), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
*pdwAlpha = WTS_ALPHATYPE::WTSAT_ARGB;
std::filesystem::remove(fileNameBmp);
}
else
{
Logger::info(L"Bmp file not generated.");
return E_FAIL;
}
}
catch (std::exception& e)
{

View File

@ -19,7 +19,7 @@ public:
// IInitializeWithStream
IFACEMETHODIMP Initialize(IStream* pstream, DWORD grfMode);
// IPreviewHandler
// IThumbnailProvider
IFACEMETHODIMP GetThumbnail(UINT cx, HBITMAP* phbmp, WTS_ALPHATYPE* pdwAlpha);
SvgThumbnailProvider();

View File

@ -22,7 +22,7 @@ namespace PdfThumbnailProviderUnitTests
public void GetThumbnailValidStreamPDF()
{
// Act
var filePath = "HelperFiles/sample.pdf";
var filePath = System.IO.Path.GetFullPath("HelperFiles/sample.pdf");
PdfThumbnailProvider provider = new PdfThumbnailProvider(filePath);
@ -35,7 +35,7 @@ namespace PdfThumbnailProviderUnitTests
public void GetThumbnailInValidSizePDF()
{
// Act
var filePath = "HelperFiles/sample.pdf";
var filePath = System.IO.Path.GetFullPath("HelperFiles/sample.pdf");
PdfThumbnailProvider provider = new PdfThumbnailProvider(filePath);
@ -48,7 +48,7 @@ namespace PdfThumbnailProviderUnitTests
public void GetThumbnailToBigPDF()
{
// Act
var filePath = "HelperFiles/sample.pdf";
var filePath = System.IO.Path.GetFullPath("HelperFiles/sample.pdf");
PdfThumbnailProvider provider = new PdfThumbnailProvider(filePath);