Changed file layout to separate class
This commit is contained in:
parent
3b5839031e
commit
65836f4789
5 changed files with 79 additions and 64 deletions
|
@ -23,10 +23,10 @@ class AbstractController
|
||||||
$this->renderView(null);
|
$this->renderView(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function renderFile()
|
public function renderFile(FileRendererOptions $options)
|
||||||
{
|
{
|
||||||
$this->switchLayout('layout-file');
|
$fileRenderer = new FileRenderer();
|
||||||
$this->renderView(null);
|
$fileRenderer->render($options);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function renderView($viewName)
|
public function renderView($viewName)
|
||||||
|
|
|
@ -157,13 +157,13 @@ class PostController extends AbstractController
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
TransferHelper::download($url, $tmpPath);
|
TransferHelper::download($url, $tmpPath);
|
||||||
$context = Core::getContext();
|
$options = new FileRendererOptions();
|
||||||
$context->transport->lastModified = time();
|
$options->lastModified = time();
|
||||||
$context->transport->mimeType = mime_content_type($tmpPath);
|
$options->mimeType = mime_content_type($tmpPath);
|
||||||
$context->transport->cacheDaysToLive = 0.5;
|
$options->cacheDaysToLive = 0.5;
|
||||||
$context->transport->fileHash = md5($url);
|
$options->fileHash = md5($url);
|
||||||
$context->transport->fileContent = file_get_contents($tmpPath);
|
$options->fileContent = file_get_contents($tmpPath);
|
||||||
$this->renderFile();
|
$this->renderFile($options);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
|
@ -336,28 +336,28 @@ class PostController extends AbstractController
|
||||||
{
|
{
|
||||||
$ret = Api::run(new GetPostContentJob(), [JobArgs::ARG_POST_NAME => $name]);
|
$ret = Api::run(new GetPostContentJob(), [JobArgs::ARG_POST_NAME => $name]);
|
||||||
|
|
||||||
$context = Core::getContext();
|
$options = new FileRendererOptions();
|
||||||
$context->transport->cacheDaysToLive = 14;
|
$options->cacheDaysToLive = 14;
|
||||||
$context->transport->customFileName = $ret->fileName;
|
$options->customFileName = $ret->fileName;
|
||||||
$context->transport->mimeType = $ret->mimeType;
|
$options->mimeType = $ret->mimeType;
|
||||||
$context->transport->fileHash = 'post' . md5(substr($ret->fileContent, 0, 4096));
|
$options->fileHash = 'post' . md5(substr($ret->fileContent, 0, 4096));
|
||||||
$context->transport->fileContent = $ret->fileContent;
|
$options->fileContent = $ret->fileContent;
|
||||||
$context->transport->lastModified = $ret->lastModified;
|
$options->lastModified = $ret->lastModified;
|
||||||
$this->renderFile();
|
$this->renderFile($options);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function thumbnailView($name)
|
public function thumbnailView($name)
|
||||||
{
|
{
|
||||||
$ret = Api::run(new GetPostThumbnailJob(), [JobArgs::ARG_POST_NAME => $name]);
|
$ret = Api::run(new GetPostThumbnailJob(), [JobArgs::ARG_POST_NAME => $name]);
|
||||||
|
|
||||||
$context = Core::getContext();
|
$options = new FileRendererOptions();
|
||||||
$context->transport->cacheDaysToLive = 365;
|
$options->cacheDaysToLive = 365;
|
||||||
$context->transport->customFileName = $ret->fileName;
|
$options->customFileName = $ret->fileName;
|
||||||
$context->transport->mimeType = 'image/jpeg';
|
$options->mimeType = 'image/jpeg';
|
||||||
$context->transport->fileHash = 'thumb' . md5(substr($ret->fileContent, 0, 4096));
|
$options->fileHash = 'thumb' . md5(substr($ret->fileContent, 0, 4096));
|
||||||
$context->transport->fileContent = $ret->fileContent;
|
$options->fileContent = $ret->fileContent;
|
||||||
$context->transport->lastModified = $ret->lastModified;
|
$options->lastModified = $ret->lastModified;
|
||||||
$this->renderFile();
|
$this->renderFile($options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
43
src/FileRenderer.php
Normal file
43
src/FileRenderer.php
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
<?php
|
||||||
|
class FileRenderer
|
||||||
|
{
|
||||||
|
public function render(FileRendererOptions $options)
|
||||||
|
{
|
||||||
|
$lastModified = $options->lastModified;
|
||||||
|
$eTag = $options->fileHash;
|
||||||
|
$ttl = $options->cacheDaysToLive * 24 * 3600;
|
||||||
|
|
||||||
|
$ifModifiedSince = isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])
|
||||||
|
? $_SERVER['HTTP_IF_MODIFIED_SINCE']
|
||||||
|
: false;
|
||||||
|
|
||||||
|
$eTagHeader = isset($_SERVER['HTTP_IF_NONE_MATCH'])
|
||||||
|
? trim(trim($_SERVER['HTTP_IF_NONE_MATCH']), '"')
|
||||||
|
: false;
|
||||||
|
|
||||||
|
\Chibi\Util\Headers::set('ETag', '"' . $eTag . '"');
|
||||||
|
\Chibi\Util\Headers::set('Last-Modified', gmdate('D, d M Y H:i:s \G\M\T', $lastModified));
|
||||||
|
\Chibi\Util\Headers::set('Pragma', 'public');
|
||||||
|
\Chibi\Util\Headers::set('Cache-Control', 'public, max-age=' . $ttl);
|
||||||
|
\Chibi\Util\Headers::set('Expires', gmdate('D, d M Y H:i:s \G\M\T', time() + $ttl));
|
||||||
|
|
||||||
|
if (isset($options->customFileName))
|
||||||
|
{
|
||||||
|
\Chibi\Util\Headers::set(
|
||||||
|
'Content-Disposition',
|
||||||
|
'inline; filename="' . $options->customFileName . '"');
|
||||||
|
}
|
||||||
|
|
||||||
|
\Chibi\Util\Headers::set('Content-Type', $options->mimeType);
|
||||||
|
|
||||||
|
if (strtotime($ifModifiedSince) == $lastModified or $eTagHeader == $eTag)
|
||||||
|
{
|
||||||
|
\Chibi\Util\Headers::setCode('304');
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
echo $options->fileContent;
|
||||||
|
|
||||||
|
flush();
|
||||||
|
}
|
||||||
|
}
|
10
src/FileRendererOptions.php
Normal file
10
src/FileRendererOptions.php
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
<?php
|
||||||
|
class FileRendererOptions
|
||||||
|
{
|
||||||
|
public $lastModified;
|
||||||
|
public $fileHash;
|
||||||
|
public $cacheDaysToLive;
|
||||||
|
public $customFileName;
|
||||||
|
public $mimeType;
|
||||||
|
public $fileContent;
|
||||||
|
}
|
|
@ -1,38 +0,0 @@
|
||||||
<?php
|
|
||||||
$lastModified = $this->context->transport->lastModified;
|
|
||||||
$eTag = $this->context->transport->fileHash;
|
|
||||||
$ttl = $this->context->transport->cacheDaysToLive * 24 * 3600;
|
|
||||||
|
|
||||||
$ifModifiedSince = isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])
|
|
||||||
? $_SERVER['HTTP_IF_MODIFIED_SINCE']
|
|
||||||
: false;
|
|
||||||
|
|
||||||
$eTagHeader = isset($_SERVER['HTTP_IF_NONE_MATCH'])
|
|
||||||
? trim(trim($_SERVER['HTTP_IF_NONE_MATCH']), '"')
|
|
||||||
: false;
|
|
||||||
|
|
||||||
\Chibi\Util\Headers::set('ETag', '"' . $eTag . '"');
|
|
||||||
\Chibi\Util\Headers::set('Last-Modified', gmdate('D, d M Y H:i:s \G\M\T', $lastModified));
|
|
||||||
\Chibi\Util\Headers::set('Pragma', 'public');
|
|
||||||
\Chibi\Util\Headers::set('Cache-Control', 'public, max-age=' . $ttl);
|
|
||||||
\Chibi\Util\Headers::set('Expires', gmdate('D, d M Y H:i:s \G\M\T', time() + $ttl));
|
|
||||||
|
|
||||||
if (isset($this->context->transport->customFileName))
|
|
||||||
{
|
|
||||||
\Chibi\Util\Headers::set(
|
|
||||||
'Content-Disposition',
|
|
||||||
'inline; filename="' . $this->context->transport->customFileName . '"');
|
|
||||||
}
|
|
||||||
|
|
||||||
\Chibi\Util\Headers::set(
|
|
||||||
'Content-Type',
|
|
||||||
$this->context->transport->mimeType);
|
|
||||||
|
|
||||||
if (strtotime($ifModifiedSince) == $lastModified or $eTagHeader == $eTag)
|
|
||||||
{
|
|
||||||
\Chibi\Util\Headers::setCode('304');
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
echo $this->context->transport->fileContent;
|
|
||||||
flush();
|
|
Loading…
Reference in a new issue