-
Notifications
You must be signed in to change notification settings - Fork 197
/
LibrarySettings.php
168 lines (142 loc) · 5.53 KB
/
LibrarySettings.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php declare(strict_types=1);
/**
* ownCloud - Music app
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Pauli Järvinen <pauli.jarvinen@gmail.com>
* @copyright Pauli Järvinen 2019 - 2022
*/
namespace OCA\Music\Utility;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\IConfig;
use OCA\Music\AppFramework\Core\Logger;
/**
* Manage the user-specific music folder setting
*/
class LibrarySettings {
private $appName;
private $configManager;
private $rootFolder;
private $logger;
public function __construct(
string $appName,
IConfig $configManager,
IRootFolder $rootFolder,
Logger $logger) {
$this->appName = $appName;
$this->configManager = $configManager;
$this->rootFolder = $rootFolder;
$this->logger = $logger;
}
public function setScanMetadataEnabled(string $userId, bool $enabled) : void {
$this->configManager->setUserValue($userId, $this->appName, 'scan_metadata', $enabled ? 1 : 0);
}
public function getScanMetadataEnabled(string $userId) : bool {
$value = $this->configManager->getUserValue($userId, $this->appName, 'scan_metadata', 1);
return ($value > 0);
}
public function setIgnoredArticles(string $userId, array $articles) : void {
$this->configManager->setUserValue($userId, $this->appName, 'ignored_articles', \json_encode($articles));
}
public function getIgnoredArticles(string $userId) : array {
$default = '["The", "El", "La", "Los", "Las", "Le", "Les"]';
$value = $this->configManager->getUserValue($userId, $this->appName, 'ignored_articles', $default);
return \json_decode($value);
}
public function setPath(string $userId, string $path) : bool {
$success = false;
$userHome = $this->rootFolder->getUserFolder($userId);
$element = $userHome->get($path);
if ($element instanceof \OCP\Files\Folder) {
if ($path[0] !== '/') {
$path = '/' . $path;
}
if ($path[\strlen($path)-1] !== '/') {
$path .= '/';
}
$this->configManager->setUserValue($userId, $this->appName, 'path', $path);
$success = true;
}
return $success;
}
public function getPath(string $userId) : string {
$path = $this->configManager->getUserValue($userId, $this->appName, 'path');
return $path ?: '/';
}
/**
* @param string[] $paths
*/
public function setExcludedPaths(string $userId, array $paths) : bool {
$this->configManager->setUserValue($userId, $this->appName, 'excluded_paths', \json_encode($paths));
return true;
}
/**
* @return string[]
*/
public function getExcludedPaths(string $userId) : array {
$paths = $this->configManager->getUserValue($userId, $this->appName, 'excluded_paths');
if (empty($paths)) {
return [];
} else {
return \json_decode($paths);
}
}
public function getFolder(string $userId) : Folder {
$userHome = $this->rootFolder->getUserFolder($userId);
$path = $this->getPath($userId);
return Util::getFolderFromRelativePath($userHome, $path);
}
public function pathBelongsToMusicLibrary(string $filePath, string $userId) : bool {
$filePath = self::normalizePath($filePath);
$musicPath = self::normalizePath($this->getFolder($userId)->getPath());
return Util::startsWith($filePath, $musicPath)
&& !$this->pathIsExcluded($filePath, $musicPath, $userId);
}
private function pathIsExcluded(string $filePath, string $musicPath, string $userId) : bool {
$userRootPath = $this->rootFolder->getUserFolder($userId)->getPath();
$excludedPaths = $this->getExcludedPaths($userId);
foreach ($excludedPaths as $excludedPath) {
if (Util::startsWith($excludedPath, '/')) {
$excludedPath = $userRootPath . $excludedPath;
} else {
$excludedPath = $musicPath . '/' . $excludedPath;
}
if (self::pathMatchesPattern($filePath, $excludedPath)) {
return true;
}
}
return false;
}
private static function pathMatchesPattern(string $path, string $pattern) : bool {
// normalize the pattern so that there is no trailing '/'
$pattern = \rtrim($pattern, '/');
if (\strpos($pattern, '*') === false && \strpos($pattern, '?') === false) {
// no wildcards, begininning of the path should match the pattern exactly
// and the next character after the matching part (if any) should be '/'
$patternLen = \strlen($pattern);
return Util::startsWith($path, $pattern)
&& (\strlen($path) === $patternLen || $path[$patternLen] === '/');
} else {
// some wildcard characters in the pattern, convert the pattern into regex:
// - '?' matches exactly one arbitrary character except the directory separator '/'
// - '*' matches zero or more arbitrary characters except the directory separator '/'
// - '**' matches zero or more arbitrary characters including directory separator '/'
$pattern = \preg_quote($pattern, '/'); // escape regex meta characters
$pattern = \str_replace('\*\*', '.*', $pattern); // convert ** to its regex equivaleant
$pattern = \str_replace('\*', '[^\/]*', $pattern); // convert * to its regex equivaleant
$pattern = \str_replace('\?', '[^\/]', $pattern); // convert ? to its regex equivaleant
$pattern = $pattern . '(\/.*)?$'; // after given pattern, there should be '/' or nothing
$pattern = '/' . $pattern . '/';
return (\preg_match($pattern, $path) === 1);
}
}
private static function normalizePath(string $path) : string {
// The file system may create paths where there are two consecutive
// path seprator characters (/). This was seen with an external local
// folder on NC13, but may apply to other cases, too. Normalize such paths.
return \str_replace('//', '/', $path);
}
}