summaryrefslogtreecommitdiff
path: root/app/Controller/FileViewerController.php
diff options
context:
space:
mode:
authorFrederic Guillot <fred@kanboard.net>2016-12-19 22:27:13 -0500
committerFrederic Guillot <fred@kanboard.net>2016-12-19 22:27:13 -0500
commit07c44d2113ee2fe67d6bdaf0018b321b1a6ebc9f (patch)
tree0e71a12e86ffb65c1b4e8e39e752488c5f9968d2 /app/Controller/FileViewerController.php
parenteed51aef63d55d8906f3f0e6c707ef1f1739d5ff (diff)
Avoid code duplication in PR #2891
Diffstat (limited to 'app/Controller/FileViewerController.php')
-rw-r--r--app/Controller/FileViewerController.php60
1 files changed, 28 insertions, 32 deletions
diff --git a/app/Controller/FileViewerController.php b/app/Controller/FileViewerController.php
index 3e0182ba..49568912 100644
--- a/app/Controller/FileViewerController.php
+++ b/app/Controller/FileViewerController.php
@@ -15,11 +15,11 @@ class FileViewerController extends BaseController
/**
* Get file content from object storage
*
- * @access private
+ * @access protected
* @param array $file
* @return string
*/
- private function getFileContent(array $file)
+ protected function getFileContent(array $file)
{
$content = '';
@@ -35,6 +35,30 @@ class FileViewerController extends BaseController
}
/**
+ * Output file with cache
+ *
+ * @param array $file
+ * @param $mimetype
+ */
+ protected function renderFileWithCache(array $file, $mimetype)
+ {
+ $etag = md5($file['path']);
+
+ if ($this->request->getHeader('If-None-Match') === '"'.$etag.'"') {
+ $this->response->status(304);
+ } else {
+ try {
+ $this->response->withContentType($mimetype);
+ $this->response->withCache(5 * 86400, $etag);
+ $this->response->send();
+ $this->objectStorage->output($file['path']);
+ } catch (ObjectStorageException $e) {
+ $this->logger->error($e->getMessage());
+ }
+ }
+ }
+
+ /**
* Show file content in a popover
*
* @access public
@@ -65,21 +89,7 @@ class FileViewerController extends BaseController
public function image()
{
$file = $this->getFile();
- $etag = md5($file['path']);
- $this->response->withContentType($this->helper->file->getImageMimeType($file['name']));
- $this->response->withCache(5 * 86400, $etag);
-
- if ($this->request->getHeader('If-None-Match') === '"'.$etag.'"') {
- $this->response->status(304);
- } else {
-
- try {
- $this->response->send();
- $this->objectStorage->output($file['path']);
- } catch (ObjectStorageException $e) {
- $this->logger->error($e->getMessage());
- }
- }
+ $this->renderFileWithCache($file, $this->helper->file->getImageMimeType($file['name']));
}
/**
@@ -90,21 +100,7 @@ class FileViewerController extends BaseController
public function browser()
{
$file = $this->getFile();
- $etag = md5($file['path']);
- $this->response->withContentType($this->helper->file->getBrowserViewType($file['name']));
- $this->response->withCache(5 * 86400, $etag);
-
- if ($this->request->getHeader('If-None-Match') === '"'.$etag.'"') {
- $this->response->status(304);
- } else {
-
- try {
- $this->response->send();
- $this->objectStorage->output($file['path']);
- } catch (ObjectStorageException $e) {
- $this->logger->error($e->getMessage());
- }
- }
+ $this->renderFileWithCache($file, $this->helper->file->getBrowserViewType($file['name']));
}
/**