Your IP : 216.73.216.79


Current Path : /var/www/html/dosmv2/backend/modules/repodl/controllers/
Upload File :
Current File : /var/www/html/dosmv2/backend/modules/repodl/controllers/DefaultController.php

<?php

namespace backend\modules\repodl\controllers;

use Yii;
use yii\web\Controller;
use backend\modules\mmngr\models\ModuleRepositoryInstall;

/**
 * Default controller for the `repodownload` module
 *
 * Class DefaultController
 */
class DefaultController extends Controller
{
    private $temp_progress;
    
    /**
     * Renders the index view for the module
     *
     * @return string
     */
    public function actionIndex()
    {
        // get all install details
        $install = ModuleRepositoryInstall::find()->all();
        return $this->render('index', [
            'install' => $install
        ]);
    }
    
    public function actionView($id) {
        // check if a module already install
        $install = ModuleRepositoryInstall::find()->where([
            'repo_module_id' => $id
        ])->one();
        
        return $this->render('view', [
            'id' => $id,
            'baseUrl' => $this->module->baseUrl . '/',
            'install' => $install
        ]);
    }
    
    /**
     * Execute the download
     */
    public function actionDownload() {
        $post = Yii::$app->request->post();
        if (empty($post)) {
            return;
        }
        // save progress to variable instead of a file
        $this->temp_progress = '';
        
        $targetFile = fopen('../../module.zip', 'w');
        $url = $this->module->baseUrl . '/admin/' . $post['path'];
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_NOPROGRESS, false);
        curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, function ($resource, $download_size, $downloaded_size, $upload_size, $uploaded_size) {
            static $previousProgress = 0;

            if ($download_size == 0) {
                $progress = 0;
            } else {
                $progress = round($downloaded_size * 100 / $download_size);
            }

            if ($progress > $previousProgress) {
                $previousProgress = $progress;
                $this->temp_progress = $progress;
                $fp = fopen('progress.json', 'w');

                $finished = $progress == '100' ? 'true' : 'false';
                fwrite($fp, '{"filename" : "module.zip","progress" : "' . $progress . '","finished" : "' . $finished . '"}');
                fclose($fp);
            }
        });
        curl_setopt($ch, CURLOPT_FILE, $targetFile);
        curl_exec($ch);
        fclose($targetFile);
    }
}