refactor(mimeloader): modernize MimeTypeLoader

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
pull/36252/head
Thomas Citharel 2023-10-19 16:27:07 +07:00
parent f316edb9ff
commit ffeb797ecc
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773
4 changed files with 27 additions and 40 deletions

@ -228,18 +228,18 @@ class SearchBuilder {
if ($field === 'mimetype') { if ($field === 'mimetype') {
$value = (string)$value; $value = (string)$value;
if ($type === ISearchComparison::COMPARE_EQUAL) { if ($type === ISearchComparison::COMPARE_EQUAL) {
$value = (int)$this->mimetypeLoader->getId($value); $value = $this->mimetypeLoader->getId($value);
} elseif ($type === ISearchComparison::COMPARE_LIKE) { } elseif ($type === ISearchComparison::COMPARE_LIKE) {
// transform "mimetype='foo/%'" to "mimepart='foo'" // transform "mimetype='foo/%'" to "mimepart='foo'"
if (preg_match('|(.+)/%|', $value, $matches)) { if (preg_match('|(.+)/%|', $value, $matches)) {
$field = 'mimepart'; $field = 'mimepart';
$value = (int)$this->mimetypeLoader->getId($matches[1]); $value = $this->mimetypeLoader->getId($matches[1]);
$type = ISearchComparison::COMPARE_EQUAL; $type = ISearchComparison::COMPARE_EQUAL;
} elseif (str_contains($value, '%')) { } elseif (str_contains($value, '%')) {
throw new \InvalidArgumentException('Unsupported query value for mimetype: ' . $value . ', only values in the format "mime/type" or "mime/%" are supported'); throw new \InvalidArgumentException('Unsupported query value for mimetype: ' . $value . ', only values in the format "mime/type" or "mime/%" are supported');
} else { } else {
$field = 'mimetype'; $field = 'mimetype';
$value = (int)$this->mimetypeLoader->getId($value); $value = $this->mimetypeLoader->getId($value);
$type = ISearchComparison::COMPARE_EQUAL; $type = ISearchComparison::COMPARE_EQUAL;
} }
} }

@ -38,14 +38,13 @@ use OCP\IDBConnection;
class Loader implements IMimeTypeLoader { class Loader implements IMimeTypeLoader {
use TTransactional; use TTransactional;
/** @var IDBConnection */ private IDBConnection $dbConnection;
private $dbConnection;
/** @var array [id => mimetype] */ /** @psalm-var array<int, string> */
protected $mimetypes; protected array $mimetypes;
/** @var array [mimetype => id] */ /** @psalm-var array<string, int> */
protected $mimetypeIds; protected array $mimetypeIds;
/** /**
* @param IDBConnection $dbConnection * @param IDBConnection $dbConnection
@ -58,11 +57,8 @@ class Loader implements IMimeTypeLoader {
/** /**
* Get a mimetype from its ID * 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) { if (!$this->mimetypes) {
$this->loadMimetypes(); $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 * 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) { if (!$this->mimetypeIds) {
$this->loadMimetypes(); $this->loadMimetypes();
} }
@ -90,11 +83,8 @@ class Loader implements IMimeTypeLoader {
/** /**
* Test if a mimetype exists in the database * 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) { if (!$this->mimetypeIds) {
$this->loadMimetypes(); $this->loadMimetypes();
} }
@ -104,7 +94,7 @@ class Loader implements IMimeTypeLoader {
/** /**
* Clear all loaded mimetypes, allow for re-loading * Clear all loaded mimetypes, allow for re-loading
*/ */
public function reset() { public function reset(): void {
$this->mimetypes = []; $this->mimetypes = [];
$this->mimetypeIds = []; $this->mimetypeIds = [];
} }
@ -115,7 +105,7 @@ class Loader implements IMimeTypeLoader {
* @param string $mimetype * @param string $mimetype
* @return int inserted ID * @return int inserted ID
*/ */
protected function store($mimetype) { protected function store(string $mimetype): int {
try { try {
$mimetypeId = $this->atomic(function () use ($mimetype) { $mimetypeId = $this->atomic(function () use ($mimetype) {
$insert = $this->dbConnection->getQueryBuilder(); $insert = $this->dbConnection->getQueryBuilder();
@ -153,29 +143,27 @@ class Loader implements IMimeTypeLoader {
/** /**
* Load all mimetypes from DB * Load all mimetypes from DB
*/ */
private function loadMimetypes() { private function loadMimetypes(): void {
$qb = $this->dbConnection->getQueryBuilder(); $qb = $this->dbConnection->getQueryBuilder();
$qb->select('id', 'mimetype') $qb->select('id', 'mimetype')
->from('mimetypes'); ->from('mimetypes');
$result = $qb->execute(); $result = $qb->executeQuery();
$results = $result->fetchAll(); $results = $result->fetchAll();
$result->closeCursor(); $result->closeCursor();
foreach ($results as $row) { foreach ($results as $row) {
$this->mimetypes[$row['id']] = $row['mimetype']; $this->mimetypes[(int) $row['id']] = $row['mimetype'];
$this->mimetypeIds[$row['mimetype']] = $row['id']; $this->mimetypeIds[$row['mimetype']] = (int) $row['id'];
} }
} }
/** /**
* Update filecache mimetype based on file extension * Update filecache mimetype based on file extension
* *
* @param string $ext file extension
* @param int $mimeTypeId
* @return int number of changed rows * @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'); $folderMimeTypeId = $this->getId('httpd/unix-directory');
$update = $this->dbConnection->getQueryBuilder(); $update = $this->dbConnection->getQueryBuilder();
$update->update('filecache') $update->update('filecache')

@ -35,7 +35,7 @@ interface IMimeTypeLoader {
* @return string|null * @return string|null
* @since 8.2.0 * @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 * Get a mimetype ID, adding the mimetype to the DB if it does not exist
@ -44,7 +44,7 @@ interface IMimeTypeLoader {
* @return int * @return int
* @since 8.2.0 * @since 8.2.0
*/ */
public function getId($mimetype); public function getId(string $mimetype): int;
/** /**
* Test if a mimetype exists in the database * Test if a mimetype exists in the database
@ -53,12 +53,12 @@ interface IMimeTypeLoader {
* @return bool * @return bool
* @since 8.2.0 * @since 8.2.0
*/ */
public function exists($mimetype); public function exists(string $mimetype): bool;
/** /**
* Clear all loaded mimetypes, allow for re-loading * Clear all loaded mimetypes, allow for re-loading
* *
* @since 8.2.0 * @since 8.2.0
*/ */
public function reset(); public function reset(): void;
} }

@ -23,15 +23,14 @@ namespace Test\Files\Type;
use OC\Files\Type\Loader; use OC\Files\Type\Loader;
use OCP\IDBConnection; use OCP\IDBConnection;
use Test\TestCase;
class LoaderTest extends \Test\TestCase { class LoaderTest extends TestCase {
/** @var IDBConnection */ protected IDBConnection $db;
protected $db; protected Loader $loader;
/** @var Loader */
protected $loader;
protected function setUp(): void { protected function setUp(): void {
$this->db = \OC::$server->getDatabaseConnection(); $this->db = \OC::$server->get(IDBConnection::class);
$this->loader = new Loader($this->db); $this->loader = new Loader($this->db);
} }