From 12f00bb6ea906f349de04fee74d529dbddf88f69 Mon Sep 17 00:00:00 2001
From: Anthony Borghi <anthony.borghi@veremes.com>
Date: Fri, 22 Feb 2019 14:33:35 +0100
Subject: [PATCH] retouche proxy pour merger les deux classes

---
 vas/rest/ws/vitis/FileDownloader.class.inc | 256 +++++----------------
 1 file changed, 59 insertions(+), 197 deletions(-)

diff --git a/vas/rest/ws/vitis/FileDownloader.class.inc b/vas/rest/ws/vitis/FileDownloader.class.inc
index c9564a93..b0389482 100755
--- a/vas/rest/ws/vitis/FileDownloader.class.inc
+++ b/vas/rest/ws/vitis/FileDownloader.class.inc
@@ -83,207 +83,69 @@ class FileDownloader extends Vitis {
      * @return  $sMessage Json or blob
      */
     function GET() {
+        session_write_close();
         $sMessage = array("status" => 0, "sMessage" => "");
         $bReturnFile = true;
         // si un paramètre est manquant on coupe
         if ((!empty($this->aValues["key"])) && (!empty($this->aValues["eTag"]))){
-            $sPath = "";
-            if ($this->aProperties["filesystem"] == 's3'){
-                $sPath = $this->aProperties['extract_dir'] . "/" . getUniqRandomId();
-                // traitement du bucket et de ses sous-dossiers
-                $sBucket = $this->aProperties['fileS3UploaderBucket'];
-                $sPrefix = "";
-                // Suppression du slash de début de ligne.
-                if (strpos($this->aValues["key"], '/') === 0)
-                $this->aValues["key"] = substr($this->aValues["key"], 1);
-                //
-                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
-                ));
-                if(strpos($this->aValues['key'], $sPrefix) === 0 || $sPrefix == ''){
-                    // récupération du fichier sur le serveur pour le téléchargement si le Etag correspond avec celui de s3
-                    try{
-                        $s3->getObject(array(
-                            'Bucket' => $sBucket,
-                            'Key' => $this->aValues['key'],
-                            'IfMatch' => $this->aValues['eTag'],
-                            'SaveAs' => $sPath
-                        ));
-                    }catch(Aws\S3\Exception\S3Exception $e){
-                        error_log($e->getMessage());
-                        $bReturnFile = false;
-                        $sMessage = array("status" => 0, "sMessage" => "This file doesn't exist or the Etag doesn't match");
-                    }
-                } else {
-                    $bReturnFile = false;
-                    $sMessage = array("status" => 0, "sMessage" => "You can't access to this object");
-                }
-
-            } else {
-                // modification de $sPath pour aller chercher le fichier dans ws_data
-                if (strpos($this->aValues["key"], 'ws_data') === 0){
-                    $sPath = str_replace('ws_data', $this->aProperties['ws_data_dir'], $this->aValues["key"]);
-                } else if (strpos($this->aValues["key"], 'public') === 0){
-                    $sPath = str_replace('public', $this->aProperties['dir_export'], $this->aValues["key"]);
-                } else if (strpos($this->aValues["key"], 'upload') === 0){
-                    $sPath = str_replace('upload', $this->aProperties['upload_dir'], $this->aValues["key"]);
-                } else if (strpos($this->aValues["key"], 'shared') === 0){
-                    $sPath = str_replace('shared', $properties['vas_home'] . '/shared', $this->aValues["key"]);
-                } else {
-                    $bReturnFile = false;
-                    $sMessage = array("status" => 0, "sMessage" => "This directory can't be replaced by our replacer");
-                }
-                // check eTag
-                if (file_exists($sPath)) {
-                    $sEtag = sha1(file_get_contents($sPath), false);
-                    if ($sEtag != $this->aValues['eTag']){
-                        $bReturnFile = false;
-                        $sMessage = array("status" => 0, "sMessage" => "This file doesn't exist or the Etag doesn't match");
-                        http_response_code(404);
-                    }
-                } else {
-                    $bReturnFile = false;
-                    $sMessage = array("status" => 0, "sMessage" => "This file doesn't exist or the Etag doesn't match");
-                    http_response_code(404);
-                }
-            }
-
-            if($bReturnFile){
-                session_write_close();
-                $sFilename = explode("/", $this->aValues["key"]);
-                $sFilename = end($sFilename);
-                $sMessage = $this->getFile($sPath, $sFilename);
-            }
-
-        } else {
-            $sMessage = array("status" => 0, "sMessage" => "Missing Parameter");
-        }
-
-        return json_encode($sMessage);
-    }
-
-    function getFile($sFilePath, $sFileName) {
-
-        // Utilisation thumnail
-        if ($this->aValues["thumbnail"] === 'true') {
-            $sMinFilePath = $sFilePath;
-            $aMinFilePath = explode('.', $sMinFilePath);
-            array_pop($aMinFilePath);
-            array_push($aMinFilePath, 'min');
-            array_push($aMinFilePath, 'jpg');
-            $sMinFilePath = implode('.', $aMinFilePath);
-            if (file_exists($sMinFilePath)) {
-                $sFilePath = $sMinFilePath;
-            }
-        }
-
-        if ($this->aValues["thumbnail"] === 'true' && $this->aValues["type"] === 'document') {
-            header("Content-disposition: attachment; filename=\"" . rawurlencode($sFileName) . "\"");
-            return array("status" => 1, "fileName" => $sFileName);
-        }
-
-        // Le fichier existe ?
-        if (file_exists($sFilePath)) {
-            $sContentType = $this->getMime($sFilePath);
-            header("Content-Type: " . $sContentType);
-            header("Content-disposition: attachment; filename=\"" . rawurlencode($sFileName) . "\"");
-            header('Content-Length: ' . filesize($sFilePath));
-            if ($sContentType === "application/octet-stream") {
-                header("Content-Transfer-Encoding: Binary");
-            }
-            readfile($sFilePath);
-        }
-    }
-
-    function getMime($sFilePath, $mode=0){
-
-        $mime_types = array(
-
-            'txt' => 'text/plain',
-            'htm' => 'text/html',
-            'html' => 'text/html',
-            'php' => 'text/html',
-            'css' => 'text/css',
-            'js' => 'application/javascript',
-            'json' => 'application/json',
-            'xml' => 'application/xml',
-            'swf' => 'application/x-shockwave-flash',
-            'flv' => 'video/x-flv',
-
-            // images
-            'png' => 'image/png',
-            'jpe' => 'image/jpeg',
-            'jpeg' => 'image/jpeg',
-            'jpg' => 'image/jpeg',
-            'gif' => 'image/gif',
-            'bmp' => 'image/bmp',
-            'ico' => 'image/vnd.microsoft.icon',
-            'tiff' => 'image/tiff',
-            'tif' => 'image/tiff',
-            'svg' => 'image/svg+xml',
-            'svgz' => 'image/svg+xml',
-
-            // archives
-            'zip' => 'application/zip',
-            'rar' => 'application/x-rar-compressed',
-            'exe' => 'application/x-msdownload',
-            'msi' => 'application/x-msdownload',
-            'cab' => 'application/vnd.ms-cab-compressed',
-
-            // audio/video
-            'mp3' => 'audio/mpeg',
-            'qt' => 'video/quicktime',
-            'mov' => 'video/quicktime',
-
-            // adobe
-            'pdf' => 'application/pdf',
-            'psd' => 'image/vnd.adobe.photoshop',
-            'ai' => 'application/postscript',
-            'eps' => 'application/postscript',
-            'ps' => 'application/postscript',
-
-            // ms office
-            'doc' => 'application/msword',
-            'rtf' => 'application/rtf',
-            'xls' => 'application/vnd.ms-excel',
-            'ppt' => 'application/vnd.ms-powerpoint',
-            'docx' => 'application/msword',
-            'xlsx' => 'application/vnd.ms-excel',
-            'pptx' => 'application/vnd.ms-powerpoint',
-
-
-            // open office
-            'odt' => 'application/vnd.oasis.opendocument.text',
-            'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
-        );
-
-        $ext = explode('.', $sFilePath);
-        $ext = array_pop($ext);
-        $ext = strtolower($ext);
-
-        if(function_exists('mime_content_type') && $mode == 0){
-            $mimetype = mime_content_type($sFilePath);
-            return $mimetype;
-
-        }elseif(function_exists('finfo_open') && $mode == 0){
-            $finfo = finfo_open(FILEINFO_MIME);
-            $mimetype = finfo_file($finfo, $sFilePath);
-            finfo_close($finfo);
-            return $mimetype;
-        }elseif(array_key_exists($ext, $mime_types)){
-            return $mime_types[$ext];
-        }else {
-            return 'application/octet-stream';
-        }
+          $sPath = "";
+
+          if (strpos($this->aValues["key"], 'ws_data') === 0){
+            $sPath = str_replace('ws_data', $this->aProperties['ws_data_dir'], $this->aValues["key"]);
+          } else if (strpos($this->aValues["key"], 'public') === 0){
+            $sPath = str_replace('public', $this->aProperties['dir_export'], $this->aValues["key"]);
+          } else if (strpos($this->aValues["key"], 'upload') === 0){
+            $sPath = str_replace('upload', $this->aProperties['upload_dir'], $this->aValues["key"]);
+          } else if (strpos($this->aValues["key"], 'shared') === 0){
+            $sPath = str_replace('shared', $properties['vas_home'] . '/shared', $this->aValues["key"]);
+          } else {
+            $sMessage = array("status" => 0, "sMessage" => "This directory can't be replaced by our replacer");
+            return json_encode($sMessage);
+          }
+
+          if($this->oFilesManager->oFileInterface->file_exists($sPath)){
+              if($this->oFilesManager->oFileInterface->getFileEtag($sPath) === $this->aValues["eTag"]){
+                  $sFileName = $this->oFilesManager->getFileName($sPath);
+                  $sContentType = $this->oFilesManager->oFileInterface->mime_content_type($sPath);
+
+                  // Utilisation thumnail
+                  if ($this->aValues["thumbnail"] === 'true') {
+                      $sMinFilePath = $sPath;
+                      $aMinFilePath = explode('.', $sMinFilePath);
+                      array_pop($aMinFilePath);
+                      array_push($aMinFilePath, 'min');
+                      array_push($aMinFilePath, 'jpg');
+                      $sMinFilePath = implode('.', $aMinFilePath);
+                      if ($this->oFilesManager->oFileInterface->file_exists($sMinFilePath)) {
+                          $sPath = $sMinFilePath;
+                      }
+                  }
+
+                  if ($this->aValues["thumbnail"] === 'true' && $this->aValues["type"] === 'document') {
+                      header("Content-disposition: attachment; filename=\"" . rawurlencode($sFileName) . "\"");
+                      return array("status" => 1, "fileName" => $sFileName);
+                  }
+
+                  header("Content-Type: " . $sContentType);
+                  if (in_array($sContentType, array("text/plain", 'application/pdf', 'text/html'))){
+                      header("Content-disposition: inline; filename=\"" . rawurlencode($sFileName) . "\"");
+                  } else {
+                      header("Content-disposition: attachment; filename=\"" . rawurlencode($sFileName) . "\"");
+                  }
+                  header('Content-Length: ' . $this->oFilesManager->oFileInterface->filesize($sPath));
+                  if ($sContentType === "application/octet-stream") {
+                    header("Content-Transfer-Encoding: Binary");
+                  }
+                  $this->oFilesManager->oFileInterface->readfile($sPath);
+              } else {
+                  $sMessage = array("status" => 0, "sMessage" => "This file doesn't exist or the Etag doesn't match");
+                  return json_encode($sMessage);
+              }
+          } else {
+              $sMessage = array("status" => 0, "sMessage" => "This file doesn't exist or the Etag doesn't match");
+              return json_encode($sMessage);
+          }
+      }
     }
 }
 
-- 
GitLab