1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/kanboard_ynh.git synced 2024-09-03 19:36:17 +02:00
kanboard_ynh/sources/app/Controller/File.php

204 lines
5.9 KiB
PHP
Raw Normal View History

2014-07-20 12:26:15 +02:00
<?php
namespace Controller;
/**
* File controller
*
* @package controller
* @author Frederic Guillot
*/
class File extends Base
{
/**
* File upload form
*
* @access public
*/
public function create()
{
$task = $this->getTask();
2014-12-22 19:15:38 +01:00
$this->response->html($this->taskLayout('file/new', array(
2014-07-20 12:26:15 +02:00
'task' => $task,
'max_size' => ini_get('upload_max_filesize'),
)));
}
/**
* File upload (save files)
*
* @access public
*/
public function save()
{
$task = $this->getTask();
if ($this->file->upload($task['project_id'], $task['id'], 'files') === true) {
2015-01-16 14:23:05 +01:00
$this->response->redirect('?controller=task&action=show&task_id='.$task['id'].'&project_id='.$task['project_id'].'#attachments');
2014-07-20 12:26:15 +02:00
}
else {
$this->session->flashError(t('Unable to upload the file.'));
2015-01-16 14:23:05 +01:00
$this->response->redirect('?controller=file&action=create&task_id='.$task['id'].'&project_id='.$task['project_id']);
2014-07-20 12:26:15 +02:00
}
}
/**
* File download
*
* @access public
*/
public function download()
{
$task = $this->getTask();
$file = $this->file->getById($this->request->getIntegerParam('file_id'));
2015-02-25 17:29:06 +01:00
$filename = FILES_DIR.$file['path'];
2014-07-20 12:26:15 +02:00
if ($file['task_id'] == $task['id'] && file_exists($filename)) {
$this->response->forceDownload($file['name']);
$this->response->binary(file_get_contents($filename));
}
2015-01-16 14:23:05 +01:00
$this->response->redirect('?controller=task&action=show&task_id='.$task['id'].'&project_id='.$task['project_id']);
2014-07-20 12:26:15 +02:00
}
/**
* Open a file (show the content in a popover)
*
* @access public
*/
public function open()
{
$task = $this->getTask();
$file = $this->file->getById($this->request->getIntegerParam('file_id'));
if ($file['task_id'] == $task['id']) {
2015-01-16 14:23:05 +01:00
$this->response->html($this->template->render('file/open', array(
'file' => $file,
'task' => $task,
2014-07-20 12:26:15 +02:00
)));
}
}
/**
* Return the file content (work only for images)
*
* @access public
*/
public function image()
{
$task = $this->getTask();
$file = $this->file->getById($this->request->getIntegerParam('file_id'));
2015-02-25 17:29:06 +01:00
$filename = FILES_DIR.$file['path'];
2014-07-20 12:26:15 +02:00
if ($file['task_id'] == $task['id'] && file_exists($filename)) {
$metadata = getimagesize($filename);
if (isset($metadata['mime'])) {
$this->response->contentType($metadata['mime']);
readfile($filename);
}
}
}
/**
2015-04-21 17:56:16 +02:00
* Return image thumbnails
*
* @access public
*/
public function thumbnail()
{
$task = $this->getTask();
$file = $this->file->getById($this->request->getIntegerParam('file_id'));
$width_param = $this->request->getIntegerParam('width');
$height_param = $this->request->getIntegerParam('height');
$filename = FILES_DIR.$file['path'];
if ($file['task_id'] == $task['id'] && file_exists($filename)) {
// Get new sizes
list($width, $height) = getimagesize($filename);
if ($width_param == 0 && $height_param == 0) {
$newwidth = 100;
$newheight = 100;
} elseif ($width_param > 0 && $height_param == 0) {
$newwidth = $width_param;
$newheight = floor($height * ($width_param / $width));
} elseif ($width_param == 0 && $height_param > 0) {
$newwidth = floor($width * ($height_param / $height));
$newheight = $height_param;
} else {
$newwidth = $width_param;
$newheight = $height_param;
}
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
switch ($extension) {
case 'jpeg':
case 'jpg':
$source = imagecreatefromjpeg($filename);
break;
case 'png':
$source = imagecreatefrompng($filename);
break;
case 'gif':
$source = imagecreatefromgif($filename);
break;
default:
die('File "' . $filename . '" is not valid jpg, png or gif image.');
break;
}
// Resize
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
$metadata = getimagesize($filename);
if (isset($metadata['mime'])) {
$this->response->contentType($metadata['mime']);
imagejpeg($thumb);
}
}
}
/**
2014-07-20 12:26:15 +02:00
* Remove a file
*
* @access public
*/
public function remove()
{
$this->checkCSRFParam();
$task = $this->getTask();
$file = $this->file->getById($this->request->getIntegerParam('file_id'));
if ($file['task_id'] == $task['id'] && $this->file->remove($file['id'])) {
$this->session->flash(t('File removed successfully.'));
} else {
$this->session->flashError(t('Unable to remove this file.'));
}
2015-01-16 14:23:05 +01:00
$this->response->redirect('?controller=task&action=show&task_id='.$task['id'].'&project_id='.$task['project_id']);
2014-07-20 12:26:15 +02:00
}
/**
* Confirmation dialog before removing a file
*
* @access public
*/
public function confirm()
{
$task = $this->getTask();
$file = $this->file->getById($this->request->getIntegerParam('file_id'));
2014-12-22 19:15:38 +01:00
$this->response->html($this->taskLayout('file/remove', array(
2014-07-20 12:26:15 +02:00
'task' => $task,
'file' => $file,
)));
}
}