Commit b3f527494ae439e1c9d41d6b13cfb70b9a37a672

Authored by Benjamin Renard
2 parents 5dd3aab7 7267aa6b
Exists in amdadev and in 1 other branch SpeasyGet

Merge branch 'FER_6265' into amdadev

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,90 @@
  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 + /*
  11 + * @brief Get working dir name
  12 + */
  13 + protected function getWorkingDirName()
  14 + {
  15 + return $this->requestDirPrefix."DownloadData_";
  16 + }
  17 +
  18 + /*
  19 + * @brief Get full working dir path
  20 + */
  21 + protected function getWorkingPath()
  22 + {
  23 + return IHMConfigClass::getRequestPath().$this->getWorkingDirName().'/';
  24 + }
  25 +
  26 + public function getInputData($input, $function, $requestId = ""){
  27 + if (is_dir(IHMConfigClass::getDownloadTmpPath())) {
  28 + foreach (glob(IHMConfigClass::getDownloadTmpPath().'*.zip') as $filename) unlink($filename);
  29 + foreach (glob(IHMConfigClass::getDownloadTmpPath().'*.tar.gz') as $filename) unlink($filename);
  30 + }
  31 + else {
  32 + mkdir(IHMConfigClass::getDownloadTmpPath());
  33 + }
  34 +
  35 + $this->downloadObjectData = new DownloadObjectRequestDataClass();
  36 +
  37 + if($input->nodeType == "download" && $input->downloadSrc == "2")
  38 + {
  39 + // Set type
  40 + $this->downloadObjectData->setType(DownloadObjectTypeEnumClass::ASTROIMG);
  41 +
  42 +
  43 + // Set list of links to download
  44 + if(sizeof($input->list) > 0){
  45 + foreach ($input->list as $file) {
  46 + $this->downloadObjectData->addFile($file->name, $file->url);
  47 + }
  48 + }
  49 +
  50 + }
  51 + else {
  52 + throw new Exception('Unknown download data type.');
  53 + }
  54 +
  55 + $outputFileName = "".$this->downloadObjectData->getType()."_".time();
  56 +
  57 + // Set working dir
  58 + $this->downloadObjectData->setWorkingPath($this->getWorkingPath());
  59 +
  60 + // Set compression format
  61 + switch ($input->compression) {
  62 + case DownloadObjectFormatEnumClass::ZIP:
  63 + $this->downloadObjectData->setCompressionFormat(DownloadObjectFormatEnumClass::ZIP);
  64 + $extension = ".zip";
  65 + break;
  66 + default:
  67 + throw new Exception('Unknown compression format type.');
  68 + }
  69 +
  70 +
  71 + // Set output
  72 + $outputPath = IHMConfigClass::getDownloadTmpPath() . $outputFileName . $extension;
  73 + $this->downloadObjectData->setOutputPath($outputPath);
  74 +
  75 + return $this->downloadObjectData;
  76 +
  77 + }
  78 + public function getOutput($data)
  79 + {
  80 + if (!$data->getSuccess())
  81 + return array(
  82 + 'success' => false,
  83 + 'message' => $data->getLastErrorMessage());
  84 +
  85 + return array(
  86 + 'success' => true,
  87 + 'download' => str_replace(IHM_SRC_DIR, '', $data->getOutputPath()),
  88 + );
  89 + }
  90 +}
0 91 \ No newline at end of file
... ...
src/InputOutput/IHMImpl/IHMInputOutputClass.php
... ... @@ -71,10 +71,10 @@ class IHMInputOutputClass implements InputOutputInterface
71 71 $this->inputOutput = new IHMInputOutputParamsGeneratorClass();
72 72 $requestId = "ParamGen";
73 73 break;
74   - case FunctionTypeEnumClass::PARAMSINFOGEN :
75   - $this->inputOutput = new IHMInputOutputParamsInfoGeneratorClass();
76   - $requestId = "ParamInfoGen";
77   - break;
  74 + case FunctionTypeEnumClass::PARAMSINFOGEN :
  75 + $this->inputOutput = new IHMInputOutputParamsInfoGeneratorClass();
  76 + $requestId = "ParamInfoGen";
  77 + break;
