Skip to content

Commit

Permalink
fix(mimetype): Fix returning value when finding existing mimetype in …
Browse files Browse the repository at this point in the history
…MimeType Loader

Also, only return the ID from the transaction as the mimetype string is
already used from the function argument value

Fixes https://github.com/nextcloud/server/pull/35744/files#r1184644610

Signed-off-by: Thomas Citharel <[email protected]>
  • Loading branch information
tcitworld committed May 4, 2023
1 parent b44de9c commit a73e15f
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions lib/private/Files/Type/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,42 +116,36 @@ public function reset() {
* @return int inserted ID
*/
protected function store($mimetype) {
$row = $this->atomic(function () use ($mimetype) {
$mimetypeId = $this->atomic(function () use ($mimetype) {
try {
$insert = $this->dbConnection->getQueryBuilder();
$insert->insert('mimetypes')
->values([
'mimetype' => $insert->createNamedParameter($mimetype)
])
->executeStatement();
return [
'mimetype' => $mimetype,
'id' => $insert->getLastInsertId(),
];
return $insert->getLastInsertId();
} catch (DbalException $e) {
if ($e->getReason() !== DBException::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
throw $e;
}
$qb = $this->dbConnection->getQueryBuilder();
$row = $qb->select('id')
$qb->select('id')
->from('mimetypes')
->where($qb->expr()->eq('mimetype', $qb->createNamedParameter($mimetype)))
->executeQuery()
->fetchOne();
if ($row) {
return [
'mimetype' => $mimetype,
'id' => $row['id'],
];
->where($qb->expr()->eq('mimetype', $qb->createNamedParameter($mimetype)));
$result = $qb->executeQuery();
$id = (int) $result->fetchOne();
$result->closeCursor();
if ($id) {
return $id;
}
throw new \Exception("Database threw an unique constraint on inserting a new mimetype, but couldn't return the ID for this very mimetype");
}
}, $this->dbConnection);

if (!$row) {
if (!$mimetypeId) {
throw new \Exception("Failed to get mimetype id for $mimetype after trying to store it");
}
$mimetypeId = (int) $row['id'];

$this->mimetypes[$mimetypeId] = $mimetype;
$this->mimetypeIds[$mimetype] = $mimetypeId;
Expand Down

0 comments on commit a73e15f

Please sign in to comment.