From f984664bee136075556963af56999e0315cbb4bb Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Fri, 13 Apr 2018 14:04:41 +0200 Subject: First step of DAV endpoint Signed-off-by: Roeland Jago Douma --- apps/dav/lib/Controller/DirectController.php | 61 ++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 apps/dav/lib/Controller/DirectController.php (limited to 'apps/dav/lib/Controller/DirectController.php') diff --git a/apps/dav/lib/Controller/DirectController.php b/apps/dav/lib/Controller/DirectController.php new file mode 100644 index 00000000000..28fdf885758 --- /dev/null +++ b/apps/dav/lib/Controller/DirectController.php @@ -0,0 +1,61 @@ + + * + * @author Roeland Jago Douma + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\DAV\Controller; + +use OCA\DAV\Db\DirectMapper; +use OCP\AppFramework\OCSController; +use OCP\Files\IRootFolder; +use OCP\IDBConnection; +use OCP\IRequest; +use OCP\IUserManager; + +class DirectController extends OCSController { + + /** @var IRootFolder */ + private $rootFolder; + + /** @var string */ + private $userId; + + /** @var DirectMapper */ + private $mapper; + + + public function __construct(string $appName, + IRequest $request, + IRootFolder $rootFolder, + string $userId, + DirectMapper $mapper) { + parent::__construct($appName, $request); + + $this->rootFolder = $rootFolder; + $this->userId = $userId; + $this->mapper = $mapper; + } + + public function getUrl(int $fileId) { + + } +} -- cgit v1.2.3 From 5c6d3b4f41d420e2ea84ba71c8173dcf931a77c6 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Fri, 13 Apr 2018 16:54:24 +0200 Subject: Request a direct link Signed-off-by: Roeland Jago Douma --- apps/dav/lib/Controller/DirectController.php | 56 ++++++++++++++++++++++++++-- apps/dav/lib/Db/DirectMapper.php | 2 +- 2 files changed, 53 insertions(+), 5 deletions(-) (limited to 'apps/dav/lib/Controller/DirectController.php') diff --git a/apps/dav/lib/Controller/DirectController.php b/apps/dav/lib/Controller/DirectController.php index 28fdf885758..c357771f3ff 100644 --- a/apps/dav/lib/Controller/DirectController.php +++ b/apps/dav/lib/Controller/DirectController.php @@ -24,12 +24,16 @@ declare(strict_types=1); namespace OCA\DAV\Controller; +use OCA\DAV\Db\Direct; use OCA\DAV\Db\DirectMapper; +use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\OCS\OCSNotFoundException; use OCP\AppFramework\OCSController; +use OCP\AppFramework\Utility\ITimeFactory; use OCP\Files\IRootFolder; -use OCP\IDBConnection; use OCP\IRequest; -use OCP\IUserManager; +use OCP\IURLGenerator; +use OCP\Security\ISecureRandom; class DirectController extends OCSController { @@ -42,20 +46,64 @@ class DirectController extends OCSController { /** @var DirectMapper */ private $mapper; + /** @var ISecureRandom */ + private $random; + + /** @var ITimeFactory */ + private $timeFactory; + + /** @var IURLGenerator */ + private $urlGenerator; + public function __construct(string $appName, IRequest $request, IRootFolder $rootFolder, string $userId, - DirectMapper $mapper) { + DirectMapper $mapper, + ISecureRandom $random, + ITimeFactory $timeFactory, + IURLGenerator $urlGenerator) { parent::__construct($appName, $request); $this->rootFolder = $rootFolder; $this->userId = $userId; $this->mapper = $mapper; + $this->random = $random; + $this->timeFactory = $timeFactory; + $this->urlGenerator = $urlGenerator; } - public function getUrl(int $fileId) { + /** + * @NoAdminRequired + */ + public function getUrl(int $fileId): DataResponse { + $userFolder = $this->rootFolder->getUserFolder($this->userId); + + $files = $userFolder->getById($fileId); + + if ($files === []) { + throw new OCSNotFoundException(); + } + + $file = array_shift($files); + + //TODO: try to get a url from the backend (like S3) + + + // Fallback to our default implemenation + $direct = new Direct(); + $direct->setUserId($this->userId); + $direct->setFileId($fileId); + + $token = $this->random->generate(60, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS); + $direct->setToken($token); + $direct->setExpiration($this->timeFactory->getTime() + 60*60*8); + + $this->mapper->insert($direct); + return new DataResponse([ + 'url' => $this->urlGenerator->getAbsoluteURL('remote.php/direct/'.$token) + ]); } } diff --git a/apps/dav/lib/Db/DirectMapper.php b/apps/dav/lib/Db/DirectMapper.php index 1e0e6396f3e..917fca2f920 100644 --- a/apps/dav/lib/Db/DirectMapper.php +++ b/apps/dav/lib/Db/DirectMapper.php @@ -29,7 +29,7 @@ use OCP\IDBConnection; class DirectMapper extends Mapper { - public function __construct(IDBConnection $db, string $tableName, string $entityClass = null) { + public function __construct(IDBConnection $db) { parent::__construct($db, 'directlink', Direct::class); } } -- cgit v1.2.3 From 6a385dd20bad8d0e6c7d923f77eea7b5f719fddd Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Fri, 13 Apr 2018 17:40:52 +0200 Subject: Add directDownload support of storage Signed-off-by: Roeland Jago Douma --- apps/dav/lib/Controller/DirectController.php | 27 ++++++++++++++++----------- apps/dav/lib/Direct/DirectHome.php | 1 - 2 files changed, 16 insertions(+), 12 deletions(-) (limited to 'apps/dav/lib/Controller/DirectController.php') diff --git a/apps/dav/lib/Controller/DirectController.php b/apps/dav/lib/Controller/DirectController.php index c357771f3ff..3b8b0d1e2a6 100644 --- a/apps/dav/lib/Controller/DirectController.php +++ b/apps/dav/lib/Controller/DirectController.php @@ -87,23 +87,28 @@ class DirectController extends OCSController { } $file = array_shift($files); + $storage = $file->getStorage(); + $directDownload = $storage->getDirectDownload($file->getInternalPath()); - //TODO: try to get a url from the backend (like S3) + if (isset($directDownload['url'])) { + $url = $directDownload['url']; + } else { + // Fallback to our default implemenation + $direct = new Direct(); + $direct->setUserId($this->userId); + $direct->setFileId($fileId); + $token = $this->random->generate(60, ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS); + $direct->setToken($token); + $direct->setExpiration($this->timeFactory->getTime() + 60 * 60 * 8); - // Fallback to our default implemenation - $direct = new Direct(); - $direct->setUserId($this->userId); - $direct->setFileId($fileId); + $this->mapper->insert($direct); - $token = $this->random->generate(60, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS); - $direct->setToken($token); - $direct->setExpiration($this->timeFactory->getTime() + 60*60*8); - - $this->mapper->insert($direct); + $url = $this->urlGenerator->getAbsoluteURL('remote.php/direct/'.$token); + } return new DataResponse([ - 'url' => $this->urlGenerator->getAbsoluteURL('remote.php/direct/'.$token) + 'url' => $url, ]); } } diff --git a/apps/dav/lib/Direct/DirectHome.php b/apps/dav/lib/Direct/DirectHome.php index 9a3ee58224b..247cca7a3c9 100644 --- a/apps/dav/lib/Direct/DirectHome.php +++ b/apps/dav/lib/Direct/DirectHome.php @@ -26,7 +26,6 @@ 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; use Sabre\DAV\Exception\MethodNotAllowed; -- cgit v1.2.3 From 1c75ddac45845d9e91997e6253e36b9ae2556e91 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Thu, 26 Apr 2018 10:34:57 +0200 Subject: Improve the directContoller * Tests * No directdownload from storage yet (as it is not tested at all) * No direct links for folders Signed-off-by: Roeland Jago Douma --- apps/dav/lib/Controller/DirectController.php | 29 ++-- .../tests/unit/Controller/DirectControllerTest.php | 155 +++++++++++++++++++++ 2 files changed, 169 insertions(+), 15 deletions(-) create mode 100644 apps/dav/tests/unit/Controller/DirectControllerTest.php (limited to 'apps/dav/lib/Controller/DirectController.php') diff --git a/apps/dav/lib/Controller/DirectController.php b/apps/dav/lib/Controller/DirectController.php index 3b8b0d1e2a6..2a14e4db2c7 100644 --- a/apps/dav/lib/Controller/DirectController.php +++ b/apps/dav/lib/Controller/DirectController.php @@ -27,9 +27,11 @@ namespace OCA\DAV\Controller; use OCA\DAV\Db\Direct; use OCA\DAV\Db\DirectMapper; use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\OCS\OCSBadRequestException; use OCP\AppFramework\OCS\OCSNotFoundException; use OCP\AppFramework\OCSController; use OCP\AppFramework\Utility\ITimeFactory; +use OCP\Files\File; use OCP\Files\IRootFolder; use OCP\IRequest; use OCP\IURLGenerator; @@ -87,25 +89,22 @@ class DirectController extends OCSController { } $file = array_shift($files); - $storage = $file->getStorage(); - $directDownload = $storage->getDirectDownload($file->getInternalPath()); + if (!($file instanceof File)) { + throw new OCSBadRequestException('Direct download only works for files'); + } - if (isset($directDownload['url'])) { - $url = $directDownload['url']; - } else { - // Fallback to our default implemenation - $direct = new Direct(); - $direct->setUserId($this->userId); - $direct->setFileId($fileId); + //TODO: at some point we should use the directdownlaod function of storages + $direct = new Direct(); + $direct->setUserId($this->userId); + $direct->setFileId($fileId); - $token = $this->random->generate(60, ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS); - $direct->setToken($token); - $direct->setExpiration($this->timeFactory->getTime() + 60 * 60 * 8); + $token = $this->random->generate(60, ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS); + $direct->setToken($token); + $direct->setExpiration($this->timeFactory->getTime() + 60 * 60 * 8); - $this->mapper->insert($direct); + $this->mapper->insert($direct); - $url = $this->urlGenerator->getAbsoluteURL('remote.php/direct/'.$token); - } + $url = $this->urlGenerator->getAbsoluteURL('remote.php/direct/'.$token); return new DataResponse([ 'url' => $url, diff --git a/apps/dav/tests/unit/Controller/DirectControllerTest.php b/apps/dav/tests/unit/Controller/DirectControllerTest.php new file mode 100644 index 00000000000..e52c67ac30c --- /dev/null +++ b/apps/dav/tests/unit/Controller/DirectControllerTest.php @@ -0,0 +1,155 @@ + + * + * @author Roeland Jago Douma + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\DAV\Tests\Unit\DAV\Controller; + +use OCA\DAV\Controller\DirectController; +use OCA\DAV\Db\Direct; +use OCA\DAV\Db\DirectMapper; +use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\OCS\OCSBadRequestException; +use OCP\AppFramework\OCS\OCSNotFoundException; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\Files\File; +use OCP\Files\Folder; +use OCP\Files\IRootFolder; +use OCP\IRequest; +use OCP\IURLGenerator; +use OCP\Security\ISecureRandom; +use Test\TestCase; + +class DirectControllerTest extends TestCase { + + /** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */ + private $rootFolder; + + /** @var DirectMapper|\PHPUnit_Framework_MockObject_MockObject */ + private $directMapper; + + /** @var ISecureRandom|\PHPUnit_Framework_MockObject_MockObject */ + private $random; + + /** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */ + private $timeFactory; + + /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */ + private $urlGenerator; + + /** @var DirectController */ + private $controller; + + public function setUp() { + parent::setUp(); + + $this->rootFolder = $this->createMock(IRootFolder::class); + $this->directMapper = $this->createMock(DirectMapper::class); + $this->random = $this->createMock(ISecureRandom::class); + $this->timeFactory = $this->createMock(ITimeFactory::class); + $this->urlGenerator = $this->createMock(IURLGenerator::class); + + $this->controller = new DirectController( + 'dav', + $this->createMock(IRequest::class), + $this->rootFolder, + 'awesomeUser', + $this->directMapper, + $this->random, + $this->timeFactory, + $this->urlGenerator + ); + } + + public function testGetUrlNonExistingFileId() { + $userFolder = $this->createMock(Folder::class); + $this->rootFolder->method('getUserFolder') + ->with('awesomeUser') + ->willReturn($userFolder); + + $userFolder->method('getById') + ->with(101) + ->willReturn([]); + + $this->expectException(OCSNotFoundException::class); + $this->controller->getUrl(101); + } + + public function testGetUrlForFolder() { + $userFolder = $this->createMock(Folder::class); + $this->rootFolder->method('getUserFolder') + ->with('awesomeUser') + ->willReturn($userFolder); + + $folder = $this->createMock(Folder::class); + + $userFolder->method('getById') + ->with(101) + ->willReturn([$folder]); + + $this->expectException(OCSBadRequestException::class); + $this->controller->getUrl(101); + } + + public function testGetUrlValid() { + $userFolder = $this->createMock(Folder::class); + $this->rootFolder->method('getUserFolder') + ->with('awesomeUser') + ->willReturn($userFolder); + + $file = $this->createMock(File::class); + + $this->timeFactory->method('getTime') + ->willReturn(42); + + $userFolder->method('getById') + ->with(101) + ->willReturn([$file]); + + $this->random->method('generate') + ->with( + 60, + ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS + )->willReturn('superduperlongtoken'); + + $this->directMapper->expects($this->once()) + ->method('insert') + ->willReturnCallback(function (Direct $direct) { + $this->assertSame('awesomeUser', $direct->getUserId()); + $this->assertSame(101, $direct->getFileId()); + $this->assertSame('superduperlongtoken', $direct->getToken()); + $this->assertSame(42 + 60*60*8, $direct->getExpiration()); + }); + + $this->urlGenerator->method('getAbsoluteURL') + ->willReturnCallback(function(string $url) { + return 'https://my.nextcloud/'.$url; + }); + + $result = $this->controller->getUrl(101); + + $this->assertInstanceOf(DataResponse::class, $result); + $this->assertSame([ + 'url' => 'https://my.nextcloud/remote.php/direct/superduperlongtoken', + ], $result->getData()); + } +} -- cgit v1.2.3