78 78 case FunctionTypeEnumClass::ACTION :
79 79 if (!$input->action->multiplot) {
80 80 $this->inputOutput = new IHMInputOutputParamsPlotClass();
... ... @@ -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,122 @@
  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 + if (!is_dir($this->requestData->getWorkingPath())) {
  18 + mkdir($this->requestData->getWorkingPath());
  19 + }
  20 + return true;
  21 + }
  22 +
  23 + public function run(){
  24 + // Array to hold the paths of the downloaded files
  25 + $filePaths = array();
  26 +
  27 + $files = $this->requestData->getFiles();
  28 + foreach($files as $fileName => $fileUrl){
  29 + $result = $this->downloadFile($fileName, $fileUrl, $this->requestData->getWorkingPath());
  30 + if (!$result['success']) {
  31 + $this->cleanup($filePaths);
  32 + $this->requestData->setLastErrorMessage($result['message']);
  33 + $this->requestData->setSuccess(false);
  34 + return false;
  35 + }
  36 + $filePaths[] = $result['filePath'];
  37 + }
  38 +
  39 +
  40 + $result = $this->zipFiles($filePaths, $this->requestData->getOutputPath());
  41 + if (!$result['success']) {
  42 + $this->cleanup($filePaths);
  43 + $this->requestData->setLastErrorMessage($result['message']);
  44 + $this->requestData->setSuccess(false);
  45 +
  46 + return false;
  47 + }
  48 +
  49 + $this->cleanup($filePaths);
  50 +
  51 + $this->requestData->setSuccess(true);
  52 +
  53 + return true;
  54 + }
  55 +
  56 + public function downloadFile($fileName, $fileUrl, $workingDir){
  57 + // Initialize cURL session
  58 + $ch = curl_init($fileUrl);
  59 + // Set cURL options
  60 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  61 + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  62 +
  63 + // Execute cURL request
  64 + $content = curl_exec($ch);
  65 +
  66 + // Check for cURL errors
  67 + if (curl_errno($ch)) {
  68 + curl_close($ch);
  69 + return array(
  70 + "success" => FALSE,
  71 + "message" => "Cannot download $fileUrl",
  72 + );
  73 + }
  74 +
  75 + // Close cURL session
  76 + curl_close($ch);
  77 +
  78 + // Write file in working dir
  79 + $filePath = $workingDir . '/' . $fileName;
  80 + file_put_contents($filePath, $content);
  81 +
  82 + return array(
  83 + "success" => TRUE,
  84 + "filePath" => $filePath,
  85 + );
  86 + }
  87 +
  88 + public function zipFiles($filePaths, $outputPath){
  89 +
  90 + // Create a zip file
  91 + $zip = new ZipArchive();
  92 +
  93 + if ($zip->open($outputPath, ZipArchive::CREATE) !== TRUE) {
  94 + return array(
  95 + "success" => FALSE,
  96 + "message" => "Cannot create output archive",
  97 + );
  98 + }
  99 +
  100 + // Add files to the zip archive
  101 + foreach ($filePaths as $filePath) {
  102 + $zip->addFile($filePath, basename($filePath));
  103 + }
  104 +
  105 + // Close the zip archive
  106 + $zip->close();
  107 +
  108 + // Return the path of the zip file
  109 + return array(
  110 + "success" => TRUE,
  111 + "zipfilepath" => $outputPath,
  112 + );
  113 + }
  114 +
  115 + public function cleanup($filePaths) {
  116 + // Clean up the temporary files
  117 + foreach ($filePaths as $filePath) {
  118 + if (file_exists($filePath))
  119 + unlink($filePath);
  120 + }
  121 + }
  122 +}
0 123 \ 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 $files = array();
  35 + private $compression = DownloadObjectFormatEnumClass::ZIP;
  36 + private $workingPath = "";
  37 + private $outputPath = "";
  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 getFiles()
  51 + {
  52 + return $this->files;
  53 + }
  54 +
  55 + public function addFile($name, $url)
  56 + {
  57 + $this->files[$name] = $url;
  58 + }
  59 +
  60 + public function getWorkingPath()
  61 + {
  62 + return $this->workingPath;
  63 + }
  64 +
  65 + public function setWorkingPath($workingPath)
  66 + {
  67 + $this->workingPath = $workingPath;
  68 + }
  69 +
  70 + public function getOutputPath()
  71 + {
  72 + return $this->outputPath;
  73 + }
  74 +
  75 + public function setOutputPath($outputPath)
  76 + {
  77 + $this->outputPath = $outputPath;
  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',
... ...