From 79dabdd90af9918888d3452d8da218ed230239d4 Mon Sep 17 00:00:00 2001
From: Armand Bahi <armand.bahi@veremes.com>
Date: Fri, 25 Jan 2019 12:40:30 +0100
Subject: [PATCH] =?UTF-8?q?Cr=C3=A9ation=20de=20miniatures=20lors=20de=20l?=
 =?UTF-8?q?'envoi?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../web_service/ws/Querys.class.inc           |  4 +-
 .../class/vmlib/files/Files_common.class.inc  |  2 +-
 .../class/vmlib/files/Files_manager.class.inc | 67 ++++++++++++++-----
 3 files changed, 52 insertions(+), 21 deletions(-)

diff --git a/src/module_vmap/web_service/ws/Querys.class.inc b/src/module_vmap/web_service/ws/Querys.class.inc
index 0d70ff76..99e92395 100644
--- a/src/module_vmap/web_service/ws/Querys.class.inc
+++ b/src/module_vmap/web_service/ws/Querys.class.inc
@@ -1446,8 +1446,8 @@ class Querys extends Vmap {
                 // Nettoyage de l'espace ws_data
                 $this->oFilesManager->emptyWsDataDir('vitis', $sBusinessObjectId, $this->aValues['my_vitis_id'], $sName, 'documents');
 
-                // Écriture du fichier
-                $aUploadReturn = $this->oFilesManager->uploadInWsDataDir('vitis', $sBusinessObjectId, $this->aValues['my_vitis_id'], $sName, 'documents');
+                // Écriture des fichiers
+                $aUploadReturn = $this->oFilesManager->uploadInWsDataDir('vitis', $sBusinessObjectId, $this->aValues['my_vitis_id'], $sName, 'documents', -1, "all", null, true);
                 $this->aValues[$sName] = $aUploadReturn['field_value'];
             }
         }
