PubPageController.php 1.6 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace OCA\Raw\Controller;
  3. use \Exception;
  4. use OCP\IRequest;
  5. use OCP\Share\IManager;
  6. use OCP\AppFramework\Controller;
  7. use OCP\Files\Folder;
  8. class PubPageController extends Controller {
  9. use RawResponse;
  10. private $manager;
  11. public function __construct(
  12. $AppName,
  13. IRequest $request,
  14. IManager $shareManager
  15. ) {
  16. parent::__construct($AppName, $request);
  17. $this->manager = $shareManager;
  18. }
  19. /**
  20. * @PublicPage
  21. * @NoAdminRequired
  22. * @NoCSRFRequired
  23. */
  24. public function getByToken($token) {
  25. $share = $this->manager->getShareByToken($token);
  26. $node = $share->getNode();
  27. if ($node->getType() === 'dir') {
  28. // Is there some reasonable thing to return for a directory? An html index? A tarball?
  29. throw new Exception("Requested share is a directory, not a file.");
  30. }
  31. $content = $node->getContent();
  32. $mimetype = $node->getMimeType();
  33. $this->returnRawResponse($content, $mimetype);
  34. }
  35. /**
  36. * @PublicPage
  37. * @NoAdminRequired
  38. * @NoCSRFRequired
  39. */
  40. public function getByTokenAndPath($token, $path) {
  41. $share = $this->manager->getShareByToken($token);
  42. $dirNode = $share->getNode();
  43. if ($dirNode->getType() !== 'dir') {
  44. throw new Exception("Received a sub-path for a share that is not a directory");
  45. }
  46. $fileNode = $dirNode->get($path);
  47. if ($fileNode->getType() === 'dir') {
  48. // Is there some reasonable thing to return for a directory? An html index? A tarball?
  49. throw new Exception("Requested share is a directory, not a file.");
  50. }
  51. $content = $fileNode->getContent();
  52. $mimetype = $fileNode->getMimeType();
  53. $this->returnRawResponse($content, $mimetype);
  54. }
  55. }