Good afternoon!
I use php for scripting - team lead task…
In general, such a moment, I send the initialization of the resumed video download, I get 201, great, I start loading chunks, I get 308, it seems to be fine too, but when I send the last chunk, I also get 308 and not 200…
Tell me what am I doing wrong?
Initialization code\/
// Video download initialization
function initializeResumableUpload($videoPath, $channelId){
// Determining the initial values of the file
$fileSize = filesize($videoPath); // File size
$videoName = basename($videoPath); // Filename with permission
$name = strstr($videoName, '.', true); // Filename and video
// data header
$headers = array(
'Authorization: Bearer '.$this->token['access_token'],
'Content-Type: application/json',
'X-Upload-Content-Length: '.$fileSize,
'X-Upload-Content-Type: video/mp4',
);
// Sending video data
$initData = array(
'filename' => $videoName,
'channelId' => $channelId,
'name' => $name,
'category' => '1',
'licence' => '7',
'description' => $name,
'language' =>'ru',
'privacy' => '4',
'commentsEnabled' => false,
'downloadEnabled' => false
);
// Auto assign tag '360:360'
if(!strripos($videoPath, 'roof')){
$initData['tags[0]'] = '360:360';
}
// Create a request
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $this->api_link.'/videos/upload-resumable',
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $initData
));
// Catching values
$response = curl_exec($ch); // Outputs NULL
$redirectUrl = curl_getinfo($ch, CURLINFO_REDIRECT_URL); // Displays complete information
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); // Displays the session code
curl_close($ch);
print_r($redirectUrl);
$redirectUrl = 'https:'.$redirectUrl['responseHeaders']['Location'][0]; // Link with ID
$upload_id = substr(strstr($redirectUrl, '='), 1); // DI loading
if($httpCode != 201){
echo 'Error initializing upload: '.curl_error($ch);
}else{
$redirect = array(
'httpCode' => $httpCode,
'upload_id' => $upload_id,
'redirectUrl' => $redirectUrl
);
print_r($redirect);
return $redirect;
}
}
Submit block for resumable video upload code\/
//Submit block for resumable video upload
function multiUploadVideo($videoPath, $channelId){
$initialize = $this->initializeResumableUpload($videoPath, $channelId);
// Start download
$chunkSize = 256 * 1024; // Block size
$fileSize = filesize($videoPath); // File size
$offset = 0; // start position
$handle = fopen($videoPath, 'r'); // Read file
//$handle = Stream::of($videoPath);
fseek($handle, $offset); // Set offset
while(!feof($handle)){
$chunkData = fread($handle, $chunkSize);
$endBytePos = ftell($handle) - 1;
// data header
$headers = array(
'Authorization: Bearer '.$this->token['access_token'],
'Content-Type: application/octet-stream',
'Content-Range: bytes '.$offset.'-'.$endBytePos.'/'.$fileSize,
'Content-Length: '.strlen($chunkData),
);
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $initialize['redirectUrl'],
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => decbin($chunkData)
));
//file_put_contents('chank/test_chank_'.$offset.'_byte_.txt', $chunkData);
//$tessst .= $chunkData;
$response = curl_exec($ch); // Outputs NULL
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); // Displays the session code
$redirect = curl_getinfo($ch); // Displays complete information
if($httpCode == 308){
// Give info
var_dump('Download status: '.strlen($chunkData).' >>> '.$offset.'-'.$endBytePos.'/'.$fileSize.' HTTP: '.$httpCode);
// Set next position
$offset += strlen($chunkData);
}else if($httpCode == 200){
var_dump('Upload complete: '.$endBytePos.'/'.$fileSize.' HTTP: '.$httpCode);
curl_close($ch);
}else{
var_dump('Download error: '.curl_error($ch).' HTTP: '.$httpCode);
curl_close($ch);
print_r($response);
break;
}
// Accelerate the speed to the maximum on peertube
//if($chunkSize < 102400){
//if($offset > 0){$chunkSize = $chunkSize + (1024 * 1024);} //Kb
//}
}
//file_put_contents('test_chank.txt', $tessst);
//rename('test_chank.txt', 'test_chank.mp4');
//print_r($redirect);
fclose($handle);
}