Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/text.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJulius Härtl <jus@bitgrid.net>2019-10-25 12:32:49 +0300
committerJulius Härtl <jus@bitgrid.net>2019-11-04 21:35:11 +0300
commit45490a60c0b043f0a871c587a0447de7d500c1ce (patch)
tree4d2b7b8fd3655b819a315ad049a41b49cabc3801 /lib
parentb1ba3db46e0ff78fc2e4011d859b1513b8e45a3f (diff)
Add workspace route and public page handling
Signed-off-by: Julius Härtl <jus@bitgrid.net>
Diffstat (limited to 'lib')
-rw-r--r--lib/Controller/WorkspaceController.php157
1 files changed, 157 insertions, 0 deletions
diff --git a/lib/Controller/WorkspaceController.php b/lib/Controller/WorkspaceController.php
new file mode 100644
index 000000000..47f7cb6ea
--- /dev/null
+++ b/lib/Controller/WorkspaceController.php
@@ -0,0 +1,157 @@
+<?php
+/**
+ * @copyright Copyright (c) 2019 Julius Härtl <jus@bitgrid.net>
+ *
+ * @author Julius Härtl <jus@bitgrid.net>
+ *
+ * @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/>.
+ *
+ */
+
+declare(strict_types=1);
+/**
+ * @copyright Copyright (c) 2019 Julius Härtl <jus@bitgrid.net>
+ *
+ * @author Julius Härtl <jus@bitgrid.net>
+ *
+ * @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\Text\Controller;
+
+use OCA\Text\AppInfo\Application;
+use OCA\Text\Service\SessionService;
+use OCP\AppFramework\Controller;
+use OCP\AppFramework\Http;
+use OCP\AppFramework\Http\DataResponse;
+use OCP\AppFramework\Http\TemplateResponse;
+use OCP\AppFramework\OCSController;
+use OCP\Files\Folder;
+use OCP\Files\IRootFolder;
+use OCP\Files\NotFoundException;
+use OCP\IRequest;
+use OCP\Share\Exceptions\ShareNotFound;
+use OCP\Share\IManager;
+
+class WorkspaceController extends OCSController {
+
+ /** @var IRootFolder */
+ private $rootFolder;
+
+ /** @var IManager */
+ private $shareManager;
+
+ private const SUPPORTED_FILENAMES = [
+ 'README.md',
+ 'Readme.md',
+ 'readme.md'
+ ];
+
+
+ public function __construct($appName, IRequest $request, IRootFolder $rootFolder, IManager $shareManager, $userId) {
+ parent::__construct($appName, $request);
+ $this->rootFolder = $rootFolder;
+ $this->shareManager = $shareManager;
+ $this->userId = $userId;
+ }
+
+ /**
+ * @NoAdminRequired
+ *
+ * Checks for available files in the current folder and returns required details to present
+ * the rich workspace
+ */
+ public function folder(string $path = '/'): DataResponse {
+ try {
+ $folder = $this->rootFolder->getUserFolder($this->userId)->get($path);
+ if ($folder instanceof Folder) {
+ $file = $this->getFile($folder);
+ if ($file === null) {
+ return new DataResponse(['message' => 'No workspace file found'], Http::STATUS_NOT_FOUND);
+ }
+
+ return new DataResponse([
+ 'file' => [
+ 'id' => $file->getId(),
+ 'mimetype' => $file->getMimetype(),
+ 'name' => $file->getName()
+ ]
+ ]);
+ }
+ } catch (NotFoundException $e) {
+ return new DataResponse(['message' => 'No valid folder found'], Http::STATUS_BAD_REQUEST);
+ }
+ }
+
+ /**
+ * @NoAdminRequired
+ * @PublicPage
+ *
+ * Checks for available files in the current folder and returns required details to present
+ * the rich workspace
+ */
+ public function publicFolder(string $shareToken, string $path = '/'): DataResponse {
+ try {
+ $share = $this->shareManager->getShareByToken($shareToken);
+ $folder = $share->getNode()->get($path);
+ if ($folder instanceof Folder) {
+ $file = $this->getFile($folder);
+ if ($file === null) {
+ return new DataResponse(['message' => 'No workspace file found'], Http::STATUS_NOT_FOUND);
+ }
+
+ return new DataResponse([
+ 'file' => [
+ 'id' => $file->getId(),
+ 'mimetype' => $file->getMimetype(),
+ 'name' => $file->getName(),
+ 'path' => $path . '/' . $file->getName()
+ ]
+ ]);
+ }
+ } catch (NotFoundException $e) {
+ return new DataResponse(['message' => 'No valid folder found'], Http::STATUS_BAD_REQUEST);
+ } catch (ShareNotFound $e) {
+ return new DataResponse(['message' => 'No valid folder found'], Http::STATUS_BAD_REQUEST);
+ }
+ }
+
+ private function getFile(Folder $folder) {
+ $file = null;
+ foreach (self::SUPPORTED_FILENAMES as $filename) {
+ if ($folder->nodeExists($filename)) {
+ $file = $folder->get($filename);
+ continue;
+ }
+ }
+ return $file;
+ }
+
+}