diff --git a/src/FunctionTypeEnumClass.php b/src/FunctionTypeEnumClass.php index 31e7457..3bc4f29 100644 --- a/src/FunctionTypeEnumClass.php +++ b/src/FunctionTypeEnumClass.php @@ -18,6 +18,7 @@ abstract class FunctionTypeEnumClass const TTMERGE = "tt_merge"; const TTUNION = "tt_union"; const TTCONVERT = "tt_convert"; + const DLOBJECT = "download_object"; const PARAMINFO = "param_info"; const USERWSINIT = "user_init"; } diff --git a/src/InputOutput/IHMImpl/DownloadObject/IHMInputOutputDownloadObjectClass.php b/src/InputOutput/IHMImpl/DownloadObject/IHMInputOutputDownloadObjectClass.php new file mode 100644 index 0000000..53f88bf --- /dev/null +++ b/src/InputOutput/IHMImpl/DownloadObject/IHMInputOutputDownloadObjectClass.php @@ -0,0 +1,49 @@ +downloadObjectData = new DownloadObjectRequestDataClass(); + + if($input->nodeType == "download" && $input->downloadSrc == "2") + { + // Set type + $this->downloadObjectData->setType(DownloadObjectTypeEnumClass::ASTROIMG); + + // Set tmp directory + $this->downloadObjectData->setTmpDir(IHMConfigClass::getRequestPath()); + + // Set compression format + if($input->compression == DownloadObjectFormatEnumClass::ZIP) + $this->downloadObjectData->setCompressionFormat(DownloadObjectFormatEnumClass::ZIP); + + // Set list of links to download + if(sizeof($input->list) > 0){ + $this->downloadObjectData->setList($input->list); + } + + } + return $this->downloadObjectData; + + } + public function getOutput($data) + { + $result = NULL; + if($data->getResult() != NULL){ + if($data->getType() == DownloadObjectTypeEnumClass::ASTROIMG && $data->getCompression() == DownloadObjectFormatEnumClass::ZIP){ + + $result = array( + "success" => true, + "download" => $data->getResult() + ); + } + } + return $result; + } +} \ No newline at end of file diff --git a/src/InputOutput/IHMImpl/IHMInputOutputClass.php b/src/InputOutput/IHMImpl/IHMInputOutputClass.php index 99fad99..c8a3d0a 100644 --- a/src/InputOutput/IHMImpl/IHMInputOutputClass.php +++ b/src/InputOutput/IHMImpl/IHMInputOutputClass.php @@ -111,6 +111,9 @@ class IHMInputOutputClass implements InputOutputInterface case FunctionTypeEnumClass::PARAMINFO : $this->inputOutput = new IHMInputOutputParamInfoClass(); break; + case FunctionTypeEnumClass::DLOBJECT : + $this->inputOutput = new IHMInputOutputDownloadObjectClass(); + break; case FunctionTypeEnumClass::USERWSINIT : return $this->userWSMgr->init(); default : diff --git a/src/Request/DownloadObjectRequestClass.php b/src/Request/DownloadObjectRequestClass.php new file mode 100644 index 0000000..5fcaf57 --- /dev/null +++ b/src/Request/DownloadObjectRequestClass.php @@ -0,0 +1,101 @@ +requestData)) + return false; + return true; + } + + public function run(){ + + // Create a temporary directory to store the files + + $tempDir = $this->requestData->getTmpDir().'/curl_files_'. uniqid(); + mkdir($tempDir); + + // Array to hold the paths of the downloaded files + $filePaths = array(); + + $fileList = $this->requestData->getList(); + foreach($fileList as $fileData){ + + $filePaths[] = $this->curlFile($fileData, $tempDir); + } + $zipFilePath = $filePaths[0]; // $this->zipFiles($filePaths, $tempDir); // fonctionne pas pour le moment. + + $basePath = '/home/amda_admin/AMDA/AMDA_IHM/'; + + $zipFilePath = str_replace($basePath, '', $zipFilePath); + $this->requestData->setResult($zipFilePath); + $this->requestData->setSuccess(true); + + return $this->requestData->getSuccess(); + } + + public function curlFile($fileData, $tempDir){ + // Initialize cURL session + $ch = curl_init($fileData->url); + // 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)) { + echo 'cURL error: ' . curl_error($ch); + curl_close($ch); + return false; + } + + // Close cURL session + curl_close($ch); + // Define the file path + $filePath = $tempDir . '/' . basename($fileData->name); + file_put_contents($filePath, $content); + + // Store the file path + return $filePath; + } + + public function zipFiles($filePaths, $tempDir){ + + // Create a zip file + + $zipFilePath = IHMConfigClass::getRequestPath().'/curl_files_'.uniqid() . '.zip'; + $zip = new ZipArchive(); + + if ($zip->open($zipFilePath, ZipArchive::CREATE) !== TRUE) { + exit("Cannot open <$zipFilePath>\n"); + } + + // Add files to the zip archive + foreach ($filePaths as $filePath) { + $zip->addFile($filePath, basename($filePath)); + } + + // Close the zip archive + $zip->close(); + + // Clean up the temporary files + foreach ($filePaths as $filePath) { + unlink($filePath); + } + rmdir($tempDir); + + // Return the path of the zip file + return $zipFilePath; + } +} \ No newline at end of file diff --git a/src/Request/DownloadObjectRequestDataClass.php b/src/Request/DownloadObjectRequestDataClass.php new file mode 100644 index 0000000..2d22caa --- /dev/null +++ b/src/Request/DownloadObjectRequestDataClass.php @@ -0,0 +1,90 @@ +type; + } + + public function setType($type) + { + $this->type = $type; + } + + public function getList() + { + return $this->list; + } + + public function setList($list) + { + $this->list = $list; + } + + public function getTmpDir() + { + return $this->tmpDir; + } + + public function setTmpDir($tmpDir) + { + $this->tmpDir = $tmpDir; + } + + public function getResult() + { + return $this->result; + } + + public function setResult($result) + { + $this->result = $result; + } + + public function getCompressionFormat() + { + return $this->compression; + } + + public function setCompressionFormat($compression) + { + $this->compression = $compression; + } + +} \ No newline at end of file diff --git a/src/RequestManagerClass.php b/src/RequestManagerClass.php index c4f2291..48ed81e 100644 --- a/src/RequestManagerClass.php +++ b/src/RequestManagerClass.php @@ -84,6 +84,8 @@ Class RequestManagerClass return new ParamInfoRequestClass($user, $userHost); case FunctionTypeEnumClass::USERWSINIT : return new UserRequestClass($user, $userHost); + case FunctionTypeEnumClass::DLOBJECT : + return new DownloadObjectRequestClass($user, $userHost); default : throw new Exception('Request '.$function.' not implemented.'); } diff --git a/src/amdaintegration_autoload.php b/src/amdaintegration_autoload.php index db0a059..0df1b8a 100644 --- a/src/amdaintegration_autoload.php +++ b/src/amdaintegration_autoload.php @@ -23,6 +23,7 @@ class AutoloadData { 'InputOutput/IHMImpl/Params/GenInfoParamImpl', 'InputOutput/IHMImpl/Process', 'InputOutput/IHMImpl/ParamInfo', + 'InputOutput/IHMImpl/DownloadObject', 'InputOutput/IHMImpl/Tools', 'InputOutput/IHMImpl/TimeTables', 'InputOutput/TestImpl', -- libgit2 0.21.2