How can i download file from url with progress bar using clearos framework?
I finded some information.
Like curl_setopt( $ch, CURLOPT_PROGRESSFUNCTION, 'progressCallback' );
And i finded function progress_bar in cleaos apps code. And some ajax calls. What this function does? Should i use javascript? How i should do that?
I finded some information.
Like curl_setopt( $ch, CURLOPT_PROGRESSFUNCTION, 'progressCallback' );
And i finded function progress_bar in cleaos apps code. And some ajax calls. What this function does? Should i use javascript? How i should do that?
Share this post:
Responses (5)
-
Accepted Answer
Now i wrote a php script and pleced it in htdocs folder.
When i type this in bash comand line all works perfect.
/usr/bin/sudo /usr/bin/php /usr/clearos/apps/server_1c/htdocs/platform_download.php 8.3.10.2252 >/dev/null 2>&1 &
but when i want to do that from code it doesnt work.
function install_rpm($platform)
{
$result='';
$shell = new Shell();
$options = array('background' => TRUE);
try{
$args = sprintf('%s %s','/usr/clearos/apps/server_1c/htdocs/platform_download.php', $platform);
$exitcode = $shell->execute('/usr/bin/php' , $args, TRUE, $options);
$result.="</BR></BR>".implode("</BR>",$shell->get_output());
}catch(Exception $e)
{
$result.=$e->getMessage();
}
return $result;
}
The code above doest work. Can anybody say why? -
Accepted Answer
Yes...forking a script is the way to do this. Example:
$options = array(
'background' => TRUE,
'lang' => 'en_US',
'log' => '/tmp/progress.txt'
);
$shell = new Shell();
$exitcode = $shell->execute(
"/usr/bin/curl",
"--some args",
FALSE, # superuser or not
$options
);
The above should solve two problems for you:
1. Fork a process so your webconfig page is not waiting around for this script to load (due to the 'background' option).
2. Pump the progress output of curl to /tmp/progress.txt. You can then read, parse and display this from an ajax call running every second from JS.
Other references/examples that may help:
https://gitlab.com/clearos/clearfoundation/app-account-import/blob/master/packaging/account-import
https://gitlab.com/clearos/clearfoundation/app-account-import/blob/master/htdocs/account_import.js.php
https://gitlab.com/clearos/clearfoundation/app-account-import/blob/master/controllers/ajax.php
B. -
Accepted Answer
-
Accepted Answer
Nick Howitt wrote:
At a guess, have a look at the Marketplace app. When it goes to the installing part, you get a progress bar.
I already did it. But i have a problem. I dont know how to organize download in the background mode. I have this code
function install_rpm($platform)
{
echo $platform;
$ch=$this->login1c('','');
$url = 'https://releases.1c.ru/version_files?nick=Platform83&ver='.$plat form;
curl_setopt($ch, CURLOPT_URL, $url);
$postResult = curl_exec($ch);
$dom = new \DOMDocument;
$dom->loadHTML($postResult);
$list_of_a = $dom -> getElementsByTagName('a');
foreach($list_of_a as $a_){
if($a_->nodeValue=='Cервер 1С:Предприятия (64-bit) для RPM-based Lin ux-систем'){
$url = $a_->getAttribute('href');
break;
};
}
$url='https://releases.1c.ru'.$url;
curl_setopt($ch, CURLOPT_URL, $url);
$postResult = curl_exec($ch);
$dom->loadHTML($postResult);
$list_of_a = $dom -> getElementsByTagName('a');
foreach($list_of_a as $a_){
if($a_->nodeValue=='Скачать дистрибутив'){
$url = $a_->getAttribute('href');
break;
};
}
//$fh = fopen(tmpfile().'/'.basename($url), "wb");
$fh = fopen(sys_get_temp_dir().'/1c.tar.gz', "wb");
curl_setopt($ch, CURLOPT_FILE, $fh);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, array($this,'progress_rpm'));
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_exec($ch);
curl_close($ch);
}
function progress_rpm( $download_size, $downloaded_size, $upload_size, $uplo aded_size )
{
static $previousProgress = 0;
if ( $download_size == 0 )
$progress = 0;
else
$progress = round( $downloaded_size * 100 / $download_size );
if ( $progress > $previousProgress)
{
$previousProgress = $progress;
$fp = fopen(sys_get_temp_dir().'/progress_server_1c.txt', 'wb' );
fputs( $fp, "$progress\n" );
fclose( $fp );
if($progress==100)
unlink($fp);
}
}
But the promlem is that the page does not load until download is complete. How cant i do that in the background mode? Now i think to use shell. -
Accepted Answer
Please login to post a reply
You will need to be logged in to be able to post a reply. Login using the form on the right or register an account if you are new here.
Register Here »