From 8d13171857a82452d218b2286a9eaf2a29bfe1b5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Carretero?=
 <frederic.carretero@veremes.com>
Date: Thu, 22 Nov 2018 10:58:37 +0100
Subject: [PATCH] getFileUrlInWsDataDir() -> retourne l'url d'un fichier sur S3

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

diff --git a/vas/rest/class/vmlib/phpUtil.inc b/vas/rest/class/vmlib/phpUtil.inc
index e295ea6a..cf7c3f7a 100755
--- a/vas/rest/class/vmlib/phpUtil.inc
+++ b/vas/rest/class/vmlib/phpUtil.inc
@@ -1776,4 +1776,63 @@ function fileExistsInWsDataDir($sModule, $sObject, $mId = '', $sDirectory = '',
             return false;
     }
 }
+
+ /**
+  *This method return the url of 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.
+  *@return File content or false.
+  */
+function getFileUrlInWsDataDir($sModule, $sObject, $mId, $sField = '', $sFileName) {
+    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);
+        // Url du fichier.
+        try {
+            return $s3->getObjectUrl($sBucket, $sServerPath . '/' . $sFileName);
+        }
+        catch(Aws\S3\Exception\S3Exception $e) {
+            writeToErrorLog($e->getMessage());
+        }
+    }
+    else {
+        // Création de l'url du fichier.
+        $sFilePath = $sDestDir . '/' . $sFileName;
+        if (file_exists($sFilePath)) {
+            $sFileUrl = $properties['web_server_name'] . '/' . $properties['ws_data_alias'] . "/$sModule/$sObject/" . $mId;
+            if (!empty($sField))
+                $sFileUrl .=  "/$sField/";
+            $sFileUrl .=  $sFileName;
+            return $sFileUrl;
+        }
+        else {
+            writeToErrorLog(ERROR_FILE_NOT_FOUND . $sFilePath);
+            return false;
+        }
+    }
+}
 ?>
-- 
GitLab