我正在尝试使用nodejs/express 创建一个后端,其中上传的文件的数据将由adobe api 提取。需要API
let readStream:fs.ReadStream | undefined = fs.createReadStream(inputFile) const inputAsset = await pdfServices.upload({readStream, mimeType: MimeType.PDF});
现在我需要从上传的文件创建一个读取流。我需要存储它还是可以直接存储它?这个问题有什么好的解决办法吗?
您可以使用哈希函数来确保您收到整个 PDF 文件,而不是将文件存储在服务器端(
pdfService
)。
const crypto = require('crypto');
/**
* Consumes the readStream, and returns hexidecimal
* digest sum calculated over the content of the readStream
*/
function calculateHashFromStream(readStream, algorithm = 'sha256') {
return new Promise((resolve, reject) => {
const hash = crypto.createHash(algorithm);
readStream.on('data', chunk => {
hash.update(chunk);
});
readStream.on('end', () => {
resolve(hash.digest('hex'));
});
readStream.on('error', err => {
reject(err);
});
});
}
要使用 PDF 数据尝试上述功能:
const { Readable } = require('node:stream');
// Emulating PDF data as a string
const pdfData = `
%PDF-1.4
1 0 obj
<< /Type /Catalog /Pages 2 0 R >>
endobj
2 0 obj
<< /Type /Pages /Count 1 >>
endobj
xref
0 3
0000000000 65535 f
0000000010 00000 n
0000000067 00000 n
trailer
<< /Root 1 0 R >>
startxref
120
%%EOF
`;
// Create a Readable stream from the PDF data
const fakePdfStream = Readable.from(pdfData);
// Calculate the hash of the simulated PDF data
calculateHashFromStream(fakePdfStream, 'sha256')
.then((hash) => {
console.log(`Hash of simulated PDF data: ${hash}`);
});
输出:
Hash of simulated PDF data: cc9addcba71e150169eb239898e3e9a8d8f6269f98bc0c28fa18359fc77167e5