7 changed files with 280 additions and 97 deletions
-
6apps/files_sharing/lib/cache.php
-
96lib/private/files/cache/cache.php
-
165lib/private/files/type/loader.php
-
14lib/private/server.php
-
59lib/public/files/imimetypeloader.php
-
8lib/public/iservercontainer.php
-
29tests/lib/repair/repairmimetypes.php
@ -0,0 +1,165 @@ |
|||
<?php |
|||
/** |
|||
* @author Robin McCorkell <rmccorkell@owncloud.com> |
|||
* |
|||
* @copyright Copyright (c) 2015, ownCloud, Inc. |
|||
* @license AGPL-3.0 |
|||
* |
|||
* This code is free software: you can redistribute it and/or modify |
|||
* it under the terms of the GNU Affero General Public License, version 3, |
|||
* as published by the Free Software Foundation. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU Affero General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU Affero General Public License, version 3, |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|||
* |
|||
*/ |
|||
|
|||
namespace OC\Files\Type; |
|||
|
|||
use OCP\Files\IMimeTypeLoader; |
|||
use OCP\IDBConnection; |
|||
|
|||
use Doctrine\DBAL\Exception\UniqueConstraintViolationException; |
|||
|
|||
/** |
|||
* Mimetype database loader |
|||
* |
|||
* @package OC\Files\Type |
|||
*/ |
|||
class Loader implements IMimeTypeLoader { |
|||
|
|||
/** @var IDBConnection */ |
|||
private $dbConnection; |
|||
|
|||
/** @var array [id => mimetype] */ |
|||
protected $mimetypes; |
|||
|
|||
/** @var array [mimetype => id] */ |
|||
protected $mimetypeIds; |
|||
|
|||
/** |
|||
* @param IDBConnection $dbConnection |
|||
*/ |
|||
public function __construct(IDBConnection $dbConnection) { |
|||
$this->dbConnection = $dbConnection; |
|||
$this->mimetypes = []; |
|||
$this->mimetypeIds = []; |
|||
} |
|||
|
|||
/** |
|||
* Get a mimetype from its ID |
|||
* |
|||
* @param int $id |
|||
* @return string|null |
|||
*/ |
|||
public function getMimetypeById($id) { |
|||
if (!$this->mimetypes) { |
|||
$this->loadMimetypes(); |
|||
} |
|||
if (isset($this->mimetypes[$id])) { |
|||
return $this->mimetypes[$id]; |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
/** |
|||
* Get a mimetype ID, adding the mimetype to the DB if it does not exist |
|||
* |
|||
* @param string $mimetype |
|||
* @return int |
|||
*/ |
|||
public function getId($mimetype) { |
|||
if (!$this->mimetypeIds) { |
|||
$this->loadMimetypes(); |
|||
} |
|||
if (isset($this->mimetypeIds[$mimetype])) { |
|||
return $this->mimetypeIds[$mimetype]; |
|||
} |
|||
return $this->store($mimetype); |
|||
} |
|||
|
|||
/** |
|||
* Test if a mimetype exists in the database |
|||
* |
|||
* @param string $mimetype |
|||
* @return bool |
|||
*/ |
|||
public function exists($mimetype) { |
|||
if (!$this->mimetypeIds) { |
|||
$this->loadMimetypes(); |
|||
} |
|||
return isset($this->mimetypeIds[$mimetype]); |
|||
} |
|||
|
|||
/** |
|||
* Store a mimetype in the DB |
|||
* |
|||
* @param string $mimetype |
|||
* @param int inserted ID |
|||
*/ |
|||
protected function store($mimetype) { |
|||
try { |
|||
$qb = $this->dbConnection->getQueryBuilder(); |
|||
$qb->insert('mimetypes') |
|||
->values([ |
|||
'mimetype' => $qb->createNamedParameter($mimetype) |
|||
]); |
|||
$qb->execute(); |
|||
} catch (UniqueConstraintViolationException $e) { |
|||
// something inserted it before us
|
|||
} |
|||
|
|||
$fetch = $this->dbConnection->getQueryBuilder(); |
|||
$fetch->select('id') |
|||
->from('mimetypes') |
|||
->where( |
|||
$fetch->expr()->eq('mimetype', $fetch->createNamedParameter($mimetype) |
|||
)); |
|||
$row = $fetch->execute()->fetch(); |
|||
|
|||
$this->mimetypes[$row['id']] = $mimetype; |
|||
$this->mimetypeIds[$mimetype] = $row['id']; |
|||
return $row['id']; |
|||
} |
|||
|
|||
/** |
|||
* Load all mimetypes from DB |
|||
*/ |
|||
private function loadMimetypes() { |
|||
$qb = $this->dbConnection->getQueryBuilder(); |
|||
$qb->select('id', 'mimetype') |
|||
->from('mimetypes'); |
|||
$results = $qb->execute()->fetchAll(); |
|||
|
|||
foreach ($results as $row) { |
|||
$this->mimetypes[$row['id']] = $row['mimetype']; |
|||
$this->mimetypeIds[$row['mimetype']] = $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) { |
|||
$update = $this->dbConnection->getQueryBuilder(); |
|||
$update->update('filecache') |
|||
->set('mimetype', $update->createNamedParameter($mimetypeId)) |
|||
->where($update->expr()->neq( |
|||
'mimetype', $update->createNamedParameter($mimetypeId) |
|||
)) |
|||
->andWhere($update->expr()->like( |
|||
$update->createFunction('LOWER(`name`)'), $update->createNamedParameter($ext) |
|||
)); |
|||
return $update->execute(); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,59 @@ |
|||
<?php |
|||
/** |
|||
* @author Robin McCorkell <rmccorkell@owncloud.com> |
|||
* |
|||
* @copyright Copyright (c) 2015, ownCloud, Inc. |
|||
* @license AGPL-3.0 |
|||
* |
|||
* This code is free software: you can redistribute it and/or modify |
|||
* it under the terms of the GNU Affero General Public License, version 3, |
|||
* as published by the Free Software Foundation. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU Affero General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU Affero General Public License, version 3, |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|||
* |
|||
*/ |
|||
|
|||
namespace OCP\Files; |
|||
|
|||
/** |
|||
* Interface IMimeTypeLoader |
|||
* @package OCP\Files |
|||
* @since 8.2.0 |
|||
* |
|||
* Interface to load mimetypes |
|||
**/ |
|||
interface IMimeTypeLoader { |
|||
|
|||
/** |
|||
* Get a mimetype from its ID |
|||
* |
|||
* @param int $id |
|||
* @return string|null |
|||
* @since 8.2.0 |
|||
*/ |
|||
public function getMimetypeById($id); |
|||
|
|||
/** |
|||
* Get a mimetype ID, adding the mimetype to the DB if it does not exist |
|||
* |
|||
* @param string $mimetype |
|||
* @return int |
|||
* @since 8.2.0 |
|||
*/ |
|||
public function getId($mimetype); |
|||
|
|||
/** |
|||
* Test if a mimetype exists in the database |
|||
* |
|||
* @param string $mimetype |
|||
* @return bool |
|||
* @since 8.2.0 |
|||
*/ |
|||
public function exists($mimetype); |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue