Commit 554f1fae389bf0946ae7c671258da6b6e68ce461

Authored by Erdogan Furkan
1 parent 8a8df0ca

#6265 - Zip todo

src/FunctionTypeEnumClass.php
... ... @@ -18,6 +18,7 @@ abstract class FunctionTypeEnumClass
18 18 const TTMERGE = "tt_merge";
19 19 const TTUNION = "tt_union";
20 20 const TTCONVERT = "tt_convert";
  21 + const DLOBJECT = "download_object";
21 22 const PARAMINFO = "param_info";
22 23 const USERWSINIT = "user_init";
23 24 }
... ...
src/InputOutput/IHMImpl/DownloadObject/IHMInputOutputDownloadObjectClass.php 0 → 100644
... ... @@ -0,0 +1,49 @@
  1 +<?php
  2 +
  3 +/**
  4 + * @class IHMInputOutputDownloadObjectClass
  5 + * @brief Class that's implement an InputOutputInterface used to treat a get download object request
  6 + * @details
  7 + */
  8 +class IHMInputOutputDownloadObjectClass implements InputOutputInterface
  9 +{
  10 + public function getInputData($input, $function, $requestId = ""){
  11 +
  12 + $this->downloadObjectData = new DownloadObjectRequestDataClass();
  13 +
  14 + if($input->nodeType == "download" && $input->downloadSrc == "2")
  15 + {
  16 + // Set type
  17 + $this->downloadObjectData->setType(DownloadObjectTypeEnumClass::ASTROIMG);
  18 +
  19 + // Set tmp directory
  20 + $this->downloadObjectData->setTmpDir(IHMConfigClass::getRequestPath());
  21 +
  22 + // Set compression format
  23 + if($input->compression == DownloadObjectFormatEnumClass::ZIP)
  24 + $this->downloadObjectData->setCompressionFormat(DownloadObjectFormatEnumClass::ZIP);
  25 +
  26 + // Set list of links to download
  27 + if(sizeof($input->list) > 0){
  28 + $this->downloadObjectData->setList($input->list);
  29 + }
  30 +
  31 + }
  32 + return $this->downloadObjectData;
  33 +
  34 + }
  35 + public function getOutput($data)
  36 + {
  37 + $result = NULL;
  38 + if($data->getResult() != NULL){
  39 + if($data->getType() == DownloadObjectTypeEnumClass::ASTROIMG && $data->getCompression() == DownloadObjectFormatEnumClass::ZIP){
  40 +
  41 + $result = array(
  42 + "success" => true,
  43 + "download" => $data->getResult()
  44 + );
  45 + }
  46 + }
  47 + return $result;
  48 + }
  49 +}
0 50 \ No newline at end of file
... ...
src/InputOutput/IHMImpl/IHMInputOutputClass.php
... ... @@ -111,6 +111,9 @@ class IHMInputOutputClass implements InputOutputInterface
111 111 case FunctionTypeEnumClass::PARAMINFO :
112 112 $this->inputOutput = new IHMInputOutputParamInfoClass();
113 113 break;
  114 + case FunctionTypeEnumClass::DLOBJECT :
  115 + $this->inputOutput = new IHMInputOutputDownloadObjectClass();
  116 + break;
114 117 case FunctionTypeEnumClass::USERWSINIT :
115 118 return $this->userWSMgr->init();
116 119 default :
... ...
src/Request/DownloadObjectRequestClass.php 0 → 100644
... ... @@ -0,0 +1,101 @@
  1 +<?php
  2 +
  3 +/**
  4 + * @class DownloadObjectRequestClass
  5 + * @brief Class used for a download request
  6 + * @details
  7 + */
  8 +class DownloadObjectRequestClass extends RequestAbstractClass
  9 +{
  10 +
  11 + /*
  12 + * @brief Init a donwload object request
  13 + */
  14 + public function init(){
  15 + if (!isset($this->requestData))
  16 + return false;
  17 + return true;
  18 + }
  19 +
  20 + public function run(){
  21 +
  22 + // Create a temporary directory to store the files
  23 +
  24 + $tempDir = $this->requestData->getTmpDir().'/curl_files_'. uniqid();
  25 + mkdir($tempDir);
  26 +
  27 + // Array to hold the paths of the downloaded files
  28 + $filePaths = array();
  29 +
  30 + $fileList = $this->requestData->getList();
  31 + foreach($fileList as $fileData){
  32 +
  33 + $filePaths[] = $this->curlFile($fileData, $tempDir);
  34 + }
  35 + $zipFilePath = $filePaths[0]; // $this->zipFiles($filePaths, $tempDir); // fonctionne pas pour le moment.
  36 +
  37 + $basePath = '/home/amda_admin/AMDA/AMDA_IHM/';
  38 +
  39 + $zipFilePath = str_replace($basePath, '', $zipFilePath);
  40 + $this->requestData->setResult($zipFilePath);
  41 + $this->requestData->setSuccess(true);
  42 +
  43 + return $this->requestData->getSuccess();
  44 + }
  45 +
  46 + public function curlFile($fileData, $tempDir){
  47 + // Initialize cURL session
  48 + $ch = curl_init($fileData->url);
  49 + // Set cURL options
  50 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  51 + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  52 +
  53 + // Execute cURL request
  54 + $content = curl_exec($ch);
  55 +
  56 + // Check for cURL errors
  57 + if (curl_errno($ch)) {
  58 + echo 'cURL error: ' . curl_error($ch);
  59 + curl_close($ch);
  60 + return false;
  61 + }
  62 +
  63 + // Close cURL session
  64 + curl_close($ch);
  65 + // Define the file path
  66 + $filePath = $tempDir . '/' . basename($fileData->name);
  67 + file_put_contents($filePath, $content);
  68 +
  69 + // Store the file path
  70 + return $filePath;
  71 + }
  72 +
  73 + public function zipFiles($filePaths, $tempDir){
  74 +
  75 + // Create a zip file
  76 +
  77 + $zipFilePath = IHMConfigClass::getRequestPath().'/curl_files_'.uniqid() . '.zip';
  78 + $zip = new ZipArchive();
  79 +
  80 + if ($zip->open($zipFilePath, ZipArchive::CREATE) !== TRUE) {
  81 + exit("Cannot open <$zipFilePath>\n");
  82 + }
  83 +
  84 + // Add files to the zip archive
  85 + foreach ($filePaths as $filePath) {
  86 + $zip->addFile($filePath, basename($filePath));
  87 + }
  88 +
  89 + // Close the zip archive
  90 + $zip->close();
  91 +
  92 + // Clean up the temporary files
  93 + foreach ($filePaths as $filePath) {
  94 + unlink($filePath);
  95 + }
  96 + rmdir($tempDir);
  97 +
  98 + // Return the path of the zip file
  99 + return $zipFilePath;
  100 + }
  101 +}
