Browse Source

Refactor, clean, catch not-found exception.

tags/v0.1.0
Gerben 1 year ago
parent
commit
0a64c4a436
4 changed files with 27 additions and 26 deletions
  1. +3
    -3
      appinfo/routes.php
  2. +1
    -3
      lib/Controller/PrivatePageController.php
  3. +12
    -19
      lib/Controller/PubPageController.php
  4. +11
    -1
      lib/Controller/RawResponse.php

+ 3
- 3
appinfo/routes.php View File

@@ -2,10 +2,10 @@

return [
'routes' => [
['name' => 'pubPage#getByToken', 'url' => '/s/{token}', 'verb' => 'GET'],
['name' => 'pubPage#getByTokenAndPath', 'url' => '/s/{token}/{path}', 'verb' => 'GET',
['name' => 'pubPage#getByToken', 'url' => '/s/{token}'],
['name' => 'pubPage#getByTokenAndPath', 'url' => '/s/{token}/{path}',
'requirements' => array('path' => '.+')],
['name' => 'privatePage#getByPath', 'url' => '/files/{path}', 'verb' => 'GET',
['name' => 'privatePage#getByPath', 'url' => '/files/{path}',
'requirements' => array('path' => '.+')],
]
];

+ 1
- 3
lib/Controller/PrivatePageController.php View File

@@ -36,8 +36,6 @@ class PrivatePageController extends Controller {
} catch (NotFoundException $e) {
return new NotFoundResponse();
}
$content = $node->getContent();
$mimetype = $node->getMimeType();
$this->returnRawResponse($content, $mimetype);
$this->returnRawResponse($node);
}
}

+ 12
- 19
lib/Controller/PubPageController.php View File

@@ -5,12 +5,13 @@ use \Exception;
use OCP\IRequest;
use OCP\Share\IManager;
use OCP\AppFramework\Controller;
use OCP\Files\Folder;
use OCP\AppFramework\Http\NotFoundResponse;
use OCP\Files\NotFoundException;

class PubPageController extends Controller {
use RawResponse;

private $manager;
private $shareManager;

public function __construct(
$AppName,
@@ -18,7 +19,7 @@ class PubPageController extends Controller {
IManager $shareManager
) {
parent::__construct($AppName, $request);
$this->manager = $shareManager;
$this->shareManager = $shareManager;
}

/**
@@ -27,15 +28,9 @@ class PubPageController extends Controller {
* @NoCSRFRequired
*/
public function getByToken($token) {
$share = $this->manager->getShareByToken($token);
$share = $this->shareManager->getShareByToken($token);
$node = $share->getNode();
if ($node->getType() === 'dir') {
// Is there some reasonable thing to return for a directory? An html index? A tarball?
throw new Exception("Requested share is a directory, not a file.");
}
$content = $node->getContent();
$mimetype = $node->getMimeType();
$this->returnRawResponse($content, $mimetype);
$this->returnRawResponse($node);
}

/**
@@ -44,18 +39,16 @@ class PubPageController extends Controller {
* @NoCSRFRequired
*/
public function getByTokenAndPath($token, $path) {
$share = $this->manager->getShareByToken($token);
$share = $this->shareManager->getShareByToken($token);
$dirNode = $share->getNode();
if ($dirNode->getType() !== 'dir') {
throw new Exception("Received a sub-path for a share that is not a directory");
}
$fileNode = $dirNode->get($path);
if ($fileNode->getType() === 'dir') {
// Is there some reasonable thing to return for a directory? An html index? A tarball?
throw new Exception("Requested share is a directory, not a file.");
try {
$fileNode = $dirNode->get($path);
} catch (NotFoundException $e) {
return new NotFoundResponse();
}
$content = $fileNode->getContent();
$mimetype = $fileNode->getMimeType();
$this->returnRawResponse($content, $mimetype);
$this->returnRawResponse($fileNode);
}
}

+ 11
- 1
lib/Controller/RawResponse.php View File

@@ -1,8 +1,18 @@
<?php
namespace OCA\Raw\Controller;

use \Exception;

trait RawResponse {
protected function returnRawResponse($content, $mimetype) {
protected function returnRawResponse($fileNode) {
if ($fileNode->getType() === 'dir') {
// Is there some reasonable thing to return for a directory? An html index? A tarball?
throw new Exception("Requested share is a directory, not a file.");
}

$content = $fileNode->getContent();
$mimetype = $fileNode->getMimeType();

// Ugly hack to prevent security middleware messing up the CSP.
header(
"Content-Security-Policy: sandbox; default-src 'none'; img-src data:; media-src data:; "


Loading…
Cancel
Save