From 07faa56dd2ad36caf000f4752f42d561104985ba Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Carretero?=
 <frederic.carretero@veremes.com>
Date: Mon, 19 Nov 2018 16:20:17 +0100
Subject: [PATCH] putFileContentInWsDataDir() -> Ecriture dans un fichier de
 ws_data

---
 vas/rest/class/vmlib/phpUtil.inc | 64 ++++++++++++++++++++++++++++++++
 1 file changed, 64 insertions(+)

diff --git a/vas/rest/class/vmlib/phpUtil.inc b/vas/rest/class/vmlib/phpUtil.inc
index 092902c4..f2e26c8d 100755
--- a/vas/rest/class/vmlib/phpUtil.inc
+++ b/vas/rest/class/vmlib/phpUtil.inc
@@ -1630,4 +1630,68 @@ function getFileContentInWsDataDir($sModule, $sObject, $mId, $sField = '', $sFil
         }
     }
 }
+
+ /**
+  *This method write data to a 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 read.
+  *@param $sFileContent Data to write to the file.
+  *@return $sErrorMsg The error message.
+  */
+function putFileContentInWsDataDir($sModule, $sObject, $mId, $sField = '', $sFileName, $sFileContent) {
+    global $properties;
+    $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);
+        // Suppression du répertoire.
+        try {
+            $aResult = $s3->putObject(array(
+                'Bucket' => $sBucket,
+                'Key' => $sServerPath . '/' . $sFileName,
+                'Body' => $sFileContent
+            ));
+        }
+        catch(Aws\S3\Exception\S3Exception $e) {
+            writeToErrorLog($e->getMessage());
+        }
+    }
+    else {
+        // Supprime le fichier si déja existant.
+        $sFilePath = $sDestDir . '/' . $sFileName;
+        if (file_exists($sFilePath)) {
+            if (!unlink($sFilePath)) {
+                writeToErrorLog(ERROR_DELETING_FILE . $sFilePath);
+                return false;
+            }
+        }
+        // Ecrit les données dans le fichier.
+        if (file_put_contents($sFilePath, $sFileContent) === false) {
+            writeToErrorLog(ERROR_WRITING_FILE_CONTENT . $sFilePath);
+            return false;
+        }
+    }
+}
 ?>
-- 
GitLab