summaryrefslogtreecommitdiff
path: root/app/Controller/FileViewer.php
blob: 3be4ea144d186a82654ed1aa0b874efee81092b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php

namespace Kanboard\Controller;

use Kanboard\Core\ObjectStorage\ObjectStorageException;

/**
 * File Viewer Controller
 *
 * @package  controller
 * @author   Frederic Guillot
 */
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
     *
     * @access public
     */
    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') {
            $params['task_id'] = $file['task_id'];
        }

        $this->response->html($this->template->render('file_viewer/show', array(
            'file' => $file,
            'params' => $params,
            'type' => $type,
            'content' => $this->getFileContent($file),
        )));
    }

    /**
     * Display image
     *
     * @access public
     */
    public function image()
    {
        $file = $this->getFile();
        $etag = md5($file['path']);
        $this->response->contentType($this->helper->file->getImageMimeType($file['name']));
        $this->response->cache(5 * 86400, $etag);

        if ($this->request->getHeader('If-None-Match') === '"'.$etag.'"') {
            return $this->response->status(304);
        }

        try {
            $this->objectStorage->output($file['path']);
        } catch (ObjectStorageException $e) {
            $this->logger->error($e->getMessage());
        }
    }

    /**
     * Display image thumbnail
     *
     * @access public
     */
    public function thumbnail()
    {
        $file = $this->getFile();
        $model = $file['model'];
        $filename = $this->$model->getThumbnailPath($file['path']);
        $etag = md5($filename);

        $this->response->cache(5 * 86400, $etag);
        $this->response->contentType('image/jpeg');

        if ($this->request->getHeader('If-None-Match') === '"'.$etag.'"') {
            return $this->response->status(304);
        }

        try {

            $this->objectStorage->output($filename);
        } catch (ObjectStorageException $e) {
            $this->logger->error($e->getMessage());

            // Try to generate thumbnail on the fly for images uploaded before Kanboard < 1.0.19
            $data = $this->objectStorage->get($file['path']);
            $this->$model->generateThumbnailFromData($file['path'], $data);
            $this->objectStorage->output($this->$model->getThumbnailPath($file['path']));
        }
    }

    /**
     * File download
     *
     * @access public
     */
    public function download()
    {
        try {
            $file = $this->getFile();
            $this->response->forceDownload($file['name']);
            $this->objectStorage->output($file['path']);
        } catch (ObjectStorageException $e) {
            $this->logger->error($e->getMessage());
        }
    }
}