/** * @dataProvider sortDataProvider */ public function testSortByName($sort, $sortDescending, $expectedOrder) { $files = self::getTestFileList(); $files = \OCA\Files\Helper::sortFiles($files, $sort, $sortDescending); $fileNames = array(); foreach ($files as $fileInfo) { $fileNames[] = $fileInfo->getName(); } $this->assertEquals($expectedOrder, $fileNames); }
/** * Retrieves the contents of a trash bin directory. * * @param string $dir path to the directory inside the trashbin * or empty to retrieve the root of the trashbin * @param string $user * @param string $sortAttribute attribute to sort on or empty to disable sorting * @param bool $sortDescending true for descending sort, false otherwise * @return \OCP\Files\FileInfo[] */ public static function getTrashFiles($dir, $user, $sortAttribute = '', $sortDescending = false) { $result = array(); $timestamp = null; $view = new \OC\Files\View('/' . $user . '/files_trashbin/files'); if (ltrim($dir, '/') !== '' && !$view->is_dir($dir)) { throw new \Exception('Directory does not exists'); } $dirContent = $view->opendir($dir); if ($dirContent === false) { return $result; } $mount = $view->getMount($dir); $storage = $mount->getStorage(); $absoluteDir = $view->getAbsolutePath($dir); $internalPath = $mount->getInternalPath($absoluteDir); if (is_resource($dirContent)) { $originalLocations = \OCA\Files_Trashbin\Trashbin::getLocations($user); while (($entryName = readdir($dirContent)) !== false) { if (!\OC\Files\Filesystem::isIgnoredDir($entryName)) { $id = $entryName; if ($dir === '' || $dir === '/') { $pathparts = pathinfo($entryName); $timestamp = substr($pathparts['extension'], 1); $id = $pathparts['filename']; } else { if ($timestamp === null) { // for subfolders we need to calculate the timestamp only once $parts = explode('/', ltrim($dir, '/')); $timestamp = substr(pathinfo($parts[0], PATHINFO_EXTENSION), 1); } } $originalPath = ''; if (isset($originalLocations[$id][$timestamp])) { $originalPath = $originalLocations[$id][$timestamp]; if (substr($originalPath, -1) === '/') { $originalPath = substr($originalPath, 0, -1); } } $i = array('name' => $id, 'mtime' => $timestamp, 'mimetype' => \OC_Helper::getFileNameMimeType($id), 'type' => $view->is_dir($dir . '/' . $entryName) ? 'dir' : 'file', 'directory' => $dir === '/' ? '' : $dir); if ($originalPath) { $i['extraData'] = $originalPath . '/' . $id; } $result[] = new FileInfo($absoluteDir . '/' . $i['name'], $storage, $internalPath . '/' . $i['name'], $i, $mount); } } closedir($dirContent); } if ($sortAttribute !== '') { return \OCA\Files\Helper::sortFiles($result, $sortAttribute, $sortDescending); } return $result; }
/** * @NoAdminRequired * @NoCSRFRequired * @SSOCORS */ public function getFileList($dir = null, $sortby = 'name', $sort = false) { \OCP\JSON::checkLoggedIn(); \OC::$server->getSession()->close(); // Load the files $dir = $dir ? (string) $dir : ''; $dir = \OC\Files\Filesystem::normalizePath($dir); try { $dirInfo = \OC\Files\Filesystem::getFileInfo($dir); if (!$dirInfo || !$dirInfo->getType() === 'dir') { header('HTTP/1.0 404 Not Found'); exit; } $data = array(); $baseUrl = \OCP\Util::linkTo('files', 'index.php') . '?dir='; $permissions = $dirInfo->getPermissions(); $sortDirection = $sort === 'desc'; $mimetypeFilters = ''; $files = []; if (is_array($mimetypeFilters) && count($mimetypeFilters)) { $mimetypeFilters = array_unique($mimetypeFilters); if (!in_array('httpd/unix-directory', $mimetypeFilters)) { $mimetypeFilters[] = 'httpd/unix-directory'; } foreach ($mimetypeFilters as $mimetypeFilter) { $files = array_merge($files, \OCA\Files\Helper::getFiles($dir, $sortby, $sortDirection, $mimetypeFilter)); } $files = \OCA\Files\Helper::sortFiles($files, $sortby, $sortDirection); } else { $files = \OCA\Files\Helper::getFiles($dir, $sortby, $sortDirection); } $files = \OCA\Files\Helper::populateTags($files); $data['directory'] = $dir; $data['files'] = \OCA\Files\Helper::formatFileInfos($files); $data['permissions'] = $permissions; return new DataResponse(array('data' => $data, 'status' => 'success')); } catch (\OCP\Files\StorageNotAvailableException $e) { \OCP\Util::logException('files', $e); return new DataResponse(array('data' => array('exception' => '\\OCP\\Files\\StorageNotAvailableException', 'message' => 'Storage not available'), 'status' => 'error')); } catch (\OCP\Files\StorageInvalidException $e) { \OCP\Util::logException('files', $e); return new DataResponse(array('data' => array('exception' => '\\OCP\\Files\\StorageInvalidException', 'message' => 'Storage invalid'), 'status' => 'error')); } catch (\Exception $e) { \OCP\Util::logException('files', $e); return new DataResponse(array('data' => array('exception' => '\\Exception', 'message' => 'Unknown error'), 'status' => 'error')); } }
/** * Retrieves the contents of a trash bin directory. * * @param string $dir path to the directory inside the trashbin * or empty to retrieve the root of the trashbin * @param string $user * @param string $sortAttribute attribute to sort on or empty to disable sorting * @param bool $sortDescending true for descending sort, false otherwise * @return \OCP\Files\FileInfo[] */ public static function getTrashFiles($dir, $user, $sortAttribute = '', $sortDescending = false) { $result = array(); $timestamp = null; $view = new \OC\Files\View('/' . $user . '/files_trashbin/files'); if (ltrim($dir, '/') !== '' && !$view->is_dir($dir)) { throw new \Exception('Directory does not exists'); } $mount = $view->getMount($dir); $storage = $mount->getStorage(); $absoluteDir = $view->getAbsolutePath($dir); $internalPath = $mount->getInternalPath($absoluteDir); $originalLocations = \OCA\Files_Trashbin\Trashbin::getLocations($user); $dirContent = $storage->getCache()->getFolderContents($mount->getInternalPath($view->getAbsolutePath($dir))); foreach ($dirContent as $entry) { $entryName = $entry->getName(); $id = $entry->getId(); $name = $entryName; if ($dir === '' || $dir === '/') { $pathparts = pathinfo($entryName); $timestamp = substr($pathparts['extension'], 1); $name = $pathparts['filename']; } else { if ($timestamp === null) { // for subfolders we need to calculate the timestamp only once $parts = explode('/', ltrim($dir, '/')); $timestamp = substr(pathinfo($parts[0], PATHINFO_EXTENSION), 1); } } $originalPath = ''; if (isset($originalLocations[$id][$timestamp])) { $originalPath = $originalLocations[$id][$timestamp]; if (substr($originalPath, -1) === '/') { $originalPath = substr($originalPath, 0, -1); } } $i = array('name' => $name, 'mtime' => $timestamp, 'mimetype' => $entry->getMimeType(), 'type' => $entry->getMimeType() === ICacheEntry::DIRECTORY_MIMETYPE ? 'dir' : 'file', 'directory' => $dir === '/' ? '' : $dir, 'size' => $entry->getSize(), 'etag' => '', 'permissions' => Constants::PERMISSION_ALL - Constants::PERMISSION_SHARE); if ($originalPath) { $i['extraData'] = $originalPath . '/' . $id; } $result[] = new FileInfo($absoluteDir . '/' . $i['name'], $storage, $internalPath . '/' . $i['name'], $i, $mount); } if ($sortAttribute !== '') { return \OCA\Files\Helper::sortFiles($result, $sortAttribute, $sortDescending); } return $result; }
/** * @param OCP\Files\FileInfo[] $files * @param string $trashRoot * @param integer $expireDate */ private function manipulateDeleteTime($files, $trashRoot, $expireDate) { $counter = 0; foreach ($files as &$file) { // modify every second file $counter = ($counter + 1) % 2; if ($counter === 1) { $source = $trashRoot . '/files/' . $file['name'] . '.d' . $file['mtime']; $target = \OC\Files\Filesystem::normalizePath($trashRoot . '/files/' . $file['name'] . '.d' . $expireDate); $this->rootView->rename($source, $target); $file['mtime'] = $expireDate; } } return \OCA\Files\Helper::sortFiles($files, 'mtime'); }
$files = []; // Clean up duplicates from array if (is_array($mimetypeFilters) && count($mimetypeFilters)) { $mimetypeFilters = array_unique($mimetypeFilters); if (!in_array('httpd/unix-directory', $mimetypeFilters)) { // append folder filter to be able to browse folders $mimetypeFilters[] = 'httpd/unix-directory'; } // create filelist with mimetype filter - as getFiles only supports on // mimetype filter at once we will filter this folder for each // mimetypeFilter foreach ($mimetypeFilters as $mimetypeFilter) { $files = array_merge($files, \OCA\Files\Helper::getFiles($dir, $sortAttribute, $sortDirection, $mimetypeFilter)); } // sort the files accordingly $files = \OCA\Files\Helper::sortFiles($files, $sortAttribute, $sortDirection); } else { // create file list without mimetype filter $files = \OCA\Files\Helper::getFiles($dir, $sortAttribute, $sortDirection); } $files = \OCA\Files\Helper::populateTags($files); $data['directory'] = $dir; $data['files'] = \OCA\Files\Helper::formatFileInfos($files); $data['permissions'] = $permissions; OCP\JSON::success(array('data' => $data)); } catch (\OCP\Files\StorageNotAvailableException $e) { \OCP\Util::logException('files', $e); OCP\JSON::error(array('data' => array('exception' => '\\OCP\\Files\\StorageNotAvailableException', 'message' => $l->t('Storage not available')))); } catch (\OCP\Files\StorageInvalidException $e) { \OCP\Util::logException('files', $e); OCP\JSON::error(array('data' => array('exception' => '\\OCP\\Files\\StorageInvalidException', 'message' => $l->t('Storage invalid'))));