From cf7c2b913e4a46073746b63544db00545f1a1042 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Carretero?=
 <frederic.carretero@veremes.com>
Date: Fri, 16 Nov 2018 15:09:37 +0100
Subject: [PATCH] =?UTF-8?q?createEmptyFileInWsDataDir()=20->=20Cr=C3=A9ati?=
 =?UTF-8?q?on=20d'un=20fichier=20vide=20dans=20ws=5Fdata.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 vas/rest/class/vmlib/lang_vmlib/en-lang.inc |  1 +
 vas/rest/class/vmlib/lang_vmlib/fr-lang.inc |  1 +
 vas/rest/class/vmlib/phpUtil.inc            | 60 +++++++++++++++++++++
 3 files changed, 62 insertions(+)

diff --git a/vas/rest/class/vmlib/lang_vmlib/en-lang.inc b/vas/rest/class/vmlib/lang_vmlib/en-lang.inc
index 44a182f3..1c43c89f 100755
--- a/vas/rest/class/vmlib/lang_vmlib/en-lang.inc
+++ b/vas/rest/class/vmlib/lang_vmlib/en-lang.inc
@@ -102,6 +102,7 @@ define('ERROR_MONTH_CODE', 'The month code "');
 define('ERROR_NUMBER_PHPUTIL', 'It can\'t be a number.');
 define('ERROR_WEEK_CODE', 'The day code of the week "');
 define('ERROR_CODE_3_VALUES', 'The code does not contain three values separated by a space.');
+define('ERROR_CREATING_DIRECTORY', 'Error when creating the directory ');
 
 define('YES', 'Yes');
 define('NO', 'No');
diff --git a/vas/rest/class/vmlib/lang_vmlib/fr-lang.inc b/vas/rest/class/vmlib/lang_vmlib/fr-lang.inc
index 36e37795..06b60838 100755
--- a/vas/rest/class/vmlib/lang_vmlib/fr-lang.inc
+++ b/vas/rest/class/vmlib/lang_vmlib/fr-lang.inc
@@ -102,6 +102,7 @@ define('ERROR_MONTH_CODE', 'Le code du mois "');
 define('ERROR_NUMBER_PHPUTIL', 'Il ne peut pas être un nombre.');
 define('ERROR_WEEK_CODE', 'Le code du jour de la semaine "');
 define('ERROR_CODE_3_VALUES', 'Le code ne contient pas trois valeurs séparées par un espace.');
+define('ERROR_CREATING_DIRECTORY', 'Erreur lors de la création du répertoire ');
 
 define('YES', 'Oui');
 define('NO', 'Non');
diff --git a/vas/rest/class/vmlib/phpUtil.inc b/vas/rest/class/vmlib/phpUtil.inc
index 5b57b125..2435bfbb 100755
--- a/vas/rest/class/vmlib/phpUtil.inc
+++ b/vas/rest/class/vmlib/phpUtil.inc
@@ -1438,4 +1438,64 @@ function copyFileInWsDataDir($sModule, $sObject, $mId, $sField = '', $sFileName,
     }
     return $sErrorMsg;
 }
+
+ /**
+  *This method create an empty file in ws_data.
+  *@file vmlib/phpUtil.inc
+  *@param $sModule Name of the module.
+  *@param $sObject Name of the object.
+  *@param $mId Id of the current object.
+  *@param $sField field name (generally DB column name).
+  *@param $sFileName Name of the file to copy.
+  *@return $sErrorMsg The error message.
+  */
+function createEmptyFileInWsDataDir($sModule, $sObject, $mId, $sField = '', $sFileName) {
+    global $properties;
+    $sErrorMsg = '';
+    $sDestDir = $properties['ws_data_dir'] . "/" . $sModule . "/" . $sObject . "/" . $mId;
+    if(!empty($sField))
+        $sDestDir .= "/" . $sField;
+    if ($properties['fileS3Uploader'] === true) {
+        require_once ("aws_sdk/aws-autoloader.php");
+        $s3 = new Aws\S3\S3Client(array(
+            'version'=>'latest',
+            'region'=> $properties['fileS3UploaderRegion'],
+            'profile'=> $properties['fileS3UploaderProfil'],
+            'debug' => false
+        ));
+        $sBucket = $properties['fileS3UploaderBucket'];
+        $sPrefix = "";
+        if (strpos($sBucket, "/") > -1){
+            $aBucket = explode("/", $sBucket );
+            $sBucket = $aBucket[0];
+            $sPrefix = implode("/", array_slice($aBucket, 1));
+        }
+        $sServerPath = str_replace($properties["vas_home"], $sPrefix, $sDestDir);
+        // Suppression du slash de début de ligne (sinon création d'un répertoire vide sur S3).
+        if (strpos($sServerPath, '/') === 0)
+            $sServerPath = substr($sServerPath, 1);
+        // Création du fichier vide.
+        try {
+            $aResult = $s3->putObject(array(
+                'Bucket' => $sBucket,
+                'Key' => $sServerPath . '/' . $sFileName,
+                'Body' => ''
+            ));
+        }
+        catch(Aws\S3\Exception\S3Exception $e) {
+            writeToErrorLog($e->getMessage());
+        }
+    }
+    else {
+        $sFilePath = $sDestDir . '/' . $sFileName;
+        // Création du répertoire de destination si inexistant.
+        if (!file_exists($sDestDir))
+            if (!mkdir($sDestDir, 0777, true)) {
+                writeToErrorLog(ERROR_CREATING_DIRECTORY . $sDestDir);
+                return ERROR_CREATING_DIRECTORY . $sDestDir;
+            }                    
+        fclose(fopen($sFilePath, "w+"));
+    }
+    return $sErrorMsg;
+}
 ?>
-- 
GitLab