templateManager->listTemplates(); $templates = []; $libs = []; foreach ($availableFileCreators as $fileCreator) { if ($fileCreator['app'] !== Application::APP_ID) { continue; } $templates = $fileCreator['templates']; break; } foreach ($templates as $template) { $templateDetails = $template->jsonSerialize(); if (str_ends_with($templateDetails['basename'], '.excalidrawlib')) { $fileId = $templateDetails['fileid']; $file = $this->rootFolder->getFirstNodeById($fileId); if (!$file instanceof File) { continue; } $lib = json_decode($file->getContent(), true, 512, JSON_THROW_ON_ERROR); $lib['basename'] = $templateDetails['basename']; $libs[] = $lib; } } return $libs; } /** * @throws NotPermittedException * @throws NotFoundException * @throws GenericFileException * @throws LockedException * @throws JsonException */ public function updateUserLib(string $uid, array $items): void { // Check if the user has a Templates folder, if not create one if (!$this->templateManager->hasTemplateDirectory()) { $this->templateManager->initializeTemplateDirectory(null, $uid, false); } // Update the .excalidrawlib files in the Templates directory $userFolder = $this->rootFolder->getUserFolder($uid); $templatesPath = $this->templateManager->getTemplatePath(); $templatesFolder = $userFolder->get($templatesPath); if (!$templatesFolder instanceof Folder) { throw new NotFoundException('Templates folder not found for user: ' . $uid); } $files = [ 'personal.excalidrawlib' => [ 'type' => 'excalidrawlib', 'version' => 2, 'libraryItems' => [], ], ]; foreach ($items as $item) { if (!isset($item['filename'])) { $files['personal.excalidrawlib']['libraryItems'][] = $item; } else { if (isset($files[$item['filename']])) { $files[$item['filename']]['libraryItems'][] = $item; } else { $files[$item['filename']] = [ 'type' => 'excalidrawlib', 'version' => 2, 'libraryItems' => [$item], ]; } } } foreach ($files as $filename => $fileData) { if ($templatesFolder->nodeExists($filename)) { $file = $templatesFolder->get($filename); } else { $file = $templatesFolder->newFile($filename); } if (!$file instanceof File) { throw new GenericFileException('Failed to create or get file: ' . $filename); } $file->putContent(json_encode($fileData, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT)); } } }