简体   繁体   English

如何从 NAPI(节点插件)接口读取文件并使用 nodejs 文件流写入不同的文件

[英]How to read file from NAPI (node-addon) interface and write to a different file using nodejs filestream

I am trying to read a file in the NAPI application and call the callback function to write it to the writestream in the nodejs application.我正在尝试读取 NAPI 应用程序中的文件并调用回调 function 将其写入 nodejs 应用程序中的 writestream。

exmaple_Class.cpp

void readFromTransientFile(const Napi::CallbackInfo &info) 
{
    Napi::Env env = info.Env();
    Napi::HandleScope scope(env);

    Napi::Function cb = info[0].As<Napi::Function>();
    

    while ((this->actualClass_->pos() < this->actualClass_->size()) &&
               ((readLength = this->actualClass_->read((void *)tempBuffer, sizeof(tempBuffer))) > 0))
        {

            //cb.Call(env.Global(), {Napi::Buffer<char>::New(env, tempBuffer, sizeof(tempBuffer))});
            cb.Call(env.Global(), {Napi::String::New(env, tempBuffer)});

            writeTotal += readLength;
        }
    std::cout << "Done!" << std::endl;
}

exmaple_Class.js

const testAddon = require("./build/Release/testaddon.node");
const fs = require("fs");

const prevInstance = new testAddon.OtClass();
const writeStream = fs.createWriteStream("./downloads/lake.jpg");

  prevInstance.readFromTransientFile(
    function (msg) {
      console.log(">> received buffer from getFile: ", msg);
      console.log(">> typeof msg", typeof msg);
      writeStream.write(msg);
    }
  );

writeStream.end();

Limitation of C++ side function is that it cannot return value, so data has to return in the callback. C++ 方面 function 的限制是它不能返回值,所以数据必须在回调中返回。 The funny part is that if it is a text file, it works correctly but for other types of files like zip or jpeg, I get garbled data.有趣的是,如果它是一个文本文件,它可以正常工作,但对于 zip 或 jpeg 等其他类型的文件,我得到的数据是乱码。 If I pass a file descriptor to a C++ function and use UNIX write function, then I get the file.如果我将文件描述符传递给 C++ function 并使用 UNIX 写入 ZC1C425268E683894F14AB 文件。 But I would like to send that data over HTTP using express as well.但我也想使用快递通过 HTTP 发送该数据。 So what is going wrong?那么出了什么问题呢? How can I correctly wrap and return binary data in NAPI objects.如何正确包装和返回 NAPI 对象中的二进制数据。

For the future generations.为子孙后代。 Data encoding was the error.数据编码是错误的。 Binary data is represented as Byte Array, then convert it into Buffer for writeStreamer to consume.二进制数据表示为 Byte Array,然后将其转换为 Buffer 供 writeStreamer 使用。

fileReader.cpp

void functionexample::ReadRawFile(const Napi::CallbackInfo &info)
{
    Napi::Env env = info.Env();
    Napi::HandleScope scope(env);

    // nodejs callback
    Napi::Function cb = info[0].As<Napi::Function>();

    char *file_name = "lake.jpg";
    int i = 0;
    const int BLOCK_SIZE = 1024;
    char buffer[BLOCK_SIZE];
    ifstream ifs;

    ifs.open(file_name, ifstream::binary | ifstream::in);

    while (1)
    {
        memset(buffer, 0, BLOCK_SIZE);

        ifs.read(buffer, BLOCK_SIZE);

        cout << i << " | buffer size: " << ifs.gcount() << endl;
        i++;

        ///  PASSING DATA TO THE NODEJS APPLICATION
        ///  AS A BINARY ARRAY PPINTER POINTING TO THE buffer
        cb.Call(env.Global(), {Napi::ArrayBuffer::New(env, buffer, ifs.gcount())});

        ofs.write(buffer, ifs.gcount());
        if (!ifs)
            break;
    }

    ifs.close();
}

fileWriter.js

const fs = require("fs");
const testAddon = require("./build/Release/testaddon.node");

// Create a write streamer 
const wstream = fs.createWriteStream("./lake-copied-nodejs.jpg");

testAddon.readFile(function(msg) {
  // handle data encoding
  const buf = Buffer.alloc(msg.byteLength);
  const view = new Uint8Array(msg);
  for (var i = 0; i < buf.length; ++i) {
    buf[i] = view[i];
  }

  // write binary data to file
  wstream.write(buf);
});
wstream.close();

module.exports = testAddon;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM