Forums

Resolved
0 votes
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?
Saturday, September 22 2018, 03:03 PM
Share this post:
Responses (5)
  • Accepted Answer

    Sunday, September 23 2018, 03:57 PM - #Permalink
    Resolved
    0 votes
    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?
    The reply is currently minimized Show
  • Accepted Answer

    Sunday, September 23 2018, 01:33 PM - #Permalink
    Resolved
    0 votes
    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.
    The reply is currently minimized Show
  • Accepted Answer

    Sunday, September 23 2018, 11:15 AM - #Permalink
    Resolved
    0 votes
    I really can't help any more as I'm not a PHP coder at all. All I was doing is pointing you to a page where I knew there was a progress bar.
    The reply is currently minimized Show
  • Accepted Answer

    Sunday, September 23 2018, 09:47 AM - #Permalink
    Resolved
    0 votes
    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.
    The reply is currently minimized Show
  • Accepted Answer

    Saturday, September 22 2018, 03:12 PM - #Permalink
    Resolved
    0 votes
    At a guess, have a look at the Marketplace app. When it goes to the installing part, you get a progress bar.
    The reply is currently minimized Show
Your Reply