当以块的形式接收文件时,如何使用gridfsbucket将大文件传输到mongodb?

时间:2018-01-26 02:41:41

标签: node.js mongodb gridfs

我正在使用nodejs,mongodb和gridfsbucket。

我在255个字节的块中接收到一个文件进入我的服务器,文件可能非常大,因此创建一个变量来存储块然后使用gridfsbucket将其传入mongo不是一个可行的选择。

目前我有一种暂时将文件存储在磁盘上然后将其传输到mongo的工作方法。这实际上工作得很好,唯一的问题是我不想在使用gridfsbucket流式传输到mongo之前临时存储数据。

有没有人知道如何在他们进入我的服务器时使用那些块并立即使用gridfsbucket将它们流入mongo?我想我需要打开管道,然后不断地将块流入管道,但我不知道如何实现这一点。

这是我目前存储到磁盘的代码:

fs.appendFileSync(this.tempFileName, Buffer.from(this.currfile.currdatachunk));
this.currfile.filename = this.currfile.filename.replace(")", Date.now() + ")");
var fileName = decodeURI(this.currfile.filename.replace("$(", "").replace(")", ""));
 fileName = encodeURI(fileName);
 var self = this;
 var gb = new GridFSBucket(mongoCacheDb, { bucketName: this.cacheCollection });
 var uploadStream = gb.openUploadStream(fileName);

 uploadStream.options.metadata = {
     'url': "/getFile/" + fileName,
     'id': uploadStream.id
 }

 uploadStream.once("finish", function uploadStream_onceFinish() {
                        if (this.length > 0) {
                            var ms = new Message();
                            ms.data = self.cacheCollection + "/" + self.currfile.filename;
                            ms.datatype = "URL";
                            ms.hasdata = "yes";
                            ms.haserrors = "no";
                            ms.type = "APPXLOADURL";
                            sendMessage(ws, ms);

                            /*Send response to server indicating file receipt*/
                            var nameAB = Buffer.from(self.currfile.filename);
                            self.clientsocket.write(Buffer.from(hton32(nameAB.length)));
                            self.clientsocket.write(Buffer.from(nameAB));
                            self.clientsocket.write(Buffer.from([3, 1]));
                            console.log("Finished: " + Date.now());
                        } else {
                            var nameAB = Buffer.from(self.currfile.filename);
                            self.clientsocket.write(Buffer.from(hton32(nameAB.length)));
                            self.clientsocket.write(Buffer.from(nameAB));
                            self.clientsocket.write(Buffer.from([0, 0]));
                        }
                        fs.unlinkSync(self.tempFileName);
                    });
                    fs.createReadStream(this.tempFileName).pipe(uploadStream);

1 个答案:

答案 0 :(得分:0)

我想我应该等一天。终于把头包住了,停止了过度思考。我在第一次运行时创建了管道,然后在接收时将缓冲的块推入管道。代码如下:

if (this.chunksReceived == 0) {
    this.rStream = new Readable({ read(size) { } });

    this.currfile.filename = this.currfile.filename.replace(")", Date.now() + ")");
    var fileName = decodeURI(this.currfile.filename.replace("$(", "").replace(")", ""));
    fileName = encodeURI(fileName);
    var self = this;
    var gb = new GridFSBucket(mongoCacheDb, { bucketName: this.cacheCollection });
    this.uploadStream = gb.openUploadStream(fileName);

    this.uploadStream.options.metadata = {
        'url': "/getFile/" + fileName,
        'id': this.uploadStream.id
    }

    this.uploadStream.once("finish", function uploadStream_onceFinish() {
        if (this.length > 0) {
            var ms = new Message();
            ms.data = self.cacheCollection + "/" + self.currfile.filename;
            ms.datatype = "URL";
            ms.hasdata = "yes";
            ms.haserrors = "no";
            ms.type = "APPXLOADURL";
            sendMessage(ws, ms);

            /*Send response to server indicating file receipt*/
            var nameAB = Buffer.from(self.currfile.filename);
            self.clientsocket.write(Buffer.from(hton32(nameAB.length)));
            self.clientsocket.write(Buffer.from(nameAB));
            self.clientsocket.write(Buffer.from([3, 1]));
            console.log("Finished: " + Date.now());
        } else {
            var nameAB = Buffer.from(self.currfile.filename);
            self.clientsocket.write(Buffer.from(hton32(nameAB.length)));
            self.clientsocket.write(Buffer.from(nameAB));
            self.clientsocket.write(Buffer.from([0, 0]));
        }
    });

    this.rStream.pipe(this.uploadStream);
}
this.rStream.push(Buffer.from(this.currfile.currdatachunk));
this.chunksReceived++;