|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) 2016, Roeland Jago Douma <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* @author Morris Jobke <[email protected]> |
|
7
|
|
|
* @author Roeland Jago Douma <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* @license GNU AGPL version 3 or any later version |
|
10
|
|
|
* |
|
11
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
12
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
13
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
14
|
|
|
* License, or (at your option) any later version. |
|
15
|
|
|
* |
|
16
|
|
|
* This program is distributed in the hope that it will be useful, |
|
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
19
|
|
|
* GNU Affero General Public License for more details. |
|
20
|
|
|
* |
|
21
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
22
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
23
|
|
|
* |
|
24
|
|
|
*/ |
|
25
|
|
|
namespace OCA\Files_Trashbin\Controller; |
|
26
|
|
|
|
|
27
|
|
|
use OCP\AppFramework\Controller; |
|
28
|
|
|
use OCP\AppFramework\Http; |
|
29
|
|
|
use OCP\AppFramework\Http\DataResponse; |
|
30
|
|
|
use OCP\AppFramework\Utility\ITimeFactory; |
|
31
|
|
|
use OCP\Files\File; |
|
32
|
|
|
use OCP\Files\Folder; |
|
33
|
|
|
use OCP\Files\IMimeTypeDetector; |
|
34
|
|
|
use OCP\Files\IRootFolder; |
|
35
|
|
|
use OCP\Files\NotFoundException; |
|
36
|
|
|
use OCP\IPreview; |
|
37
|
|
|
use OCP\IRequest; |
|
38
|
|
|
|
|
39
|
|
|
class PreviewController extends Controller { |
|
40
|
|
|
|
|
41
|
|
|
/** @var IRootFolder */ |
|
42
|
|
|
private $rootFolder; |
|
43
|
|
|
|
|
44
|
|
|
/** @var string */ |
|
45
|
|
|
private $userId; |
|
46
|
|
|
|
|
47
|
|
|
/** @var IMimeTypeDetector */ |
|
48
|
|
|
private $mimeTypeDetector; |
|
49
|
|
|
|
|
50
|
|
|
/** @var IPreview */ |
|
51
|
|
|
private $previewManager; |
|
52
|
|
|
|
|
53
|
|
|
/** @var ITimeFactory */ |
|
54
|
|
|
private $time; |
|
55
|
|
|
|
|
56
|
|
View Code Duplication |
public function __construct(string $appName, |
|
57
|
|
|
IRequest $request, |
|
58
|
|
|
IRootFolder $rootFolder, |
|
59
|
|
|
string $userId, |
|
60
|
|
|
IMimeTypeDetector $mimeTypeDetector, |
|
61
|
|
|
IPreview $previewManager, |
|
62
|
|
|
ITimeFactory $time) { |
|
63
|
|
|
parent::__construct($appName, $request); |
|
64
|
|
|
|
|
65
|
|
|
$this->rootFolder = $rootFolder; |
|
66
|
|
|
$this->userId = $userId; |
|
67
|
|
|
$this->mimeTypeDetector = $mimeTypeDetector; |
|
68
|
|
|
$this->previewManager = $previewManager; |
|
69
|
|
|
$this->time = $time; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @NoAdminRequired |
|
74
|
|
|
* @NoCSRFRequired |
|
75
|
|
|
* |
|
76
|
|
|
* @return DataResponse|Http\FileDisplayResponse |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getPreview( |
|
79
|
|
|
int $fileId, |
|
80
|
|
|
int $x = 44, |
|
81
|
|
|
int $y = 44 |
|
82
|
|
|
) { |
|
83
|
|
|
|
|
84
|
|
|
if ($x === 0 || $y === 0) { |
|
85
|
|
|
return new DataResponse([], Http::STATUS_BAD_REQUEST); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
try { |
|
89
|
|
|
$userFolder = $this->rootFolder->getUserFolder($this->userId); |
|
90
|
|
|
/** @var Folder $trash */ |
|
91
|
|
|
$trash = $userFolder->getParent()->get('files_trashbin/files'); |
|
92
|
|
|
$trashFiles = $trash->getById($fileId); |
|
93
|
|
|
|
|
94
|
|
|
if (empty($trashFiles)) { |
|
95
|
|
|
throw new NotFoundException(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$trashFile = array_pop($trashFiles); |
|
99
|
|
|
|
|
100
|
|
|
if ($trashFile instanceof Folder) { |
|
101
|
|
|
return new DataResponse([], Http::STATUS_BAD_REQUEST); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/* |
|
105
|
|
|
* Files in the root of the trashbin are timetamped. |
|
106
|
|
|
* So we have to strip that in order to properly detect the mimetype of the file. |
|
107
|
|
|
*/ |
|
108
|
|
|
if ($trashFile->getParent()->getPath() === $trash->getPath()) { |
|
109
|
|
|
/** @var File $trashFile */ |
|
110
|
|
|
$fileName = $trashFile->getName(); |
|
111
|
|
|
$i = strrpos($fileName, '.'); |
|
112
|
|
|
if ($i !== false) { |
|
113
|
|
|
$fileName = substr($fileName, 0, $i); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$mimeType = $this->mimeTypeDetector->detectPath($fileName); |
|
117
|
|
|
} else { |
|
118
|
|
|
$mimeType = $this->mimeTypeDetector->detectPath($trashFile->getName()); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$f = $this->previewManager->getPreview($trashFile, $x, $y, true, IPreview::MODE_FILL, $mimeType); |
|
|
|
|
|
|
122
|
|
|
$response = new Http\FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]); |
|
123
|
|
|
|
|
124
|
|
|
// Cache previews for 24H |
|
125
|
|
|
$response->cacheFor(3600 * 24); |
|
126
|
|
|
return $response; |
|
127
|
|
|
} catch (NotFoundException $e) { |
|
128
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND); |
|
129
|
|
|
} catch (\InvalidArgumentException $e) { |
|
130
|
|
|
return new DataResponse([], Http::STATUS_BAD_REQUEST); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: