fix(tiktok): calculate valid upload chunks
This commit is contained in:
@@ -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: {
|
||||
|
||||
Reference in New Issue
Block a user