Commit 7267aa6bc9f49c3619d01750ea4110e3dd309d25

Authored by Benjamin Renard
1 parent 554f1fae

Finalize download object request

src/InputOutput/IHMImpl/DownloadObject/IHMInputOutputDownloadObjectClass.php
... ... @@ -7,7 +7,30 @@
7 7 */
8 8 class IHMInputOutputDownloadObjectClass implements InputOutputInterface
9 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 +
10 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 + }
11 34  
12 35 $this->downloadObjectData = new DownloadObjectRequestDataClass();
13 36  
... ... @@ -15,35 +38,53 @@ class IHMInputOutputDownloadObjectClass implements InputOutputInterface
15 38 {
16 39 // Set type
17 40 $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   -
  41 +
  42 +
26 43 // Set list of links to download
27 44 if(sizeof($input->list) > 0){
28   - $this->downloadObjectData->setList($input->list);
  45 + foreach ($input->list as $file) {
  46 + $this->downloadObjectData->addFile($file->name, $file->url);
  47 + }
29 48 }
30 49  
31 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 +
32 75 return $this->downloadObjectData;
33 76  
34 77 }
35 78 public function getOutput($data)
36 79 {
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;
  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 + );
48 89 }
49 90 }
50 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();
... ...
src/Request/DownloadObjectRequestClass.php
... ... @@ -14,38 +14,48 @@ class DownloadObjectRequestClass extends RequestAbstractClass
14 14 public function init(){
15 15 if (!isset($this->requestData))
16 16 return false;
  17 + if (!is_dir($this->requestData->getWorkingPath())) {
  18 + mkdir($this->requestData->getWorkingPath());
  19 + }
17 20 return true;
18 21 }
19 22  
20 23 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 24 // Array to hold the paths of the downloaded files
28 25 $filePaths = array();
29 26  
30   - $fileList = $this->requestData->getList();
31   - foreach($fileList as $fileData){
  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 +
32 39  
33   - $filePaths[] = $this->curlFile($fileData, $tempDir);
  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;
34 47 }
35   - $zipFilePath = $filePaths[0]; // $this->zipFiles($filePaths, $tempDir); // fonctionne pas pour le moment.
36 48  
37   - $basePath = '/home/amda_admin/AMDA/AMDA_IHM/';
  49 + $this->cleanup($filePaths);
38 50  
39   - $zipFilePath = str_replace($basePath, '', $zipFilePath);
40   - $this->requestData->setResult($zipFilePath);
41 51 $this->requestData->setSuccess(true);
42 52  
43   - return $this->requestData->getSuccess();
  53 + return true;
44 54 }
45 55  
46   - public function curlFile($fileData, $tempDir){
  56 + public function downloadFile($fileName, $fileUrl, $workingDir){
47 57 // Initialize cURL session
48   - $ch = curl_init($fileData->url);
  58 + $ch = curl_init($fileUrl);
49 59 // Set cURL options
50 60 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
51 61 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
... ... @@ -55,47 +65,58 @@ class DownloadObjectRequestClass extends RequestAbstractClass
55 65  
56 66 // Check for cURL errors
57 67 if (curl_errno($ch)) {
58   - echo 'cURL error: ' . curl_error($ch);
59 68 curl_close($ch);
60   - return false;
  69 + return array(
  70 + "success" => FALSE,
  71 + "message" => "Cannot download $fileUrl",
  72 + );
61 73 }
62 74  
63   - // Close cURL session
64   - curl_close($ch);
65   - // Define the file path
66   - $filePath = $tempDir . '/' . basename($fileData->name);
  75 + // Close cURL session
  76 + curl_close($ch);
  77 +
  78 + // Write file in working dir
  79 + $filePath = $workingDir . '/' . $fileName;
67 80 file_put_contents($filePath, $content);
68 81  
69   - // Store the file path
70   - return $filePath;
  82 + return array(
  83 + "success" => TRUE,
  84 + "filePath" => $filePath,
  85 + );
71 86 }
72 87  
73   - public function zipFiles($filePaths, $tempDir){
  88 + public function zipFiles($filePaths, $outputPath){
74 89  
75 90 // Create a zip file
  91 + $zip = new ZipArchive();
76 92  
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   - }
  93 + if ($zip->open($outputPath, ZipArchive::CREATE) !== TRUE) {
  94 + return array(
  95 + "success" => FALSE,
  96 + "message" => "Cannot create output archive",
  97 + );
  98 + }
83 99  
84   - // Add files to the zip archive
85   - foreach ($filePaths as $filePath) {
86   - $zip->addFile($filePath, basename($filePath));
87   - }
  100 + // Add files to the zip archive
  101 + foreach ($filePaths as $filePath) {
  102 + $zip->addFile($filePath, basename($filePath));
  103 + }
88 104  
89   - // Close the zip archive
90   - $zip->close();
  105 + // Close the zip archive
  106 + $zip->close();
91 107  
92   - // Clean up the temporary files
93   - foreach ($filePaths as $filePath) {
94   - unlink($filePath);
  108 + // Return the path of the zip file
  109 + return array(
  110 + "success" => TRUE,
  111 + "zipfilepath" => $outputPath,
  112 + );
95 113 }
96   - rmdir($tempDir);
97 114  
98   - // Return the path of the zip file
99   - return $zipFilePath;
  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 + }
100 121 }
101 122 }
102 123 \ No newline at end of file
... ...
src/Request/DownloadObjectRequestDataClass.php
... ... @@ -30,11 +30,11 @@ abstract class DownloadObjectFormatEnumClass
30 30 */
31 31 class DownloadObjectRequestDataClass extends RequestDataClass
32 32 {
33   - private $type = DownloadObjectTypeEnumClass::UNKNOWN;
34   - private $list = NULL;
  33 + private $type = DownloadObjectTypeEnumClass::UNKNOWN;
  34 + private $files = array();
35 35 private $compression = DownloadObjectFormatEnumClass::ZIP;
36   - private $tmpDir = "";
37   - private $result = NULL;
  36 + private $workingPath = "";
  37 + private $outputPath = "";
38 38  
39 39  
40 40 public function getType()
... ... @@ -47,34 +47,34 @@ class DownloadObjectRequestDataClass extends RequestDataClass
47 47 $this->type = $type;
48 48 }
49 49  
50   - public function getList()
  50 + public function getFiles()
51 51 {
52   - return $this->list;
  52 + return $this->files;
53 53 }
54 54  
55   - public function setList($list)
  55 + public function addFile($name, $url)
56 56 {
57   - $this->list = $list;
  57 + $this->files[$name] = $url;
58 58 }
59 59  
60   - public function getTmpDir()
  60 + public function getWorkingPath()
61 61 {
62   - return $this->tmpDir;
  62 + return $this->workingPath;
63 63 }
64 64  
65   - public function setTmpDir($tmpDir)
  65 + public function setWorkingPath($workingPath)
66 66 {
67   - $this->tmpDir = $tmpDir;
  67 + $this->workingPath = $workingPath;
68 68 }
69 69  
70   - public function getResult()
  70 + public function getOutputPath()
71 71 {
72   - return $this->result;
  72 + return $this->outputPath;
73 73 }
74 74  
75   - public function setResult($result)
  75 + public function setOutputPath($outputPath)
76 76 {
77   - $this->result = $result;
  77 + $this->outputPath = $outputPath;
78 78 }
79 79  
80 80 public function getCompressionFormat()
... ...