Completed
Push — master ( 8bf574...c0bbae )
by Lukas
28:01 queued 15:22
created

PreviewController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 6
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, Roeland Jago Douma <[email protected]>
4
 *
5
 * @author Roeland Jago Douma <[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
namespace OCA\Files_Trashbin\Controller;
24
25
use OCP\AppFramework\Controller;
26
use OCP\AppFramework\Http;
27
use OCP\AppFramework\Http\DataResponse;
28
use OCP\Files\File;
29
use OCP\Files\Folder;
30
use OCP\Files\IMimeTypeDetector;
31
use OCP\Files\IRootFolder;
32
use OCP\Files\NotFoundException;
33
use OCP\IPreview;
34
use OCP\IRequest;
35
36
class PreviewController extends Controller {
37
38
	/** @var IRootFolder */
39
	private $rootFolder;
40
41
	/** @var string */
42
	private $userId;
43
44
	/** @var IMimeTypeDetector */
45
	private $mimeTypeDetector;
46
47
	/** @var IPreview */
48
	private $previewManager;
49
50
	/**
51
	 * @param string $appName
52
	 * @param IRequest $request
53
	 * @param IRootFolder $rootFolder
54
	 * @param $userId
55
	 * @param IMimeTypeDetector $mimeTypeDetector
56
	 * @param IPreview $previewManager
57
	 */
58 View Code Duplication
	public function __construct($appName,
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
								IRequest $request,
60
								IRootFolder $rootFolder,
61
								$userId,
62
								IMimeTypeDetector $mimeTypeDetector,
63
								IPreview $previewManager) {
64
		parent::__construct($appName, $request);
65
66
		$this->rootFolder = $rootFolder;
67
		$this->userId = $userId;
68
		$this->mimeTypeDetector = $mimeTypeDetector;
69
		$this->previewManager = $previewManager;
70
	}
71
72
	/**
73
	 * @NoAdminRequired
74
	 * @NoCSRFRequired
75
	 *
76
	 * @param string $file
77
	 * @param int $x
78
	 * @param int $y
79
	 * @return DataResponse|Http\FileDisplayResponse
80
	 */
81
	public function getPreview(
82
		$file = '',
83
		$x = 44,
84
		$y = 44
85
	) {
86
		if ($file === '') {
87
			return new DataResponse([], Http::STATUS_BAD_REQUEST);
88
		}
89
90
		if ($x === 0 || $y === 0) {
91
			return new DataResponse([], Http::STATUS_BAD_REQUEST);
92
		}
93
94
		try {
95
			$userFolder = $this->rootFolder->getUserFolder($this->userId);
96
			/** @var Folder $trash */
97
			$trash = $userFolder->getParent()->get('files_trashbin/files');
98
			$trashFile = $trash->get($file);
99
100
			if ($trashFile instanceof Folder) {
101
				return new DataResponse([], Http::STATUS_BAD_REQUEST);
102
			}
103
104
			/** @var File $trashFile */
105
			$fileName = $trashFile->getName();
106
			$i = strrpos($fileName, '.');
107
			if ($i !== false) {
108
				$fileName = substr($fileName, 0, $i);
109
			}
110
111
			$mimeType = $this->mimeTypeDetector->detectPath($fileName);
112
113
			$f = $this->previewManager->getPreview($trashFile, $x, $y, true, IPreview::MODE_FILL, $mimeType);
114
			return new Http\FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]);
115
		} catch (NotFoundException $e) {
116
			return new DataResponse([], Http::STATUS_NOT_FOUND);
117
		}
118
	}
119
}
120