Passed
Push — split-cluster-person ( 45a2b1...5162aa )
by Matias
05:48
created

UrlService::getThumbUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 5
ccs 0
cts 5
cp 0
crap 2
rs 10
1
<?php
2
/**
3
 * @copyright Copyright (c) 2018-2020 Matias De lellis <[email protected]>
4
 *
5
 * @author Matias De lellis <[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\FaceRecognition\Service;
25
26
use OCP\IRequest;
27
use OCP\Files\IRootFolder;
28
use OCP\Files\File;
29
use OCP\IUserSession;
30
use OCP\IURLGenerator;
31
32
use OCP\AppFramework\Http;
33
use OCP\AppFramework\Http\DataResponse;
34
use OCP\AppFramework\Http\JSONResponse;
35
use OCP\AppFramework\Http\DataDisplayResponse;
36
use OCP\AppFramework\Controller;
37
38
use OCA\FaceRecognition\Service\SettingsService;
39
40
41
class UrlService {
42
43
	/** @var IRootFolder */
44
	private $rootFolder;
45
46
	/** @var IUserSession */
47
	private $userSession;
48
49
	/** @var IURLGenerator */
50
	private $urlGenerator;
51
52
	/** @var SettingsService */
53
	private $settingsService;
54
55
	/** @var string */
56
	private $userId;
57
58
	public function __construct($AppName,
0 ignored issues
show
Unused Code introduced by
The parameter $AppName is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

58
	public function __construct(/** @scrutinizer ignore-unused */ $AppName,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
59
	                            IRequest        $request,
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

59
	                            /** @scrutinizer ignore-unused */ IRequest        $request,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
60
	                            IRootFolder     $rootFolder,
61
	                            IUserSession    $userSession,
62
	                            IURLGenerator   $urlGenerator,
63
	                            SettingsService $settingsService,
64
	                            $UserId)
65
	{
66
		$this->rootFolder      = $rootFolder;
67
		$this->userSession     = $userSession;
68
		$this->urlGenerator    = $urlGenerator;
69
		$this->settingsService = $settingsService;
70
		$this->userId          = $UserId;
71
	}
72
73
	/**
74
	 * Url to thumb face
75
	 *
76
	 * @param int $faceId face id to show
77
	 * @param int $size Size of face thumbnails
78
	 */
79
	public function getThumbUrl(int $faceId, int $size) {
80
		$params = [];
81
		$params['id'] = $faceId;
82
		$params['size'] = $size;
83
		return $this->urlGenerator->linkToRoute('facerecognition.face.getThumb', $params);
84
	}
85
86
	/**
87
	 * Get thumbnail of the give file id
88
	 *
89
	 * @param int $fileId file id to show
90
	 * @param int $sideSize side lenght to show
91
	 */
92
	public function getPreviewUrl(int $fileId, int $sideSize): ?string {
93
		$userFolder = $this->rootFolder->getUserFolder($this->userId);
94
		$file = current($userFolder->getById($fileId));
95
96
		if (!($file instanceof File)) {
97
			// If we cannot find a file probably it was deleted out of our control and we must clean our tables.
98
			$this->settingsService->setNeedRemoveStaleImages(true, $this->userId);
99
			return null;
100
		}
101
102
		return '/core/preview?fileId=' . $fileId .'&x=' . $sideSize . '&y=' . $sideSize . '&a=false&v=' . $file->getETag();
103
	}
104
105
	/**
106
	 * Redirects to the file list and highlight the given file id
107
	 *
108
	 * @param int $fileId file id to show
109
	 */
110
	public function getRedirectToFileUrl(int $fileId) {
111
		$uid        = $this->userSession->getUser()->getUID();
112
		$baseFolder = $this->rootFolder->getUserFolder($uid);
113
		$files      = $baseFolder->getById($fileId);
114
		$file       = current($files);
115
116
		if(!($file instanceof File)) {
117
			// If we cannot find a file probably it was deleted out of our control and we must clean our tables.
118
			$this->settingsService->setNeedRemoveStaleImages(true, $this->userId);
119
			return null;
120
		}
121
122
		$params = [];
123
		$params['dir'] = $baseFolder->getRelativePath($file->getParent()->getPath());
124
		$params['scrollto'] = $file->getName();
125
126
		return $this->urlGenerator->linkToRoute('files.view.index', $params);
127
	}
128
129
}
130