public static function downloadChunk($url, $outputPath, $isContinuation)
{
// make sure file doesn't already exist if we're starting a new download
if (!$isContinuation && file_exists($outputPath)) {
throw new Exception(Piwik::translate('General_DownloadFail_FileExists', "'" . $outputPath . "'") . ' ' . Piwik::translate('General_DownloadPleaseRemoveExisting'));
}
// if we're starting a download, get the expected file size & save as an option
$downloadOption = $outputPath . '_expectedDownloadSize';
if (!$isContinuation) {
$expectedFileSizeResult = Http::sendHttpRequest($url, $timeout = 300, $userAgent = null, $destinationPath = null, $followDepth = 0, $acceptLanguage = false, $byteRange = false, $getExtendedInfo = true, $httpMethod = 'HEAD');
$expectedFileSize = 0;
if (isset($expectedFileSizeResult['headers']['Content-Length'])) {
$expectedFileSize = (int) $expectedFileSizeResult['headers']['Content-Length'];
}
if ($expectedFileSize == 0) {
Log::info("HEAD request for '%s' failed, got following: %s", $url, print_r($expectedFileSizeResult, true));
throw new Exception(Piwik::translate('General_DownloadFail_HttpRequestFail'));
}
Option::set($downloadOption, $expectedFileSize);
} else {
$expectedFileSize = (int) Option::get($downloadOption);
if ($expectedFileSize === false) {
// sanity check
throw new Exception("Trying to continue a download that never started?! That's not supposed to happen...");
}
}
// if existing file is already big enough, then fail so we don't accidentally overwrite
// existing DB
$existingSize = file_exists($outputPath) ? filesize($outputPath) : 0;
if ($existingSize >= $expectedFileSize) {
throw new Exception(Piwik::translate('General_DownloadFail_FileExistsContinue', "'" . $outputPath . "'") . ' ' . Piwik::translate('General_DownloadPleaseRemoveExisting'));
}
// download a chunk of the file
$result = Http::sendHttpRequest($url, $timeout = 300, $userAgent = null, $destinationPath = null, $followDepth = 0, $acceptLanguage = false, $byteRange = array($existingSize, min($existingSize + 1024 * 1024 - 1, $expectedFileSize)), $getExtendedInfo = true);
if ($result === false || $result['status'] < 200 || $result['status'] > 299) {
$result['data'] = self::truncateStr($result['data'], 1024);
Log::info("Failed to download range '%s-%s' of file from url '%s'. Got result: %s", $byteRange[0], $byteRange[1], $url, print_r($result, true));
throw new Exception(Piwik::translate('General_DownloadFail_HttpRequestFail'));
}
// write chunk to file
$f = fopen($outputPath, 'ab');
fwrite($f, $result['data']);
fclose($f);
clearstatcache($clear_realpath_cache = true, $outputPath);
return array('current_size' => filesize($outputPath), 'expected_file_size' => $expectedFileSize);
}