1
0

SimpleFolder.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Robin Appelman <robin@icewind.nl>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Files\SimpleFS;
  26. use OCP\Files\File;
  27. use OCP\Files\Folder;
  28. use OCP\Files\Node;
  29. use OCP\Files\NotFoundException;
  30. use OCP\Files\SimpleFS\ISimpleFile;
  31. use OCP\Files\SimpleFS\ISimpleFolder;
  32. class SimpleFolder implements ISimpleFolder {
  33. /** @var Folder */
  34. private $folder;
  35. /**
  36. * Folder constructor.
  37. *
  38. * @param Folder $folder
  39. */
  40. public function __construct(Folder $folder) {
  41. $this->folder = $folder;
  42. }
  43. public function getName(): string {
  44. return $this->folder->getName();
  45. }
  46. public function getDirectoryListing(): array {
  47. $listing = $this->folder->getDirectoryListing();
  48. $fileListing = array_map(function (Node $file) {
  49. if ($file instanceof File) {
  50. return new SimpleFile($file);
  51. }
  52. return null;
  53. }, $listing);
  54. $fileListing = array_filter($fileListing);
  55. return array_values($fileListing);
  56. }
  57. public function delete(): void {
  58. $this->folder->delete();
  59. }
  60. public function fileExists(string $name): bool {
  61. return $this->folder->nodeExists($name);
  62. }
  63. public function getFile(string $name): ISimpleFile {
  64. $file = $this->folder->get($name);
  65. if (!($file instanceof File)) {
  66. throw new NotFoundException();
  67. }
  68. return new SimpleFile($file);
  69. }
  70. public function newFile(string $name, $content = null): ISimpleFile {
  71. if ($content === null) {
  72. // delay creating the file until it's written to
  73. return new NewSimpleFile($this->folder, $name);
  74. } else {
  75. $file = $this->folder->newFile($name, $content);
  76. return new SimpleFile($file);
  77. }
  78. }
  79. public function getFolder(string $name): ISimpleFolder {
  80. $folder = $this->folder->get($name);
  81. if (!($folder instanceof Folder)) {
  82. throw new NotFoundException();
  83. }
  84. return new SimpleFolder($folder);
  85. }
  86. public function newFolder(string $path): ISimpleFolder {
  87. $folder = $this->folder->newFolder($path);
  88. return new SimpleFolder($folder);
  89. }
  90. }