From 07c44d2113ee2fe67d6bdaf0018b321b1a6ebc9f Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Mon, 19 Dec 2016 22:27:13 -0500 Subject: Avoid code duplication in PR #2891 --- app/Controller/FileViewerController.php | 60 +++++++++++++++------------------ 1 file changed, 28 insertions(+), 32 deletions(-) (limited to 'app/Controller') 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 = ''; @@ -34,6 +34,30 @@ class FileViewerController extends BaseController return $content; } + /** + * 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 * @@ -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'])); } /** -- cgit v1.2.3