@@ -1471,10 +1471,10 @@ public function addSubMounts(FileInfo $info, $extOnly = false): void {
14711471 * get the content of a directory
14721472 *
14731473 * @param string $directory path under datadirectory
1474- * @param string $mimetype_filter limit returned content to this mimetype or mimepart
1474+ * @param ?non-empty- string $mimeTypeFilter limit returned content to this mimetype or mimepart
14751475 * @return FileInfo[]
14761476 */
1477- public function getDirectoryContent ($ directory , $ mimetype_filter = '' , ?\OCP \Files \FileInfo $ directoryInfo = null ) {
1477+ public function getDirectoryContent (string $ directory , ? string $ mimeTypeFilter = null , ?\OCP \Files \FileInfo $ directoryInfo = null ) {
14781478 $ this ->assertPathLength ($ directory );
14791479 if (!Filesystem::isValidPath ($ directory )) {
14801480 return [];
@@ -1506,7 +1506,8 @@ public function getDirectoryContent($directory, $mimetype_filter = '', ?\OCP\Fil
15061506 }
15071507
15081508 $ folderId = $ data ->getId ();
1509- $ contents = $ cache ->getFolderContentsById ($ folderId ); //TODO: mimetype_filter
1509+ /** @psalm-suppress TypeDoesNotContainType For legacy compatibility */
1510+ $ contents = $ cache ->getFolderContentsById ($ folderId , $ mimeTypeFilter === '' ? null : $ mimeTypeFilter );
15101511
15111512 $ sharingDisabled = \OCP \Util::isSharingDisabledForUser ();
15121513 $ permissionsMask = ~\OCP \Constants::PERMISSION_SHARE ;
@@ -1567,79 +1568,79 @@ public function getDirectoryContent($directory, $mimetype_filter = '', ?\OCP\Fil
15671568 $ rootEntry = $ subCache ->get ('' );
15681569 }
15691570
1570- if ($ rootEntry && ($ rootEntry ->getPermissions () & Constants::PERMISSION_READ )) {
1571- $ relativePath = trim (substr ($ mountPoint , $ dirLength ), '/ ' );
1572- if ($ pos = strpos ($ relativePath , '/ ' )) {
1573- //mountpoint inside subfolder add size to the correct folder
1574- $ entryName = substr ($ relativePath , 0 , $ pos );
1575-
1576- // Create parent folders if the mountpoint is inside a subfolder that doesn't exist yet
1577- if (!isset ($ files [$ entryName ])) {
1578- try {
1579- [$ storage , ] = $ this ->resolvePath ($ path . '/ ' . $ entryName );
1580- // make sure we can create the mountpoint folder, even if the user has a quota of 0
1581- if ($ storage ->instanceOfStorage (Quota::class)) {
1582- $ storage ->enableQuota (false );
1583- }
1571+ if (!$ rootEntry || !($ rootEntry ->getPermissions () & Constants::PERMISSION_READ )) {
1572+ continue ;
1573+ }
15841574
1585- if ($ this ->mkdir ($ path . '/ ' . $ entryName ) !== false ) {
1586- $ info = $ this ->getFileInfo ($ path . '/ ' . $ entryName );
1587- if ($ info !== false ) {
1588- $ files [$ entryName ] = $ info ;
1589- }
1590- }
1575+ if ($ mimeTypeFilter ) {
1576+ if (strpos ($ mimeTypeFilter , '/ ' ) !== false && $ rootEntry ['mimetype ' ] !== $ mimeTypeFilter ) {
1577+ continue ;
1578+ } elseif (strpos ($ mimeTypeFilter , '/ ' ) === false && $ rootEntry ['mimepart ' ] !== $ mimeTypeFilter ) {
1579+ continue ;
1580+ }
1581+ }
15911582
1592- if ($ storage ->instanceOfStorage (Quota::class)) {
1593- $ storage ->enableQuota (true );
1583+ $ relativePath = trim (substr ($ mountPoint , $ dirLength ), '/ ' );
1584+ if ($ pos = strpos ($ relativePath , '/ ' )) {
1585+ //mountpoint inside subfolder add size to the correct folder
1586+ $ entryName = substr ($ relativePath , 0 , $ pos );
1587+
1588+ // Create parent folders if the mountpoint is inside a subfolder that doesn't exist yet
1589+ if (!isset ($ files [$ entryName ])) {
1590+ try {
1591+ [$ storage , ] = $ this ->resolvePath ($ path . '/ ' . $ entryName );
1592+ // make sure we can create the mountpoint folder, even if the user has a quota of 0
1593+ if ($ storage ->instanceOfStorage (Quota::class)) {
1594+ $ storage ->enableQuota (false );
1595+ }
1596+
1597+ if ($ this ->mkdir ($ path . '/ ' . $ entryName ) !== false ) {
1598+ $ info = $ this ->getFileInfo ($ path . '/ ' . $ entryName );
1599+ if ($ info !== false ) {
1600+ $ files [$ entryName ] = $ info ;
15941601 }
1595- } catch (\Exception $ e ) {
1596- // Creating the parent folder might not be possible, for example due to a lack of permissions.
1597- $ this ->logger ->debug ('Failed to create non-existent parent ' , ['exception ' => $ e , 'path ' => $ path . '/ ' . $ entryName ]);
15981602 }
1599- }
16001603
1601- if (isset ($ files [$ entryName ])) {
1602- $ files [$ entryName ]->addSubEntry ($ rootEntry , $ mountPoint );
1603- }
1604- } else { //mountpoint in this folder, add an entry for it
1605- $ rootEntry ['name ' ] = $ relativePath ;
1606- $ rootEntry ['type ' ] = $ rootEntry ['mimetype ' ] === 'httpd/unix-directory ' ? 'dir ' : 'file ' ;
1607- $ permissions = $ rootEntry ['permissions ' ];
1608- // do not allow renaming/deleting the mount point if they are not shared files/folders
1609- // for shared files/folders we use the permissions given by the owner
1610- if ($ mount instanceof MoveableMount) {
1611- $ rootEntry ['permissions ' ] = $ permissions | \OCP \Constants::PERMISSION_UPDATE | \OCP \Constants::PERMISSION_DELETE ;
1612- } else {
1613- $ rootEntry ['permissions ' ] = $ permissions & (\OCP \Constants::PERMISSION_ALL - (\OCP \Constants::PERMISSION_UPDATE | \OCP \Constants::PERMISSION_DELETE ));
1604+ if ($ storage ->instanceOfStorage (Quota::class)) {
1605+ $ storage ->enableQuota (true );
1606+ }
1607+ } catch (\Exception $ e ) {
1608+ // Creating the parent folder might not be possible, for example due to a lack of permissions.
1609+ $ this ->logger ->debug ('Failed to create non-existent parent ' , ['exception ' => $ e , 'path ' => $ path . '/ ' . $ entryName ]);
16141610 }
1611+ }
16151612
1616- $ rootEntry ['path ' ] = substr (Filesystem::normalizePath ($ path . '/ ' . $ rootEntry ['name ' ]), strlen ($ user ) + 2 ); // full path without /$user/
1613+ if (isset ($ files [$ entryName ])) {
1614+ $ files [$ entryName ]->addSubEntry ($ rootEntry , $ mountPoint );
1615+ }
1616+ } else { //mountpoint in this folder, add an entry for it
1617+ $ rootEntry ['name ' ] = $ relativePath ;
1618+ $ rootEntry ['type ' ] = $ rootEntry ['mimetype ' ] === 'httpd/unix-directory ' ? 'dir ' : 'file ' ;
1619+ $ permissions = $ rootEntry ['permissions ' ];
1620+ // do not allow renaming/deleting the mount point if they are not shared files/folders
1621+ // for shared files/folders we use the permissions given by the owner
1622+ if ($ mount instanceof MoveableMount) {
1623+ $ rootEntry ['permissions ' ] = $ permissions | \OCP \Constants::PERMISSION_UPDATE | \OCP \Constants::PERMISSION_DELETE ;
1624+ } else {
1625+ $ rootEntry ['permissions ' ] = $ permissions & (\OCP \Constants::PERMISSION_ALL - (\OCP \Constants::PERMISSION_UPDATE | \OCP \Constants::PERMISSION_DELETE ));
1626+ }
16171627
1618- // if sharing was disabled for the user we remove the share permissions
1619- if ($ sharingDisabled ) {
1620- $ rootEntry ['permissions ' ] = $ rootEntry ['permissions ' ] & ~\OCP \Constants::PERMISSION_SHARE ;
1621- }
1628+ $ rootEntry ['path ' ] = substr (Filesystem::normalizePath ($ path . '/ ' . $ rootEntry ['name ' ]), strlen ($ user ) + 2 ); // full path without /$user/
16221629
1623- $ ownerId = $ subStorage ->getOwner ('' );
1624- if ($ ownerId !== false ) {
1625- $ owner = $ this ->getUserObjectForOwner ($ ownerId );
1626- } else {
1627- $ owner = null ;
1628- }
1629- $ files [$ rootEntry ->getName ()] = new FileInfo ($ path . '/ ' . $ rootEntry ['name ' ], $ subStorage , '' , $ rootEntry , $ mount , $ owner );
1630+ // if sharing was disabled for the user we remove the share permissions
1631+ if ($ sharingDisabled ) {
1632+ $ rootEntry ['permissions ' ] = $ rootEntry ['permissions ' ] & ~\OCP \Constants::PERMISSION_SHARE ;
16301633 }
1631- }
1632- }
1633- }
16341634
1635- if ($ mimetype_filter ) {
1636- $ files = array_filter ($ files , function (FileInfo $ file ) use ($ mimetype_filter ) {
1637- if (strpos ($ mimetype_filter , '/ ' )) {
1638- return $ file ->getMimetype () === $ mimetype_filter ;
1639- } else {
1640- return $ file ->getMimePart () === $ mimetype_filter ;
1635+ $ ownerId = $ subStorage ->getOwner ('' );
1636+ if ($ ownerId !== false ) {
1637+ $ owner = $ this ->getUserObjectForOwner ($ ownerId );
1638+ } else {
1639+ $ owner = null ;
1640+ }
1641+ $ files [$ rootEntry ->getName ()] = new FileInfo ($ path . '/ ' . $ rootEntry ['name ' ], $ subStorage , '' , $ rootEntry , $ mount , $ owner );
16411642 }
1642- });
1643+ }
16431644 }
16441645
16451646 return array_values ($ files );
0 commit comments