3030use OCA \Files_External \Lib \InsufficientDataForMeaningfulAnswerException ;
3131use OCA \Files_External \Lib \StorageConfig ;
3232use OCA \Files_External \Service \GlobalStoragesService ;
33+ use OCP \DB \QueryBuilder \IQueryBuilder ;
3334use OCP \Files \Notify \IChange ;
3435use OCP \Files \Notify \INotifyHandler ;
3536use OCP \Files \Notify \IRenameChange ;
@@ -48,8 +49,6 @@ class Notify extends Base {
4849 private $ globalService ;
4950 /** @var IDBConnection */
5051 private $ connection ;
51- /** @var \OCP\DB\QueryBuilder\IQueryBuilder */
52- private $ updateQuery ;
5352 /** @var ILogger */
5453 private $ logger ;
5554
@@ -58,7 +57,6 @@ function __construct(GlobalStoragesService $globalService, IDBConnection $connec
5857 $ this ->globalService = $ globalService ;
5958 $ this ->connection = $ connection ;
6059 $ this ->logger = $ logger ;
61- $ this ->updateQuery = $ this ->getUpdateQuery ($ this ->connection );
6260 }
6361
6462 protected function configure () {
@@ -162,13 +160,23 @@ private function markParentAsOutdated($mountId, $path, OutputInterface $output)
162160 }
163161
164162 try {
165- $ this ->updateQuery -> execute ([ $ parent , $ mountId] );
163+ $ storageIds = $ this ->getStorageIds ( $ mountId );
166164 } catch (DriverException $ ex ) {
167- $ this ->logger ->logException ($ ex , ['app ' => 'files_external ' , 'message ' => 'Error while trying to mark folder as outdated ' , 'level ' => ILogger::WARN ]);
165+ $ this ->logger ->logException ($ ex , ['app ' => 'files_external ' , 'message ' => 'Error while trying to find correct storage ids. ' , 'level ' => ILogger::ERROR ]);
168166 $ this ->connection = $ this ->reconnectToDatabase ($ this ->connection , $ output );
169167 $ output ->writeln ('<info>Needed to reconnect to the database</info> ' );
170- $ this ->updateQuery = $ this ->getUpdateQuery ($ this ->connection );
171- $ this ->updateQuery ->execute ([$ parent , $ mountId ]);
168+ $ storageIds = $ this ->getStorageIds ($ mountId );
169+ }
170+
171+ if (!$ storageIds || count ($ storageIds ) === 0 ) {
172+ throw new StorageNotAvailableException ('No storages found by mount ID ' . $ mountId );
173+ }
174+ $ storageIds = array_map ('intval ' , $ storageIds );
175+
176+ $ result = $ this ->updateParent ($ storageIds , $ parent );
177+ if ($ result === 0 ) {
178+ //TODO: Find existing parent further up the tree in the database and register that folder instead.
179+ $ this ->logger ->warning ('Failed updating parent for " ' . $ path . '" while trying to register change. It may not exist in the filecache. ' , ['app ' => 'files_external ' ]);
172180 }
173181 }
174182
@@ -198,16 +206,24 @@ private function logUpdate(IChange $change, OutputInterface $output) {
198206 $ output ->writeln ($ text );
199207 }
200208
201- /**
202- * @return \Doctrine\DBAL\Statement
203- */
204- private function getUpdateQuery (IDBConnection $ connection ) {
205- // the query builder doesn't really like subqueries with parameters
206- return $ connection ->prepare (
207- 'UPDATE *PREFIX*filecache SET size = -1
208- WHERE `path` = ?
209- AND `storage` IN (SELECT storage_id FROM *PREFIX*mounts WHERE mount_id = ?) '
210- );
209+ private function getStorageIds ($ mountId ) {
210+ $ qb = $ this ->connection ->getQueryBuilder ();
211+ return $ qb
212+ ->select ('storage_id ' )
213+ ->from ('mounts ' )
214+ ->where ($ qb ->expr ()->eq ('mount_id ' , $ qb ->createNamedParameter ($ mountId , IQueryBuilder::PARAM_INT )))
215+ ->execute ()->fetchAll (\PDO ::FETCH_COLUMN );
216+ }
217+
218+ private function updateParent ($ storageIds , $ parent ) {
219+ $ pathHash = md5 (trim (\OC_Util::normalizeUnicode ($ parent ), '/ ' ));
220+ $ qb = $ this ->connection ->getQueryBuilder ();
221+ $ qb
222+ ->update ('filecache ' )
223+ ->set ('size ' , $ qb ->createNamedParameter (-1 , IQueryBuilder::PARAM_INT ))
224+ ->where ($ qb ->expr ()->in ('storage ' , $ qb ->createNamedParameter ($ storageIds , IQueryBuilder::PARAM_INT_ARRAY , ':storage_ids ' )))
225+ ->andWhere ($ qb ->expr ()->eq ('path_hash ' , $ qb ->createNamedParameter ($ pathHash , IQueryBuilder::PARAM_STR )));
226+ return $ qb ->execute ();
211227 }
212228
213229 /**
0 commit comments