aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorThomas Citharel <tcit@tcit.fr>2023-10-19 16:27:07 +0200
committerThomas Citharel <tcit@tcit.fr>2024-03-14 17:24:36 +0100
commitffeb797eccfc90e5cf53d5737d58bfb1346f8718 (patch)
tree45365f709a97182e4cdaebe9f530e23e4ff89cc4 /lib
parentf316edb9ff448077fa8494dbbb2c6fdacd158bed (diff)
downloadnextcloud-server-ffeb797eccfc90e5cf53d5737d58bfb1346f8718.tar.gz
nextcloud-server-ffeb797eccfc90e5cf53d5737d58bfb1346f8718.zip
refactor(mimeloader): modernize MimeTypeLoader
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
Diffstat (limited to 'lib')
-rw-r--r--lib/private/Files/Cache/SearchBuilder.php6
-rw-r--r--lib/private/Files/Type/Loader.php42
-rw-r--r--lib/public/Files/IMimeTypeLoader.php8
3 files changed, 22 insertions, 34 deletions
diff --git a/lib/private/Files/Cache/SearchBuilder.php b/lib/private/Files/Cache/SearchBuilder.php
index 9d306db7748..e8063b77aa5 100644
--- a/lib/private/Files/Cache/SearchBuilder.php
+++ b/lib/private/Files/Cache/SearchBuilder.php
@@ -228,18 +228,18 @@ class SearchBuilder {
if ($field === 'mimetype') {
$value = (string)$value;
if ($type === ISearchComparison::COMPARE_EQUAL) {
- $value = (int)$this->mimetypeLoader->getId($value);
+ $value = $this->mimetypeLoader->getId($value);
} elseif ($type === ISearchComparison::COMPARE_LIKE) {
// transform "mimetype='foo/%'" to "mimepart='foo'"
if (preg_match('|(.+)/%|', $value, $matches)) {
$field = 'mimepart';
- $value = (int)$this->mimetypeLoader->getId($matches[1]);
+ $value = $this->mimetypeLoader->getId($matches[1]);
$type = ISearchComparison::COMPARE_EQUAL;
} elseif (str_contains($value, '%')) {
throw new \InvalidArgumentException('Unsupported query value for mimetype: ' . $value . ', only values in the format "mime/type" or "mime/%" are supported');
} else {
$field = 'mimetype';
- $value = (int)$this->mimetypeLoader->getId($value);
+ $value = $this->mimetypeLoader->getId($value);
$type = ISearchComparison::COMPARE_EQUAL;
}
}
diff --git a/lib/private/Files/Type/Loader.php b/lib/private/Files/Type/Loader.php
index 7032e619385..f0dbf0c665a 100644
--- a/lib/private/Files/Type/Loader.php
+++ b/lib/private/Files/Type/Loader.php
@@ -38,14 +38,13 @@ use OCP\IDBConnection;
class Loader implements IMimeTypeLoader {
use TTransactional;
- /** @var IDBConnection */
- private $dbConnection;
+ private IDBConnection $dbConnection;
- /** @var array [id => mimetype] */
- protected $mimetypes;
+ /** @psalm-var array<int, string> */
+ protected array $mimetypes;
- /** @var array [mimetype => id] */
- protected $mimetypeIds;
+ /** @psalm-var array<string, int> */
+ protected array $mimetypeIds;
/**
* @param IDBConnection $dbConnection
@@ -58,11 +57,8 @@ class Loader implements IMimeTypeLoader {
/**
* Get a mimetype from its ID
- *
- * @param int $id
- * @return string|null
*/
- public function getMimetypeById($id) {
+ public function getMimetypeById(int $id): ?string {
if (!$this->mimetypes) {
$this->loadMimetypes();
}
@@ -74,11 +70,8 @@ class Loader implements IMimeTypeLoader {
/**
* Get a mimetype ID, adding the mimetype to the DB if it does not exist
- *
- * @param string $mimetype
- * @return int
*/
- public function getId($mimetype) {
+ public function getId(string $mimetype): int {
if (!$this->mimetypeIds) {
$this->loadMimetypes();
}
@@ -90,11 +83,8 @@ class Loader implements IMimeTypeLoader {
/**
* Test if a mimetype exists in the database
- *
- * @param string $mimetype
- * @return bool
*/
- public function exists($mimetype) {
+ public function exists(string $mimetype): bool {
if (!$this->mimetypeIds) {
$this->loadMimetypes();
}
@@ -104,7 +94,7 @@ class Loader implements IMimeTypeLoader {
/**
* Clear all loaded mimetypes, allow for re-loading
*/
- public function reset() {
+ public function reset(): void {
$this->mimetypes = [];
$this->mimetypeIds = [];
}
@@ -115,7 +105,7 @@ class Loader implements IMimeTypeLoader {
* @param string $mimetype
* @return int inserted ID
*/
- protected function store($mimetype) {
+ protected function store(string $mimetype): int {
try {
$mimetypeId = $this->atomic(function () use ($mimetype) {
$insert = $this->dbConnection->getQueryBuilder();
@@ -153,29 +143,27 @@ class Loader implements IMimeTypeLoader {
/**
* Load all mimetypes from DB
*/
- private function loadMimetypes() {
+ private function loadMimetypes(): void {
$qb = $this->dbConnection->getQueryBuilder();
$qb->select('id', 'mimetype')
->from('mimetypes');
- $result = $qb->execute();
+ $result = $qb->executeQuery();
$results = $result->fetchAll();
$result->closeCursor();
foreach ($results as $row) {
- $this->mimetypes[$row['id']] = $row['mimetype'];
- $this->mimetypeIds[$row['mimetype']] = $row['id'];
+ $this->mimetypes[(int) $row['id']] = $row['mimetype'];
+ $this->mimetypeIds[$row['mimetype']] = (int) $row['id'];
}
}
/**
* Update filecache mimetype based on file extension
*
- * @param string $ext file extension
- * @param int $mimeTypeId
* @return int number of changed rows
*/
- public function updateFilecache($ext, $mimeTypeId) {
+ public function updateFilecache(string $ext, int $mimeTypeId): int {
$folderMimeTypeId = $this->getId('httpd/unix-directory');
$update = $this->dbConnection->getQueryBuilder();
$update->update('filecache')
diff --git a/lib/public/Files/IMimeTypeLoader.php b/lib/public/Files/IMimeTypeLoader.php
index d4f2767bc17..39a5db85c1a 100644
--- a/lib/public/Files/IMimeTypeLoader.php
+++ b/lib/public/Files/IMimeTypeLoader.php
@@ -35,7 +35,7 @@ interface IMimeTypeLoader {
* @return string|null
* @since 8.2.0
*/
- public function getMimetypeById($id);
+ public function getMimetypeById(int $id): ?string;
/**
* Get a mimetype ID, adding the mimetype to the DB if it does not exist
@@ -44,7 +44,7 @@ interface IMimeTypeLoader {
* @return int
* @since 8.2.0
*/
- public function getId($mimetype);
+ public function getId(string $mimetype): int;
/**
* Test if a mimetype exists in the database
@@ -53,12 +53,12 @@ interface IMimeTypeLoader {
* @return bool
* @since 8.2.0
*/
- public function exists($mimetype);
+ public function exists(string $mimetype): bool;
/**
* Clear all loaded mimetypes, allow for re-loading
*
* @since 8.2.0
*/
- public function reset();
+ public function reset(): void;
}