You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

list.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Vincent Petry <pvince81@owncloud.com>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. use OCP\Files\StorageNotAvailableException;
  29. use OCP\Files\StorageInvalidException;
  30. \OC_JSON::checkLoggedIn();
  31. \OC::$server->getSession()->close();
  32. $l = \OC::$server->getL10N('files');
  33. // Load the files
  34. $dir = isset($_GET['dir']) ? (string)$_GET['dir'] : '';
  35. $dir = \OC\Files\Filesystem::normalizePath($dir);
  36. try {
  37. $dirInfo = \OC\Files\Filesystem::getFileInfo($dir);
  38. if (!$dirInfo || !$dirInfo->getType() === 'dir') {
  39. http_response_code(404);
  40. exit();
  41. }
  42. $data = [];
  43. $baseUrl = \OC::$server->getURLGenerator()->linkTo('files', 'index.php') . '?dir=';
  44. $permissions = $dirInfo->getPermissions();
  45. $sortAttribute = isset($_GET['sort']) ? (string)$_GET['sort'] : 'name';
  46. $sortDirection = isset($_GET['sortdirection']) ? ($_GET['sortdirection'] === 'desc') : false;
  47. $mimetypeFilters = isset($_GET['mimetypes']) ? json_decode($_GET['mimetypes']) : '';
  48. $files = [];
  49. // Clean up duplicates from array
  50. if (is_array($mimetypeFilters) && count($mimetypeFilters)) {
  51. $mimetypeFilters = array_unique($mimetypeFilters);
  52. if (!in_array('httpd/unix-directory', $mimetypeFilters)) {
  53. // append folder filter to be able to browse folders
  54. $mimetypeFilters[] = 'httpd/unix-directory';
  55. }
  56. // create filelist with mimetype filter - as getFiles only supports on
  57. // mimetype filter at once we will filter this folder for each
  58. // mimetypeFilter
  59. foreach ($mimetypeFilters as $mimetypeFilter) {
  60. $files = array_merge($files, \OCA\Files\Helper::getFiles($dir, $sortAttribute, $sortDirection, $mimetypeFilter));
  61. }
  62. // sort the files accordingly
  63. $files = \OCA\Files\Helper::sortFiles($files, $sortAttribute, $sortDirection);
  64. } else {
  65. // create file list without mimetype filter
  66. $files = \OCA\Files\Helper::getFiles($dir, $sortAttribute, $sortDirection);
  67. }
  68. $data['directory'] = $dir;
  69. $data['files'] = \OCA\Files\Helper::formatFileInfos($files);
  70. $data['permissions'] = $permissions;
  71. \OC_JSON::success(['data' => $data]);
  72. } catch (\OCP\Files\StorageNotAvailableException $e) {
  73. \OC::$server->getLogger()->logException($e, ['app' => 'files']);
  74. \OC_JSON::error([
  75. 'data' => [
  76. 'exception' => StorageNotAvailableException::class,
  77. 'message' => $l->t('Storage is temporarily not available')
  78. ]
  79. ]);
  80. } catch (\OCP\Files\StorageInvalidException $e) {
  81. \OC::$server->getLogger()->logException($e, ['app' => 'files']);
  82. \OC_JSON::error([
  83. 'data' => [
  84. 'exception' => StorageInvalidException::class,
  85. 'message' => $l->t('Storage invalid')
  86. ]
  87. ]);
  88. } catch (\Exception $e) {
  89. \OC::$server->getLogger()->logException($e, ['app' => 'files']);
  90. \OC_JSON::error([
  91. 'data' => [
  92. 'exception' => \Exception::class,
  93. 'message' => $l->t('Unknown error')
  94. ]
  95. ]);
  96. }