@@ -27,7 +27,6 @@ |
||
| 27 | 27 | |
| 28 | 28 | use OCP\Files\IMimeTypeLoader; |
| 29 | 29 | use OCP\IDBConnection; |
| 30 | -use Doctrine\DBAL\Exception\UniqueConstraintViolationException; |
|
| 31 | 30 | |
| 32 | 31 | /** |
| 33 | 32 | * Mimetype database loader |
@@ -36,143 +36,143 @@ |
||
| 36 | 36 | */ |
| 37 | 37 | class Loader implements IMimeTypeLoader { |
| 38 | 38 | |
| 39 | - /** @var IDBConnection */ |
|
| 40 | - private $dbConnection; |
|
| 41 | - |
|
| 42 | - /** @var array [id => mimetype] */ |
|
| 43 | - protected $mimetypes; |
|
| 44 | - |
|
| 45 | - /** @var array [mimetype => id] */ |
|
| 46 | - protected $mimetypeIds; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * @param IDBConnection $dbConnection |
|
| 50 | - */ |
|
| 51 | - public function __construct(IDBConnection $dbConnection) { |
|
| 52 | - $this->dbConnection = $dbConnection; |
|
| 53 | - $this->mimetypes = []; |
|
| 54 | - $this->mimetypeIds = []; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Get a mimetype from its ID |
|
| 59 | - * |
|
| 60 | - * @param int $id |
|
| 61 | - * @return string|null |
|
| 62 | - */ |
|
| 63 | - public function getMimetypeById($id) { |
|
| 64 | - if (!$this->mimetypes) { |
|
| 65 | - $this->loadMimetypes(); |
|
| 66 | - } |
|
| 67 | - if (isset($this->mimetypes[$id])) { |
|
| 68 | - return $this->mimetypes[$id]; |
|
| 69 | - } |
|
| 70 | - return null; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Get a mimetype ID, adding the mimetype to the DB if it does not exist |
|
| 75 | - * |
|
| 76 | - * @param string $mimetype |
|
| 77 | - * @return int |
|
| 78 | - */ |
|
| 79 | - public function getId($mimetype) { |
|
| 80 | - if (!$this->mimetypeIds) { |
|
| 81 | - $this->loadMimetypes(); |
|
| 82 | - } |
|
| 83 | - if (isset($this->mimetypeIds[$mimetype])) { |
|
| 84 | - return $this->mimetypeIds[$mimetype]; |
|
| 85 | - } |
|
| 86 | - return $this->store($mimetype); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Test if a mimetype exists in the database |
|
| 91 | - * |
|
| 92 | - * @param string $mimetype |
|
| 93 | - * @return bool |
|
| 94 | - */ |
|
| 95 | - public function exists($mimetype) { |
|
| 96 | - if (!$this->mimetypeIds) { |
|
| 97 | - $this->loadMimetypes(); |
|
| 98 | - } |
|
| 99 | - return isset($this->mimetypeIds[$mimetype]); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Clear all loaded mimetypes, allow for re-loading |
|
| 104 | - */ |
|
| 105 | - public function reset() { |
|
| 106 | - $this->mimetypes = []; |
|
| 107 | - $this->mimetypeIds = []; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Store a mimetype in the DB |
|
| 112 | - * |
|
| 113 | - * @param string $mimetype |
|
| 114 | - * @param int inserted ID |
|
| 115 | - */ |
|
| 116 | - protected function store($mimetype) { |
|
| 117 | - $this->dbConnection->insertIfNotExist('*PREFIX*mimetypes', [ |
|
| 118 | - 'mimetype' => $mimetype |
|
| 119 | - ]); |
|
| 120 | - |
|
| 121 | - $fetch = $this->dbConnection->getQueryBuilder(); |
|
| 122 | - $fetch->select('id') |
|
| 123 | - ->from('mimetypes') |
|
| 124 | - ->where( |
|
| 125 | - $fetch->expr()->eq('mimetype', $fetch->createNamedParameter($mimetype) |
|
| 126 | - )); |
|
| 127 | - $row = $fetch->execute()->fetch(); |
|
| 128 | - |
|
| 129 | - if (!$row) { |
|
| 130 | - throw new \Exception("Failed to get mimetype id for $mimetype after trying to store it"); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - $this->mimetypes[$row['id']] = $mimetype; |
|
| 134 | - $this->mimetypeIds[$mimetype] = $row['id']; |
|
| 135 | - return $row['id']; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Load all mimetypes from DB |
|
| 140 | - */ |
|
| 141 | - private function loadMimetypes() { |
|
| 142 | - $qb = $this->dbConnection->getQueryBuilder(); |
|
| 143 | - $qb->select('id', 'mimetype') |
|
| 144 | - ->from('mimetypes'); |
|
| 145 | - $results = $qb->execute()->fetchAll(); |
|
| 146 | - |
|
| 147 | - foreach ($results as $row) { |
|
| 148 | - $this->mimetypes[$row['id']] = $row['mimetype']; |
|
| 149 | - $this->mimetypeIds[$row['mimetype']] = $row['id']; |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Update filecache mimetype based on file extension |
|
| 155 | - * |
|
| 156 | - * @param string $ext file extension |
|
| 157 | - * @param int $mimeTypeId |
|
| 158 | - * @return int number of changed rows |
|
| 159 | - */ |
|
| 160 | - public function updateFilecache($ext, $mimeTypeId) { |
|
| 161 | - $folderMimeTypeId = $this->getId('httpd/unix-directory'); |
|
| 162 | - $update = $this->dbConnection->getQueryBuilder(); |
|
| 163 | - $update->update('filecache') |
|
| 164 | - ->set('mimetype', $update->createNamedParameter($mimeTypeId)) |
|
| 165 | - ->where($update->expr()->neq( |
|
| 166 | - 'mimetype', $update->createNamedParameter($mimeTypeId) |
|
| 167 | - )) |
|
| 168 | - ->andWhere($update->expr()->neq( |
|
| 169 | - 'mimetype', $update->createNamedParameter($folderMimeTypeId) |
|
| 170 | - )) |
|
| 171 | - ->andWhere($update->expr()->like( |
|
| 172 | - $update->createFunction('LOWER(' . $update->getColumnName('name') . ')'), |
|
| 173 | - $update->createNamedParameter('%' . $this->dbConnection->escapeLikeParameter('.' . $ext)) |
|
| 174 | - )); |
|
| 175 | - return $update->execute(); |
|
| 176 | - } |
|
| 39 | + /** @var IDBConnection */ |
|
| 40 | + private $dbConnection; |
|
| 41 | + |
|
| 42 | + /** @var array [id => mimetype] */ |
|
| 43 | + protected $mimetypes; |
|
| 44 | + |
|
| 45 | + /** @var array [mimetype => id] */ |
|
| 46 | + protected $mimetypeIds; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * @param IDBConnection $dbConnection |
|
| 50 | + */ |
|
| 51 | + public function __construct(IDBConnection $dbConnection) { |
|
| 52 | + $this->dbConnection = $dbConnection; |
|
| 53 | + $this->mimetypes = []; |
|
| 54 | + $this->mimetypeIds = []; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Get a mimetype from its ID |
|
| 59 | + * |
|
| 60 | + * @param int $id |
|
| 61 | + * @return string|null |
|
| 62 | + */ |
|
| 63 | + public function getMimetypeById($id) { |
|
| 64 | + if (!$this->mimetypes) { |
|
| 65 | + $this->loadMimetypes(); |
|
| 66 | + } |
|
| 67 | + if (isset($this->mimetypes[$id])) { |
|
| 68 | + return $this->mimetypes[$id]; |
|
| 69 | + } |
|
| 70 | + return null; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Get a mimetype ID, adding the mimetype to the DB if it does not exist |
|
| 75 | + * |
|
| 76 | + * @param string $mimetype |
|
| 77 | + * @return int |
|
| 78 | + */ |
|
| 79 | + public function getId($mimetype) { |
|
| 80 | + if (!$this->mimetypeIds) { |
|
| 81 | + $this->loadMimetypes(); |
|
| 82 | + } |
|
| 83 | + if (isset($this->mimetypeIds[$mimetype])) { |
|
| 84 | + return $this->mimetypeIds[$mimetype]; |
|
| 85 | + } |
|
| 86 | + return $this->store($mimetype); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Test if a mimetype exists in the database |
|
| 91 | + * |
|
| 92 | + * @param string $mimetype |
|
| 93 | + * @return bool |
|
| 94 | + */ |
|
| 95 | + public function exists($mimetype) { |
|
| 96 | + if (!$this->mimetypeIds) { |
|
| 97 | + $this->loadMimetypes(); |
|
| 98 | + } |
|
| 99 | + return isset($this->mimetypeIds[$mimetype]); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Clear all loaded mimetypes, allow for re-loading |
|
| 104 | + */ |
|
| 105 | + public function reset() { |
|
| 106 | + $this->mimetypes = []; |
|
| 107 | + $this->mimetypeIds = []; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Store a mimetype in the DB |
|
| 112 | + * |
|
| 113 | + * @param string $mimetype |
|
| 114 | + * @param int inserted ID |
|
| 115 | + */ |
|
| 116 | + protected function store($mimetype) { |
|
| 117 | + $this->dbConnection->insertIfNotExist('*PREFIX*mimetypes', [ |
|
| 118 | + 'mimetype' => $mimetype |
|
| 119 | + ]); |
|
| 120 | + |
|
| 121 | + $fetch = $this->dbConnection->getQueryBuilder(); |
|
| 122 | + $fetch->select('id') |
|
| 123 | + ->from('mimetypes') |
|
| 124 | + ->where( |
|
| 125 | + $fetch->expr()->eq('mimetype', $fetch->createNamedParameter($mimetype) |
|
| 126 | + )); |
|
| 127 | + $row = $fetch->execute()->fetch(); |
|
| 128 | + |
|
| 129 | + if (!$row) { |
|
| 130 | + throw new \Exception("Failed to get mimetype id for $mimetype after trying to store it"); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + $this->mimetypes[$row['id']] = $mimetype; |
|
| 134 | + $this->mimetypeIds[$mimetype] = $row['id']; |
|
| 135 | + return $row['id']; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Load all mimetypes from DB |
|
| 140 | + */ |
|
| 141 | + private function loadMimetypes() { |
|
| 142 | + $qb = $this->dbConnection->getQueryBuilder(); |
|
| 143 | + $qb->select('id', 'mimetype') |
|
| 144 | + ->from('mimetypes'); |
|
| 145 | + $results = $qb->execute()->fetchAll(); |
|
| 146 | + |
|
| 147 | + foreach ($results as $row) { |
|
| 148 | + $this->mimetypes[$row['id']] = $row['mimetype']; |
|
| 149 | + $this->mimetypeIds[$row['mimetype']] = $row['id']; |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Update filecache mimetype based on file extension |
|
| 155 | + * |
|
| 156 | + * @param string $ext file extension |
|
| 157 | + * @param int $mimeTypeId |
|
| 158 | + * @return int number of changed rows |
|
| 159 | + */ |
|
| 160 | + public function updateFilecache($ext, $mimeTypeId) { |
|
| 161 | + $folderMimeTypeId = $this->getId('httpd/unix-directory'); |
|
| 162 | + $update = $this->dbConnection->getQueryBuilder(); |
|
| 163 | + $update->update('filecache') |
|
| 164 | + ->set('mimetype', $update->createNamedParameter($mimeTypeId)) |
|
| 165 | + ->where($update->expr()->neq( |
|
| 166 | + 'mimetype', $update->createNamedParameter($mimeTypeId) |
|
| 167 | + )) |
|
| 168 | + ->andWhere($update->expr()->neq( |
|
| 169 | + 'mimetype', $update->createNamedParameter($folderMimeTypeId) |
|
| 170 | + )) |
|
| 171 | + ->andWhere($update->expr()->like( |
|
| 172 | + $update->createFunction('LOWER(' . $update->getColumnName('name') . ')'), |
|
| 173 | + $update->createNamedParameter('%' . $this->dbConnection->escapeLikeParameter('.' . $ext)) |
|
| 174 | + )); |
|
| 175 | + return $update->execute(); |
|
| 176 | + } |
|
| 177 | 177 | |
| 178 | 178 | } |