Skip to content

Commit

Permalink
Add "fileids" virtual collection with file ids
Browse files Browse the repository at this point in the history
Redirects any queries on "fileids/$fileId" to the matching DAV path
inside of "files/$userId" if applicable. If the file id is not reachable
or in trashbin, returns 404.
  • Loading branch information
Vincent Petry committed Sep 7, 2017
1 parent acfa465 commit 04ea3de
Showing 1 changed file with 74 additions and 3 deletions.
77 changes: 74 additions & 3 deletions apps/dav/lib/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use OCA\DAV\Connector\Sabre\DummyGetResponsePlugin;
use OCA\DAV\Connector\Sabre\FakeLockerPlugin;
use OCA\DAV\Connector\Sabre\FilesPlugin;
use OCA\DAV\Connector\Sabre\FilesByIdPlugin;
use OCA\DAV\Connector\Sabre\FilesReportPlugin;
use OCA\DAV\Connector\Sabre\SharesPlugin;
use OCA\DAV\DAV\PublicAuth;
Expand All @@ -53,6 +54,13 @@
use OCA\DAV\AppInfo\PluginManager;
use OCA\DAV\Connector\Sabre\MaintenancePlugin;
use OCA\DAV\Connector\Sabre\ValidateRequestPlugin;
use Sabre\HTTP\ResponseInterface;
use Sabre\HTTP\RequestInterface;
use OCP\Files\Folder;
use OCP\IUserSession;
use Sabre\DAV\Exception\NotFound;
use Sabre\DAV\Exception\MethodNotAllowed;
use OCP\Files\NotFoundException;

class Server {

Expand Down Expand Up @@ -159,12 +167,19 @@ public function __construct(IRequest $request, $baseUri) {
}

// wait with registering these until auth is handled and the filesystem is setup
$this->server->on('beforeMethod', function () use ($root) {
$this->server->on('beforeMethod', function (RequestInterface $request, ResponseInterface $response) use ($root) {
// custom properties plugin must be the last one
$userSession = \OC::$server->getUserSession();
$user = $userSession->getUser();
if (!is_null($user)) {
$view = \OC\Files\Filesystem::getView();

// TODO: switch to LazyUserFolder
$userFolder = \OC::$server->getUserFolder();
if ($this->handleRedirectFileId($request, $response, $userSession, $userFolder) === false) {
return false;
}

$this->server->addPlugin(
new FilesPlugin(
$this->server->tree,
Expand Down Expand Up @@ -193,8 +208,6 @@ public function __construct(IRequest $request, $baseUri) {
$this->server->tree, \OC::$server->getTagManager()
)
);
// TODO: switch to LazyUserFolder
$userFolder = \OC::$server->getUserFolder();
$this->server->addPlugin(new SharesPlugin(
$this->server->tree,
$userSession,
Expand Down Expand Up @@ -232,6 +245,64 @@ public function __construct(IRequest $request, $baseUri) {
});
}

private function handleRedirectFileId(RequestInterface $request, ResponseInterface $response, IUserSession $userSession, Folder $userFolder) {
if ($request->getPath() === 'fileids') {
// any method on the "fileids" folder
throw new MethodNotAllowed();
}

if (strpos($request->getPath(), 'fileids/') !== 0) {
return;
}

$sections = explode('/', $request->getPath());

array_shift($sections);
$fileId = array_shift($sections);

$results = $userFolder->getById($fileId);
if (empty($results)) {
throw new NotFound('File with id ' . $fileId . ' not found');
}

$node = $results[0];
if (count($sections) > 0) {
if ($node instanceof Folder) {
// continue searching deeper
try {
$node = $node->get(implode('/', $sections));
if ($node === null) {
throw new NotFound();
}
} catch (NotFoundException $e) {
throw new NotFound();
}
} else {
// not a folder
throw new NotFound();
}
}

$nodePath = trim($userFolder->getRelativePath($node->getPath()), '/');

$nodePath = implode(
'/', array_map(
rawurlencode,
explode('/', $nodePath)
)
);

// redirect
$davPath = rtrim($request->getBaseUrl(), '/') . '/files/' . rawurlencode($userSession->getUser()->getUid()) . '/' . $nodePath;
$response->setStatus(302);
$response->setHeader('Location', $davPath);
$response->setHeader('Redirect-ref', 'URI');

$this->server->sapi->sendResponse($response);

return false;
}

public function exec() {
$this->server->exec();
}
Expand Down

0 comments on commit 04ea3de

Please sign in to comment.