diff --git a/src/vitis/vas/rest/class/vmlib/files/Files_common.class.inc b/src/vitis/vas/rest/class/vmlib/files/Files_common.class.inc
index 7931ed74..4e2ee795 100644
--- a/src/vitis/vas/rest/class/vmlib/files/Files_common.class.inc
+++ b/src/vitis/vas/rest/class/vmlib/files/Files_common.class.inc
@@ -17,7 +17,7 @@ abstract class Files_common {
      *@return Retourne une chaine.
      */
     public function extension($sPath) {
-        $aTemp = explode(".", $sString);
+        $aTemp = explode(".", $sPath);
         $sString = strtolower($aTemp[count($aTemp) - 1]);
         return $sString;
     }
diff --git a/src/vitis/vas/rest/class/vmlib/files/Files_manager.class.inc b/src/vitis/vas/rest/class/vmlib/files/Files_manager.class.inc
index 2ff3e6f3..058d6eba 100644
--- a/src/vitis/vas/rest/class/vmlib/files/Files_manager.class.inc
+++ b/src/vitis/vas/rest/class/vmlib/files/Files_manager.class.inc
@@ -135,7 +135,17 @@ class Files_manager{
 
                 // si c'est une image avec les infos de taille final on resample
                 if($sFileType === "image" && (isset($aFileValues["width"]) && isset($aFileValues["height"]))){
+
+                    // Resample l'image
                     $sTmpFile = pictureResampler($sTmpFile, $aFileValues["width"], $aFileValues["height"], 0, 0, 0, 0, $sFileExtension);
+
+                    // Change l'extension de la destination
+                    $sMinFileExtension = $this->extension($sTmpFile);
+                    $aDestPathExt = explode('.', $sServerPath);
+                    array_pop($aDestPathExt);
+                    array_push($aDestPathExt, 'jpg');
+                    $sServerPath = implode('.', $aDestPathExt);
+
                     if(!$sTmpFile){
                         $bAllowUpload = false;
                         writeToErrorLog(ERROR_COPYING_FILE . $aFileValues['name'] . ON_SERVER_PHPUTIL . ', while resampling picture, ' . $sServerPath);
@@ -231,9 +241,10 @@ class Files_manager{
      *@param $iMaxSize Maximum size to upload on server. (set to -1 to disable this control)
      *@param $sFileTypeCtrl Type of the document. (set to all to disable this control)
      *@param $aFileStruct File structure to upload
+     *@param $bCreateThumbnailImage true to create automatically a thumnail image
      *@return $sErrorMsg The error message.
      */
-    public function uploadInWsDataDir($sModule, $sObject, $mId, $sField, $sContainer = "", $iMaxSize = -1, $sFileTypeCtrl = "all", $aFileStruct = null){
+    public function uploadInWsDataDir($sModule, $sObject, $mId, $sField, $sContainer = "", $iMaxSize = -1, $sFileTypeCtrl = "all", $aFileStruct = null, $bCreateThumbnail = false){
 
         // controle les attributs pour éviter les mauvais placements
         if (strpos($sModule, '/') > -1){
@@ -269,9 +280,9 @@ class Files_manager{
         // Fichiers multiples
         if (!empty($aFileStruct[0]['name'])) {
             $aReturn = array();
+            // Appel récursif
             for ($i=0; $i < count($aFileStruct); $i++) {
-                // Appel récursif
-                $aResult = $this->uploadInWsDataDir($sModule, $sObject, $mId, $sField, $sContainer, $iMaxSize, $sFileTypeCtrl, $aFileStruct[$i]);
+                $aResult = $this->uploadInWsDataDir($sModule, $sObject, $mId, $sField, $sContainer, $iMaxSize, $sFileTypeCtrl, $aFileStruct[$i], $bCreateThumbnail);
                 $aReturn['message'][] = $aResult['message'];
                 $aReturn['file_name'][] = $aResult['file_name'];
                 $aReturn['file_path'][] = $aResult['file_path'];
@@ -299,10 +310,12 @@ class Files_manager{
             writeToErrorLog("This function doesn't accept relative reference : " . $sDestPath);
             return "This function doesn't accept relative reference : " . $sDestPath;
         }
+
         // si taille max vaut -1 alors taille max = taille fichier + 1
         if ($iMaxSize == -1){
             $iMaxSize = $aFileStruct["size"] + 1;
         }
+
         // création du fichier si besoin
         if (!is_dir($sDestDir)){
             if(!mkdir($sDestDir, 0777, true)){
@@ -310,14 +323,44 @@ class Files_manager{
                 return 'ERROR_CREATING_DIRECTORY ' . $sDestDir;
             }
         }
+
         // Upload du fichier
         $sMessage = $this->uploadFile($sField, $sFileTypeCtrl, $sDestPath, $iMaxSize, $aFileStruct);
 
+        // Image miniature
+        $sMinDestPath = null;
+        if ($bCreateThumbnail === true && file_exists($sDestPath)) {
+
+            // Si image
+            if(@is_array(getimagesize($sDestPath))){
+
+                $aThumbnailFileStruct = $aFileStruct;
+                $aThumbnailFileStruct["width"] = 200;
+                $aThumbnailFileStruct["height"] = 200;
+                $aThumbnailFileStruct['tmp_name'] = $aThumbnailFileStruct['tmp_name'] . '.min';
+                $aDestPathExt = explode('.', $sDestPath);
+
+                // Ajoute l'extension à l'image
+                $aThumbnailFileStruct['tmp_name'] .= '.' . end($aDestPathExt);
+
+                // Ajoute .min sur la destination
+                array_splice($aDestPathExt, -1, 0, 'min');
+                $sMinDestPath = implode('.', $aDestPathExt);
+
+                // Copie le fichier dans tmp
+                copy($sDestPath, $aThumbnailFileStruct['tmp_name']);
+
+                // Upload le fichier .min.jpg
+                $this->uploadFile($sField, "image", $sMinDestPath, $iMaxSize, $aThumbnailFileStruct);
+            }
+        }
+
         return array(
             'message' => $sMessage,
             'field_value' => $aFileStruct["name"],
             'file_name' => $aFileStruct["name"],
-            'file_path' => $sDestPath
+            'file_path' => $sDestPath,
+            'file_min_path' => $sMinDestPath
         );
     }
 
@@ -462,11 +505,6 @@ class Files_manager{
             return "Container can't contain path : "  . $sContainer;
         }
 
-        // Génère la Structure fichier
-        if ($aFileStruct == null) {
-            $aFileStruct = $this->extractFileStruct($sField);
-        }
-
         // Génère la destination
         $sDestDir = $this->oProperties['ws_data_dir'] . "/" . $sModule . "/" . $sObject;
 
@@ -479,17 +517,10 @@ class Files_manager{
         if(!empty($sField)){
             $sDestDir .= "/" . $sField;
         }
-        $sDestPath =  $sDestDir . "/" . $aFileStruct["name"];
-
-        // controle la destination pour éviter les mauvais placements
-        if (strpos($sDestPath, "/\.\./") > -1){
-            writeToErrorLog("This function doesn't accept relative reference : " . $sDestPath);
-            return "This function doesn't accept relative reference : " . $sDestPath;
-        }
 
         // création du fichier si besoin
-        if (is_dir($sDestPath)){
-            $files = glob($sDestPath . '/*'); // get all file names
+        if (is_dir($sDestDir)){
+            $files = glob($sDestDir . '/*'); // get all file names
             foreach($files as $file){ // iterate files
                 if(is_file($file)){
                     unlink($file); // delete file
-- 
GitLab