Browse Source

Support serving a file within a shared directory

tags/v0.1.0
Gerben 1 year ago
parent
commit
ed6f9c2c8c
2 changed files with 28 additions and 1 deletions
  1. +2
    -0
      appinfo/routes.php
  2. +26
    -1
      lib/Controller/PubPageController.php

+ 2
- 0
appinfo/routes.php View File

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


+ 26
- 1
lib/Controller/PubPageController.php View File

@@ -1,6 +1,7 @@
<?php
namespace OCA\Raw\Controller;

use \Exception;
use OCP\IRequest;
use OCP\Share\IManager;
use OCP\AppFramework\Controller;
@@ -28,9 +29,33 @@ class PubPageController extends Controller {
public function getByToken($token) {
$share = $this->manager->getShareByToken($token);
$node = $share->getNode();
// if ($node->getType() === 'dir') { TODO }
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);
}

/**
* @PublicPage
* @NoAdminRequired
* @NoCSRFRequired
*/
public function getByTokenAndPath($token, $path) {
$share = $this->manager->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.");
}
$content = $fileNode->getContent();
$mimetype = $fileNode->getMimeType();
$this->returnRawResponse($content, $mimetype);
}
}

Loading…
Cancel
Save