DownloadObjectRequestClass.php 3.3 KB
<?php

/**
 * @class DownloadObjectRequestClass
 * @brief Class used for a download request 
 * @details
 */
class DownloadObjectRequestClass extends RequestAbstractClass
{

    /*
	 * @brief Init a donwload object request
	*/
    public function init(){ 
        if (!isset($this->requestData))
			return false;
        if (!is_dir($this->requestData->getWorkingPath())) {
            mkdir($this->requestData->getWorkingPath());
        }
        return true;
    }

    public function run(){ 
        // Array to hold the paths of the downloaded files
        $filePaths = array();

        $files = $this->requestData->getFiles();
        foreach($files as $fileName => $fileUrl){
            $result = $this->downloadFile($fileName, $fileUrl, $this->requestData->getWorkingPath());
            if (!$result['success']) {
                $this->cleanup($filePaths);
                $this->requestData->setLastErrorMessage($result['message']);
                $this->requestData->setSuccess(false);
                return false;
            }
            $filePaths[] = $result['filePath'];
        }


        $result = $this->zipFiles($filePaths, $this->requestData->getOutputPath());
        if (!$result['success']) {
            $this->cleanup($filePaths);
            $this->requestData->setLastErrorMessage($result['message']);
            $this->requestData->setSuccess(false);

            return false;
        }

        $this->cleanup($filePaths);

        $this->requestData->setSuccess(true);

        return  true;
    }

    public function downloadFile($fileName, $fileUrl, $workingDir){
         // Initialize cURL session
        $ch = curl_init($fileUrl);
        // Set cURL options
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        
        // Execute cURL request
        $content = curl_exec($ch);

         // Check for cURL errors
         if (curl_errno($ch)) {
            curl_close($ch);
            return array(
                "success" => FALSE,
                "message" => "Cannot download $fileUrl",
            );
        }

        // Close cURL session
        curl_close($ch);
        
        // Write file in working dir
        $filePath = $workingDir . '/' . $fileName;
        file_put_contents($filePath, $content);
        
        return array(
            "success" => TRUE,
            "filePath" => $filePath,
        );
    }

    public function zipFiles($filePaths, $outputPath){
    
        // Create a zip file
        $zip = new ZipArchive();
    
        if ($zip->open($outputPath, ZipArchive::CREATE) !== TRUE) {
            return array(
                "success" => FALSE,
                "message" => "Cannot create output archive",
            );
        }

        // Add files to the zip archive
        foreach ($filePaths as $filePath) {
            $zip->addFile($filePath, basename($filePath));
        }

        // Close the zip archive
        $zip->close();

        // Return the path of the zip file
        return array(
            "success" => TRUE,
            "zipfilepath" => $outputPath,
        );
    }

    public function cleanup($filePaths) {
        // Clean up the temporary files
        foreach ($filePaths as $filePath) {
            if (file_exists($filePath))
                unlink($filePath);
        }
    }
}