Page MenuHomePhabricator

D7485.id25479.diff
No OneTemporary

D7485.id25479.diff

diff --git a/native/cpp/CommonCpp/NativeModules/CommUtilsModule.cpp b/native/cpp/CommonCpp/NativeModules/CommUtilsModule.cpp
--- a/native/cpp/CommonCpp/NativeModules/CommUtilsModule.cpp
+++ b/native/cpp/CommonCpp/NativeModules/CommUtilsModule.cpp
@@ -1,6 +1,8 @@
#include "CommUtilsModule.h"
#include <ReactCommon/TurboModuleUtils.h>
+#include <fstream>
+#include <string>
namespace comm {
@@ -15,12 +17,85 @@
jsi::Runtime &rt,
jsi::String path,
jsi::Object data) {
- return jsi::Value::undefined();
+ auto arrayBuffer = data.getArrayBuffer(rt);
+ auto size = arrayBuffer.size(rt);
+ auto dataPtr = arrayBuffer.data(rt);
+ auto filePath = path.utf8(rt);
+ return createPromiseAsJSIValue(
+ rt, [=](jsi::Runtime &innerRt, std::shared_ptr<Promise> promise) {
+ taskType job = [=, &innerRt]() {
+ std::string error;
+ try {
+ std::ofstream file;
+ file.exceptions(std::fstream::failbit | std::fstream::badbit);
+ file.open(filePath, std::ios::binary);
+ file.write((char *)dataPtr, size);
+ file.close();
+ } catch (const std::exception &e) {
+ error = "Failed to write file " + filePath + ": " + e.what();
+ }
+
+ this->jsInvoker_->invokeAsync([error, promise]() {
+ if (error.size()) {
+ promise->reject(error);
+ } else {
+ promise->resolve(jsi::Value::undefined());
+ }
+ });
+ };
+ this->utilsThread->scheduleTask(job);
+ });
}
jsi::Value
CommUtilsModule::readBufferFromFile(jsi::Runtime &rt, jsi::String path) {
- return jsi::Value::undefined();
+ auto filePath = path.utf8(rt);
+ return createPromiseAsJSIValue(
+ rt, [=](jsi::Runtime &innerRt, std::shared_ptr<Promise> promise) {
+ taskType job = [=, &innerRt]() {
+ std::string error;
+ std::streampos file_size;
+ std::shared_ptr<uint8_t> data{nullptr};
+ try {
+ // Open a file
+ std::ifstream file;
+ file.exceptions(std::fstream::failbit | std::fstream::badbit);
+ file.open(filePath, std::ios::binary);
+
+ // Get file size
+ std::streampos current_pos = file.tellg();
+ file.seekg(0, std::ios::end);
+ file_size = file.tellg();
+ file.seekg(current_pos);
+
+ // Read file content
+ data = std::shared_ptr<uint8_t>(new uint8_t[file_size]);
+ file.read((char *)data.get(), file_size);
+ file.close();
+ } catch (const std::exception &e) {
+ error = "Failed to read file " + filePath + ": " + e.what();
+ }
+
+ this->jsInvoker_->invokeAsync([=, &innerRt]() {
+ if (error.size()) {
+ promise->reject(error);
+ return;
+ }
+ auto arrayBuffer =
+ innerRt.global()
+ .getPropertyAsFunction(innerRt, "ArrayBuffer")
+ // ArrayBuffer constructor takes one parameter: byte length
+ .callAsConstructor(
+ innerRt, {static_cast<double>(file_size)})
+ .asObject(innerRt)
+ .getArrayBuffer(innerRt);
+ auto bufferPtr = arrayBuffer.data(innerRt);
+ memcpy((void *)bufferPtr, data.get(), file_size);
+ promise->resolve(std::move(arrayBuffer));
+ });
+ };
+ this->utilsThread->scheduleTask(job);
+ });
}
jsi::String

File Metadata

Mime Type
text/plain
Expires
Tue, Dec 3, 9:44 AM (20 h, 36 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2611208
Default Alt Text
D7485.id25479.diff (3 KB)

Event Timeline