diff options
author | Roeland Jago Douma <roeland@famdouma.nl> | 2018-07-10 14:32:16 +0300 |
---|---|---|
committer | Roeland Jago Douma <roeland@famdouma.nl> | 2018-07-20 21:24:33 +0300 |
commit | 21059397a906a6032cd6551034f06f0e9ef09ac4 (patch) | |
tree | 8360195ec1786c95dbb834b4ec498b9a766e87d9 /lib | |
parent | 4d412b35d59b0066be7852e59a681f1510ddbbac (diff) |
Initial mobile edit
* Add OCS endpoint for client to request a view
* Endpoint returns an URL
* DirectView controller to show info
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Controller/DirectViewController.php | 114 | ||||
-rw-r--r-- | lib/Controller/OCSController.php | 94 | ||||
-rw-r--r-- | lib/Db/Direct.php | 51 | ||||
-rw-r--r-- | lib/Db/DirectMapper.php | 79 |
4 files changed, 338 insertions, 0 deletions
diff --git a/lib/Controller/DirectViewController.php b/lib/Controller/DirectViewController.php new file mode 100644 index 00000000..d30828e4 --- /dev/null +++ b/lib/Controller/DirectViewController.php @@ -0,0 +1,114 @@ +<?php +/** + * @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl> + * + * @author Roeland Jago Douma <roeland@famdouma.nl> + * + * @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 <http://www.gnu.org/licenses/>. + * + */ +namespace OCA\Richdocuments\Controller; + +use OCA\Richdocuments\AppConfig; +use OCA\Richdocuments\Db\DirectMapper; +use OCA\Richdocuments\TokenManager; +use OCP\AppFramework\Controller; +use OCP\AppFramework\Db\DoesNotExistException; +use OCP\AppFramework\Http\ContentSecurityPolicy; +use OCP\AppFramework\Http\TemplateResponse; +use OCP\Files\IRootFolder; +use OCP\Files\Node; +use OCP\IConfig; +use OCP\IRequest; + +class DirectViewController extends Controller { + /** @var IRootFolder */ + private $rootFolder; + + /** @var TokenManager */ + private $tokenManager; + + /** @var DirectMapper */ + private $directMapper; + + /** @var IConfig */ + private $config; + + /** @var AppConfig */ + private $appConfig; + + public function __construct($appName, + IRequest $request, + IRootFolder $rootFolder, + TokenManager $tokenManager, + DirectMapper $directMapper, + IConfig $config, + AppConfig $appConfig) { + parent::__construct($appName, $request); + + $this->rootFolder = $rootFolder; + $this->tokenManager = $tokenManager; + $this->directMapper = $directMapper; + $this->config = $config; + $this->appConfig = $appConfig; + } + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @PublicPage + * + * @param string $token + */ + public function show($token) { + try { + $direct = $this->directMapper->getBytoken($token); + } catch (DoesNotExistException $e) { + //TODO show 404 + throw new \Exception('NOPE!'); + } + + try { + $folder = $this->rootFolder->getUserFolder($direct->getUid()); + $item = $folder->getById($direct->getFileid())[0]; + if(!($item instanceof Node)) { + throw new \Exception(); + } + list($urlSrc, $token) = $this->tokenManager->getToken($item->getId()); + $params = [ + 'permissions' => $item->getPermissions(), + 'title' => $item->getName(), + 'fileId' => $item->getId() . '_' . $this->config->getSystemValue('instanceid'), + 'token' => $token, + 'urlsrc' => $urlSrc, + 'path' => $folder->getRelativePath($item->getPath()), + 'instanceId' => $this->config->getSystemValue('instanceid'), + 'canonical_webroot' => $this->appConfig->getAppValue('canonical_webroot'), + 'direct' => true, + ]; + + $response = new TemplateResponse('richdocuments', 'documents', $params, 'empty'); + $policy = new ContentSecurityPolicy(); + $policy->allowInlineScript(true); + $policy->addAllowedFrameDomain($this->appConfig->getAppValue('wopi_url')); + $response->setContentSecurityPolicy($policy); + return $response; + } catch (\Exception $e) { + throw $e; + } + + } +} diff --git a/lib/Controller/OCSController.php b/lib/Controller/OCSController.php new file mode 100644 index 00000000..14651409 --- /dev/null +++ b/lib/Controller/OCSController.php @@ -0,0 +1,94 @@ +<?php +/** + * @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl> + * + * @author Roeland Jago Douma <roeland@famdouma.nl> + * + * @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 <http://www.gnu.org/licenses/>. + * + */ +namespace OCA\Richdocuments\Controller; + +use OCA\Richdocuments\Db\DirectMapper; +use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\OCS\OCSBadRequestException; +use OCP\AppFramework\OCS\OCSNotFoundException; +use OCP\Files\Folder; +use OCP\Files\IRootFolder; +use OCP\Files\NotFoundException; +use OCP\IRequest; +use OCP\IURLGenerator; + +class OCSController extends \OCP\AppFramework\OCSController { + /** @var IRootFolder */ + private $rootFolder; + + /** @var string */ + private $userId; + + /** @var DirectMapper */ + private $directMapper; + + /** @var IURLGenerator */ + private $urlGenerator; + + public function __construct($appName, + IRequest $request, + IRootFolder $rootFolder, + $userId, + DirectMapper $directMapper, + IURLGenerator $urlGenerator) { + parent::__construct($appName, $request); + + $this->rootFolder = $rootFolder; + $this->userId = $userId; + $this->directMapper = $directMapper; + $this->urlGenerator = $urlGenerator; + } + + /** + * @NoAdminRequired + * + * @param int $fileId + */ + public function create($fileId) { + try { + $userFolder = $this->rootFolder->getUserFolder($this->userId); + $nodes = $userFolder->getById($fileId); + + if ($nodes === []) { + throw new NotFoundException(); + } + + $node = $nodes[0]; + if ($node instanceof Folder) { + throw new OCSBadRequestException('Cannot view folder'); + } + + //TODO check if we can even edit this file with collabora + + $direct = $this->directMapper->newDirect($this->userId, $fileId); + + return new DataResponse([ + 'url' => $this->urlGenerator->linkToRouteAbsolute('richdocuments.directView.show', [ + 'token' => $direct->getToken() + ]) + ]); + } catch (NotFoundException $e) { + throw new OCSNotFoundException(); + } + } +} diff --git a/lib/Db/Direct.php b/lib/Db/Direct.php new file mode 100644 index 00000000..b9987d7a --- /dev/null +++ b/lib/Db/Direct.php @@ -0,0 +1,51 @@ +<?php +/** + * @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl> + * + * @author Roeland Jago Douma <roeland@famdouma.nl> + * + * @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 <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\Richdocuments\Db; + +use OCP\AppFramework\Db\Entity; + +/** + * @method void setToken(string $token) + * @method string getToken() + * @method void setUid(string $uid) + * @method string getUid() + * @method void setFileid(int $fileid) + * @method int getFileid() + */ +class Direct extends Entity { + /** @var string */ + protected $token; + + /** @var string */ + protected $uid; + + /** @var int */ + protected $fileid; + + public function __construct() { + $this->addType('token', 'string'); + $this->addType('uid', 'string'); + $this->addType('fileid', 'int'); + } +} diff --git a/lib/Db/DirectMapper.php b/lib/Db/DirectMapper.php new file mode 100644 index 00000000..100f1332 --- /dev/null +++ b/lib/Db/DirectMapper.php @@ -0,0 +1,79 @@ +<?php +/** + * @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl> + * + * @author Roeland Jago Douma <roeland@famdouma.nl> + * + * @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 <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\Richdocuments\Db; + +use OCP\AppFramework\Db\DoesNotExistException; +use OCP\AppFramework\Db\Mapper; +use OCP\IDBConnection; +use OCP\Security\ISecureRandom; +use PhpParser\Node\Scalar\MagicConst\Dir; + +class DirectMapper extends Mapper { + + /** @var ISecureRandom */ + protected $random; + + public function __construct(IDBConnection $db, ISecureRandom $random) { + parent::__construct($db, 'richdocuments_direct', Direct::class); + + $this->random = $random; + } + + /** + * @param string $uid + * @param int $fileid + * @return Direct + */ + public function newDirect($uid, $fileid) { + $direct = new Direct(); + $direct->setUid($uid); + $direct->setFileid($fileid); + $direct->setToken($this->random->generate(64, ISecureRandom::CHAR_DIGITS . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER)); + + $direct = $this->insert($direct); + return $direct; + } + + /** + * @param string $token + * @return Direct + */ + public function getBytoken($token) { + $qb = $this->db->getQueryBuilder(); + $qb->select('*') + ->from('richdocuments_direct') + ->where($qb->expr()->eq('token', $qb->createNamedParameter($token))); + + $cursor = $qb->execute(); + $row = $cursor->fetch(); + $cursor->closeCursor(); + + //There can only be one as the token is unique + if ($row === false) { + throw new DoesNotExistException('Could not find token.'); + } + + return Direct::fromRow($row); + } +} |