adamdebreceni commented on code in PR #2088:
URL: https://github.com/apache/nifi-minifi-cpp/pull/2088#discussion_r2732330944


##########
libminifi/src/c2/C2Agent.cpp:
##########
@@ -1140,25 +1164,32 @@ void C2Agent::handleAssetUpdate(const 
C2ContentResponse& resp) {
     return;
   }
 
-  C2Payload file_response = protocol_->fetch(url);
-
-  if (file_response.getStatus().getState() != 
state::UpdateState::READ_COMPLETE) {
-    send_error("Failed to fetch asset from '" + url + "'");
-    return;
-  }
-
-  auto raw_data = std::move(file_response).moveRawData();
   // ensure directory exists for file
   if (utils::file::create_dir(file_path.parent_path()) != 0) {
     send_error("Failed to create directory '" + 
file_path.parent_path().string() + "'");
     return;
   }
 
-  {
-    std::ofstream file{file_path, std::ofstream::binary};
-    file.write(reinterpret_cast<const char*>(raw_data.data()), 
gsl::narrow<std::streamsize>(raw_data.size()));
+  std::filesystem::path tmp_file{file_path.string() + ".part"};
+
+  std::ofstream file{tmp_file, std::ofstream::binary};
+  if (!file) {
+    send_error("Failed to open asset file to write '" + tmp_file.string() + 
"'");
+    return;
+  }
+  bool success = protocol_->fetch(url, [&] (std::span<const char> chunk) {
+    file.write(chunk.data(), gsl::narrow<std::streamsize>(chunk.size()));
+    return file.good();
+  });
+  file.close();
+  if (!file || !success) {
+    std::filesystem::remove(tmp_file);
+    send_error("Failed to fetch asset from '" + url + "'");
+    return;
   }
 
+  std::filesystem::rename(tmp_file, file_path);

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to