סליחה על המענה המאוחר, אני השתמשתי בפיתרון ש @ivrtikshoret כתב פה,
עבד לי מעולה, ערכתי את הפונקציה שתתאים גם לקבצים קטנים יותר.
const readInChunks = (filePath, chunkSize = 5000000) => {
const file = fs.readFileSync(filePath);
const chunks = [];
for (let offset = 0; offset < file.length; offset += chunkSize) {
const chunk = file.slice(offset, offset + chunkSize);
chunks.push(chunk);
}
return chunks;
};
export const uploadFileInYemot = async (filePath, path, tokenYemot) => {
const fileSize = fs.statSync(filePath).size;
const contentName = filePath.split('/').pop();
if (fileSize <= 5000000) {
const formData = new FormData();
formData.append('token', tokenYemot);
formData.append('path', path);
formData.append('convertAudio', 1);
formData.append('autoNumbering', 1);
formData.append('file', fs.readFileSync(filePath), {
filename: contentName,
contentType: 'application/octet-stream',
});
const response = await fetch('https://www.call2all.co.il/ym/api/UploadFile', {
method: 'POST',
body: formData,
});
return await response.json();
} else {
const chunks = readInChunks(filePath);
const qquuid = uuidv4().toString();
for (let i = 0; i < chunks.length; i++) {
const chunk = chunks[i];
const formData = new FormData();
formData.append('token', tokenYemot);
formData.append('path', path);
formData.append('qquuid', qquuid);
formData.append('convertAudio', 0);
formData.append('autoNumbering', 'true');
formData.append('uploader', 'yemot-admin');
formData.append('qqfilename', contentName);
formData.append('qqtotalfilesize', fileSize);
formData.append('qqtotalparts', chunks.length);
formData.append('qqchunksize', chunk.length);
formData.append('qqpartbyteoffset', i * chunk.length);
formData.append('qqpartindex', i);
formData.append('qqfile', chunk, {
filename: contentName,
contentType: 'application/octet-stream',
});
const response = await fetch('https://www.call2all.co.il/ym/api/UploadFile', {
method: 'POST',
body: formData,
});
const responseData = await response.json();
console.log(responseData);
}
const finalFormData = new FormData();
finalFormData.append('token', tokenYemot);
finalFormData.append('path', path);
finalFormData.append('uploader', 'yemot-admin');
finalFormData.append('convertAudio', '0');
finalFormData.append('autoNumbering', 'true');
finalFormData.append('qquuid', qquuid);
finalFormData.append('qqfilename', contentName);
finalFormData.append('qqtotalfilesize', fileSize);
finalFormData.append('qqtotalparts', chunks.length);
try {
const response = await fetch('https://www.call2all.co.il/ym/api/UploadFile?done', {
method: 'POST',
body: finalFormData,
});
const responseData = await response.text();
console.log(responseData);
} catch (error) {
console.error("Error during the fetch:", error);
}
}
};
תודה לכל העוזרים.
נ.ב. תודה למי שפנה אליי באישי והאיר לי על הנושא.