2019-10-14 17:55:39 +03:00
|
|
|
<?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
|
2019-12-03 21:57:53 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-14 17:55:39 +03:00
|
|
|
* 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\Files\Controller;
|
|
|
|
|
|
|
|
use Exception;
|
2019-11-27 16:36:07 +03:00
|
|
|
use OCA\Files\Service\DirectEditingService;
|
2019-10-14 17:55:39 +03:00
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
|
|
|
use OCP\AppFramework\OCSController;
|
|
|
|
use OCP\DirectEditing\IManager;
|
|
|
|
use OCP\DirectEditing\RegisterDirectEditorEvent;
|
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
|
|
|
use OCP\ILogger;
|
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\IURLGenerator;
|
|
|
|
|
|
|
|
class DirectEditingController extends OCSController {
|
|
|
|
|
|
|
|
/** @var IEventDispatcher */
|
|
|
|
private $eventDispatcher;
|
|
|
|
|
|
|
|
/** @var IManager */
|
|
|
|
private $directEditingManager;
|
|
|
|
|
|
|
|
/** @var IURLGenerator */
|
|
|
|
private $urlGenerator;
|
|
|
|
|
|
|
|
/** @var ILogger */
|
|
|
|
private $logger;
|
|
|
|
|
2019-11-27 16:36:07 +03:00
|
|
|
/** @var DirectEditingService */
|
|
|
|
private $directEditingService;
|
|
|
|
|
2019-10-14 17:55:39 +03:00
|
|
|
public function __construct($appName, IRequest $request, $corsMethods, $corsAllowedHeaders, $corsMaxAge,
|
2019-11-27 16:36:07 +03:00
|
|
|
IEventDispatcher $eventDispatcher, IURLGenerator $urlGenerator, IManager $manager, DirectEditingService $directEditingService, ILogger $logger) {
|
2019-10-14 17:55:39 +03:00
|
|
|
parent::__construct($appName, $request, $corsMethods, $corsAllowedHeaders, $corsMaxAge);
|
|
|
|
|
|
|
|
$this->eventDispatcher = $eventDispatcher;
|
|
|
|
$this->directEditingManager = $manager;
|
2019-11-27 16:36:07 +03:00
|
|
|
$this->directEditingService = $directEditingService;
|
2019-10-14 17:55:39 +03:00
|
|
|
$this->logger = $logger;
|
|
|
|
$this->urlGenerator = $urlGenerator;
|
|
|
|
}
|
|
|
|
|
2019-11-27 16:36:07 +03:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*/
|
|
|
|
public function info(): DataResponse {
|
|
|
|
$response = new DataResponse($this->directEditingService->getDirectEditingCapabilitites());
|
|
|
|
$response->setETag($this->directEditingService->getDirectEditingETag());
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2019-10-14 17:55:39 +03:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*/
|
|
|
|
public function create(string $path, string $editorId, string $creatorId, string $templateId = null): DataResponse {
|
2019-11-25 16:09:38 +03:00
|
|
|
$this->eventDispatcher->dispatchTyped(new RegisterDirectEditorEvent($this->directEditingManager));
|
2019-10-14 17:55:39 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
$token = $this->directEditingManager->create($path, $editorId, $creatorId, $templateId);
|
|
|
|
return new DataResponse([
|
|
|
|
'url' => $this->urlGenerator->linkToRouteAbsolute('files.DirectEditingView.edit', ['token' => $token])
|
|
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->logger->logException($e, ['message' => 'Exception when creating a new file through direct editing']);
|
2019-12-20 13:09:05 +03:00
|
|
|
return new DataResponse('Failed to create file: ' . $e->getMessage(), Http::STATUS_FORBIDDEN);
|
2019-10-14 17:55:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*/
|
2019-12-04 14:09:33 +03:00
|
|
|
public function open(string $path, string $editorId = null): DataResponse {
|
2019-11-25 16:09:38 +03:00
|
|
|
$this->eventDispatcher->dispatchTyped(new RegisterDirectEditorEvent($this->directEditingManager));
|
2019-10-14 17:55:39 +03:00
|
|
|
|
|
|
|
try {
|
2019-12-04 14:09:33 +03:00
|
|
|
$token = $this->directEditingManager->open($path, $editorId);
|
2019-10-14 17:55:39 +03:00
|
|
|
return new DataResponse([
|
|
|
|
'url' => $this->urlGenerator->linkToRouteAbsolute('files.DirectEditingView.edit', ['token' => $token])
|
|
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->logger->logException($e, ['message' => 'Exception when opening a file through direct editing']);
|
2019-12-20 13:09:05 +03:00
|
|
|
return new DataResponse('Failed to open file: ' . $e->getMessage(), Http::STATUS_FORBIDDEN);
|
2019-10-14 17:55:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*/
|
|
|
|
public function templates(string $editorId, string $creatorId): DataResponse {
|
2019-11-25 16:09:38 +03:00
|
|
|
$this->eventDispatcher->dispatchTyped(new RegisterDirectEditorEvent($this->directEditingManager));
|
2019-10-14 17:55:39 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
return new DataResponse($this->directEditingManager->getTemplates($editorId, $creatorId));
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->logger->logException($e);
|
2019-12-20 13:09:05 +03:00
|
|
|
return new DataResponse('Failed to obtain template list: ' . $e->getMessage(), Http::STATUS_INTERNAL_SERVER_ERROR);
|
2019-10-14 17:55:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|