From a43e99aa10f3c555efcb25b4700bd7567eb56cab Mon Sep 17 00:00:00 2001 From: gunshiz Date: Sun, 2 Aug 2026 18:50:05 +0700 Subject: [PATCH] fix(tiktok): calculate valid upload chunks --- lib/video/video-platforms.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/video/video-platforms.ts b/lib/video/video-platforms.ts index 4e0cf0f..a535ef2 100644 --- a/lib/video/video-platforms.ts +++ b/lib/video/video-platforms.ts @@ -267,6 +267,7 @@ async function publishTiktok(upload: typeof videoUploads.$inferSelect) { "SELF_ONLY"; const video = await readFile(upload.videoPath); const chunkSize = Math.min(10 * 1024 * 1024, video.byteLength); + const totalChunkCount = Math.max(1, Math.floor(video.byteLength / chunkSize)); const init = await fetch( "https://open.tiktokapis.com/v2/post/publish/video/init/", { @@ -291,7 +292,7 @@ async function publishTiktok(upload: typeof videoUploads.$inferSelect) { source: "FILE_UPLOAD", video_size: video.byteLength, chunk_size: chunkSize, - total_chunk_count: Math.ceil(video.byteLength / chunkSize), + total_chunk_count: totalChunkCount, }, }), }, @@ -308,8 +309,12 @@ async function publishTiktok(upload: typeof videoUploads.$inferSelect) { const uploadUrl = initData.data?.upload_url; if (!uploadUrl || !initData.data?.publish_id) throw new Error("TikTok did not return an upload ticket."); - for (let start = 0; start < video.byteLength; start += chunkSize) { - const end = Math.min(start + chunkSize, video.byteLength) - 1; + for (let chunk = 0; chunk < totalChunkCount; chunk += 1) { + const start = chunk * chunkSize; + const end = + chunk === totalChunkCount - 1 + ? video.byteLength - 1 + : start + chunkSize - 1; const result = await fetch(uploadUrl, { method: "PUT", headers: {