@@ -24,15 +24,28 @@ using namespace Windows::Storage::Streams;
2424
2525namespace Microsoft ::ReactNative {
2626
27+ static const char *ERROR_INVALID_URI = " E_INVALID_URI" ;
28+ static const char *ERROR_GET_SIZE_FAILURE = " E_GET_SIZE_FAILURE" ;
29+
2730winrt::fire_and_forget GetImageSizeAsync (
2831 const winrt::Microsoft::ReactNative::IReactPropertyBag &properties,
2932 std::string uriString,
3033 winrt::Microsoft::ReactNative::JSValue &&headers,
3134 Mso::Functor<void (int32_t width, int32_t height)> successCallback,
32- Mso::Functor<void()> errorCallback) {
35+ Mso::Functor<void(const char *errorCode, std::string errorMessage )> errorCallback) {
3336 bool succeeded{false };
37+ const char *errorCode = ERROR_GET_SIZE_FAILURE;
38+ std::string errorMessage;
3439
3540 try {
41+ // Validate URI is not empty
42+ if (uriString.empty ()) {
43+ errorCode = ERROR_INVALID_URI;
44+ errorMessage = " Cannot get the size of an image for an empty URI" ;
45+ errorCallback (errorCode, errorMessage);
46+ co_return ;
47+ }
48+
3649 ReactImageSource source;
3750 source.uri = uriString;
3851 if (!headers.IsNull ()) {
@@ -45,28 +58,38 @@ winrt::fire_and_forget GetImageSizeAsync(
4558 winrt::hstring scheme{uri.SchemeName ()};
4659 bool needsDownload = (scheme == L" http" ) || (scheme == L" https" );
4760 bool inlineData = scheme == L" data" ;
61+ bool isLocalFile = (scheme == L" file" ) || (scheme == L" ms-appx" ) || (scheme == L" ms-appdata" );
4862
4963 winrt::IRandomAccessStream memoryStream;
50- if (needsDownload) {
64+ if (needsDownload || isLocalFile ) {
5165 memoryStream = co_await GetImageStreamAsync (properties, source);
5266 } else if (inlineData) {
5367 memoryStream = co_await GetImageInlineDataAsync (source);
5468 }
55- auto result = wicBitmapSourceFromStream (memoryStream);
56- if (!std::get<std::shared_ptr<facebook::react::ImageErrorInfo>>(result)) {
57- auto imagingFactory = std::get<winrt::com_ptr<IWICImagingFactory>>(result);
58- auto wicBmpSource = std::get<winrt::com_ptr<IWICBitmapSource>>(result);
59- UINT width, height;
60- if (SUCCEEDED (wicBmpSource->GetSize (&width, &height))) {
61- successCallback (width, height);
62- succeeded = true ;
69+
70+ if (memoryStream) {
71+ auto result = wicBitmapSourceFromStream (memoryStream);
72+ if (!std::get<std::shared_ptr<facebook::react::ImageErrorInfo>>(result)) {
73+ auto imagingFactory = std::get<winrt::com_ptr<IWICImagingFactory>>(result);
74+ auto wicBmpSource = std::get<winrt::com_ptr<IWICBitmapSource>>(result);
75+ UINT width, height;
76+ if (SUCCEEDED (wicBmpSource->GetSize (&width, &height))) {
77+ successCallback (width, height);
78+ succeeded = true ;
79+ }
6380 }
6481 }
65- } catch (winrt::hresult_error const &) {
82+ } catch (winrt::hresult_error const &e) {
83+ errorMessage = " Failed to get image size: " + Microsoft::Common::Unicode::Utf16ToUtf8 (std::wstring (e.message ())) +
84+ " for URI: " + uriString;
6685 }
6786
68- if (!succeeded)
69- errorCallback ();
87+ if (!succeeded) {
88+ if (errorMessage.empty ()) {
89+ errorMessage = " Failed to get image size for URI: " + uriString;
90+ }
91+ errorCallback (errorCode, errorMessage);
92+ }
7093
7194 co_return ;
7295}
@@ -85,7 +108,9 @@ void ImageLoader::getSize(std::string uri, React::ReactPromise<std::vector<doubl
85108 [result](double width, double height) noexcept {
86109 result.Resolve (std::vector<double >{width, height});
87110 },
88- [result]() noexcept { result.Reject (" Failed" ); });
111+ [result](const char *errorCode, std::string errorMessage) noexcept {
112+ result.Reject (React::ReactError{errorCode, errorMessage});
113+ });
89114 });
90115}
91116
@@ -105,7 +130,9 @@ void ImageLoader::getSizeWithHeaders(
105130 [result](double width, double height) noexcept {
106131 result.Resolve (Microsoft::ReactNativeSpecs::ImageLoaderIOSSpec_getSizeWithHeaders_returnType{width, height});
107132 },
108- [result]() noexcept { result.Reject (" Failed" ); });
133+ [result](const char *errorCode, std::string errorMessage) noexcept {
134+ result.Reject (React::ReactError{errorCode, errorMessage});
135+ });
109136 });
110137}
111138
@@ -130,4 +157,4 @@ void ImageLoader::queryCache(
130157 result.Resolve (React::JSValueObject{});
131158}
132159
133- } // namespace Microsoft::ReactNative
160+ } // namespace Microsoft::ReactNative
0 commit comments