| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @copyright Copyright (c) 2019 Julius Härtl <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @author Julius Härtl <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @license GNU AGPL version 3 or any later version | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * This program is free software: you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * it under the terms of the GNU Affero General Public License as | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * published by the Free Software Foundation, either version 3 of the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * License, or (at your option) any later version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * This program is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * GNU Affero General Public License for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * You should have received a copy of the GNU Affero General Public License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * along with this program. If not, see <http://www.gnu.org/licenses/>. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | namespace OCA\Files\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | use OCA\Files\Service\DirectEditingService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use OCP\AppFramework\Http; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | use OCP\AppFramework\Http\DataResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | use OCP\AppFramework\OCSController; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | use OCP\DirectEditing\ACreateEmpty; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | use OCP\DirectEditing\ACreateFromTemplate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | use OCP\DirectEditing\IEditor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | use OCP\DirectEditing\IManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | use OCP\DirectEditing\RegisterDirectEditorEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | use OCP\EventDispatcher\IEventDispatcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | use OCP\ILogger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | use OCP\IRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | use OCP\IURLGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | class DirectEditingController extends OCSController { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	/** @var IEventDispatcher */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	private $eventDispatcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	/** @var IManager */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	private $directEditingManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	/** @var IURLGenerator */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	private $urlGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	/** @var ILogger */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	private $logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	/** @var DirectEditingService */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	private $directEditingService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	public function __construct($appName, IRequest $request, $corsMethods, $corsAllowedHeaders, $corsMaxAge, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 								IEventDispatcher $eventDispatcher, IURLGenerator $urlGenerator, IManager $manager, DirectEditingService $directEditingService, ILogger $logger) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		parent::__construct($appName, $request, $corsMethods, $corsAllowedHeaders, $corsMaxAge); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		$this->eventDispatcher = $eventDispatcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 		$this->directEditingManager = $manager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		$this->directEditingService = $directEditingService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		$this->logger = $logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		$this->urlGenerator = $urlGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	 * @NoAdminRequired | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	public function info(): DataResponse { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 		$response = new DataResponse($this->directEditingService->getDirectEditingCapabilitites()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 		$response->setETag($this->directEditingService->getDirectEditingETag()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 		return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	 * @NoAdminRequired | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 81 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  | 	public function create(string $path, string $editorId, string $creatorId, string $templateId = null): DataResponse { | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  | 		$this->eventDispatcher->dispatchTyped(new RegisterDirectEditorEvent($this->directEditingManager)); | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  | 		try { | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  | 			$token = $this->directEditingManager->create($path, $editorId, $creatorId, $templateId); | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  | 			return new DataResponse([ | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  | 				'url' => $this->urlGenerator->linkToRouteAbsolute('files.DirectEditingView.edit', ['token' => $token]) | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  | 			]); | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  | 		} catch (Exception $e) { | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  | 			$this->logger->logException($e, ['message' => 'Exception when creating a new file through direct editing']); | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  | 			return new DataResponse('Failed to create file', Http::STATUS_FORBIDDEN); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	 * @NoAdminRequired | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 	public function open(int $fileId, string $editorId = null): DataResponse { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 		$this->eventDispatcher->dispatchTyped(new RegisterDirectEditorEvent($this->directEditingManager)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 		try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 			$token = $this->directEditingManager->open($fileId, $editorId); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 			return new DataResponse([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 				'url' => $this->urlGenerator->linkToRouteAbsolute('files.DirectEditingView.edit', ['token' => $token]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 			]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 		} catch (Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 			$this->logger->logException($e, ['message' => 'Exception when opening a file through direct editing']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 			return new DataResponse('Failed to open file', Http::STATUS_FORBIDDEN); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 	 * @NoAdminRequired | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	public function templates(string $editorId, string $creatorId): DataResponse { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 		$this->eventDispatcher->dispatchTyped(new RegisterDirectEditorEvent($this->directEditingManager)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 		try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 			return new DataResponse($this->directEditingManager->getTemplates($editorId, $creatorId)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 		} catch (Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 			$this->logger->logException($e); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 			return new DataResponse('Failed to open file', Http::STATUS_INTERNAL_SERVER_ERROR); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 128 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 129 |  |  |  |