serverContainer = $serverContainer; $this->eventDispatcher = $eventDispatcher; $this->bootstrapCoordinator = $coordinator; $this->rootFolder = $rootFolder; $this->userManager = $userManager; $this->previewManager = $previewManager; $this->config = $config; $this->l10nFactory = $l10nFactory; $this->l10n = $l10nFactory->get('lib'); $this->logger = $logger; $user = $userSession->getUser(); $this->userId = $user ? $user->getUID() : null; } public function registerTemplateFileCreator(callable $callback): void { $this->registeredTypes[] = $callback; } public function getRegisteredProviders(): array { if ($this->providers !== null) { return $this->providers; } $context = $this->bootstrapCoordinator->getRegistrationContext(); $this->providers = []; foreach ($context->getTemplateProviders() as $provider) { $class = $provider->getService(); $this->providers[$class] = $this->serverContainer->get($class); } return $this->providers; } public function getTypes(): array { if (!empty($this->types)) { return $this->types; } $this->eventDispatcher->dispatchTyped(new RegisterTemplateCreatorEvent($this)); foreach ($this->registeredTypes as $registeredType) { $this->types[] = $registeredType(); } return $this->types; } public function listCreators(): array { $types = $this->getTypes(); usort($types, function (TemplateFileCreator $a, TemplateFileCreator $b) { return $a->getOrder() - $b->getOrder(); }); return $types; } public function listTemplates(): array { return array_values(array_map(function (TemplateFileCreator $entry) { return array_merge($entry->jsonSerialize(), [ 'templates' => $this->getTemplateFiles($entry) ]); }, $this->listCreators())); } /** * @param string $filePath * @param string $templateId * @param array $templateFields * @return array * @throws GenericFileException */ public function createFromTemplate(string $filePath, string $templateId = '', string $templateType = 'user', array $templateFields = []): array { $userFolder = $this->rootFolder->getUserFolder($this->userId); try { $userFolder->get($filePath); throw new GenericFileException($this->l10n->t('File already exists')); } catch (NotFoundException $e) { } try { if (!$userFolder->nodeExists(dirname($filePath))) { throw new GenericFileException($this->l10n->t('Invalid path')); } $folder = $userFolder->get(dirname($filePath)); $template = null; if ($templateType === 'user' && $templateId !== '') { $template = $userFolder->get($templateId); } else { $matchingProvider = array_filter($this->getRegisteredProviders(), function (ICustomTemplateProvider $provider) use ($templateType) { return $templateType === get_class($provider); }); $provider = array_shift($matchingProvider); if ($provider) { $template = $provider->getCustomTemplate($templateId); } } $targetFile = $folder->newFile(basename($filePath), ($template instanceof File ? $template->fopen('rb') : null)); $this->eventDispatcher->dispatchTyped(new FileCreatedFromTemplateEvent($template, $targetFile, $templateFields)); return $this->formatFile($userFolder->get($filePath)); } catch (\Exception $e) { $this->logger->error($e->getMessage(), ['exception' => $e]); throw new GenericFileException($this->l10n->t('Failed to create file from template')); } } /** * @return Folder * @throws \OCP\Files\NotFoundException * @throws \OCP\Files\NotPermittedException * @throws \OC\User\NoUserException */ private function getTemplateFolder(): Folder { if ($this->getTemplatePath() !== '') { $path = $this->rootFolder->getUserFolder($this->userId)->get($this->getTemplatePath()); if ($path instanceof Folder) { return $path; } $this->logger->warning('Template folder ' . $path . ' not found or invalid', ['app' => 'files_templates']); } throw new NotFoundException(); } /** * @return list