1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Jitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) Jitamin Team |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Jitamin\Http\Controllers; |
13
|
|
|
|
14
|
|
|
use Jitamin\Foundation\ObjectStorage\ObjectStorageException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Attachment Controller. |
18
|
|
|
*/ |
19
|
|
|
class AttachmentController extends Controller |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Show file content in a popover. |
23
|
|
|
*/ |
24
|
|
|
public function show() |
25
|
|
|
{ |
26
|
|
|
$file = $this->getFile(); |
27
|
|
|
$type = $this->helper->file->getPreviewType($file['name']); |
|
|
|
|
28
|
|
|
$params = ['file_id' => $file['id'], 'project_id' => $this->request->getIntegerParam('project_id')]; |
|
|
|
|
29
|
|
|
|
30
|
|
|
if ($file['model'] === 'taskFileModel') { |
31
|
|
|
$params['task_id'] = $file['task_id']; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$this->response->html($this->template->render('attachment/show', [ |
|
|
|
|
35
|
|
|
'file' => $file, |
36
|
|
|
'params' => $params, |
37
|
|
|
'type' => $type, |
38
|
|
|
'content' => $this->getFileContent($file), |
39
|
|
|
])); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Display image. |
44
|
|
|
*/ |
45
|
|
|
public function image() |
46
|
|
|
{ |
47
|
|
|
$file = $this->getFile(); |
48
|
|
|
$this->renderFileWithCache($file, $this->helper->file->getImageMimeType($file['name'])); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Display browser mime file. |
53
|
|
|
*/ |
54
|
|
|
public function browser() |
55
|
|
|
{ |
56
|
|
|
$file = $this->getFile(); |
57
|
|
|
$this->renderFileWithCache($file, $this->helper->file->getBrowserViewType($file['name'])); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Display image thumbnail. |
62
|
|
|
*/ |
63
|
|
|
public function thumbnail() |
64
|
|
|
{ |
65
|
|
|
$file = $this->getFile(); |
66
|
|
|
$model = $file['model']; |
67
|
|
|
$filename = $this->$model->getThumbnailPath($file['path']); |
68
|
|
|
$etag = md5($filename); |
69
|
|
|
|
70
|
|
|
$this->response->withCache(5 * 86400, $etag); |
|
|
|
|
71
|
|
|
$this->response->withContentType('image/jpeg'); |
|
|
|
|
72
|
|
|
|
73
|
|
|
if ($this->request->getHeader('If-None-Match') === '"'.$etag.'"') { |
|
|
|
|
74
|
|
|
$this->response->status(304); |
|
|
|
|
75
|
|
|
} else { |
76
|
|
|
$this->response->send(); |
|
|
|
|
77
|
|
|
|
78
|
|
|
try { |
79
|
|
|
$this->objectStorage->output($filename); |
|
|
|
|
80
|
|
|
} catch (ObjectStorageException $e) { |
81
|
|
|
$this->logger->error($e->getMessage()); |
|
|
|
|
82
|
|
|
|
83
|
|
|
// Try to generate thumbnail on the fly for images uploaded before Jitamin < 1.0.19 |
84
|
|
|
$data = $this->objectStorage->get($file['path']); |
|
|
|
|
85
|
|
|
$this->$model->generateThumbnailFromData($file['path'], $data); |
86
|
|
|
$this->objectStorage->output($this->$model->getThumbnailPath($file['path'])); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* File download. |
93
|
|
|
*/ |
94
|
|
|
public function download() |
95
|
|
|
{ |
96
|
|
|
try { |
97
|
|
|
$file = $this->getFile(); |
98
|
|
|
$this->response->withFileDownload($file['name']); |
|
|
|
|
99
|
|
|
$this->response->send(); |
|
|
|
|
100
|
|
|
$this->objectStorage->output($file['path']); |
|
|
|
|
101
|
|
|
} catch (ObjectStorageException $e) { |
102
|
|
|
$this->logger->error($e->getMessage()); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get file content from object storage. |
108
|
|
|
* |
109
|
|
|
* @param array $file |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
protected function getFileContent(array $file) |
114
|
|
|
{ |
115
|
|
|
$content = ''; |
116
|
|
|
|
117
|
|
|
try { |
118
|
|
|
if ($file['is_image'] == 0) { |
119
|
|
|
$content = $this->objectStorage->get($file['path']); |
|
|
|
|
120
|
|
|
} |
121
|
|
|
} catch (ObjectStorageException $e) { |
122
|
|
|
$this->logger->error($e->getMessage()); |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $content; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Output file with cache. |
130
|
|
|
* |
131
|
|
|
* @param array $file |
132
|
|
|
* @param $mimetype |
133
|
|
|
*/ |
134
|
|
|
protected function renderFileWithCache(array $file, $mimetype) |
135
|
|
|
{ |
136
|
|
|
$etag = md5($file['path']); |
137
|
|
|
if ($this->request->getHeader('If-None-Match') === '"'.$etag.'"') { |
|
|
|
|
138
|
|
|
$this->response->status(304); |
|
|
|
|
139
|
|
|
} else { |
140
|
|
|
try { |
141
|
|
|
$this->response->withContentType($mimetype); |
|
|
|
|
142
|
|
|
$this->response->withCache(5 * 86400, $etag); |
|
|
|
|
143
|
|
|
$this->response->send(); |
|
|
|
|
144
|
|
|
$this->objectStorage->output($file['path']); |
|
|
|
|
145
|
|
|
} catch (ObjectStorageException $e) { |
146
|
|
|
$this->logger->error($e->getMessage()); |
|
|
|
|
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.