mirror of https://github.com/nextcloud/server
Merge pull request #36252 from nextcloud/modernize-mime-typeloader
This commit is contained in:
commit
d15e45c2ff
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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')
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -23,15 +23,14 @@ namespace Test\Files\Type;
|
|||
|
||||
use OC\Files\Type\Loader;
|
||||
use OCP\IDBConnection;
|
||||
use Test\TestCase;
|
||||
|
||||
class LoaderTest extends \Test\TestCase {
|
||||
/** @var IDBConnection */
|
||||
protected $db;
|
||||
/** @var Loader */
|
||||
protected $loader;
|
||||
class LoaderTest extends TestCase {
|
||||
protected IDBConnection $db;
|
||||
protected Loader $loader;
|
||||
|
||||
protected function setUp(): void {
|
||||
$this->db = \OC::$server->getDatabaseConnection();
|
||||
$this->db = \OC::$server->get(IDBConnection::class);
|
||||
$this->loader = new Loader($this->db);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue