From 8da754c1b6f133255bb3bf8d9b20b78e910d8045 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 5 Feb 2026 15:23:04 +0000 Subject: [PATCH] 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> --- .../ImageResizerContextMenu/dllmain.cpp | 27 ++-------------- .../ImageResizerLib/ImageResizerConstants.h | 24 ++++++++++++++ .../imageresizer/dll/ContextMenuHandler.cpp | 31 ++----------------- 3 files changed, 29 insertions(+), 53 deletions(-) diff --git a/src/modules/imageresizer/ImageResizerContextMenu/dllmain.cpp b/src/modules/imageresizer/ImageResizerContextMenu/dllmain.cpp index b321925b44..b06acb5542 100644 --- a/src/modules/imageresizer/ImageResizerContextMenu/dllmain.cpp +++ b/src/modules/imageresizer/ImageResizerContextMenu/dllmain.cpp @@ -6,8 +6,6 @@ #include #include #include -#include -#include #include #include @@ -15,6 +13,7 @@ #include #include #include +#include #include #include @@ -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 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; diff --git a/src/modules/imageresizer/ImageResizerLib/ImageResizerConstants.h b/src/modules/imageresizer/ImageResizerLib/ImageResizerConstants.h index d880cfdda0..ea3183410f 100644 --- a/src/modules/imageresizer/ImageResizerLib/ImageResizerConstants.h +++ b/src/modules/imageresizer/ImageResizerLib/ImageResizerConstants.h @@ -1,5 +1,7 @@ #pragma once #include +#include +#include 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 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(); + } } diff --git a/src/modules/imageresizer/dll/ContextMenuHandler.cpp b/src/modules/imageresizer/dll/ContextMenuHandler.cpp index 881b89f755..6486867c31 100644 --- a/src/modules/imageresizer/dll/ContextMenuHandler.cpp +++ b/src/modules/imageresizer/dll/ContextMenuHandler.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include @@ -12,34 +13,8 @@ #include #include -#include -#include -#include - 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 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;