1
0

SimpleFolder.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Files\SimpleFS;
  7. use OCP\Files\File;
  8. use OCP\Files\Folder;
  9. use OCP\Files\Node;
  10. use OCP\Files\NotFoundException;
  11. use OCP\Files\SimpleFS\ISimpleFile;
  12. use OCP\Files\SimpleFS\ISimpleFolder;
  13. class SimpleFolder implements ISimpleFolder {
  14. /** @var Folder */
  15. private $folder;
  16. /**
  17. * Folder constructor.
  18. *
  19. * @param Folder $folder
  20. */
  21. public function __construct(Folder $folder) {
  22. $this->folder = $folder;
  23. }
  24. public function getName(): string {
  25. return $this->folder->getName();
  26. }
  27. public function getDirectoryListing(): array {
  28. $listing = $this->folder->getDirectoryListing();
  29. $fileListing = array_map(function (Node $file) {
  30. if ($file instanceof File) {
  31. return new SimpleFile($file);
  32. }
  33. return null;
  34. }, $listing);
  35. $fileListing = array_filter($fileListing);
  36. return array_values($fileListing);
  37. }
  38. public function delete(): void {
  39. $this->folder->delete();
  40. }
  41. public function fileExists(string $name): bool {
  42. return $this->folder->nodeExists($name);
  43. }
  44. public function getFile(string $name): ISimpleFile {
  45. $file = $this->folder->get($name);
  46. if (!($file instanceof File)) {
  47. throw new NotFoundException();
  48. }
  49. return new SimpleFile($file);
  50. }
  51. public function newFile(string $name, $content = null): ISimpleFile {
  52. if ($content === null) {
  53. // delay creating the file until it's written to
  54. return new NewSimpleFile($this->folder, $name);
  55. } else {
  56. $file = $this->folder->newFile($name, $content);
  57. return new SimpleFile($file);
  58. }
  59. }
  60. public function getFolder(string $name): ISimpleFolder {
  61. $folder = $this->folder->get($name);
  62. if (!($folder instanceof Folder)) {
  63. throw new NotFoundException();
  64. }
  65. return new SimpleFolder($folder);
  66. }
  67. public function newFolder(string $path): ISimpleFolder {
  68. $folder = $this->folder->newFolder($path);
  69. return new SimpleFolder($folder);
  70. }
  71. }