Passed
Push — split-cluster-person ( 441d8c...bb4f7f )
by Matias
05:18
created

UrlService   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 1 Features 1
Metric Value
eloc 36
c 3
b 1
f 1
dl 0
loc 97
ccs 0
cts 47
cp 0
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getThumbUrl() 0 5 1
A getPreviewUrl() 0 11 2
A __construct() 0 11 1
A getRedirectToFileUrl() 0 17 2
A getRedirectToPersonUrl() 0 6 1
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\Files\IRootFolder;
27
use OCP\Files\File;
28
use OCP\IUserSession;
29
use OCP\IURLGenerator;
30
31
use OCA\FaceRecognition\Service\SettingsService;
32
33
34
class UrlService {
35
36
	/** @var IRootFolder */
37
	private $rootFolder;
38
39
	/** @var IUserSession */
40
	private $userSession;
41
42
	/** @var IURLGenerator */
43
	private $urlGenerator;
44
45
	/** @var SettingsService */
46
	private $settingsService;
47
48
	/** @var string */
49
	private $userId;
50
51
	public function __construct(IRootFolder     $rootFolder,
52
	                            IUserSession    $userSession,
53
	                            IURLGenerator   $urlGenerator,
54
	                            SettingsService $settingsService,
55
	                            $UserId)
56
	{
57
		$this->rootFolder      = $rootFolder;
58
		$this->userSession     = $userSession;
59
		$this->urlGenerator    = $urlGenerator;
60
		$this->settingsService = $settingsService;
61
		$this->userId          = $UserId;
62
	}
63
64
	/**
65
	 * Url to thumb face
66
	 *
67
	 * @param int $faceId face id to show
68
	 * @param int $size Size of face thumbnails
69
	 */
70
	public function getThumbUrl(int $faceId, int $size) {
71
		$params = [];
72
		$params['id'] = $faceId;
73
		$params['size'] = $size;
74
		return $this->urlGenerator->linkToRoute('facerecognition.face.getThumb', $params);
75
	}
76
77
	/**
78
	 * Get thumbnail of the give file id
79
	 *
80
	 * @param int $fileId file id to show
81
	 * @param int $sideSize side lenght to show
82
	 */
83
	public function getPreviewUrl(int $fileId, int $sideSize): ?string {
84
		$userFolder = $this->rootFolder->getUserFolder($this->userId);
85
		$file = current($userFolder->getById($fileId));
86
87
		if (!($file instanceof File)) {
88
			// If we cannot find a file probably it was deleted out of our control and we must clean our tables.
89
			$this->settingsService->setNeedRemoveStaleImages(true, $this->userId);
90
			return null;
91
		}
92
93
		return '/core/preview?fileId=' . $fileId .'&x=' . $sideSize . '&y=' . $sideSize . '&a=false&v=' . $file->getETag();
94
	}
95
96
	/**
97
	 * Redirects to the file list and highlight the given file id
98
	 *
99
	 * @param int $fileId file id to show
100
	 */
101
	public function getRedirectToFileUrl(int $fileId) {
102
		$uid        = $this->userSession->getUser()->getUID();
103
		$baseFolder = $this->rootFolder->getUserFolder($uid);
104
		$files      = $baseFolder->getById($fileId);
105
		$file       = current($files);
106
107
		if(!($file instanceof File)) {
108
			// If we cannot find a file probably it was deleted out of our control and we must clean our tables.
109
			$this->settingsService->setNeedRemoveStaleImages(true, $this->userId);
110
			return null;
111
		}
112
113
		$params = [];
114
		$params['dir'] = $baseFolder->getRelativePath($file->getParent()->getPath());
115
		$params['scrollto'] = $file->getName();
116
117
		return $this->urlGenerator->linkToRoute('files.view.index', $params);
118
	}
119
120
	/**
121
	 * Redirects to the facerecognition page to show photos of an person.
122
	 *
123
	 * @param int $personId person id to show
124
	 */
125
	public function getRedirectToPersonUrl(string $personId) {
126
		$params = [
127
			'section' => 'facerecognition',
128
			'name' => $personId
129
		];
130
		return $this->urlGenerator->linkToRoute('settings.PersonalSettings.index', $params);
131
	}
132
133
}
134