Commit e2152286 authored by Jordan Breton's avatar Jordan Breton

Added : file response to send private files

parent 26d3ca3d
......@@ -7,6 +7,7 @@ use wfw\engine\core\notifier\Message;
use wfw\engine\core\response\errors\ResponseHandlerNotEnabled;
use wfw\engine\core\response\responses\ErrorResponse;
use wfw\engine\core\response\errors\ResponseResolutionFailure;
use wfw\engine\core\response\responses\FileResponse;
use wfw\engine\core\response\responses\StaticResponse;
use wfw\engine\core\app\context\IWebAppContext;
use wfw\engine\core\response\responses\Redirection;
......@@ -102,6 +103,8 @@ final class WebApp {
$permission->getMessage()
);
}else $this->redirect($response->getUrl(),$response->getCode(), $response->isAbsolute());
}else if($response instanceof FileResponse){
$this->sendFile($response->getData());
}
$responseRouter = $this->_context->getResponseRouter();
......@@ -161,4 +164,31 @@ final class WebApp {
header("Location: $url");
exit(0);
}
/**
* @param string $filename
*/
private function sendFile(string $filename):void{
//Get file type and set it as Content Type
$finfo = finfo_open(FILEINFO_MIME_TYPE);
header('Content-Type: '.finfo_file($finfo, $filename));
finfo_close($finfo);
//Use Content-Disposition: attachment to specify the filename
header('Content-Disposition: attachment; filename='.basename($filename));
//No cache
header('Expires: 0');
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
//Define file size
header('Content-Length: ' . filesize($filename));
ob_clean();
flush();
readfile($filename);
exit;
}
}
\ No newline at end of file
<?php
namespace wfw\engine\core\response\responses;
use wfw\engine\core\response\IResponse;
/**
* Class FileResponse
*
* @package wfw\engine\core\response\responses
*/
class FileResponse implements IResponse {
/** @var string $_fileName */
private $_fileName;
/**
* FileResponse constructor.
*
* @param string $fileName
* @throws \InvalidArgumentException
*/
public function __construct(string $fileName) {
if(!file_exists($fileName)) throw new \InvalidArgumentException(
"File not found $fileName"
);
else $this->_fileName = $fileName;
}
/**
* @return mixed Données de la réponse
*/
public function getData() {
return $this->_fileName;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment