|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2018 Julius Härtl <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @author Julius Härtl <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* @license GNU AGPL version 3 or any later version |
|
8
|
|
|
* |
|
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
12
|
|
|
* License, or (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU Affero General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace OCA\Deck\Service; |
|
25
|
|
|
|
|
26
|
|
|
use OC\Security\CSP\ContentSecurityPolicyManager; |
|
27
|
|
|
use OCA\Deck\Db\Attachment; |
|
28
|
|
|
use OCA\Deck\StatusException; |
|
29
|
|
|
use OCP\AppFramework\Http\ContentSecurityPolicy; |
|
30
|
|
|
use OCP\AppFramework\Http\EmptyContentSecurityPolicy; |
|
31
|
|
|
use OCP\AppFramework\Http\FileDisplayResponse; |
|
32
|
|
|
use OCP\AppFramework\Http\StreamResponse; |
|
33
|
|
|
use OCP\Files\Cache\IScanner; |
|
34
|
|
|
use OCP\Files\Folder; |
|
35
|
|
|
use OCP\Files\IAppData; |
|
36
|
|
|
use OCP\Files\IRootFolder; |
|
37
|
|
|
use OCP\Files\NotFoundException; |
|
38
|
|
|
use OCP\Files\NotPermittedException; |
|
39
|
|
|
use OCP\Files\SimpleFS\ISimpleFile; |
|
40
|
|
|
use OCP\Files\SimpleFS\ISimpleFolder; |
|
41
|
|
|
use OCP\IConfig; |
|
42
|
|
|
use OCP\IL10N; |
|
43
|
|
|
use OCP\ILogger; |
|
44
|
|
|
use OCP\IRequest; |
|
45
|
|
|
|
|
46
|
|
|
class FileService implements IAttachmentService { |
|
47
|
|
|
|
|
48
|
|
|
private $l10n; |
|
49
|
|
|
private $appData; |
|
50
|
|
|
private $request; |
|
51
|
|
|
private $logger; |
|
52
|
|
|
private $rootFolder; |
|
53
|
|
|
private $config; |
|
54
|
|
|
|
|
55
|
|
|
public function __construct( |
|
56
|
|
|
IL10N $l10n, |
|
57
|
|
|
IAppData $appData, |
|
58
|
|
|
IRequest $request, |
|
59
|
|
|
ILogger $logger, |
|
60
|
|
|
IRootFolder $rootFolder, |
|
61
|
|
|
IConfig $config |
|
62
|
|
|
) { |
|
63
|
|
|
$this->l10n = $l10n; |
|
64
|
|
|
$this->appData = $appData; |
|
65
|
|
|
$this->request = $request; |
|
66
|
|
|
$this->logger = $logger; |
|
67
|
|
|
$this->rootFolder = $rootFolder; |
|
68
|
|
|
$this->config = $config; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param Attachment $attachment |
|
73
|
|
|
* @return ISimpleFile |
|
74
|
|
|
* @throws NotFoundException |
|
75
|
|
|
* @throws NotPermittedException |
|
76
|
|
|
*/ |
|
77
|
|
|
private function getFileForAttachment(Attachment $attachment) { |
|
78
|
|
|
return $this->getFolder($attachment) |
|
79
|
|
|
->getFile($attachment->getData()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param Attachment $attachment |
|
84
|
|
|
* @return ISimpleFolder |
|
85
|
|
|
* @throws NotPermittedException |
|
86
|
|
|
*/ |
|
87
|
|
|
private function getFolder(Attachment $attachment) { |
|
88
|
|
|
$folderName = 'file-card-' . (int)$attachment->getCardId(); |
|
89
|
|
|
try { |
|
90
|
|
|
$folder = $this->appData->getFolder($folderName); |
|
91
|
|
|
} catch (NotFoundException $e) { |
|
92
|
|
|
$folder = $this->appData->newFolder($folderName); |
|
93
|
|
|
} |
|
94
|
|
|
return $folder; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function extendData(Attachment $attachment) { |
|
98
|
|
|
try { |
|
99
|
|
|
$file = $this->getFileForAttachment($attachment); |
|
100
|
|
|
} catch (NotFoundException $e) { |
|
101
|
|
|
$this->logger->info('Extending data for file attachment failed'); |
|
102
|
|
|
return $attachment; |
|
103
|
|
|
} catch (NotPermittedException $e) { |
|
104
|
|
|
$this->logger->info('Extending data for file attachment failed'); |
|
105
|
|
|
return $attachment; |
|
106
|
|
|
} |
|
107
|
|
|
$attachment->setExtendedData([ |
|
108
|
|
|
'filesize' => $file->getSize(), |
|
109
|
|
|
'mimetype' => $file->getMimeType(), |
|
110
|
|
|
'info' => pathinfo($file->getName()) |
|
111
|
|
|
]); |
|
112
|
|
|
return $attachment; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return array |
|
117
|
|
|
* @throws StatusException |
|
118
|
|
|
*/ |
|
119
|
|
|
private function getUploadedFile () { |
|
120
|
|
|
$file = $this->request->getUploadedFile('file'); |
|
121
|
|
|
$error = null; |
|
122
|
|
|
$phpFileUploadErrors = [ |
|
123
|
|
|
UPLOAD_ERR_OK => $this->l10n->t('The file was uploaded'), |
|
124
|
|
|
UPLOAD_ERR_INI_SIZE => $this->l10n->t('The uploaded file exceeds the upload_max_filesize directive in php.ini'), |
|
125
|
|
|
UPLOAD_ERR_FORM_SIZE => $this->l10n->t('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'), |
|
126
|
|
|
UPLOAD_ERR_PARTIAL => $this->l10n->t('The file was only partially uploaded'), |
|
127
|
|
|
UPLOAD_ERR_NO_FILE => $this->l10n->t('No file was uploaded'), |
|
128
|
|
|
UPLOAD_ERR_NO_TMP_DIR => $this->l10n->t('Missing a temporary folder'), |
|
129
|
|
|
UPLOAD_ERR_CANT_WRITE => $this->l10n->t('Could not write file to disk'), |
|
130
|
|
|
UPLOAD_ERR_EXTENSION => $this->l10n->t('A PHP extension stopped the file upload'), |
|
131
|
|
|
]; |
|
132
|
|
|
|
|
133
|
|
|
if (empty($file)) { |
|
134
|
|
|
$error = $this->l10n->t('No file uploaded or file size exceeds maximum of %s', [\OCP\Util::humanFileSize(\OCP\Util::uploadLimit())]); |
|
135
|
|
|
} |
|
136
|
|
|
if (!empty($file) && array_key_exists('error', $file) && $file['error'] !== UPLOAD_ERR_OK) { |
|
137
|
|
|
$error = $phpFileUploadErrors[$file['error']]; |
|
138
|
|
|
} |
|
139
|
|
|
if ($error !== null) { |
|
140
|
|
|
throw new StatusException($error); |
|
141
|
|
|
} |
|
142
|
|
|
return $file; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param Attachment $attachment |
|
147
|
|
|
* @throws NotPermittedException |
|
148
|
|
|
* @throws StatusException |
|
149
|
|
|
*/ |
|
150
|
|
|
public function create(Attachment $attachment) { |
|
151
|
|
|
$file = $this->getUploadedFile(); |
|
152
|
|
|
$folder = $this->getFolder($attachment); |
|
153
|
|
|
$fileName = $file['name']; |
|
154
|
|
|
if ($folder->fileExists($fileName)) { |
|
155
|
|
|
throw new StatusException('File already exists.'); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
$target = $folder->newFile($fileName); |
|
159
|
|
|
$content = fopen($file['tmp_name'], 'rb'); |
|
160
|
|
|
if ($content === false) { |
|
161
|
|
|
throw new StatusException('Could not read file'); |
|
162
|
|
|
} |
|
163
|
|
|
$target->putContent($content); |
|
164
|
|
|
if (is_resource($content)) { |
|
165
|
|
|
fclose($content); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
$attachment->setData($fileName); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* This method requires to be used with POST so we can properly get the form data |
|
173
|
|
|
* |
|
174
|
|
|
* @throws \Exception |
|
175
|
|
|
*/ |
|
176
|
|
|
public function update(Attachment $attachment) { |
|
177
|
|
|
$file = $this->getUploadedFile(); |
|
178
|
|
|
$fileName = $file['name']; |
|
179
|
|
|
$attachment->setData($fileName); |
|
180
|
|
|
|
|
181
|
|
|
$target = $this->getFileForAttachment($attachment); |
|
182
|
|
|
$content = fopen($file['tmp_name'], 'rb'); |
|
183
|
|
|
if ($content === false) { |
|
184
|
|
|
throw new StatusException('Could not read file'); |
|
185
|
|
|
} |
|
186
|
|
|
$target->putContent($content); |
|
187
|
|
|
if (is_resource($content)) { |
|
188
|
|
|
fclose($content); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
$attachment->setLastModified(time()); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @param Attachment $attachment |
|
196
|
|
|
* @throws NotPermittedException |
|
197
|
|
|
*/ |
|
198
|
|
|
public function delete(Attachment $attachment) { |
|
199
|
|
|
try { |
|
200
|
|
|
$file = $this->getFileForAttachment($attachment); |
|
201
|
|
|
$file->delete(); |
|
202
|
|
|
} catch (NotFoundException $e) { |
|
|
|
|
|
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Workaround until ISimpleFile can be fetched as a resource |
|
208
|
|
|
* |
|
209
|
|
|
* @throws \Exception |
|
210
|
|
|
*/ |
|
211
|
|
|
private function getFileFromRootFolder(Attachment $attachment) { |
|
212
|
|
|
$folderName = 'file-card-' . (int)$attachment->getCardId(); |
|
213
|
|
|
$instanceId = $this->config->getSystemValue('instanceid', null); |
|
214
|
|
|
if ($instanceId === null) { |
|
215
|
|
|
throw new \Exception('no instance id!'); |
|
216
|
|
|
} |
|
217
|
|
|
$name = 'appdata_' . $instanceId; |
|
218
|
|
|
$appDataFolder = $this->rootFolder->get($name); |
|
219
|
|
|
$appDataFolder = $appDataFolder->get('deck'); |
|
|
|
|
|
|
220
|
|
|
$cardFolder = $appDataFolder->get($folderName); |
|
221
|
|
|
return $cardFolder->get($attachment->getData()); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* @param Attachment $attachment |
|
226
|
|
|
* @return FileDisplayResponse|\OCP\AppFramework\Http\Response|StreamResponse |
|
227
|
|
|
* @throws \Exception |
|
228
|
|
|
*/ |
|
229
|
|
|
public function display(Attachment $attachment) { |
|
230
|
|
|
$file = $this->getFileFromRootFolder($attachment); |
|
231
|
|
|
if (method_exists($file, 'fopen')) { |
|
232
|
|
|
$response = new StreamResponse($file->fopen('r')); |
|
233
|
|
|
$response->addHeader('Content-Disposition', 'inline; filename="' . rawurldecode($file->getName()) . '"'); |
|
234
|
|
|
} else { |
|
235
|
|
|
$response = new FileDisplayResponse($file); |
|
236
|
|
|
} |
|
237
|
|
|
if ($file->getMimeType() === 'application/pdf') { |
|
238
|
|
|
// We need those since otherwise chrome won't show the PDF file with CSP rule object-src 'none' |
|
239
|
|
|
// https://bugs.chromium.org/p/chromium/issues/detail?id=271452 |
|
240
|
|
|
$policy = new ContentSecurityPolicy(); |
|
241
|
|
|
$policy->addAllowedObjectDomain('\'self\''); |
|
242
|
|
|
$policy->addAllowedObjectDomain('blob:'); |
|
243
|
|
|
$response->setContentSecurityPolicy($policy); |
|
244
|
|
|
} |
|
245
|
|
|
$response->addHeader('Content-Type', $file->getMimeType()); |
|
246
|
|
|
return $response; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* Should undo be allowed and the delete action be done by a background job |
|
251
|
|
|
* |
|
252
|
|
|
* @return bool |
|
253
|
|
|
*/ |
|
254
|
|
|
public function allowUndo() { |
|
255
|
|
|
return true; |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* Mark an attachment as deleted |
|
260
|
|
|
* |
|
261
|
|
|
* @param Attachment $attachment |
|
262
|
|
|
*/ |
|
263
|
|
|
public function markAsDeleted(Attachment $attachment) { |
|
264
|
|
|
$attachment->setDeletedAt(time()); |
|
265
|
|
|
} |
|
266
|
|
|
} |