1
0

list.php 3.6 KB

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