diff options
author | phecho <phecho@163.com> | 2016-12-13 19:49:16 +0800 |
---|---|---|
committer | phecho <phecho@163.com> | 2016-12-13 19:49:16 +0800 |
commit | 89feb0ae03c5a492526df077b2a3b382b0428ca8 (patch) | |
tree | 19d2c52ecdeda45179957515b7e9fb8b1acc3300 | |
parent | f83178bef8084bfe57963b1820e73b4a9ab0bab5 (diff) |
Implement feature #2843
-rw-r--r-- | app/Controller/FileViewerController.php | 25 | ||||
-rw-r--r-- | app/Helper/FileHelper.php | 19 | ||||
-rw-r--r-- | app/Template/project_overview/files.php | 5 |
3 files changed, 49 insertions, 0 deletions
diff --git a/app/Controller/FileViewerController.php b/app/Controller/FileViewerController.php index 518f5b0b..3e0182ba 100644 --- a/app/Controller/FileViewerController.php +++ b/app/Controller/FileViewerController.php @@ -83,6 +83,31 @@ class FileViewerController extends BaseController } /** + * Display file in browser + * + * @access public + */ + 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()); + } + } + } + + /** * Display image thumbnail * * @access public diff --git a/app/Helper/FileHelper.php b/app/Helper/FileHelper.php index cabf371c..82b444d0 100644 --- a/app/Helper/FileHelper.php +++ b/app/Helper/FileHelper.php @@ -106,4 +106,23 @@ class FileHelper extends Base return null; } + + /** + * Return the browser view mimetype based on the file extension. + * + * @param $filename + * + * @return string + */ + public function getBrowserViewType($filename) + { + $extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); + + switch ($extension) { + case 'pdf': + return 'application/pdf'; + } + + return null; + } } diff --git a/app/Template/project_overview/files.php b/app/Template/project_overview/files.php index 826e6325..1326a7d1 100644 --- a/app/Template/project_overview/files.php +++ b/app/Template/project_overview/files.php @@ -18,6 +18,11 @@ <i class="fa fa-eye fa-fw"></i> <?= $this->url->link(t('View file'), 'FileViewerController', 'show', array('project_id' => $project['id'], 'file_id' => $file['id']), false, 'popover') ?> </li> + <?php elseif ($this->file->getBrowserViewType($file['name']) !== null): ?> + <li> + <i class="fa fa-eye fa-fw"></i> + <?= $this->url->link(t('View file'), 'FileViewerController', 'browser', array('project_id' => $project['id'], 'file_id' => $file['id']), false, '', '', true) ?> + </li> <?php endif ?> <li> <i class="fa fa-download fa-fw"></i> |