Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F33305561
D7485.1768791271.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
3 KB
Referenced Files
None
Subscribers
None
D7485.1768791271.diff
View Options
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
Details
Attached
Mime Type
text/plain
Expires
Mon, Jan 19, 2:54 AM (8 h, 59 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5954014
Default Alt Text
D7485.1768791271.diff (3 KB)
Attached To
Mode
D7485: [native][utils-module] Implement file r/w operations
Attached
Detach File
Event Timeline
Log In to Comment