You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Loader.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Robin McCorkell <robin@mccorkell.me.uk>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC\Files\Type;
  23. use OCP\Files\IMimeTypeLoader;
  24. use OCP\IDBConnection;
  25. use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
  26. /**
  27. * Mimetype database loader
  28. *
  29. * @package OC\Files\Type
  30. */
  31. class Loader implements IMimeTypeLoader {
  32. /** @var IDBConnection */
  33. private $dbConnection;
  34. /** @var array [id => mimetype] */
  35. protected $mimetypes;
  36. /** @var array [mimetype => id] */
  37. protected $mimetypeIds;
  38. /**
  39. * @param IDBConnection $dbConnection
  40. */
  41. public function __construct(IDBConnection $dbConnection) {
  42. $this->dbConnection = $dbConnection;
  43. $this->mimetypes = [];
  44. $this->mimetypeIds = [];
  45. }
  46. /**
  47. * Get a mimetype from its ID
  48. *
  49. * @param int $id
  50. * @return string|null
  51. */
  52. public function getMimetypeById($id) {
  53. if (!$this->mimetypes) {
  54. $this->loadMimetypes();
  55. }
  56. if (isset($this->mimetypes[$id])) {
  57. return $this->mimetypes[$id];
  58. }
  59. return null;
  60. }
  61. /**
  62. * Get a mimetype ID, adding the mimetype to the DB if it does not exist
  63. *
  64. * @param string $mimetype
  65. * @return int
  66. */
  67. public function getId($mimetype) {
  68. if (!$this->mimetypeIds) {
  69. $this->loadMimetypes();
  70. }
  71. if (isset($this->mimetypeIds[$mimetype])) {
  72. return $this->mimetypeIds[$mimetype];
  73. }
  74. return $this->store($mimetype);
  75. }
  76. /**
  77. * Test if a mimetype exists in the database
  78. *
  79. * @param string $mimetype
  80. * @return bool
  81. */
  82. public function exists($mimetype) {
  83. if (!$this->mimetypeIds) {
  84. $this->loadMimetypes();
  85. }
  86. return isset($this->mimetypeIds[$mimetype]);
  87. }
  88. /**
  89. * Clear all loaded mimetypes, allow for re-loading
  90. */
  91. public function reset() {
  92. $this->mimetypes = [];
  93. $this->mimetypeIds = [];
  94. }
  95. /**
  96. * Store a mimetype in the DB
  97. *
  98. * @param string $mimetype
  99. * @param int inserted ID
  100. */
  101. protected function store($mimetype) {
  102. try {
  103. $qb = $this->dbConnection->getQueryBuilder();
  104. $qb->insert('mimetypes')
  105. ->values([
  106. 'mimetype' => $qb->createNamedParameter($mimetype)
  107. ]);
  108. $qb->execute();
  109. } catch (UniqueConstraintViolationException $e) {
  110. // something inserted it before us
  111. }
  112. $fetch = $this->dbConnection->getQueryBuilder();
  113. $fetch->select('id')
  114. ->from('mimetypes')
  115. ->where(
  116. $fetch->expr()->eq('mimetype', $fetch->createNamedParameter($mimetype)
  117. ));
  118. $row = $fetch->execute()->fetch();
  119. $this->mimetypes[$row['id']] = $mimetype;
  120. $this->mimetypeIds[$mimetype] = $row['id'];
  121. return $row['id'];
  122. }
  123. /**
  124. * Load all mimetypes from DB
  125. */
  126. private function loadMimetypes() {
  127. $qb = $this->dbConnection->getQueryBuilder();
  128. $qb->select('id', 'mimetype')
  129. ->from('mimetypes');
  130. $results = $qb->execute()->fetchAll();
  131. foreach ($results as $row) {
  132. $this->mimetypes[$row['id']] = $row['mimetype'];
  133. $this->mimetypeIds[$row['mimetype']] = $row['id'];
  134. }
  135. }
  136. /**
  137. * Update filecache mimetype based on file extension
  138. *
  139. * @param string $ext file extension
  140. * @param int $mimetypeId
  141. * @return int number of changed rows
  142. */
  143. public function updateFilecache($ext, $mimetypeId) {
  144. $update = $this->dbConnection->getQueryBuilder();
  145. $update->update('filecache')
  146. ->set('mimetype', $update->createNamedParameter($mimetypeId))
  147. ->where($update->expr()->neq(
  148. 'mimetype', $update->createNamedParameter($mimetypeId)
  149. ))
  150. ->andWhere($update->expr()->like(
  151. $update->createFunction('LOWER(`name`)'), $update->createNamedParameter($ext)
  152. ));
  153. return $update->execute();
  154. }
  155. }