Как загрузить видео с помощью multipart/form-data и axios
Я пытаюсь реализовать загрузку видео, используя multipart/form-data и axios.
Но при попытке загрузить разделенный видеофайл на чанки по байтам. Программа выдает такую ошибку:
AxiosError.call(axiosError, error.message, code, config, request, response);
^
AxiosError: read ECONNRESET
at AxiosError.from (d:\PH Auto\node_modules\axios\dist\node\axios.cjs:725:14)
at RedirectableRequest.handleRequestError (d:\PH Auto\node_modules\axios\dist\node\axios.cjs:2467:25)
at RedirectableRequest.emit (node:events:513:28)
at eventHandlers.<computed> (d:\PH Auto\node_modules\follow-redirects\index.js:14:24)
at ClientRequest.emit (node:events:513:28)
at TLSSocket.socketErrorListener (node:_http_client:494:9)
at TLSSocket.emit (node:events:513:28)
at emitErrorNT (node:internal/streams/destroy:151:8)
at emitErrorCloseNT (node:internal/streams/destroy:116:3)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
syscall: 'read',
code: 'ECONNRESET',
errno: -4077
Мой код, который преобразует видео в чанки байтов и пытается их отправить.
async function getChunk(videoPath, fileSize, params, cookie2, token, file_id, file_time, file_key, useragent){
let chunks = [];
let readStream = fs.createReadStream(videoPath);
let stat = fs.statSync(videoPath);
let step = 1;
let chunk;
readStream.on('readable', function() {
while (null !== (chunk = readStream.read(chunkSize))) {
chunks.push(chunk);
};
});
readStream.on('end', async () => {
for (i in chunks) {
console.log(`Yes, prepared cell`)
await uploadVideo(chunks[i], Number(i)+1, fileSize, chunks.length, params, cookie2, token, file_id, file_time, file_key, useragent);
step++;
};
// await fs.unlinkSync(videoPath);
return
})
};
async function uploadVideo(chunks, step, fileSize, chunksLength, params, cookie2, token, file_id, file_time, file_key, useragent) {
function bufferToStream(binary) {
const readableInstanceStream = new Readable({
read() {
this.push(binary);
this.push(null);
}
});
return readableInstanceStream;
};
let firstChunkSize;
let secondChunkSize;
if (step == 1) {
firstChunkSize = 0;
secondChunkSize = chunkSize;
} else if (step == chunksLength) {
firstChunkSize = chunkSize * (step - 1) + step - 1;
secondChunkSize = fileSize - 1;
} else {
firstChunkSize = chunkSize * (step - 1) + step - 1;
secondChunkSize = chunkSize * (step - 1) + step - 1 + chunkSize;
}
const formData = new FormData();
formData.append('value', bufferToStream(chunks));
let result = await axios.post(params, formData, {
data: {
token: token,
file_id: file_id,
file_time: file_time,
file_key: file_key
},
headers: {
'cookie': cookie2,
'user-agent': useragent,
'x-requested-with': 'XMLHttpRequest',
'content-disposition': `attachment; filename=${encodeURI(`./video/${videoNames[0]}`)}`,
'content-type': 'multipart/form-data;',
'content-range': `bytes ${firstChunkSize}-${secondChunkSize}/${fileSize}`,
'origin': 'https://rt.pornhub.com',
'referer': 'https://rt.pornhub.com/upload/videodata'
}
});
return result
};
Я также пытался сделать аналогичный запрос через request, но ошибка была точно такой же. Возможно, я как-то неправильно отправляю multipart/form-data. Но я не уверен.
Запрос, загружающий чанк:
Полный лог ошибки: https://disk.yandex.ru/d/c6NLEdgTIfavHg
