MCPcopy Create free account
hub / github.com/CommE2E/comm / writeBufferToFile

Method writeBufferToFile

native/cpp/CommonCpp/NativeModules/CommUtilsModule.cpp:23–55  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

21}
22
23jsi::Value CommUtilsModule::writeBufferToFile(
24 jsi::Runtime &rt,
25 jsi::String path,
26 jsi::Object data) {
27 auto arrayBuffer = data.getArrayBuffer(rt);
28 auto size = arrayBuffer.size(rt);
29 auto dataPtr = arrayBuffer.data(rt);
30 auto filePath = path.utf8(rt);
31 return createPromiseAsJSIValue(
32 rt, [=](jsi::Runtime &innerRt, std::shared_ptr<Promise> promise) {
33 taskType job = [=, &innerRt]() {
34 std::string error;
35 try {
36 std::ofstream file;
37 file.exceptions(std::fstream::failbit | std::fstream::badbit);
38 file.open(filePath, std::ios::binary);
39 file.write((char *)dataPtr, size);
40 file.close();
41 } catch (const std::exception &e) {
42 error = "Failed to write file " + filePath + ": " + e.what();
43 }
44
45 this->jsInvoker_->invokeAsync([error, promise]() {
46 if (error.size()) {
47 promise->reject(error);
48 } else {
49 promise->resolve(jsi::Value::undefined());
50 }
51 });
52 };
53 this->utilsThread->scheduleTask(job);
54 });
55}
56
57jsi::Value
58CommUtilsModule::readBufferFromFile(jsi::Runtime &rt, jsi::String path) {

Callers

nothing calls this directly

Calls 6

rejectMethod · 0.80
resolveMethod · 0.80
scheduleTaskMethod · 0.80
openMethod · 0.65
writeMethod · 0.65
closeMethod · 0.65

Tested by

no test coverage detected