@@ -38,67 +38,79 @@ winrt::fire_and_forget RNPrint::Print(
38
38
std::string filePathStr = options.filePath .value ();
39
39
auto filePathHstring = winrt::to_hstring (filePathStr);
40
40
41
- // Check if the filePath is a URL (http/https )
42
- if (filePathStr. rfind ( " http:// " , 0 ) == 0 || filePathStr. rfind ( " https:// " , 0 ) == 0 ) {
43
- // Download the file to a temporary location first
44
- m_context.UIDispatcher ().Post ([options , filePathStr, promise = std::move ( promise) ]() mutable -> winrt::fire_and_forget {
41
+ if (filePathStr. rfind ( " http:// " , 0 ) == 0 || filePathStr. rfind ( " https:// " , 0 ) == 0 )
42
+ {
43
+ auto context = m_context; // Capture context safely
44
+ m_context.UIDispatcher ().Post ([context , filePathStr, jobName = options. jobName , promise]() mutable -> winrt::fire_and_forget {
45
45
using namespace winrt ::Windows::Storage;
46
46
using namespace winrt ::Windows::Web::Http;
47
- using namespace winrt ::Windows::Foundation;
48
47
49
48
try {
50
49
auto tempFolder = ApplicationData::Current ().TemporaryFolder ();
51
- auto uri = winrt::Windows::Foundation::Uri ( winrt::to_hstring (filePathStr)) ;
50
+ winrt::Windows::Foundation::Uri uri{ winrt::to_hstring (filePathStr) } ;
52
51
HttpClient httpClient;
53
52
54
53
auto buffer = co_await httpClient.GetBufferAsync (uri);
54
+ std::wstring fileName = L" printfile.pdf" ;
55
+
55
56
auto path = uri.Path ();
56
- std::wstring fileName = L" printfile" ;
57
57
if (!path.empty ()) {
58
58
std::wstring wpath = path.c_str ();
59
59
size_t pos = wpath.find_last_of (L" /\\ " );
60
- if (pos != std::wstring::npos && pos + 1 < wpath.size ()) {
60
+ if (pos != std::wstring::npos && pos + 1 < wpath.length ()) {
61
61
fileName = wpath.substr (pos + 1 );
62
62
}
63
- else {
64
- fileName = wpath;
65
- }
66
63
}
64
+
67
65
auto file = co_await tempFolder.CreateFileAsync (fileName, CreationCollisionOption::GenerateUniqueName);
68
66
co_await FileIO::WriteBufferAsync (file, buffer);
69
67
70
- std::wstring nativePath = file.Path ().c_str (); // Get full native path
71
- ShellExecuteW (NULL , L" print" , nativePath.c_str (), NULL , NULL , SW_SHOWNORMAL);
72
- promise.Resolve (options.jobName );
68
+ std::wstring nativePath = file.Path ().c_str ();
69
+ auto result = ShellExecuteW (nullptr , L" print" , nativePath.c_str (), nullptr , nullptr , SW_SHOWNORMAL);
70
+
71
+ if ((INT_PTR)result <= 32 ) {
72
+ promise.Reject (L" Failed to print downloaded file." );
73
+ } else {
74
+ promise.Resolve (jobName);
75
+ }
73
76
}
74
77
catch (...) {
75
78
promise.Reject (L" Exception occurred while downloading or printing file." );
76
79
}
80
+
77
81
co_return ;
78
82
});
79
- } else {
80
- // Local file path
81
- m_context.UIDispatcher ().Post ([options, filePathHstring, promise = std::move (promise)]() mutable -> winrt::fire_and_forget {
83
+ }
84
+ else
85
+ {
86
+ auto context = m_context; // Capture safely
87
+ m_context.UIDispatcher ().Post ([context, filePathHstring, jobName = options.jobName , promise]() mutable -> winrt::fire_and_forget {
82
88
using namespace winrt ::Windows::Storage;
83
- using namespace winrt ::Windows::Foundation;
84
89
85
90
try {
86
91
StorageFile file = co_await StorageFile::GetFileFromPathAsync (filePathHstring);
87
- std::wstring nativePath = file.Path ().c_str (); // Get full native path
88
- ShellExecuteW (NULL , L" print" , nativePath.c_str (), NULL , NULL , SW_SHOWNORMAL);
89
- promise.Resolve (options.jobName );
92
+ std::wstring nativePath = file.Path ().c_str ();
93
+ auto result = ShellExecuteW (nullptr , L" print" , nativePath.c_str (), nullptr , nullptr , SW_SHOWNORMAL);
94
+
95
+ if ((INT_PTR)result <= 32 ) {
96
+ promise.Reject (L" Failed to print local file." );
97
+ } else {
98
+ promise.Resolve (jobName);
99
+ }
90
100
}
91
101
catch (...) {
92
102
promise.Reject (L" Failed to open or print local file." );
93
103
}
104
+
94
105
co_return ;
95
106
});
96
107
}
97
108
}
98
109
catch (...)
99
110
{
100
- promise.Reject (L" Unknown error in Print function" );
111
+ promise.Reject (L" Unknown error in Print function. " );
101
112
}
113
+
102
114
co_return ;
103
115
}
104
116
} // namespace winrt::RNPrint
0 commit comments