예제 #1
0
 /**
  * get the content of a directory
  *
  * @param string $directory path under datadirectory
  * @param string $mimetype_filter limit returned content to this mimetype or mimepart
  * @return FileInfo[]
  */
 public function getDirectoryContent($directory, $mimetype_filter = '')
 {
     $this->assertPathLength($directory);
     $result = array();
     if (!Filesystem::isValidPath($directory)) {
         return $result;
     }
     $path = $this->getAbsolutePath($directory);
     $path = Filesystem::normalizePath($path);
     $mount = $this->getMount($directory);
     $storage = $mount->getStorage();
     $internalPath = $mount->getInternalPath($path);
     if ($storage) {
         $cache = $storage->getCache($internalPath);
         $user = \OC_User::getUser();
         $data = $cache->get($internalPath);
         $watcher = $storage->getWatcher($internalPath);
         if (!$data or $data['size'] === -1) {
             if (!$storage->file_exists($internalPath)) {
                 return array();
             }
             $scanner = $storage->getScanner($internalPath);
             $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW);
             $data = $cache->get($internalPath);
         } else {
             if ($watcher->checkUpdate($internalPath, $data)) {
                 $this->updater->propagate($path);
                 $data = $cache->get($internalPath);
             }
         }
         $folderId = $data['fileid'];
         /**
          * @var \OC\Files\FileInfo[] $files
          */
         $files = array();
         $contents = $cache->getFolderContentsById($folderId);
         //TODO: mimetype_filter
         foreach ($contents as $content) {
             if ($content['permissions'] === 0) {
                 $content['permissions'] = $storage->getPermissions($content['path']);
                 $cache->update($content['fileid'], array('permissions' => $content['permissions']));
             }
             // if sharing was disabled for the user we remove the share permissions
             if (\OCP\Util::isSharingDisabledForUser()) {
                 $content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE;
             }
             $files[] = new FileInfo($path . '/' . $content['name'], $storage, $content['path'], $content, $mount);
         }
         //add a folder for any mountpoint in this directory and add the sizes of other mountpoints to the folders
         $mounts = Filesystem::getMountManager()->findIn($path);
         $dirLength = strlen($path);
         foreach ($mounts as $mount) {
             $mountPoint = $mount->getMountPoint();
             $subStorage = $mount->getStorage();
             if ($subStorage) {
                 $subCache = $subStorage->getCache('');
                 if ($subCache->getStatus('') === Cache\Cache::NOT_FOUND) {
                     $subScanner = $subStorage->getScanner('');
                     try {
                         $subScanner->scanFile('');
                     } catch (\OCP\Files\StorageNotAvailableException $e) {
                         continue;
                     } catch (\OCP\Files\StorageInvalidException $e) {
                         continue;
                     } catch (\Exception $e) {
                         // sometimes when the storage is not available it can be any exception
                         \OCP\Util::writeLog('core', 'Exception while scanning storage "' . $subStorage->getId() . '": ' . get_class($e) . ': ' . $e->getMessage(), \OCP\Util::ERROR);
                         continue;
                     }
                 }
                 $rootEntry = $subCache->get('');
                 if ($rootEntry) {
                     $relativePath = trim(substr($mountPoint, $dirLength), '/');
                     if ($pos = strpos($relativePath, '/')) {
                         //mountpoint inside subfolder add size to the correct folder
                         $entryName = substr($relativePath, 0, $pos);
                         foreach ($files as &$entry) {
                             if ($entry['name'] === $entryName) {
                                 $entry['size'] += $rootEntry['size'];
                             }
                         }
                     } else {
                         //mountpoint in this folder, add an entry for it
                         $rootEntry['name'] = $relativePath;
                         $rootEntry['type'] = $rootEntry['mimetype'] === 'httpd/unix-directory' ? 'dir' : 'file';
                         $permissions = $rootEntry['permissions'];
                         // do not allow renaming/deleting the mount point if they are not shared files/folders
                         // for shared files/folders we use the permissions given by the owner
                         if ($mount instanceof MoveableMount) {
                             $rootEntry['permissions'] = $permissions | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE;
                         } else {
                             $rootEntry['permissions'] = $permissions & \OCP\Constants::PERMISSION_ALL - (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE);
                         }
                         //remove any existing entry with the same name
                         foreach ($files as $i => $file) {
                             if ($file['name'] === $rootEntry['name']) {
                                 unset($files[$i]);
                                 break;
                             }
                         }
                         $rootEntry['path'] = substr(Filesystem::normalizePath($path . '/' . $rootEntry['name']), strlen($user) + 2);
                         // full path without /$user/
                         // if sharing was disabled for the user we remove the share permissions
                         if (\OCP\Util::isSharingDisabledForUser()) {
                             $content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE;
                         }
                         $files[] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry, $mount);
                     }
                 }
             }
         }
         if ($mimetype_filter) {
             foreach ($files as $file) {
                 if (strpos($mimetype_filter, '/')) {
                     if ($file['mimetype'] === $mimetype_filter) {
                         $result[] = $file;
                     }
                 } else {
                     if ($file['mimepart'] === $mimetype_filter) {
                         $result[] = $file;
                     }
                 }
             }
         } else {
             $result = $files;
         }
     }
     return $result;
 }