Skip to content

Commit a829862

Browse files
committed
ExifTool:
* Add an extra check if a file is a directory
1 parent 70a05c3 commit a829862

5 files changed

Lines changed: 33 additions & 8 deletions

File tree

src/DOpusScriptingExtensions/ExifTool/ExifTool.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ class ATL_NO_VTABLE CExifTool :
2121
} CATCH_ALL_EXCEPTIONS()
2222

2323
STDMETHOD(GetInfoAsJson)(BSTR fileFullName, IDispatch* tagNamesJsArray, BSTR* infoAsJson) override try {
24-
*infoAsJson = Copy(ToWide(
25-
exifToolWrapper->GetTagInfosJson(fileFullName, ToUtf8StringVector(JsStringArrayToVector(tagNamesJsArray)))));
24+
*infoAsJson = Copy(
25+
exifToolWrapper->GetTagInfosJson(fileFullName, ToUtf8StringVector(JsStringArrayToVector(tagNamesJsArray))));
2626
return S_OK;
2727
} CATCH_ALL_EXCEPTIONS()
2828

src/DOpusScriptingExtensions/ExifTool/ExifToolWrapper.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,17 @@ class ExifToolWrapper : NonCopyableAndNonMovable {
2828
// },
2929
// ...
3030
// }]
31-
std::string GetTagInfosJson(std::wstring_view filePath, const std::vector<std::string>& tagNames) {
31+
std::wstring GetTagInfosJson(std::wstring_view filePath, const std::vector<std::string>& tagNames) {
3232
if (!std::filesystem::exists(filePath))
3333
{
3434
THROW_WEXCEPTION(L"File not found '{}'", filePath);
3535
}
3636

37+
if (std::filesystem::is_directory(filePath))
38+
{
39+
THROW_WEXCEPTION(L"File is a directory '{}'", filePath);
40+
}
41+
3742
ioCtx.restart();
3843

3944
boost::asio::write(stdInPipe, boost::asio::buffer(
@@ -47,12 +52,12 @@ class ExifToolWrapper : NonCopyableAndNonMovable {
4752

4853
ioCtx.run();
4954

50-
const std::string stdErr = std::string{ static_cast<const char*>(errBuf.data().data()), errBuf.size() - readyErrStr.size() };
55+
const auto& stdErr = ToWide({ GetData(errBuf), errBuf.size() - readyErrStr.size() });
5156
if (!stdErr.empty()) {
52-
THROW_WEXCEPTION(L"ExifTool.exe failed to get information from the file: '{}'. Error message: {}", filePath, ToWide(stdErr));
57+
THROW_WEXCEPTION(L"ExifTool.exe failed to get information from the file: '{}'. Error message: {}", filePath, stdErr);
5358
}
5459

55-
return { static_cast<const char*>(outBuf.data().data()), outBuf.size() - readyStr.size() };
60+
return ToWide({ GetData(outBuf), outBuf.size() - readyStr.size() });
5661
}
5762

5863
private:

src/DOpusScriptingExtensions/ProcessRunner/ProcessRunner.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ class ATL_NO_VTABLE CProcessRunner :
7979

8080
const auto& exitCode = proc.wait();
8181

82-
return { ToWide({ static_cast<const char*>(outBuffer.data().data()), outBuffer.size() }),
83-
ToWide({ static_cast<const char*>(errBuffer.data().data()), errBuffer.size() }),
82+
return { ToWide({ GetData(outBuffer), outBuffer.size() }),
83+
ToWide({ GetData(errBuffer), errBuffer.size() }),
8484
exitCode };
8585
}
8686
};

src/DOpusScriptingExtensions/Utils/StringUtils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ inline std::vector<std::string> ToUtf8StringVector(const std::vector<std::wstrin
1919
return std::vector<std::string>(transformed.begin(), transformed.end());
2020
}
2121

22+
inline const char* GetData(const boost::asio::streambuf& buf) {
23+
return static_cast<const char*>(buf.data().data());
24+
}
25+
2226
// Allow std::format(L"..") to format std::filesystem::path and boost::filesystem::path
2327
namespace std {
2428
template <>

src/Test/test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,11 @@ function ExifTool_throws_if_file_not_found() {
526526
assertStringContains(res, "File not found 'C:/non_existent_file'")
527527
}
528528

529+
function ExifTool_throws_if_file_is_a_directory() {
530+
var res = assertThrows(function () { exifTool.GetInfoAsJson("C:/Windows") })
531+
assertStringContains(res, "File is a directory 'C:/Windows'")
532+
}
533+
529534
function ExifTool_can_get_all_tags() {
530535
var json = exifTool.GetInfoAsJson("C:/Windows/SystemResources/Windows.UI.SettingsAppThreshold/SystemSettings/Assets/HDRSample.mkv")
531536

@@ -562,6 +567,15 @@ function ExifTool_if_specific_tag_does_not_exists_it_is_ignored() {
562567
assertStringDoesNotContain(json, 'NonExistentTagName')
563568
}
564569

570+
function ExifTool_throws_if_tags_have_wrong_format() {
571+
var res = assertThrows(function () {
572+
exifTool.GetInfoAsJson(
573+
"C:/Windows/SystemResources/Windows.UI.SettingsAppThreshold/SystemSettings/Assets/HDRSample.mkv",
574+
["TrackType", 1, "NonExistentTagName"])
575+
})
576+
assertStringContains(res, "the element with the index 1 is not a string but a variant type #3")
577+
}
578+
565579
function ExifTool_returns_empty_string_if_file_does_not_have_specified_tags() {
566580
var json = exifTool.GetInfoAsJson(
567581
"C:/Windows/SystemResources/Windows.UI.SettingsAppThreshold/SystemSettings/Assets/HDRSample.mkv",
@@ -651,9 +665,11 @@ runTest(MediaInfoRetriever_can_open_media_file)
651665
runTest(MediaInfoRetriever_can_close_file)
652666

653667
runTest(ExifTool_throws_if_file_not_found)
668+
runTest(ExifTool_throws_if_file_is_a_directory)
654669
runTest(ExifTool_can_get_all_tags)
655670
runTest(ExifTool_can_get_specific_tags)
656671
runTest(ExifTool_if_specific_tag_does_not_exists_it_is_ignored)
672+
runTest(ExifTool_throws_if_tags_have_wrong_format)
657673
runTest(ExifTool_returns_no_tags_if_file_does_not_have_specified_tags)
658674
runTest(ExifTool_works_with_file_names_containing_unicode_characters)
659675
runTest(ExifTool_can_parse_json)

0 commit comments

Comments
 (0)