/** * Resume the upload token with the uploaded file optionally at a given offset * * @param file $fileData * @param bool $finalChunk * @param float $resumeAt */ protected function handleResume($fileData, $finalChunk, $resumeAt) { $uploadFilePath = $this->_uploadToken->getUploadTempPath(); if (!file_exists($uploadFilePath)) { throw new kUploadTokenException("Temp file [{$uploadFilePath}] was not found when trying to resume", kUploadTokenException::UPLOAD_TOKEN_FILE_NOT_FOUND_FOR_RESUME); } $sourceFilePath = $fileData['tmp_name']; if ($resumeAt != -1) { // if this is the final chunk the expected file size would be the resume position + the last chunk size $expectedFileSize = $finalChunk ? $resumeAt + filesize($sourceFilePath) : 0; rename($sourceFilePath, "{$uploadFilePath}.chunk.{$resumeAt}"); $uploadFinalChunkMaxAppendTime = kConf::get('upload_final_chunk_max_append_time', 'local', 30); // if finalChunk, try appending chunks till reaching expected file size for up to 30 seconds while sleeping for 1 second each iteration $count = 0; do { if ($count++) { Sleep(1); } $currentFileSize = self::appendAvailableChunks($uploadFilePath); KalturaLog::log("handleResume iteration: {$count} finalChunk: {$finalChunk} filesize: {$currentFileSize}"); } while ($finalChunk && $currentFileSize != $expectedFileSize && $count < $uploadFinalChunkMaxAppendTime); if ($finalChunk && $currentFileSize != $expectedFileSize) { throw new kUploadTokenException("final size {$currentFileSize} failed to match expected size {$expectedFileSize}", kUploadTokenException::UPLOAD_TOKEN_CANNOT_MATCH_EXPECTED_SIZE); } } else { $uploadFileResource = fopen($uploadFilePath, 'r+b'); fseek($uploadFileResource, 0, SEEK_END); self::appendChunk($sourceFilePath, $uploadFileResource); fclose($uploadFileResource); } }
/** * Resume the upload token with the uploaded file optionally at a given offset * @param file $fileData * @param float $resumeAt */ protected function handleResume($fileData, $resumeAt = -1) { $uploadFilePath = $this->_uploadToken->getUploadTempPath(); if (!file_exists($uploadFilePath)) { throw new kUploadTokenException("Temp file [{$uploadFilePath}] was not found when trying to resume", kUploadTokenException::UPLOAD_TOKEN_FILE_NOT_FOUND_FOR_RESUME); } $this->resumeFile($fileData['tmp_name'], $uploadFilePath, $resumeAt); $fileWasDeleted = unlink($fileData['tmp_name']); if ($fileWasDeleted) { KalturaLog::info("Temp file was deleted successfully"); } else { KalturaLog::err("Failed to delete temp file [{$fileData['tmp_name']}"); } }
/** * Resume the upload token with the uploaded file optionally at a given offset * @param file $fileData * @param int $resumeAt */ protected function handleResume($fileData, $resumeAt = -1) { KalturaLog::info("Trying to resume the uploaded file"); $uploadFilePath = $this->_uploadToken->getUploadTempPath(); if (!file_exists($uploadFilePath)) { throw new kUploadTokenException("Temp file [{$uploadFilePath}] was not found when trying to resume", kUploadTokenException::UPLOAD_TOKEN_FILE_NOT_FOUND_FOR_RESUME); } if ($resumeAt > filesize($uploadFilePath)) { throw new kUploadTokenException("Temp file [{$uploadFilePath}] attempted to resume at invalid position {$resumeAt}", kUploadTokenException::UPLOAD_TOKEN_RESUMING_INVALID_POSITION); } $this->resumeFile($fileData['tmp_name'], $uploadFilePath, $resumeAt); KalturaLog::info("The file resumed successfully"); $fileWasDeleted = unlink($fileData['tmp_name']); if ($fileWasDeleted) { KalturaLog::info("Temp file was deleted successfully"); } else { KalturaLog::err("Failed to delete temp file [{$fileData['tmp_name']}"); } }