Skip to content
Snippets Groups Projects
Commit 12f00bb6 authored by Anthony Borghi's avatar Anthony Borghi
Browse files

retouche proxy pour merger les deux classes

parent b02063f4
No related branches found
No related tags found
No related merge requests found
......@@ -83,53 +83,13 @@ 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){
......@@ -139,50 +99,25 @@ class FileDownloader extends Vitis {
} 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) {
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 = $sFilePath;
$sMinFilePath = $sPath;
$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->oFilesManager->oFileInterface->file_exists($sMinFilePath)) {
$sPath = $sMinFilePath;
}
}
......@@ -191,98 +126,25 @@ class FileDownloader extends Vitis {
return array("status" => 1, "fileName" => $sFileName);
}
// Le fichier existe ?
if (file_exists($sFilePath)) {
$sContentType = $this->getMime($sFilePath);
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: ' . filesize($sFilePath));
}
header('Content-Length: ' . $this->oFilesManager->oFileInterface->filesize($sPath));
if ($sContentType === "application/octet-stream") {
header("Content-Transfer-Encoding: Binary");
}
readfile($sFilePath);
}
$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);
}
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';
$sMessage = array("status" => 0, "sMessage" => "This file doesn't exist or the Etag doesn't match");
return json_encode($sMessage);
}
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment