Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize mime typeloader #36252

Merged
merged 1 commit into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/private/Files/Cache/SearchBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,18 +228,18 @@ private function getOperatorFieldAndValueInner(string $field, mixed $value, stri
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;
}
}
Expand Down
42 changes: 15 additions & 27 deletions lib/private/Files/Type/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,13 @@
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
Expand All @@ -58,11 +57,8 @@ public function __construct(IDBConnection $dbConnection) {

/**
* 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();
}
Expand All @@ -74,11 +70,8 @@ public function getMimetypeById($id) {

/**
* 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();
}
Expand All @@ -90,11 +83,8 @@ public function getId($mimetype) {

/**
* 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();
}
Expand All @@ -104,7 +94,7 @@ public function exists($mimetype) {
/**
* Clear all loaded mimetypes, allow for re-loading
*/
public function reset() {
public function reset(): void {
$this->mimetypes = [];
$this->mimetypeIds = [];
}
Expand All @@ -115,7 +105,7 @@ public function reset() {
* @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();
Expand Down Expand Up @@ -153,29 +143,27 @@ protected function store($mimetype) {
/**
* 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')
Expand Down
8 changes: 4 additions & 4 deletions lib/public/Files/IMimeTypeLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ interface IMimeTypeLoader {
* @return string|null
* @since 8.2.0
*/
public function getMimetypeById($id);
public function getMimetypeById(int $id): ?string;
tcitworld marked this conversation as resolved.
Show resolved Hide resolved

/**
* Get a mimetype ID, adding the mimetype to the DB if it does not exist
Expand All @@ -44,7 +44,7 @@ public function getMimetypeById($id);
* @return int
* @since 8.2.0
*/
public function getId($mimetype);
public function getId(string $mimetype): int;

/**
* Test if a mimetype exists in the database
Expand All @@ -53,12 +53,12 @@ public function getId($mimetype);
* @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;
}
11 changes: 5 additions & 6 deletions tests/lib/Files/Type/LoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@

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);
}

Expand Down
Loading