Avoid code duplication in PR #2891

This commit is contained in:
Frederic Guillot
2016-12-19 22:27:13 -05:00
parent eed51aef63
commit 07c44d2113
7 changed files with 60 additions and 50 deletions

View File

@@ -8,6 +8,7 @@ New features:
Improvements: Improvements:
* Open PDF attachments in browser tab (preview)
* Handle username with dots in user mentions * Handle username with dots in user mentions
* Replace Chosen jQuery plugin by custom UI component * Replace Chosen jQuery plugin by custom UI component
* Rewrite UI component that change user/group roles * Rewrite UI component that change user/group roles

View File

@@ -15,11 +15,11 @@ class FileViewerController extends BaseController
/** /**
* Get file content from object storage * Get file content from object storage
* *
* @access private * @access protected
* @param array $file * @param array $file
* @return string * @return string
*/ */
private function getFileContent(array $file) protected function getFileContent(array $file)
{ {
$content = ''; $content = '';
@@ -34,6 +34,30 @@ class FileViewerController extends BaseController
return $content; 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 * Show file content in a popover
* *
@@ -65,21 +89,7 @@ class FileViewerController extends BaseController
public function image() public function image()
{ {
$file = $this->getFile(); $file = $this->getFile();
$etag = md5($file['path']); $this->renderFileWithCache($file, $this->helper->file->getImageMimeType($file['name']));
$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());
}
}
} }
/** /**
@@ -90,21 +100,7 @@ class FileViewerController extends BaseController
public function browser() public function browser()
{ {
$file = $this->getFile(); $file = $this->getFile();
$etag = md5($file['path']); $this->renderFileWithCache($file, $this->helper->file->getBrowserViewType($file['name']));
$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());
}
}
} }
/** /**

View File

@@ -21,9 +21,7 @@ class FileHelper extends Base
*/ */
public function icon($filename) public function icon($filename)
{ {
$extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); switch (get_file_extension($filename)) {
switch ($extension) {
case 'jpeg': case 'jpeg':
case 'jpg': case 'jpg':
case 'png': case 'png':
@@ -70,9 +68,7 @@ class FileHelper extends Base
*/ */
public function getImageMimeType($filename) public function getImageMimeType($filename)
{ {
$extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); switch (get_file_extension($filename)) {
switch ($extension) {
case 'jpeg': case 'jpeg':
case 'jpg': case 'jpg':
return 'image/jpeg'; return 'image/jpeg';
@@ -94,9 +90,7 @@ class FileHelper extends Base
*/ */
public function getPreviewType($filename) public function getPreviewType($filename)
{ {
$extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); switch (get_file_extension($filename)) {
switch ($extension) {
case 'md': case 'md':
case 'markdown': case 'markdown':
return 'markdown'; return 'markdown';
@@ -108,17 +102,15 @@ class FileHelper extends Base
} }
/** /**
* Return the browser view mimetype based on the file extension. * Return the browser view mime-type based on the file extension.
* *
* @access public
* @param $filename * @param $filename
*
* @return string * @return string
*/ */
public function getBrowserViewType($filename) public function getBrowserViewType($filename)
{ {
$extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); switch (get_file_extension($filename)) {
switch ($extension) {
case 'pdf': case 'pdf':
return 'application/pdf'; return 'application/pdf';
} }

View File

@@ -210,9 +210,7 @@ abstract class FileModel extends Base
*/ */
public function isImage($filename) public function isImage($filename)
{ {
$extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); switch (get_file_extension($filename)) {
switch ($extension) {
case 'jpeg': case 'jpeg':
case 'jpg': case 'jpg':
case 'png': case 'png':

View File

@@ -18,6 +18,11 @@
<i class="fa fa-eye fa-fw"></i> <i class="fa fa-eye fa-fw"></i>
<?= $this->url->link(t('View file'), 'FileViewerController', 'show', array('task_id' => $task['id'], 'project_id' => $task['project_id'], 'file_id' => $file['id']), false, 'popover') ?> <?= $this->url->link(t('View file'), 'FileViewerController', 'show', array('task_id' => $task['id'], 'project_id' => $task['project_id'], 'file_id' => $file['id']), false, 'popover') ?>
</li> </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('task_id' => $task['id'], 'project_id' => $task['project_id'], 'file_id' => $file['id']), false, '', '', true) ?>
</li>
<?php endif ?> <?php endif ?>
<li> <li>
<i class="fa fa-download fa-fw"></i> <i class="fa fa-download fa-fw"></i>

View File

@@ -145,6 +145,17 @@ function get_upload_max_size()
return min(ini_get('upload_max_filesize'), ini_get('post_max_size')); return min(ini_get('upload_max_filesize'), ini_get('post_max_size'));
} }
/**
* Get file extension
*
* @param $filename
* @return string
*/
function get_file_extension($filename)
{
return strtolower(pathinfo($filename, PATHINFO_EXTENSION));
}
/** /**
* Translate a string * Translate a string
* *

View File

@@ -30,7 +30,14 @@ class FileHelperTest extends Base
$helper = new FileHelper($this->container); $helper = new FileHelper($this->container);
$this->assertEquals('text', $helper->getPreviewType('test.txt')); $this->assertEquals('text', $helper->getPreviewType('test.txt'));
$this->assertEquals('markdown', $helper->getPreviewType('test.markdown')); $this->assertEquals('markdown', $helper->getPreviewType('test.markdown'));
$this->assertEquals('md', $helper->getPreviewType('test.md')); $this->assertEquals('markdown', $helper->getPreviewType('test.md'));
$this->assertEquals(null, $helper->getPreviewType('test.doc')); $this->assertEquals(null, $helper->getPreviewType('test.doc'));
} }
public function testGetBrowserViewType()
{
$fileHelper = new FileHelper($this->container);
$this->assertSame('application/pdf', $fileHelper->getBrowserViewType('SomeFile.PDF'));
$this->assertSame(null, $fileHelper->getBrowserViewType('SomeFile.doc'));
}
} }