UrlService::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 1
eloc 6
c 2
b 1
f 1
nc 1
nop 5
dl 0
loc 13
ccs 0
cts 7
cp 0
crap 2
rs 10
1
<?php
2
/**
3
 * @copyright Copyright (c) 2018-2021 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\Files\IRootFolder;
27
use OCP\Files\File;
28
use OCP\IUserSession;
29
use OCP\IURLGenerator;
30
31
use OCA\FaceRecognition\Helper\Requirements;
32
use OCA\FaceRecognition\Service\SettingsService;
33
34
35
class UrlService {
36
37
	/** @var IRootFolder */
38
	private $rootFolder;
39
40
	/** @var IUserSession */
41
	private $userSession;
42
43
	/** @var IURLGenerator */
44
	private $urlGenerator;
45
46
	/** @var SettingsService */
47
	private $settingsService;
48
49
	/** @var string */
50
	private $userId;
51
52
	/** */
53
	private $userFolder;
54
55
	public function __construct(IRootFolder     $rootFolder,
56
	                            IUserSession    $userSession,
57
	                            IURLGenerator   $urlGenerator,
58
	                            SettingsService $settingsService,
59
	                            $userId)
60
	{
61
		$this->rootFolder      = $rootFolder;
62
		$this->userSession     = $userSession;
63
		$this->urlGenerator    = $urlGenerator;
64
		$this->settingsService = $settingsService;
65
		$this->userId          = $userId;
66
67
		$this->userFolder      = $rootFolder->getUserFolder($userId);
68
	}
69
70
71
	/**
72
	 * Get to the Node file
73
	 *
74
	 * @param int $fileId file id to show
75
	 *
76
	 * @return null|File
77
	 */
78
	public function getFileNode(int $fileId): ?File {
79
		$files = $this->userFolder->getById($fileId);
80
		$file  = current($files);
81
		if(!($file instanceof File)) {
82
			// If we cannot find a file probably it was deleted out of our control and we must clean our tables.
83
			$this->settingsService->setNeedRemoveStaleImages(true, $this->userId);
84
			return null;
85
		}
86
		return $file;
87
	}
88
89
	public function getBasename(File $node): string {
90
		return $node->getName();
91
	}
92
93
	public function getFilename(File $node): ?string {
94
		return $this->userFolder->getRelativePath($node->getPath());
95
	}
96
97
	public function getMimetype(File $node): string {
98
		return $node->getMimetype();
99
	}
100
101
	/**
102
	 * Get thumbnail of the give file id
103
	 *
104
	 * @param File $file file to show
105
	 * @param int $sideSize side lenght to show
106
	 */
107
	public function getPreviewUrl(File $file, int $sideSize): string {
108
		return $this->urlGenerator->getAbsoluteURL('index.php/core/preview?fileId=' . $file->getId() .'&x=' . $sideSize . '&y=' . $sideSize . '&a=false&v=' . $file->getETag());
109
	}
110
111
	/**
112
	 * Redirects to the file list and highlight the given file id
113
	 *
114
	 * @param int $fileId file id to show
115
	 *
116
	 * @return null|string
117
	 */
118
	public function getRedirectToFileUrl(File $file): ?string {
119
		$params = [];
120
		$params['dir'] = $this->userFolder->getRelativePath($file->getParent()->getPath());
121
		$params['scrollto'] = $file->getName();
122
		return $this->urlGenerator->linkToRoute('files.view.index', $params);
123
	}
124
125
	/**
126
	 * Redirects to the facerecognition page to show photos of an person.
127
	 *
128
	 * @param string $personName
129
	 *
130
	 * @return string
131
	 */
132
	public function getRedirectToPersonUrl(string $personName): string {
133
		if (Requirements::memoriesIsInstalled()) {
134
			return $this->urlGenerator->linkToRouteAbsolute('memories.Page.main') . 'facerecognition/' . $this->userId . '/' . $personName ;
135
		}
136
		$params = [
137
			'section' => 'facerecognition',
138
			'name' => $personName
139
		];
140
		return $this->urlGenerator->linkToRoute('settings.PersonalSettings.index', $params);
141
	}
142
143
	/**
144
	 * Url to thumb face
145
	 *
146
	 * @param int $faceId face id to show
147
	 * @param int $size Size of face thumbnails
148
	 *
149
	 * @return string
150
	 */
151
	public function getThumbUrl(int $faceId, int $size): string {
152
		$params = [];
153
		$params['id'] = $faceId;
154
		$params['size'] = $size;
155
		return $this->urlGenerator->linkToRoute('facerecognition.face.getThumb', $params);
156
	}
157
158
	/**
159
	 * Url to thumb person
160
	 *
161
	 * @param string $name name person to show
162
	 * @param int $size Size of face thumbnails
163
	 *
164
	 * @return string
165
	 */
166
	public function getPersonThumbUrl(string $name, int $size): string {
167
		$params = [];
168
		$params['name'] = $name;
169
		$params['size'] = $size;
170
		return $this->urlGenerator->linkToRoute('facerecognition.face.getPersonThumb', $params);
171
	}
172
173
}
174