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

PreviewController::getPreview()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 24
Code Lines 16

Duplication

Lines 3
Ratio 12.5 %

Importance

Changes 0
Metric Value
cc 6
eloc 16
nc 8
nop 4
dl 3
loc 24
rs 8.5125
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_Versions\Controller;
24
25
use OCP\AppFramework\Controller;
26
use OCP\AppFramework\Http;
27
use OCP\AppFramework\Http\DataResponse;
28
use OCP\AppFramework\Http\FileDisplayResponse;
29
use OCP\Files\File;
30
use OCP\Files\Folder;
31
use OCP\Files\IMimeTypeDetector;
32
use OCP\Files\IRootFolder;
33
use OCP\Files\NotFoundException;
34
use OCP\IPreview;
35
use OCP\IRequest;
36
37
class PreviewController extends Controller {
38
39
	/** @var IRootFolder */
40
	private $rootFolder;
41
42
	/** @var string */
43
	private $userId;
44
45
	/** @var IMimeTypeDetector */
46
	private $mimeTypeDetector;
47
48
	/** @var IPreview */
49
	private $previewManager;
50
51 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...
52
								IRequest $request,
53
								IRootFolder $rootFolder,
54
								$userId,
55
								IMimeTypeDetector $mimeTypeDetector,
56
								IPreview $previewManager) {
57
		parent::__construct($appName, $request);
58
59
		$this->rootFolder = $rootFolder;
60
		$this->userId = $userId;
61
		$this->mimeTypeDetector = $mimeTypeDetector;
62
		$this->previewManager = $previewManager;
63
	}
64
65
	/**
66
	 * @NoAdminRequired
67
	 * @NoCSRFRequired
68
	 *
69
	 * @param string $file
70
	 * @param int $x
71
	 * @param int $y
72
	 * @param string $version
73
	 * @return DataResponse|FileDisplayResponse
74
	 */
75
	public function getPreview(
76
		$file = '',
77
		$x = 44,
78
		$y = 44,
79
		$version = ''
80
	) {
81 View Code Duplication
		if($file === '' || $version === '' || $x === 0 || $y === 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
82
			return new DataResponse([], Http::STATUS_BAD_REQUEST);
83
		}
84
85
		try {
86
			$userFolder = $this->rootFolder->getUserFolder($this->userId);
87
			/** @var Folder $versionFolder */
88
			$versionFolder = $userFolder->getParent()->get('files_versions');
89
			$mimeType = $this->mimeTypeDetector->detectPath($file);
90
			$file = $versionFolder->get($file.'.v'.$version);
91
92
			/** @var File $file */
93
			$f = $this->previewManager->getPreview($file, $x, $y, true, IPreview::MODE_FILL, $mimeType);
94
			return new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]);
95
		} catch (NotFoundException $e) {
96
			return new DataResponse([], Http::STATUS_NOT_FOUND);
97
		}
98
	}
99
}
100