Refactor: Move supported extensions to shared constants file

- Move extension list and validation function to ImageResizerConstants.h
- Eliminate code duplication between MSI and MSIX handlers
- Single source of truth for supported extensions

Co-authored-by: yeelam-gordon <73506701+yeelam-gordon@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-02-05 15:23:04 +00:00
parent 92fe5b531e
commit 8da754c1b6
3 changed files with 29 additions and 53 deletions

View File

@@ -6,8 +6,6 @@
#include <Shlwapi.h>
#include <shobjidl_core.h>
#include <string>
#include <algorithm>
#include <vector>
#include <common/telemetry/EtwTrace/EtwTrace.h>
#include <common/utils/elevation.h>
@@ -15,6 +13,7 @@
#include <common/utils/resources.h>
#include <Settings.h>
#include <trace.h>
#include <ImageResizerConstants.h>
#include <wil/win32_helpers.h>
#include <wrl/module.h>
@@ -27,28 +26,6 @@ Shared::Trace::ETWTrace trace(L"ImageResizerContextMenu");
#define BUFSIZE 4096 * 4
// List of supported image extensions that Image Resizer can process
// This must match the list in RuntimeRegistration.h
static const std::vector<std::wstring> g_supportedExtensions = {
L".bmp", L".dib", L".gif", L".jfif", L".jpe", L".jpeg", L".jpg",
L".jxr", L".png", L".rle", L".tif", L".tiff", L".wdp"
};
// Helper function to check if a file extension is supported by Image Resizer
static bool IsSupportedImageExtension(LPCWSTR extension)
{
if (nullptr == extension || wcslen(extension) == 0)
{
return false;
}
// Convert to lowercase for case-insensitive comparison
std::wstring ext(extension);
std::transform(ext.begin(), ext.end(), ext.begin(), ::towlower);
return std::find(g_supportedExtensions.begin(), g_supportedExtensions.end(), ext) != g_supportedExtensions.end();
}
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
@@ -147,7 +124,7 @@ public:
// Check if the extension is actually supported by Image Resizer
// This prevents showing the menu for file types like .psd that Windows
// perceives as images but Image Resizer cannot process
if (!IsSupportedImageExtension(pszExt))
if (!ImageResizerConstants::IsSupportedImageExtension(pszExt))
{
CoTaskMemFree(pszPath);
*cmdState = ECS_HIDDEN;

View File

@@ -1,5 +1,7 @@
#pragma once
#include <string>
#include <vector>
#include <algorithm>
namespace ImageResizerConstants
{
@@ -10,4 +12,26 @@ namespace ImageResizerConstants
inline const std::wstring ModuleOldSaveFolderKey = L"ImageResizer";
inline const std::wstring ModuleSaveFolderKey = L"Image Resizer";
inline const std::wstring ModulePackageDisplayName = L"ImageResizerContextMenu";
// List of supported image extensions that Image Resizer can process
// This must match the list in RuntimeRegistration.h
inline const std::vector<std::wstring> SupportedImageExtensions = {
L".bmp", L".dib", L".gif", L".jfif", L".jpe", L".jpeg", L".jpg",
L".jxr", L".png", L".rle", L".tif", L".tiff", L".wdp"
};
// Helper function to check if a file extension is supported by Image Resizer
inline bool IsSupportedImageExtension(LPCWSTR extension)
{
if (nullptr == extension || wcslen(extension) == 0)
{
return false;
}
// Convert to lowercase for case-insensitive comparison
std::wstring ext(extension);
std::transform(ext.begin(), ext.end(), ext.begin(), ::towlower);
return std::find(SupportedImageExtensions.begin(), SupportedImageExtensions.end(), ext) != SupportedImageExtensions.end();
}
}

View File

@@ -5,6 +5,7 @@
#include <Settings.h>
#include <trace.h>
#include <ImageResizerConstants.h>
#include <common/themes/icon_helpers.h>
#include <common/utils/process_path.h>
@@ -12,34 +13,8 @@
#include <common/utils/HDropIterator.h>
#include <common/utils/package.h>
#include <algorithm>
#include <vector>
#include <string>
extern HINSTANCE g_hInst_imageResizer;
// List of supported image extensions that Image Resizer can process
// This must match the list in RuntimeRegistration.h
static const std::vector<std::wstring> g_supportedExtensions = {
L".bmp", L".dib", L".gif", L".jfif", L".jpe", L".jpeg", L".jpg",
L".jxr", L".png", L".rle", L".tif", L".tiff", L".wdp"
};
// Helper function to check if a file extension is supported by Image Resizer
static bool IsSupportedImageExtension(LPCWSTR extension)
{
if (nullptr == extension || wcslen(extension) == 0)
{
return false;
}
// Convert to lowercase for case-insensitive comparison
std::wstring ext(extension);
std::transform(ext.begin(), ext.end(), ext.begin(), ::towlower);
return std::find(g_supportedExtensions.begin(), g_supportedExtensions.end(), ext) != g_supportedExtensions.end();
}
CContextMenuHandler::CContextMenuHandler()
{
m_pidlFolder = NULL;
@@ -126,7 +101,7 @@ HRESULT CContextMenuHandler::QueryContextMenu(_In_ HMENU hmenu, UINT indexMenu,
// Check if the extension is actually supported by Image Resizer
// This prevents showing the menu for file types like .psd that Windows
// perceives as images but Image Resizer cannot process
if (!IsSupportedImageExtension(pszExt))
if (!ImageResizerConstants::IsSupportedImageExtension(pszExt))
{
free(pszPath);
return S_OK;
@@ -455,7 +430,7 @@ HRESULT __stdcall CContextMenuHandler::GetState(IShellItemArray* psiItemArray, B
// Check if the extension is actually supported by Image Resizer
// This prevents showing the menu for file types like .psd that Windows
// perceives as images but Image Resizer cannot process
if (!IsSupportedImageExtension(pszExt))
if (!ImageResizerConstants::IsSupportedImageExtension(pszExt))
{
CoTaskMemFree(pszPath);
*pCmdState = ECS_HIDDEN;