Node.js 如何使用流来加密数据,然后将数据流到GridFs?(mongodb)
我目前正在构建一个应用程序,该应用程序接收用户上传,然后我想对其进行加密,然后使用grid fs库将其保存到mongodb。我使用multer接收文件,并尝试对其进行加密,然后使用gridfs流库将其保存到mongodb。有人能帮我看看这个吗?我不知道如何在nodejs中使用流Node.js 如何使用流来加密数据,然后将数据流到GridFs?(mongodb),node.js,mongodb,stream,gridfs,Node.js,Mongodb,Stream,Gridfs,我目前正在构建一个应用程序,该应用程序接收用户上传,然后我想对其进行加密,然后使用grid fs库将其保存到mongodb。我使用multer接收文件,并尝试对其进行加密,然后使用gridfs流库将其保存到mongodb。有人能帮我看看这个吗?我不知道如何在nodejs中使用流 import { createCipher } from "crypto"; import { AES_PASSWORD } from "../config"; import stream from "stream";
import { createCipher } from "crypto";
import { AES_PASSWORD } from "../config";
import stream from "stream";
import GridFs from "../models/GridFs";
const cipher = createCipher("aes-256-cbc", AES_PASSWORD);
const maxFileSize = 1024 * 1024 * 10; // 10MB
const fileChunkSize = 2 * 255 * 1024; // 510kb
const uploadController = (req,res,next) => {
// User information
const user_id = req.user._id;
// File meta data from the file infromation
const {
mimetype,
buffer,
size
} = req.file;
const content_type = mimetype;
// File information provided by Resumable.js
const {
resumableChunkNumber,
resumableChunkSize,
resumableCurrentChunkSize,
resumableTotalSize,
resumableType,
resumableIdentifier,
resumableFilename,
resumableRelativePath,
resumableTotalChunks
} = req.body;
/*
* Developer's note:
* Somehow the data being posted automatically by resumable.js
* is of type string. Thus we have to parse the string here
* to prevent errors when doing any comparisons
*/
const data = buffer;
const chunk_n = parseInt(resumableChunkNumber) - 1;
const chunk_total = parseInt(resumableTotalChunks);
const chunk_size = parseInt(resumableChunkSize);
const file_length = parseInt(resumableTotalSize);
const uid = resumableIdentifier;
const file_name = resumableFilename;
// Verify Chunk size as configured
if(chunk_size != fileChunkSize){
return res.status(400).json({
error: "chunk_size is not equal to configured chunk size as the backend"
});
}
// Ensure file size is not larger than max allowable at the moment
if( file_length && file_length > maxFileSize) {
return res.status(400).json({
error: "file length is greater than configured maxFileSize"
});
}
// Basic logic in ensuring the max number of chunks is correct when
// there is overlap
let expectedNumberOfChunks = file_length / chunk_size;
if(file_length % chunk_size){
expectedNumberOfChunks += 1;
}
if(chunk_n > expectedNumberOfChunks){
return res.status(400).json({
error: "chunk_n is greater than the number of expected chunks"
});
}
const writeStream = GridFs.createWriteStream({
content_type,
chunk_size,
chunk_n,
uid,
file_name,
file_length,
user_id
});
// All is good Start encrytion
const bufferStream = new stream.PassThrough();
try{
bufferStream.write(buffer);
bufferStream.pipe(cipher).pipe(writeStream).on("close", () => {
buffer.end();
return res.status(200).send("ok");
});
} catch(error){
console.log(error);
return res.status(500).json({ error });
}
};
export default uploadController;
没有发生任何事情,请求只是超时。我现在说不出话来。我是不是遗漏了什么?文件来自哪里?是不是应该在上传请求中?如果在请求中,那么无论使用什么中间件,都应该将其作为文件句柄公开,但是我没有看到任何试图从这样的句柄读取的内容,这里大概是
req
。如果它作为一个“流”可用,并且您的加密处理流,那么它应该只是一个从源到加密,最后到GridFS的writeStream创建的管道问题。但是我看不到源代码。哦,很抱歉,我没有包括我正在使用的中间件。我使用的是multer.js,它用上传文件的信息填充req.body对象。那么句柄和输入流在哪里呢?您需要在某个地方打开输入流。这就是我没有看到的。除非我完全错过了,我以为它就在那里的某个地方。是否可以将缓冲区转换为读取流,然后通过管道将其转换为加密流,再转换为gridfs?我把bufferStream称为通过哪个缓冲区的流?这基本上就是我所说的。我看不出有人试图以流的形式打开上传的内容。因此,这将只是挂起,什么也不做。如果上传了一个文件,那么它将作为临时文件或其他文件在文件系统中创建。但是您的中间件应该能够告诉您该文件在哪里。然后打开一个读流到文件位置,然后使用管道等等。了解了?