У меня есть файл, который я зашифровал с помощью crypto.createCipher, и теперь я хочу передать этот файл и расшифровать его во время потоковой передачи.
Я пишу следующий код, но когда я устанавливаю «Content-Range»"и заголовок" Accept-Ranges ", потоковая передача не работает.
Как я могу транслировать и расшифровывать это с помощью content-range?
const path = "file path";
const stat = fs.statSync(path);
const fileSize = stat.size;
const range = req.headers.range;
const algorithm = 'aes-256-ctr';
const password = '123456';
const encrypt = crypto.createCipher(algorithm, password);
const decrypt = crypto.createDecipher(algorithm, password);
if (range) {
const parts = range.replace(/bytes=/, "").split("-");
const start = parseInt(parts[0], 10);
const end = parts[1] ? parseInt(parts[1], 10) : fileSize - 1;
const chunksize = end - start + 1;
const file = fs.createReadStream(path, { start, end });
const head = {
"Content-Range": `bytes ${start}-${end}/${fileSize}`,
"Accept-Ranges": "bytes",
"Content-Length": chunksize,
"Content-Type": "video/mp4"
};
res.writeHead(200, head);
file
.pipe(decrypt)
.pipe(res);
} else {
const head = {
"Content-Length": fileSize,
"Content-Type": "video/mp4"
};
res.writeHead(200, head);
fs.createReadStream(path).pipe(decrypt).pipe(res);
}