mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 02:36:19 +02:00
[PowerRename] Support using photo metadata to replace in the PowerRename (#41728)
<!-- Enter a brief description/summary of your PR here. What does it fix/what does it change/how was it tested (even manually, if necessary)? --> ## Summary of the Pull Request 1. Introduce WIC for power rename and add new class WICMetadataExtractor to use WIC to extract metadata. 2. Add some patterns for metadata extract. 3. Support XMP and EXIF metadata extract. 4. Add test data for xmp and exif extractor 5. Add attribution for the test data uploader. UI: <img width="2052" height="1415" alt="image" src="https://github.com/user-attachments/assets/9051b12e-4e66-4fdc-a4d4-3bada661c235" /> <img width="284" height="170" alt="image" src="https://github.com/user-attachments/assets/2fd67193-77a7-48f0-a5ac-08a69fe64e55" /> <img width="715" height="1160" alt="image" src="https://github.com/user-attachments/assets/5fa68a8c-d129-44dd-b747-099dfbcded12" /> demo: https://github.com/user-attachments/assets/e90bc206-62e5-4101-ada2-3187ee7e2039 <!-- Please review the items on the PR checklist before submitting--> ## PR Checklist - [x] Closes: #5612 - [x] **Communication:** I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected - [x] **Tests:** Added/updated and all pass - [x] **Localization:** All end-user-facing strings can be localized - [ ] **Dev docs:** Added/updated - [ ] **New binaries:** Added on the required places - [ ] [JSON for signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json) for new binaries - [ ] [WXS for installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs) for new binaries and localization folder - [ ] [YML for CI pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml) for new test projects - [ ] [YML for signed pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml) - [ ] **Documentation updated:** If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys) and link it here: #xxx <!-- Provide a more detailed description of the PR, other things fixed, or any additional comments/features here --> ## Detailed Description of the Pull Request / Additional comments <!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well --> ## Validation Steps Performed --------- Co-authored-by: Yu Leng <yuleng@microsoft.com>
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
#include "pch.h"
|
||||
#include <winrt/base.h>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
|
||||
#include "Renaming.h"
|
||||
#include <Helpers.h>
|
||||
|
||||
#include "MetadataPatternExtractor.h"
|
||||
#include "PowerRenameRegEx.h"
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
bool DoRename(CComPtr<IPowerRenameRegEx>& spRenameRegEx, unsigned long& itemEnumIndex, CComPtr<IPowerRenameItem>& spItem)
|
||||
@@ -14,6 +18,7 @@ bool DoRename(CComPtr<IPowerRenameRegEx>& spRenameRegEx, unsigned long& itemEnum
|
||||
|
||||
PWSTR replaceTerm = nullptr;
|
||||
bool useFileTime = false;
|
||||
bool useMetadata = false;
|
||||
|
||||
winrt::check_hresult(spRenameRegEx->GetReplaceTerm(&replaceTerm));
|
||||
|
||||
@@ -21,7 +26,6 @@ bool DoRename(CComPtr<IPowerRenameRegEx>& spRenameRegEx, unsigned long& itemEnum
|
||||
{
|
||||
useFileTime = true;
|
||||
}
|
||||
CoTaskMemFree(replaceTerm);
|
||||
|
||||
int id = -1;
|
||||
winrt::check_hresult(spItem->GetId(&id));
|
||||
@@ -30,6 +34,29 @@ bool DoRename(CComPtr<IPowerRenameRegEx>& spRenameRegEx, unsigned long& itemEnum
|
||||
bool isSubFolderContent = false;
|
||||
winrt::check_hresult(spItem->GetIsFolder(&isFolder));
|
||||
winrt::check_hresult(spItem->GetIsSubFolderContent(&isSubFolderContent));
|
||||
|
||||
// Get metadata type to check if metadata patterns are used
|
||||
PowerRenameLib::MetadataType metadataType;
|
||||
HRESULT hr = spRenameRegEx->GetMetadataType(&metadataType);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
// Fallback to default metadata type if call fails
|
||||
metadataType = PowerRenameLib::MetadataType::EXIF;
|
||||
}
|
||||
|
||||
// Check if metadata is used AND if this file type supports metadata
|
||||
// Get file path early for metadata type checking and reuse later
|
||||
PWSTR filePath = nullptr;
|
||||
winrt::check_hresult(spItem->GetPath(&filePath));
|
||||
std::wstring filePathStr(filePath); // Copy once for reuse
|
||||
CoTaskMemFree(filePath); // Free immediately after copying
|
||||
|
||||
if (isMetadataUsed(replaceTerm, metadataType, filePathStr.c_str(), isFolder))
|
||||
{
|
||||
useMetadata = true;
|
||||
}
|
||||
|
||||
CoTaskMemFree(replaceTerm);
|
||||
if ((isFolder && (flags & PowerRenameFlags::ExcludeFolders)) ||
|
||||
(!isFolder && (flags & PowerRenameFlags::ExcludeFiles)) ||
|
||||
(isSubFolderContent && (flags & PowerRenameFlags::ExcludeSubfolders)) ||
|
||||
@@ -82,6 +109,53 @@ bool DoRename(CComPtr<IPowerRenameRegEx>& spRenameRegEx, unsigned long& itemEnum
|
||||
winrt::check_hresult(spRenameRegEx->PutFileTime(fileTime));
|
||||
}
|
||||
|
||||
if (useMetadata)
|
||||
{
|
||||
// Extract metadata patterns from the file
|
||||
// Note: filePathStr was already obtained and saved earlier for reuse
|
||||
|
||||
// Get metadata type using the interface method
|
||||
PowerRenameLib::MetadataType metadataType;
|
||||
HRESULT hr = spRenameRegEx->GetMetadataType(&metadataType);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
// Fallback to default metadata type if call fails
|
||||
metadataType = PowerRenameLib::MetadataType::EXIF;
|
||||
}
|
||||
// Extract all patterns for the selected metadata type
|
||||
// At this point we know the file is a supported image format (jpg/jpeg/png/tif/tiff)
|
||||
static std::mutex s_metadataMutex; // Mutex to protect static variables
|
||||
static std::once_flag s_metadataExtractorInitFlag;
|
||||
static std::shared_ptr<PowerRenameLib::MetadataPatternExtractor> s_metadataExtractor;
|
||||
static std::optional<PowerRenameLib::MetadataType> s_activeMetadataType;
|
||||
|
||||
// Initialize the extractor only once
|
||||
std::call_once(s_metadataExtractorInitFlag, []() {
|
||||
s_metadataExtractor = std::make_shared<PowerRenameLib::MetadataPatternExtractor>();
|
||||
});
|
||||
|
||||
// Protect access to shared state
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(s_metadataMutex);
|
||||
|
||||
// Clear cache if metadata type has changed
|
||||
if (s_activeMetadataType.has_value() && s_activeMetadataType.value() != metadataType)
|
||||
{
|
||||
s_metadataExtractor->ClearCache();
|
||||
}
|
||||
|
||||
// Update the active metadata type
|
||||
s_activeMetadataType = metadataType;
|
||||
}
|
||||
|
||||
// Extract patterns (this can be done outside the lock if ExtractPatterns is thread-safe)
|
||||
PowerRenameLib::MetadataPatternMap patterns = s_metadataExtractor->ExtractPatterns(filePathStr, metadataType);
|
||||
|
||||
// Always call PutMetadataPatterns to ensure all patterns get replaced
|
||||
// Even if empty, this keeps metadata placeholders consistent when no values are extracted
|
||||
winrt::check_hresult(spRenameRegEx->PutMetadataPatterns(patterns));
|
||||
}
|
||||
|
||||
PWSTR newName = nullptr;
|
||||
|
||||
// Failure here means we didn't match anything or had nothing to match
|
||||
@@ -93,6 +167,10 @@ bool DoRename(CComPtr<IPowerRenameRegEx>& spRenameRegEx, unsigned long& itemEnum
|
||||
winrt::check_hresult(spRenameRegEx->ResetFileTime());
|
||||
}
|
||||
|
||||
if (useMetadata)
|
||||
{
|
||||
winrt::check_hresult(spRenameRegEx->ResetMetadata());
|
||||
}
|
||||
wchar_t resultName[MAX_PATH] = { 0 };
|
||||
|
||||
PWSTR newNameToUse = nullptr;
|
||||
@@ -206,4 +284,4 @@ bool DoRename(CComPtr<IPowerRenameRegEx>& spRenameRegEx, unsigned long& itemEnum
|
||||
CoTaskMemFree(originalName);
|
||||
|
||||
return wouldRename;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user