From d55d241f1864b037d6da135b1f72bb3bd67b2a2d Mon Sep 17 00:00:00 2001
From: Anthony Borghi <anthony.borghi@veremes.com>
Date: Fri, 19 Oct 2018 11:16:10 +0200
Subject: [PATCH] =?UTF-8?q?Modification=20du=20g=C3=A9n=C3=A9rateur=20d'UR?=
 =?UTF-8?q?L=20de=20Vitis=20pour=20utiliser=20le=20File=5FDownloader?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 vas/rest/ws/vitis/Vitis.class.inc | 75 +++++++++++++++++++++++++++----
 1 file changed, 66 insertions(+), 9 deletions(-)

diff --git a/vas/rest/ws/vitis/Vitis.class.inc b/vas/rest/ws/vitis/Vitis.class.inc
index 8a8bb421..e6022e65 100644
--- a/vas/rest/ws/vitis/Vitis.class.inc
+++ b/vas/rest/ws/vitis/Vitis.class.inc
@@ -1,6 +1,7 @@
 <?php
 
 require_once(__DIR__ . "/../../class/vitis_lib/DbClass.class.inc");
+require_once ("aws_sdk/aws-autoloader.php");
 
 /**
  * \file vitis.class.inc
@@ -209,7 +210,7 @@ class Vitis extends DbClass {
      * @param $sVitisObjectName Name of the vitis object, if provide and the object contains files stored in ws_data/vitis/$sVitisObjectName the values will be url formed
      * @return the array of objects
      */
-    function genericGet($sSchema, $sTable, $sIdField, $bOnlyReturnStatus = false, $sVitisObjectName = "") {
+    function genericGet($sSchema, $sTable, $sIdField, $bOnlyReturnStatus = false, $sVitisObjectName = "", $sVitisModuleName = "vitis", $sVitisPathComplement = "documents") {
         if (!empty($this->aProperties['ws_data_alias']))
             $ws_data_alias = $this->aProperties['ws_data_alias'];
         else
@@ -249,15 +250,71 @@ class Vitis extends DbClass {
 
                     // Vérifie si il y a des fichiers à renvoyer
                     if ($sVitisObjectName != "") {
-                        $sDataDir = $this->aProperties['ws_data_dir'] . '/vitis/' . $sVitisObjectName . '/documents/' . $oObject->aFields[$sIdField];
-                        $sDataUrl = $this->aProperties['web_server_name'] . "/" . $ws_data_alias . "/vitis/" . $sVitisObjectName . "/documents/" . $oObject->aFields[$sIdField];
-                        if (is_dir($sDataDir)) {
-                            // Remplace le nom du fichier par son url
-                            foreach ($oObject->aFields as $key => $value) {
-                                if (is_dir($sDataDir . "/" . $key)) {
-                                    $oObject->aFields[$key] = $sDataUrl . "/" . $key . "/" . $value . "?d=" . $date->getTimestamp();
-                                }
+                        $sDataUrl = $this->aProperties['web_server_name'] . "/rest/vitis/file_downloader?key=[KEY]&eTag=[ETAG]&d=" . $date->getTimestamp();
+
+                        if($this->aProperties['fileS3Uploader']){
+                          // use HEAD on s3 Object
+                          // traitement du bucket et de ses sous-dossiers
+                          $sBucket = $this->aProperties['fileS3UploaderBucket'];
+                          $sPrefix = "";
+
+                          if (strpos($sBucket, "/") > -1){
+                              $aBucket = explode("/", $sBucket );
+                              $sBucket = $aBucket[0];
+                              $sPrefix = implode("/", array_slice($aBucket, 1));
+                          }
+                          // Création du client S3
+                          $s3 = new Aws\S3\S3Client(array(
+                              'version'=>'latest',
+                              'region'=> $this->aProperties['fileS3UploaderRegion'],
+                              'profile'=> $this->aProperties['fileS3UploaderProfil'],
+                              'debug' => false
+                          ));
+
+                          $sListPrefix = $sPrefix . "/ws_data/".$sVitisModuleName.'/' . $sVitisObjectName . '/';
+
+                          if (!empty($sVitisPathComplement)){
+                            $sListPrefix .= $sVitisPathComplement . '/';
+                          }
+
+                          $sListPrefix .= $oObject->aFields[$sIdField];
+
+                          try{
+                            $aList = $s3->listObjectsV2(array(
+                              'Bucket' => $sBucket,
+                              'Prefix' => $sListPrefix
+                            ));
+
+
+
+                            if ($aList["KeyCount"] > 0){
+                              foreach ($aList["Contents"] as $aObject){
+                                $aPath = explode("/", $aObject["Key"]);
+                                $sObjectField = $aPath[count($aPath) - 2];
+
+                                $sFileUrl = str_replace("[KEY]", $aObject["Key"], $sDataUrl);
+                                $oObject->aFields[$sObjectField] = str_replace("[ETAG]", str_replace('"', '', $aObject["ETag"]), $sFileUrl);
+                              }
                             }
+
+                          }catch(Aws\S3\Exception\S3Exception $e){
+                            writeToErrorLog($e->getMessage());
+                          }
+                        } else {
+                          // check dans ws_data
+                          $sDataDir = $this->aProperties['ws_data_dir'] . '/'.$sVitisModuleName.'/' . $sVitisObjectName . '/' . $sVitisPathComplement . '/' . $oObject->aFields[$sIdField];
+                          if (is_dir($sDataDir)) {
+                              // Remplace le nom du fichier par son url
+                              foreach ($oObject->aFields as $key => $value) {
+                                  if (is_dir($sDataDir . "/" . $key)) {
+                                      $sKey = "ws_data/".$sVitisModuleName.'/' . $sVitisObjectName . '/' . $sVitisPathComplement . '/' . $oObject->aFields[$sIdField]. "/" . $key . "/" . $value;
+                                      $sEtag = sha1(file_get_contents($sDataDir . "/" . $key . "/" . $value ), false);
+
+                                      $sFileUrl = str_replace("[KEY]", $sKey, $sDataUrl);
+                                      $oObject->aFields[$key] = str_replace("[ETAG]", $sEtag, $sFileUrl);
+                                  }
+                              }
+                          }
                         }
                     }
                     array_push($this->aObjects, $oObject);
-- 
GitLab