2424 */
2525namespace OC \Files \Type ;
2626
27+ use OCP \AppFramework \Db \TTransactional ;
2728use OCP \Files \IMimeTypeLoader ;
2829use OCP \IDBConnection ;
2930
3334 * @package OC\Files\Type
3435 */
3536class Loader implements IMimeTypeLoader {
37+ use TTransactional;
3638
37- /** @var IDBConnection */
38- private $ dbConnection ;
39+ private IDBConnection $ dbConnection ;
3940
40- /** @var array [id => mimetype] */
41- protected $ mimetypes ;
41+ /** @psalm- var array<int, string> */
42+ protected array $ mimetypes ;
4243
43- /** @var array [mimetype => id] */
44- protected $ mimetypeIds ;
44+ /** @psalm- var array<string, int> */
45+ protected array $ mimetypeIds ;
4546
4647 /**
4748 * @param IDBConnection $dbConnection
@@ -54,11 +55,8 @@ public function __construct(IDBConnection $dbConnection) {
5455
5556 /**
5657 * Get a mimetype from its ID
57- *
58- * @param int $id
59- * @return string|null
6058 */
61- public function getMimetypeById ($ id ) {
59+ public function getMimetypeById (int $ id ): ? string {
6260 if (!$ this ->mimetypes ) {
6361 $ this ->loadMimetypes ();
6462 }
@@ -70,11 +68,8 @@ public function getMimetypeById($id) {
7068
7169 /**
7270 * Get a mimetype ID, adding the mimetype to the DB if it does not exist
73- *
74- * @param string $mimetype
75- * @return int
7671 */
77- public function getId ($ mimetype ) {
72+ public function getId (string $ mimetype ): int {
7873 if (!$ this ->mimetypeIds ) {
7974 $ this ->loadMimetypes ();
8075 }
@@ -86,11 +81,8 @@ public function getId($mimetype) {
8681
8782 /**
8883 * Test if a mimetype exists in the database
89- *
90- * @param string $mimetype
91- * @return bool
9284 */
93- public function exists ($ mimetype ) {
85+ public function exists (string $ mimetype ): bool {
9486 if (!$ this ->mimetypeIds ) {
9587 $ this ->loadMimetypes ();
9688 }
@@ -100,46 +92,49 @@ public function exists($mimetype) {
10092 /**
10193 * Clear all loaded mimetypes, allow for re-loading
10294 */
103- public function reset () {
95+ public function reset (): void {
10496 $ this ->mimetypes = [];
10597 $ this ->mimetypeIds = [];
10698 }
10799
108100 /**
109101 * Store a mimetype in the DB
110- *
111- * @param string $mimetype
112- * @param int inserted ID
113102 */
114- protected function store ($ mimetype ) {
115- $ this ->dbConnection ->insertIfNotExist ('*PREFIX*mimetypes ' , [
116- 'mimetype ' => $ mimetype
117- ]);
118-
119- $ fetch = $ this ->dbConnection ->getQueryBuilder ();
120- $ fetch ->select ('id ' )
121- ->from ('mimetypes ' )
122- ->where (
123- $ fetch ->expr ()->eq ('mimetype ' , $ fetch ->createNamedParameter ($ mimetype )
124- ));
125-
126- $ result = $ fetch ->execute ();
127- $ row = $ result ->fetch ();
128- $ result ->closeCursor ();
103+ protected function store (string $ mimetype ): int {
104+ $ row = $ this ->atomic (function () use ($ mimetype ) {
105+ $ insert = $ this ->dbConnection ->getQueryBuilder ();
106+ $ insert ->insert ('mimetypes ' )
107+ ->values ([
108+ 'mimetype ' => $ insert ->createNamedParameter ($ mimetype )
109+ ])
110+ ->executeStatement ();
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+
119+ $ result = $ fetch ->execute ();
120+ $ row = $ result ->fetch ();
121+ $ result ->closeCursor ();
122+ return $ row ;
123+ }, $ this ->dbConnection );
129124
130125 if (!$ row ) {
131126 throw new \Exception ("Failed to get mimetype id for $ mimetype after trying to store it " );
132127 }
133128
134129 $ this ->mimetypes [$ row ['id ' ]] = $ mimetype ;
135130 $ this ->mimetypeIds [$ mimetype ] = $ row ['id ' ];
136- return $ row ['id ' ];
131+ return ( int ) $ row ['id ' ];
137132 }
138133
139134 /**
140135 * Load all mimetypes from DB
141136 */
142- private function loadMimetypes () {
137+ private function loadMimetypes (): void {
143138 $ qb = $ this ->dbConnection ->getQueryBuilder ();
144139 $ qb ->select ('id ' , 'mimetype ' )
145140 ->from ('mimetypes ' );
@@ -157,11 +152,9 @@ private function loadMimetypes() {
157152 /**
158153 * Update filecache mimetype based on file extension
159154 *
160- * @param string $ext file extension
161- * @param int $mimeTypeId
162155 * @return int number of changed rows
163156 */
164- public function updateFilecache ($ ext , $ mimeTypeId ) {
157+ public function updateFilecache (string $ ext , int $ mimeTypeId ): int {
165158 $ folderMimeTypeId = $ this ->getId ('httpd/unix-directory ' );
166159 $ update = $ this ->dbConnection ->getQueryBuilder ();
167160 $ update ->update ('filecache ' )
0 commit comments