在nodejs中的createReadStream中追加数据

发布于 2025-01-09 17:57:39 字数 1271 浏览 0 评论 0原文

我有一个 node.js 应用程序,我在电子缓冲区中发布视频数据。

app.post("/add/", (req, res) => {
    var user_file = req.body.user_file;
    
    // this initializes a writestream where I can write my buffers and then save this file name in the database

    storage_stream = require("fs").createWriteStream(user_file+'.webm');

// db logic here...

    res.end(JSON.stringify({'success':'added'}))
})

上面的函数按预期运行,现在我想以 10 秒的间隔在另一个函数上发布我的缓冲区

app.post("/post_buffer/", (req, res) => {
    const user_file = req.body.user_file
    const buffer = Buffer.from(req.body.video_buffer);
      
//    I want to read the existing stream of the user and append the buffers in that stream, something like this

//  if I do something like this
//  const stored_stream = require("fs").createWriteStream(user_file+'.webm');

//  this overwrites the existing file.

    const stored_stream = require("fs").createReadStream(user_file+'.webm');
    stored_stream.write(buffer);     // this throws error, read stream don't have .write() function
    
//  OR
//  stored_stream.push(buffer);      // this doesn't throw any error but makes the file corrupted

    res.end(JSON.stringify({'success':true}))

})

是否有任何解决方法?如何将缓冲区附加到现有流中? 任何帮助将不胜感激。

I have a node.js app where I'm posting video data in buffers from electron.

app.post("/add/", (req, res) => {
    var user_file = req.body.user_file;
    
    // this initializes a writestream where I can write my buffers and then save this file name in the database

    storage_stream = require("fs").createWriteStream(user_file+'.webm');

// db logic here...

    res.end(JSON.stringify({'success':'added'}))
})

above function runs as expected, now I want to post my buffers on 10 seconds interval on another function

app.post("/post_buffer/", (req, res) => {
    const user_file = req.body.user_file
    const buffer = Buffer.from(req.body.video_buffer);
      
//    I want to read the existing stream of the user and append the buffers in that stream, something like this

//  if I do something like this
//  const stored_stream = require("fs").createWriteStream(user_file+'.webm');

//  this overwrites the existing file.

    const stored_stream = require("fs").createReadStream(user_file+'.webm');
    stored_stream.write(buffer);     // this throws error, read stream don't have .write() function
    
//  OR
//  stored_stream.push(buffer);      // this doesn't throw any error but makes the file corrupted

    res.end(JSON.stringify({'success':true}))

})

Is there any workaround for this? How can I append my buffers in the existing stream?
Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三生殊途 2025-01-16 17:57:39

该文件必须以附加模式从 app.post("/post_buffer/") 打开。如果文件不存在,则使用追加模式将创建该文件,并且在任何情况下都会将数据追加到文件末尾。

官方文档中记录了不同的操作系统文件处理程序标志: file-system-flags

app.post("/post_buffer/", (req, res) => {
    ...
    // Open file handle to append incoming buffer.
    const fileHandle = await fs.promises.open(user_file+'.webm', 'a'); // 'a' -> append mode
    // Initialize a write stream from file handle.
    const stored_stream = fileHandle.createWriteStream();
    // Write the incoming buffer to the write stream and 
    // await the buffer to be flushed.
    await new Promise(resolve => stored_stream.end(buffer, resolve));
   
    ...
})

The file must be opened in append mode from app.post("/post_buffer/"). Using append mode will create the file if it does not exist and, in any case, append data to the end of the file.

The different OS file handler flags are documented in the official documentation: file-system-flags

app.post("/post_buffer/", (req, res) => {
    ...
    // Open file handle to append incoming buffer.
    const fileHandle = await fs.promises.open(user_file+'.webm', 'a'); // 'a' -> append mode
    // Initialize a write stream from file handle.
    const stored_stream = fileHandle.createWriteStream();
    // Write the incoming buffer to the write stream and 
    // await the buffer to be flushed.
    await new Promise(resolve => stored_stream.end(buffer, resolve));
   
    ...
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文