Dav endpoint returns proper data

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
Roeland Jago Douma 2018-04-13 17:11:25 +02:00
parent 5c6d3b4f41
commit b3e7865d9b
No known key found for this signature in database
GPG Key ID: F941078878347C0C
5 changed files with 90 additions and 15 deletions

View File

@ -34,6 +34,11 @@ ignore_user_abort(true);
$requestUri = \OC::$server->getRequest()->getRequestUri();
$serverFactory = new \OCA\DAV\Direct\ServerFactory(\OC::$server->getConfig());
$server = $serverFactory->createServer($baseuri, $requestUri);
$server = $serverFactory->createServer(
$baseuri,
$requestUri,
\OC::$server->getRootFolder(),
\OC::$server->query(\OCA\DAV\Db\DirectMapper::class)
);
$server->exec();

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace OCA\DAV\Db;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\Mapper;
use OCP\IDBConnection;
@ -32,4 +33,24 @@ class DirectMapper extends Mapper {
public function __construct(IDBConnection $db) {
parent::__construct($db, 'directlink', Direct::class);
}
public function getByToken(string $token): Direct {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from('directlink')
->where(
$qb->expr()->eq('token', $qb->createNamedParameter($token))
);
$cursor = $qb->execute();
$data = $cursor->fetch();
$cursor->closeCursor();
if ($data === false) {
throw new DoesNotExistException();
}
return Direct::fromRow($data);
}
}

View File

@ -24,16 +24,26 @@ declare(strict_types=1);
namespace OCA\DAV\Direct;
use OCA\DAV\Db\Direct;
use OCP\Files\File;
use OCP\Files\IRootFolder;
use Sabre\DAV\Exception\Forbidden;
use Sabre\DAV\Exception\NotFound;
use Sabre\DAV\IFile;
class DirectFile implements IFile {
/** @var Direct */
private $direct;
/** @var IRootFolder */
private $rootFolder;
/** @var File */
private $file;
public function __construct(File $file) {
$this->file = $file;
public function __construct(Direct $direct, IRootFolder $rootFolder) {
$this->direct = $direct;
$this->rootFolder = $rootFolder;
}
function put($data) {
@ -41,30 +51,35 @@ class DirectFile implements IFile {
}
function get() {
// TODO: Implement get() method.
$this->getFile();
return $this->file->fopen('rb');
}
function getContentType() {
// TODO: Implement getContentType() method.
$this->getFile();
return $this->file->getMimeType();
}
function getETag() {
$this->getFile();
return $this->file->getEtag();
// TODO: Implement getETag() method.
}
function getSize() {
$this->getFile();
return $this->file->getSize();
// TODO: Implement getSize() method.
}
function delete() {
throw new Forbidden();
// TODO: Implement delete() method.
}
function getName() {
return $this->file->getName();
return $this->direct->getToken();
}
function setName($name) {
@ -72,8 +87,26 @@ class DirectFile implements IFile {
}
function getLastModified() {
$this->getFile();
return $this->file->getMTime();
// TODO: Implement getLastModified() method.
}
private function getFile() {
if ($this->file === null) {
$userFolder = $this->rootFolder->getUserFolder($this->direct->getUserId());
$files = $userFolder->getById($this->direct->getFileId());
//TODO check expiration
if ($files === []) {
throw new NotFound();
}
$this->file = array_shift($files);
}
return $this->file;
}
}

View File

@ -24,6 +24,8 @@ declare(strict_types=1);
namespace OCA\DAV\Direct;
use OCA\DAV\Db\DirectMapper;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\Files\File;
use OCP\Files\IRootFolder;
use Sabre\DAV\Exception\Forbidden;
@ -35,8 +37,12 @@ class DirectHome implements ICollection {
/** @var IRootFolder */
private $rootFolder;
public function __construct(IRootFolder $rootFolder) {
/** @var DirectMapper */
private $mapper;
public function __construct(IRootFolder $rootFolder, DirectMapper $mapper) {
$this->rootFolder = $rootFolder;
$this->mapper = $mapper;
}
function createFile($name, $data = null) {
@ -47,8 +53,14 @@ class DirectHome implements ICollection {
throw new Forbidden();
}
function getChild($name) {
throw new NotFound();
public function getChild($name) {
try {
$direct = $this->mapper->getByToken($name);
return new DirectFile($direct, $this->rootFolder);
} catch (DoesNotExistException $e) {
throw new NotFound();
}
}
function getChildren() {

View File

@ -24,6 +24,8 @@ declare(strict_types=1);
namespace OCA\DAV\Direct;
use OCA\DAV\Db\DirectMapper;
use OCP\Files\IRootFolder;
use OCP\IConfig;
class ServerFactory {
@ -35,8 +37,10 @@ class ServerFactory {
}
public function createServer(string $baseURI,
string $requestURI) {
$home = new DirectHome(\OC::$server->getRootFolder());
string $requestURI,
IRootFolder $rootFolder,
DirectMapper $mapper) {
$home = new DirectHome($rootFolder, $mapper);
$server = new Server($home);
$server->httpRequest->setUrl($requestURI);