From 924949850322144036e281bf94fb8b88c42bbd74 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Tue, 16 Feb 2016 22:06:53 -0500 Subject: Add file preview for Markdown and text files --- app/Controller/FileViewer.php | 27 ++++++++++++++++++++++++++ app/Helper/File.php | 22 +++++++++++++++++++++ app/ServiceProvider/AuthenticationProvider.php | 1 + app/Template/file_viewer/show.php | 16 ++++++++++----- app/Template/project_overview/files.php | 20 ++++++++++++------- app/Template/task_file/show.php | 20 ++++++++++++------- 6 files changed, 87 insertions(+), 19 deletions(-) (limited to 'app') diff --git a/app/Controller/FileViewer.php b/app/Controller/FileViewer.php index 24ccc691..bc91c3d8 100644 --- a/app/Controller/FileViewer.php +++ b/app/Controller/FileViewer.php @@ -12,6 +12,30 @@ use Kanboard\Core\ObjectStorage\ObjectStorageException; */ class FileViewer extends Base { + /** + * Get file content from object storage + * + * @access private + * @param array $file + * @return string + */ + private function getFileContent(array $file) + { + $content = ''; + + try { + + if ($file['is_image'] == 0) { + $content = $this->objectStorage->get($file['path']); + } + + } catch (ObjectStorageException $e) { + $this->logger->error($e->getMessage()); + } + + return $content; + } + /** * Show file content in a popover * @@ -20,6 +44,7 @@ class FileViewer extends Base public function show() { $file = $this->getFile(); + $type = $this->helper->file->getPreviewType($file['name']); $params = array('file_id' => $file['id'], 'project_id' => $this->request->getIntegerParam('project_id')); if ($file['model'] === 'taskFile') { @@ -29,6 +54,8 @@ class FileViewer extends Base $this->response->html($this->template->render('file_viewer/show', array( 'file' => $file, 'params' => $params, + 'type' => $type, + 'content' => $this->getFileContent($file), ))); } diff --git a/app/Helper/File.php b/app/Helper/File.php index 6948fe6a..b493e64f 100644 --- a/app/Helper/File.php +++ b/app/Helper/File.php @@ -82,4 +82,26 @@ class File extends \Kanboard\Core\Base return 'image/jpeg'; } } + + /** + * Get the preview type + * + * @access public + * @param string $filename + * @return string + */ + public function getPreviewType($filename) + { + $extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); + + switch ($extension) { + case 'md': + case 'markdown': + return 'markdown'; + case 'txt': + return 'text'; + } + + return null; + } } diff --git a/app/ServiceProvider/AuthenticationProvider.php b/app/ServiceProvider/AuthenticationProvider.php index cc7b9302..700fe05b 100644 --- a/app/ServiceProvider/AuthenticationProvider.php +++ b/app/ServiceProvider/AuthenticationProvider.php @@ -81,6 +81,7 @@ class AuthenticationProvider implements ServiceProviderInterface $acl->add('Project', array('share', 'integrations', 'notifications', 'duplicate', 'disable', 'enable', 'remove'), Role::PROJECT_MANAGER); $acl->add('ProjectPermission', '*', Role::PROJECT_MANAGER); $acl->add('ProjectEdit', '*', Role::PROJECT_MANAGER); + $acl->add('ProjectFile', '*', Role::PROJECT_MEMBER); $acl->add('Projectuser', '*', Role::PROJECT_MANAGER); $acl->add('Subtask', '*', Role::PROJECT_MEMBER); $acl->add('SubtaskRestriction', '*', Role::PROJECT_MEMBER); diff --git a/app/Template/file_viewer/show.php b/app/Template/file_viewer/show.php index e0d1b21e..c71ef91c 100644 --- a/app/Template/file_viewer/show.php +++ b/app/Template/file_viewer/show.php @@ -1,8 +1,14 @@ +
+ + <?= $this->e($file['name']) ?> + +
+ text->markdown($content) ?> +
+ +
+
\ No newline at end of file diff --git a/app/Template/project_overview/files.php b/app/Template/project_overview/files.php index fea20ad6..03835f6b 100644 --- a/app/Template/project_overview/files.php +++ b/app/Template/project_overview/files.php @@ -24,16 +24,16 @@ @@ -64,16 +64,22 @@ diff --git a/app/Template/task_file/show.php b/app/Template/task_file/show.php index 67d7a99c..8aa76520 100644 --- a/app/Template/task_file/show.php +++ b/app/Template/task_file/show.php @@ -14,16 +14,16 @@ @@ -54,16 +54,22 @@ -- cgit v1.2.3