0 102 \ No newline at end of file
... ...
src/Request/DownloadObjectRequestDataClass.php 0 → 100644
... ... @@ -0,0 +1,90 @@
  1 +<?php
  2 +
  3 +/**
  4 + * @class DownloadObjectTypeEnumClass
  5 + * @brief Enumerate for download request type
  6 + * @details
  7 + */
  8 +abstract class DownloadObjectTypeEnumClass
  9 +{
  10 + const UNKNOWN = "";
  11 + const ASTROIMG = "astro_image";
  12 +}
  13 +
  14 +/**
  15 + * @class DownloadObjectFormatEnumClass
  16 + * @brief Enumerate for download extention request type
  17 + * @details
  18 + */
  19 +abstract class DownloadObjectFormatEnumClass
  20 +{
  21 + const UNKNOWN = "";
  22 + const ZIP = "zip";
  23 +}
  24 +
  25 +
  26 +/**
  27 + * @class DownloadObjectRequestDataClass
  28 + * @brief Data for a download request
  29 + * @details
  30 + */
  31 +class DownloadObjectRequestDataClass extends RequestDataClass
  32 +{
  33 + private $type = DownloadObjectTypeEnumClass::UNKNOWN;
  34 + private $list = NULL;
  35 + private $compression = DownloadObjectFormatEnumClass::ZIP;
  36 + private $tmpDir = "";
  37 + private $result = NULL;
  38 +
  39 +
  40 + public function getType()
  41 + {
  42 + return $this->type;
  43 + }
  44 +
  45 + public function setType($type)
  46 + {
  47 + $this->type = $type;
  48 + }
  49 +
  50 + public function getList()
  51 + {
  52 + return $this->list;
  53 + }
  54 +
  55 + public function setList($list)
  56 + {
  57 + $this->list = $list;
  58 + }
  59 +
  60 + public function getTmpDir()
  61 + {
  62 + return $this->tmpDir;
  63 + }
  64 +
  65 + public function setTmpDir($tmpDir)
  66 + {
  67 + $this->tmpDir = $tmpDir;
  68 + }
  69 +
  70 + public function getResult()
  71 + {
  72 + return $this->result;
  73 + }
  74 +
  75 + public function setResult($result)
  76 + {
  77 + $this->result = $result;
  78 + }
  79 +
  80 + public function getCompressionFormat()
  81 + {
  82 + return $this->compression;
  83 + }
  84 +
  85 + public function setCompressionFormat($compression)
  86 + {
  87 + $this->compression = $compression;
  88 + }
  89 +
  90 +}
0 91 \ No newline at end of file
... ...
src/RequestManagerClass.php
... ... @@ -84,6 +84,8 @@ Class RequestManagerClass
84 84 return new ParamInfoRequestClass($user, $userHost);
85 85 case FunctionTypeEnumClass::USERWSINIT :
86 86 return new UserRequestClass($user, $userHost);
  87 + case FunctionTypeEnumClass::DLOBJECT :
  88 + return new DownloadObjectRequestClass($user, $userHost);
87 89 default :
88 90 throw new Exception('Request '.$function.' not implemented.');
89 91 }
... ...
src/amdaintegration_autoload.php
... ... @@ -23,6 +23,7 @@ class AutoloadData {
23 23 'InputOutput/IHMImpl/Params/GenInfoParamImpl',
24 24 'InputOutput/IHMImpl/Process',
25 25 'InputOutput/IHMImpl/ParamInfo',
  26 + 'InputOutput/IHMImpl/DownloadObject',
26 27 'InputOutput/IHMImpl/Tools',
27 28 'InputOutput/IHMImpl/TimeTables',
28 29 'InputOutput/TestImpl',
... ...