gpt4 book ai didi

node.js - 如何使用 ExpressJS/Connect/Crypto 获取(大)文件的校验和?

转载 作者:太空宇宙 更新时间:2023-11-03 23:43:08 25 4
gpt4 key购买 nike

我需要在文件上传时为大文件 (.ISO) 生成校验和。我正在使用 ExpressJS/Connect 通过 POST(多部分)处理上传请求

  //express setup app.js
app.use('/upload',express.bodyParser({
defer : true, //enable progress event
keepExtensions : true,
limit : app.locals.fileLimit,
uploadDir : path.join( __dirname, '..', 'tmp'),
hash : true //this doesnt appear to do anything
}));

//express route routes.js
app.post("/upload", function(req, res){

var fs = require('fs');
var format = require('util').format;

req.form.hash = true; //this doesnt return the hash for some reason

req.form.on('fileBegin', function(err) {
log.info("Starting new upload...");
});

req.form.on('file', function(name, file) {
fs.rename(file.path, req.form.uploadDir + "/" + file.name);
console.info(file);
});

req.form.on('error', function(err) {
log.error('error',err);
req.resume();
});



req.form.on('field', function(field, value) {
log.info("Passed Field (%s=%s)...",field,value);
});

//this part event never gets called for some reason but
//I imagine this is where you might do a hash update
req.form.on('part', function(part){
console.log('PART upload %s %s', part.name, part.filename);
var out = fs.createWriteStream('../tmpstrm/' + part.filename);
part.pipe(out);
});


req.form.on('progress', function(bytesReceived, bytesExpected) {
var percent = (bytesReceived / bytesExpected * 100) | 0;
console.info( "PROGRESS :"+ ((bytesReceived / bytesExpected)*100) + "% uploaded");
});

req.form.on('end', function() {
var fileInfo = format('\nuploaded %s (%d Kb) to %s as %s'
, req.files.upload.name
, req.files.upload.size / 1024 | 0
, req.files.upload.path
, req.body.title);

res.locals.fileInfo = fileInfo; //renders info in jade

res.render('media/index');
});

});

我找不到关于我的特定问题的太多信息;这可能不是典型的用例。但我确实发现的一个问题有一个简短的例子 how to update the hash on a data event

var hash = crypto.createHash('sha1');
var data = // incoming file data

data.on('data', function(chunk){
hash.update(chunk);
});
data.on('end', function(){
var sha = hash.digest('hex');

// Do something with it
})

我对流媒体、 block 等还不熟悉,所以这里的一些指导值得赞赏。

最佳答案

在我的 writers-digest npm 模块源代码中,您可能会发现一些有用的示例代码:

https://github.com/focusaurus/writers-digest/blob/ab52ffca6e989ca657dc449e05ff258c428fa3af/index.js#L38

但基本上你有两种方法:

  1. 允许 bodyParser 将 HTTP 请求正文流式传输到文件系统,然后通过从文件系统流式传输数据来计算校验和(以 writers-digest 代码为例)。

  2. 您可以尝试在实际 HTTP 请求正文到达时计算校验和,方法是将事件处理函数绑定(bind)到 req 对象 dataend 事件。您应该能够执行此操作,同时 bodyParser 还将其流式传输到磁盘。只需确保事件绑定(bind)得足够早,这样您就不会错过任何 block 。

关于node.js - 如何使用 ExpressJS/Connect/Crypto 获取(大)文件的校验和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19100872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com