shareManager->getShareByToken($shareToken); if ($share->getPassword()) { $authenticatedLinks = $this->session->get('public_link_authenticated'); $isAuthenticated = (is_array($authenticatedLinks) && in_array($share->getId(), $authenticatedLinks)); $isAuthenticated = $isAuthenticated || ($authenticatedLinks === (string)$share->getId()); if (!$isAuthenticated) { throw new Exception('Invalid password'); } } if (!($share->getPermissions() & \OCP\Constants::PERMISSION_CREATE)) { throw new Exception('No create permissions'); } } $rootFolder = $shareToken !== null ? $share->getNode() : $this->rootFolder->getUserFolder($this->userId); $folder = $rootFolder->get($directoryPath); if (!($folder instanceof Folder)) { throw new Exception('Node is not a folder'); } } catch (Throwable $e) { $this->logger->error('Failed to create document', ['exception' => $e]); $response = new JSONResponse([ 'status' => 'error', 'message' => $this->l10n->t('Cannot create document') ], Http::STATUS_BAD_REQUEST); $response->throttle(); return $response; } $basename = $this->l10n->t('New Document.odt'); switch ($mimeType) { case 'application/vnd.oasis.opendocument.spreadsheet': $basename = $this->l10n->t('New Spreadsheet.ods'); break; case 'application/vnd.oasis.opendocument.presentation': $basename = $this->l10n->t('New Presentation.odp'); break; case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': $basename = $this->l10n->t('New Document.docx'); break; case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': $basename = $this->l10n->t('New Spreadsheet.xlsx'); break; case 'application/vnd.openxmlformats-officedocument.presentationml.presentation': $basename = $this->l10n->t('New Presentation.pptx'); break; default: break; } if (!$fileName) { $fileName = Helper::getNewFileName($folder, $basename); } if ($folder->nodeExists($fileName)) { return new JSONResponse([ 'status' => 'error', 'message' => $this->l10n->t('File already exists') ], Http::STATUS_BAD_REQUEST); } try { $file = $folder->newFile($fileName); $templateType = $this->templateManager->getTemplateTypeForExtension(pathinfo($fileName, PATHINFO_EXTENSION)); $empty = $this->templateManager->getEmpty($templateType); $templateFile = array_shift($empty); if ($templateId) { $templateFile = $this->templateManager->get($templateId); } $file->putContent($this->templateManager->getEmptyFileContent($file->getExtension())); if ($this->templateManager->isSupportedTemplateSource($templateFile->getExtension())) { // Only use TemplateSource if supported filetype $this->templateManager->setTemplateSource($file->getId(), $templateFile->getId()); } } catch (Exception $e) { $this->logger->error('Failed to create file from template', ['exception' => $e]); return new JSONResponse([ 'status' => 'error', 'message' => $this->l10n->t('Not allowed to create document') ], Http::STATUS_BAD_REQUEST); } return new JSONResponse([ 'status' => 'success', 'data' => \OCA\Files\Helper::formatFileInfo($file->getFileInfo()) ]); } #[NoAdminRequired] public function openLocal(int $fileId): DataResponse { try { $file = $this->rootFolder->getUserFolder($this->userId)->getFirstNodeById($fileId); if ($file === null) { return new DataResponse([], Http::STATUS_NOT_FOUND); } $this->lockManager->unlock(new LockContext( $file, ILock::TYPE_APP, Application::APPNAME )); return new DataResponse([]); } catch (NoLockProviderException|PreConditionNotMetException) { return new DataResponse([], Http::STATUS_BAD_REQUEST); } catch (\Exception) { return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); } } }