@@ -51,311 +51,311 @@ |
||
51 | 51 | use Psr\Log\LoggerInterface; |
52 | 52 | |
53 | 53 | class TemplateManager implements ITemplateManager { |
54 | - private $registeredTypes = []; |
|
55 | - private $types = []; |
|
56 | - |
|
57 | - /** @var array|null */ |
|
58 | - private $providers = null; |
|
59 | - |
|
60 | - private $serverContainer; |
|
61 | - private $eventDispatcher; |
|
62 | - private $rootFolder; |
|
63 | - private $userManager; |
|
64 | - private $previewManager; |
|
65 | - private $config; |
|
66 | - private $l10n; |
|
67 | - private $logger; |
|
68 | - private $userId; |
|
69 | - private $l10nFactory; |
|
70 | - /** @var Coordinator */ |
|
71 | - private $bootstrapCoordinator; |
|
72 | - |
|
73 | - public function __construct( |
|
74 | - IServerContainer $serverContainer, |
|
75 | - IEventDispatcher $eventDispatcher, |
|
76 | - Coordinator $coordinator, |
|
77 | - IRootFolder $rootFolder, |
|
78 | - IUserSession $userSession, |
|
79 | - IUserManager $userManager, |
|
80 | - IPreview $previewManager, |
|
81 | - IConfig $config, |
|
82 | - IFactory $l10nFactory, |
|
83 | - LoggerInterface $logger |
|
84 | - ) { |
|
85 | - $this->serverContainer = $serverContainer; |
|
86 | - $this->eventDispatcher = $eventDispatcher; |
|
87 | - $this->bootstrapCoordinator = $coordinator; |
|
88 | - $this->rootFolder = $rootFolder; |
|
89 | - $this->userManager = $userManager; |
|
90 | - $this->previewManager = $previewManager; |
|
91 | - $this->config = $config; |
|
92 | - $this->l10nFactory = $l10nFactory; |
|
93 | - $this->l10n = $l10nFactory->get('lib'); |
|
94 | - $this->logger = $logger; |
|
95 | - $user = $userSession->getUser(); |
|
96 | - $this->userId = $user ? $user->getUID() : null; |
|
97 | - } |
|
98 | - |
|
99 | - public function registerTemplateFileCreator(callable $callback): void { |
|
100 | - $this->registeredTypes[] = $callback; |
|
101 | - } |
|
102 | - |
|
103 | - public function getRegisteredProviders(): array { |
|
104 | - if ($this->providers !== null) { |
|
105 | - return $this->providers; |
|
106 | - } |
|
107 | - |
|
108 | - $context = $this->bootstrapCoordinator->getRegistrationContext(); |
|
109 | - |
|
110 | - $this->providers = []; |
|
111 | - foreach ($context->getTemplateProviders() as $provider) { |
|
112 | - $class = $provider->getService(); |
|
113 | - $this->providers[$class] = $this->serverContainer->get($class); |
|
114 | - } |
|
115 | - return $this->providers; |
|
116 | - } |
|
117 | - |
|
118 | - public function getTypes(): array { |
|
119 | - if (!empty($this->types)) { |
|
120 | - return $this->types; |
|
121 | - } |
|
122 | - foreach ($this->registeredTypes as $registeredType) { |
|
123 | - $this->types[] = $registeredType(); |
|
124 | - } |
|
125 | - return $this->types; |
|
126 | - } |
|
127 | - |
|
128 | - public function listCreators(): array { |
|
129 | - $types = $this->getTypes(); |
|
130 | - usort($types, function (TemplateFileCreator $a, TemplateFileCreator $b) { |
|
131 | - return $a->getOrder() - $b->getOrder(); |
|
132 | - }); |
|
133 | - return $types; |
|
134 | - } |
|
135 | - |
|
136 | - public function listTemplates(): array { |
|
137 | - return array_map(function (TemplateFileCreator $entry) { |
|
138 | - return array_merge($entry->jsonSerialize(), [ |
|
139 | - 'templates' => $this->getTemplateFiles($entry) |
|
140 | - ]); |
|
141 | - }, $this->listCreators()); |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * @param string $filePath |
|
146 | - * @param string $templateId |
|
147 | - * @return array |
|
148 | - * @throws GenericFileException |
|
149 | - */ |
|
150 | - public function createFromTemplate(string $filePath, string $templateId = '', string $templateType = 'user'): array { |
|
151 | - $userFolder = $this->rootFolder->getUserFolder($this->userId); |
|
152 | - try { |
|
153 | - $userFolder->get($filePath); |
|
154 | - throw new GenericFileException($this->l10n->t('File already exists')); |
|
155 | - } catch (NotFoundException $e) { |
|
156 | - } |
|
157 | - try { |
|
158 | - if (!$userFolder->nodeExists(dirname($filePath))) { |
|
159 | - throw new GenericFileException($this->l10n->t('Invalid path')); |
|
160 | - } |
|
161 | - $folder = $userFolder->get(dirname($filePath)); |
|
162 | - $targetFile = $folder->newFile(basename($filePath)); |
|
163 | - $template = null; |
|
164 | - if ($templateType === 'user' && $templateId !== '') { |
|
165 | - $template = $userFolder->get($templateId); |
|
166 | - $template->copy($targetFile->getPath()); |
|
167 | - } else { |
|
168 | - $matchingProvider = array_filter($this->getRegisteredProviders(), function (ICustomTemplateProvider $provider) use ($templateType) { |
|
169 | - return $templateType === get_class($provider); |
|
170 | - }); |
|
171 | - $provider = array_shift($matchingProvider); |
|
172 | - if ($provider) { |
|
173 | - $template = $provider->getCustomTemplate($templateId); |
|
174 | - $template->copy($targetFile->getPath()); |
|
175 | - } |
|
176 | - } |
|
177 | - $this->eventDispatcher->dispatchTyped(new FileCreatedFromTemplateEvent($template, $targetFile)); |
|
178 | - return $this->formatFile($userFolder->get($filePath)); |
|
179 | - } catch (\Exception $e) { |
|
180 | - $this->logger->error($e->getMessage(), ['exception' => $e]); |
|
181 | - throw new GenericFileException($this->l10n->t('Failed to create file from template')); |
|
182 | - } |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * @return Folder |
|
187 | - * @throws \OCP\Files\NotFoundException |
|
188 | - * @throws \OCP\Files\NotPermittedException |
|
189 | - * @throws \OC\User\NoUserException |
|
190 | - */ |
|
191 | - private function getTemplateFolder(): Node { |
|
192 | - if ($this->getTemplatePath() !== '') { |
|
193 | - return $this->rootFolder->getUserFolder($this->userId)->get($this->getTemplatePath()); |
|
194 | - } |
|
195 | - throw new NotFoundException(); |
|
196 | - } |
|
197 | - |
|
198 | - private function getTemplateFiles(TemplateFileCreator $type): array { |
|
199 | - $templates = []; |
|
200 | - foreach ($this->getRegisteredProviders() as $provider) { |
|
201 | - foreach ($type->getMimetypes() as $mimetype) { |
|
202 | - foreach ($provider->getCustomTemplates($mimetype) as $template) { |
|
203 | - $templates[] = $template; |
|
204 | - } |
|
205 | - } |
|
206 | - } |
|
207 | - try { |
|
208 | - $userTemplateFolder = $this->getTemplateFolder(); |
|
209 | - } catch (\Exception $e) { |
|
210 | - return $templates; |
|
211 | - } |
|
212 | - foreach ($type->getMimetypes() as $mimetype) { |
|
213 | - foreach ($userTemplateFolder->searchByMime($mimetype) as $templateFile) { |
|
214 | - $template = new Template( |
|
215 | - 'user', |
|
216 | - $this->rootFolder->getUserFolder($this->userId)->getRelativePath($templateFile->getPath()), |
|
217 | - $templateFile |
|
218 | - ); |
|
219 | - $template->setHasPreview($this->previewManager->isAvailable($templateFile)); |
|
220 | - $templates[] = $template; |
|
221 | - } |
|
222 | - } |
|
223 | - |
|
224 | - return $templates; |
|
225 | - } |
|
226 | - |
|
227 | - /** |
|
228 | - * @param Node|File $file |
|
229 | - * @return array |
|
230 | - * @throws NotFoundException |
|
231 | - * @throws \OCP\Files\InvalidPathException |
|
232 | - */ |
|
233 | - private function formatFile(Node $file): array { |
|
234 | - return [ |
|
235 | - 'basename' => $file->getName(), |
|
236 | - 'etag' => $file->getEtag(), |
|
237 | - 'fileid' => $file->getId(), |
|
238 | - 'filename' => $this->rootFolder->getUserFolder($this->userId)->getRelativePath($file->getPath()), |
|
239 | - 'lastmod' => $file->getMTime(), |
|
240 | - 'mime' => $file->getMimetype(), |
|
241 | - 'size' => $file->getSize(), |
|
242 | - 'type' => $file->getType(), |
|
243 | - 'hasPreview' => $this->previewManager->isAvailable($file) |
|
244 | - ]; |
|
245 | - } |
|
246 | - |
|
247 | - public function hasTemplateDirectory(): bool { |
|
248 | - try { |
|
249 | - $this->getTemplateFolder(); |
|
250 | - return true; |
|
251 | - } catch (\Exception $e) { |
|
252 | - } |
|
253 | - return false; |
|
254 | - } |
|
255 | - |
|
256 | - public function setTemplatePath(string $path): void { |
|
257 | - $this->config->setUserValue($this->userId, 'core', 'templateDirectory', $path); |
|
258 | - } |
|
259 | - |
|
260 | - public function getTemplatePath(): string { |
|
261 | - return $this->config->getUserValue($this->userId, 'core', 'templateDirectory', ''); |
|
262 | - } |
|
263 | - |
|
264 | - public function initializeTemplateDirectory(string $path = null, string $userId = null, $copyTemplates = true): string { |
|
265 | - if ($userId !== null) { |
|
266 | - $this->userId = $userId; |
|
267 | - } |
|
268 | - |
|
269 | - $defaultSkeletonDirectory = \OC::$SERVERROOT . '/core/skeleton'; |
|
270 | - $defaultTemplateDirectory = \OC::$SERVERROOT . '/core/skeleton/Templates'; |
|
271 | - $skeletonPath = $this->config->getSystemValue('skeletondirectory', $defaultSkeletonDirectory); |
|
272 | - $skeletonTemplatePath = $this->config->getSystemValue('templatedirectory', $defaultTemplateDirectory); |
|
273 | - $isDefaultSkeleton = $skeletonPath === $defaultSkeletonDirectory; |
|
274 | - $isDefaultTemplates = $skeletonTemplatePath === $defaultTemplateDirectory; |
|
275 | - $userLang = $this->l10nFactory->getUserLanguage($this->userManager->get($this->userId)); |
|
276 | - |
|
277 | - try { |
|
278 | - $l10n = $this->l10nFactory->get('lib', $userLang); |
|
279 | - $userFolder = $this->rootFolder->getUserFolder($this->userId); |
|
280 | - $userTemplatePath = $path ?? $this->config->getAppValue('core', 'defaultTemplateDirectory', $l10n->t('Templates')) . '/'; |
|
281 | - |
|
282 | - // Initial user setup without a provided path |
|
283 | - if ($path === null) { |
|
284 | - // All locations are default so we just need to rename the directory to the users language |
|
285 | - if ($isDefaultSkeleton && $isDefaultTemplates) { |
|
286 | - if (!$userFolder->nodeExists('Templates')) { |
|
287 | - return ''; |
|
288 | - } |
|
289 | - $newPath = Filesystem::normalizePath($userFolder->getPath() . '/' . $userTemplatePath); |
|
290 | - if ($newPath !== $userFolder->get('Templates')->getPath()) { |
|
291 | - $userFolder->get('Templates')->move($newPath); |
|
292 | - } |
|
293 | - $this->setTemplatePath($userTemplatePath); |
|
294 | - return $userTemplatePath; |
|
295 | - } |
|
296 | - |
|
297 | - if ($isDefaultSkeleton && !empty($skeletonTemplatePath) && !$isDefaultTemplates && $userFolder->nodeExists('Templates')) { |
|
298 | - $shippedSkeletonTemplates = $userFolder->get('Templates'); |
|
299 | - $shippedSkeletonTemplates->delete(); |
|
300 | - } |
|
301 | - } |
|
302 | - |
|
303 | - try { |
|
304 | - $folder = $userFolder->get($userTemplatePath); |
|
305 | - } catch (NotFoundException $e) { |
|
306 | - $folder = $userFolder->get(dirname($userTemplatePath)); |
|
307 | - $folder = $folder->newFolder(basename($userTemplatePath)); |
|
308 | - } |
|
309 | - |
|
310 | - $folderIsEmpty = count($folder->getDirectoryListing()) === 0; |
|
311 | - |
|
312 | - if (!$copyTemplates) { |
|
313 | - $this->setTemplatePath($userTemplatePath); |
|
314 | - return $userTemplatePath; |
|
315 | - } |
|
316 | - |
|
317 | - if (!$isDefaultTemplates && $folderIsEmpty) { |
|
318 | - $localizedSkeletonTemplatePath = $this->getLocalizedTemplatePath($skeletonTemplatePath, $userLang); |
|
319 | - if (!empty($localizedSkeletonTemplatePath) && file_exists($localizedSkeletonTemplatePath)) { |
|
320 | - \OC_Util::copyr($localizedSkeletonTemplatePath, $folder); |
|
321 | - $userFolder->getStorage()->getScanner()->scan($folder->getInternalPath(), Scanner::SCAN_RECURSIVE); |
|
322 | - $this->setTemplatePath($userTemplatePath); |
|
323 | - return $userTemplatePath; |
|
324 | - } |
|
325 | - } |
|
326 | - |
|
327 | - if ($path !== null && $isDefaultSkeleton && $isDefaultTemplates && $folderIsEmpty) { |
|
328 | - $localizedSkeletonPath = $this->getLocalizedTemplatePath($skeletonPath . '/Templates', $userLang); |
|
329 | - if (!empty($localizedSkeletonPath) && file_exists($localizedSkeletonPath)) { |
|
330 | - \OC_Util::copyr($localizedSkeletonPath, $folder); |
|
331 | - $userFolder->getStorage()->getScanner()->scan($folder->getInternalPath(), Scanner::SCAN_RECURSIVE); |
|
332 | - $this->setTemplatePath($userTemplatePath); |
|
333 | - return $userTemplatePath; |
|
334 | - } |
|
335 | - } |
|
336 | - |
|
337 | - $this->setTemplatePath($path ?? ''); |
|
338 | - return $this->getTemplatePath(); |
|
339 | - } catch (\Throwable $e) { |
|
340 | - $this->logger->error('Failed to initialize templates directory to user language ' . $userLang . ' for ' . $userId, ['app' => 'files_templates', 'exception' => $e]); |
|
341 | - } |
|
342 | - $this->setTemplatePath(''); |
|
343 | - return $this->getTemplatePath(); |
|
344 | - } |
|
345 | - |
|
346 | - private function getLocalizedTemplatePath(string $skeletonTemplatePath, string $userLang) { |
|
347 | - $localizedSkeletonTemplatePath = str_replace('{lang}', $userLang, $skeletonTemplatePath); |
|
348 | - |
|
349 | - if (!file_exists($localizedSkeletonTemplatePath)) { |
|
350 | - $dialectStart = strpos($userLang, '_'); |
|
351 | - if ($dialectStart !== false) { |
|
352 | - $localizedSkeletonTemplatePath = str_replace('{lang}', substr($userLang, 0, $dialectStart), $skeletonTemplatePath); |
|
353 | - } |
|
354 | - if ($dialectStart === false || !file_exists($localizedSkeletonTemplatePath)) { |
|
355 | - $localizedSkeletonTemplatePath = str_replace('{lang}', 'default', $skeletonTemplatePath); |
|
356 | - } |
|
357 | - } |
|
358 | - |
|
359 | - return $localizedSkeletonTemplatePath; |
|
360 | - } |
|
54 | + private $registeredTypes = []; |
|
55 | + private $types = []; |
|
56 | + |
|
57 | + /** @var array|null */ |
|
58 | + private $providers = null; |
|
59 | + |
|
60 | + private $serverContainer; |
|
61 | + private $eventDispatcher; |
|
62 | + private $rootFolder; |
|
63 | + private $userManager; |
|
64 | + private $previewManager; |
|
65 | + private $config; |
|
66 | + private $l10n; |
|
67 | + private $logger; |
|
68 | + private $userId; |
|
69 | + private $l10nFactory; |
|
70 | + /** @var Coordinator */ |
|
71 | + private $bootstrapCoordinator; |
|
72 | + |
|
73 | + public function __construct( |
|
74 | + IServerContainer $serverContainer, |
|
75 | + IEventDispatcher $eventDispatcher, |
|
76 | + Coordinator $coordinator, |
|
77 | + IRootFolder $rootFolder, |
|
78 | + IUserSession $userSession, |
|
79 | + IUserManager $userManager, |
|
80 | + IPreview $previewManager, |
|
81 | + IConfig $config, |
|
82 | + IFactory $l10nFactory, |
|
83 | + LoggerInterface $logger |
|
84 | + ) { |
|
85 | + $this->serverContainer = $serverContainer; |
|
86 | + $this->eventDispatcher = $eventDispatcher; |
|
87 | + $this->bootstrapCoordinator = $coordinator; |
|
88 | + $this->rootFolder = $rootFolder; |
|
89 | + $this->userManager = $userManager; |
|
90 | + $this->previewManager = $previewManager; |
|
91 | + $this->config = $config; |
|
92 | + $this->l10nFactory = $l10nFactory; |
|
93 | + $this->l10n = $l10nFactory->get('lib'); |
|
94 | + $this->logger = $logger; |
|
95 | + $user = $userSession->getUser(); |
|
96 | + $this->userId = $user ? $user->getUID() : null; |
|
97 | + } |
|
98 | + |
|
99 | + public function registerTemplateFileCreator(callable $callback): void { |
|
100 | + $this->registeredTypes[] = $callback; |
|
101 | + } |
|
102 | + |
|
103 | + public function getRegisteredProviders(): array { |
|
104 | + if ($this->providers !== null) { |
|
105 | + return $this->providers; |
|
106 | + } |
|
107 | + |
|
108 | + $context = $this->bootstrapCoordinator->getRegistrationContext(); |
|
109 | + |
|
110 | + $this->providers = []; |
|
111 | + foreach ($context->getTemplateProviders() as $provider) { |
|
112 | + $class = $provider->getService(); |
|
113 | + $this->providers[$class] = $this->serverContainer->get($class); |
|
114 | + } |
|
115 | + return $this->providers; |
|
116 | + } |
|
117 | + |
|
118 | + public function getTypes(): array { |
|
119 | + if (!empty($this->types)) { |
|
120 | + return $this->types; |
|
121 | + } |
|
122 | + foreach ($this->registeredTypes as $registeredType) { |
|
123 | + $this->types[] = $registeredType(); |
|
124 | + } |
|
125 | + return $this->types; |
|
126 | + } |
|
127 | + |
|
128 | + public function listCreators(): array { |
|
129 | + $types = $this->getTypes(); |
|
130 | + usort($types, function (TemplateFileCreator $a, TemplateFileCreator $b) { |
|
131 | + return $a->getOrder() - $b->getOrder(); |
|
132 | + }); |
|
133 | + return $types; |
|
134 | + } |
|
135 | + |
|
136 | + public function listTemplates(): array { |
|
137 | + return array_map(function (TemplateFileCreator $entry) { |
|
138 | + return array_merge($entry->jsonSerialize(), [ |
|
139 | + 'templates' => $this->getTemplateFiles($entry) |
|
140 | + ]); |
|
141 | + }, $this->listCreators()); |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * @param string $filePath |
|
146 | + * @param string $templateId |
|
147 | + * @return array |
|
148 | + * @throws GenericFileException |
|
149 | + */ |
|
150 | + public function createFromTemplate(string $filePath, string $templateId = '', string $templateType = 'user'): array { |
|
151 | + $userFolder = $this->rootFolder->getUserFolder($this->userId); |
|
152 | + try { |
|
153 | + $userFolder->get($filePath); |
|
154 | + throw new GenericFileException($this->l10n->t('File already exists')); |
|
155 | + } catch (NotFoundException $e) { |
|
156 | + } |
|
157 | + try { |
|
158 | + if (!$userFolder->nodeExists(dirname($filePath))) { |
|
159 | + throw new GenericFileException($this->l10n->t('Invalid path')); |
|
160 | + } |
|
161 | + $folder = $userFolder->get(dirname($filePath)); |
|
162 | + $targetFile = $folder->newFile(basename($filePath)); |
|
163 | + $template = null; |
|
164 | + if ($templateType === 'user' && $templateId !== '') { |
|
165 | + $template = $userFolder->get($templateId); |
|
166 | + $template->copy($targetFile->getPath()); |
|
167 | + } else { |
|
168 | + $matchingProvider = array_filter($this->getRegisteredProviders(), function (ICustomTemplateProvider $provider) use ($templateType) { |
|
169 | + return $templateType === get_class($provider); |
|
170 | + }); |
|
171 | + $provider = array_shift($matchingProvider); |
|
172 | + if ($provider) { |
|
173 | + $template = $provider->getCustomTemplate($templateId); |
|
174 | + $template->copy($targetFile->getPath()); |
|
175 | + } |
|
176 | + } |
|
177 | + $this->eventDispatcher->dispatchTyped(new FileCreatedFromTemplateEvent($template, $targetFile)); |
|
178 | + return $this->formatFile($userFolder->get($filePath)); |
|
179 | + } catch (\Exception $e) { |
|
180 | + $this->logger->error($e->getMessage(), ['exception' => $e]); |
|
181 | + throw new GenericFileException($this->l10n->t('Failed to create file from template')); |
|
182 | + } |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * @return Folder |
|
187 | + * @throws \OCP\Files\NotFoundException |
|
188 | + * @throws \OCP\Files\NotPermittedException |
|
189 | + * @throws \OC\User\NoUserException |
|
190 | + */ |
|
191 | + private function getTemplateFolder(): Node { |
|
192 | + if ($this->getTemplatePath() !== '') { |
|
193 | + return $this->rootFolder->getUserFolder($this->userId)->get($this->getTemplatePath()); |
|
194 | + } |
|
195 | + throw new NotFoundException(); |
|
196 | + } |
|
197 | + |
|
198 | + private function getTemplateFiles(TemplateFileCreator $type): array { |
|
199 | + $templates = []; |
|
200 | + foreach ($this->getRegisteredProviders() as $provider) { |
|
201 | + foreach ($type->getMimetypes() as $mimetype) { |
|
202 | + foreach ($provider->getCustomTemplates($mimetype) as $template) { |
|
203 | + $templates[] = $template; |
|
204 | + } |
|
205 | + } |
|
206 | + } |
|
207 | + try { |
|
208 | + $userTemplateFolder = $this->getTemplateFolder(); |
|
209 | + } catch (\Exception $e) { |
|
210 | + return $templates; |
|
211 | + } |
|
212 | + foreach ($type->getMimetypes() as $mimetype) { |
|
213 | + foreach ($userTemplateFolder->searchByMime($mimetype) as $templateFile) { |
|
214 | + $template = new Template( |
|
215 | + 'user', |
|
216 | + $this->rootFolder->getUserFolder($this->userId)->getRelativePath($templateFile->getPath()), |
|
217 | + $templateFile |
|
218 | + ); |
|
219 | + $template->setHasPreview($this->previewManager->isAvailable($templateFile)); |
|
220 | + $templates[] = $template; |
|
221 | + } |
|
222 | + } |
|
223 | + |
|
224 | + return $templates; |
|
225 | + } |
|
226 | + |
|
227 | + /** |
|
228 | + * @param Node|File $file |
|
229 | + * @return array |
|
230 | + * @throws NotFoundException |
|
231 | + * @throws \OCP\Files\InvalidPathException |
|
232 | + */ |
|
233 | + private function formatFile(Node $file): array { |
|
234 | + return [ |
|
235 | + 'basename' => $file->getName(), |
|
236 | + 'etag' => $file->getEtag(), |
|
237 | + 'fileid' => $file->getId(), |
|
238 | + 'filename' => $this->rootFolder->getUserFolder($this->userId)->getRelativePath($file->getPath()), |
|
239 | + 'lastmod' => $file->getMTime(), |
|
240 | + 'mime' => $file->getMimetype(), |
|
241 | + 'size' => $file->getSize(), |
|
242 | + 'type' => $file->getType(), |
|
243 | + 'hasPreview' => $this->previewManager->isAvailable($file) |
|
244 | + ]; |
|
245 | + } |
|
246 | + |
|
247 | + public function hasTemplateDirectory(): bool { |
|
248 | + try { |
|
249 | + $this->getTemplateFolder(); |
|
250 | + return true; |
|
251 | + } catch (\Exception $e) { |
|
252 | + } |
|
253 | + return false; |
|
254 | + } |
|
255 | + |
|
256 | + public function setTemplatePath(string $path): void { |
|
257 | + $this->config->setUserValue($this->userId, 'core', 'templateDirectory', $path); |
|
258 | + } |
|
259 | + |
|
260 | + public function getTemplatePath(): string { |
|
261 | + return $this->config->getUserValue($this->userId, 'core', 'templateDirectory', ''); |
|
262 | + } |
|
263 | + |
|
264 | + public function initializeTemplateDirectory(string $path = null, string $userId = null, $copyTemplates = true): string { |
|
265 | + if ($userId !== null) { |
|
266 | + $this->userId = $userId; |
|
267 | + } |
|
268 | + |
|
269 | + $defaultSkeletonDirectory = \OC::$SERVERROOT . '/core/skeleton'; |
|
270 | + $defaultTemplateDirectory = \OC::$SERVERROOT . '/core/skeleton/Templates'; |
|
271 | + $skeletonPath = $this->config->getSystemValue('skeletondirectory', $defaultSkeletonDirectory); |
|
272 | + $skeletonTemplatePath = $this->config->getSystemValue('templatedirectory', $defaultTemplateDirectory); |
|
273 | + $isDefaultSkeleton = $skeletonPath === $defaultSkeletonDirectory; |
|
274 | + $isDefaultTemplates = $skeletonTemplatePath === $defaultTemplateDirectory; |
|
275 | + $userLang = $this->l10nFactory->getUserLanguage($this->userManager->get($this->userId)); |
|
276 | + |
|
277 | + try { |
|
278 | + $l10n = $this->l10nFactory->get('lib', $userLang); |
|
279 | + $userFolder = $this->rootFolder->getUserFolder($this->userId); |
|
280 | + $userTemplatePath = $path ?? $this->config->getAppValue('core', 'defaultTemplateDirectory', $l10n->t('Templates')) . '/'; |
|
281 | + |
|
282 | + // Initial user setup without a provided path |
|
283 | + if ($path === null) { |
|
284 | + // All locations are default so we just need to rename the directory to the users language |
|
285 | + if ($isDefaultSkeleton && $isDefaultTemplates) { |
|
286 | + if (!$userFolder->nodeExists('Templates')) { |
|
287 | + return ''; |
|
288 | + } |
|
289 | + $newPath = Filesystem::normalizePath($userFolder->getPath() . '/' . $userTemplatePath); |
|
290 | + if ($newPath !== $userFolder->get('Templates')->getPath()) { |
|
291 | + $userFolder->get('Templates')->move($newPath); |
|
292 | + } |
|
293 | + $this->setTemplatePath($userTemplatePath); |
|
294 | + return $userTemplatePath; |
|
295 | + } |
|
296 | + |
|
297 | + if ($isDefaultSkeleton && !empty($skeletonTemplatePath) && !$isDefaultTemplates && $userFolder->nodeExists('Templates')) { |
|
298 | + $shippedSkeletonTemplates = $userFolder->get('Templates'); |
|
299 | + $shippedSkeletonTemplates->delete(); |
|
300 | + } |
|
301 | + } |
|
302 | + |
|
303 | + try { |
|
304 | + $folder = $userFolder->get($userTemplatePath); |
|
305 | + } catch (NotFoundException $e) { |
|
306 | + $folder = $userFolder->get(dirname($userTemplatePath)); |
|
307 | + $folder = $folder->newFolder(basename($userTemplatePath)); |
|
308 | + } |
|
309 | + |
|
310 | + $folderIsEmpty = count($folder->getDirectoryListing()) === 0; |
|
311 | + |
|
312 | + if (!$copyTemplates) { |
|
313 | + $this->setTemplatePath($userTemplatePath); |
|
314 | + return $userTemplatePath; |
|
315 | + } |
|
316 | + |
|
317 | + if (!$isDefaultTemplates && $folderIsEmpty) { |
|
318 | + $localizedSkeletonTemplatePath = $this->getLocalizedTemplatePath($skeletonTemplatePath, $userLang); |
|
319 | + if (!empty($localizedSkeletonTemplatePath) && file_exists($localizedSkeletonTemplatePath)) { |
|
320 | + \OC_Util::copyr($localizedSkeletonTemplatePath, $folder); |
|
321 | + $userFolder->getStorage()->getScanner()->scan($folder->getInternalPath(), Scanner::SCAN_RECURSIVE); |
|
322 | + $this->setTemplatePath($userTemplatePath); |
|
323 | + return $userTemplatePath; |
|
324 | + } |
|
325 | + } |
|
326 | + |
|
327 | + if ($path !== null && $isDefaultSkeleton && $isDefaultTemplates && $folderIsEmpty) { |
|
328 | + $localizedSkeletonPath = $this->getLocalizedTemplatePath($skeletonPath . '/Templates', $userLang); |
|
329 | + if (!empty($localizedSkeletonPath) && file_exists($localizedSkeletonPath)) { |
|
330 | + \OC_Util::copyr($localizedSkeletonPath, $folder); |
|
331 | + $userFolder->getStorage()->getScanner()->scan($folder->getInternalPath(), Scanner::SCAN_RECURSIVE); |
|
332 | + $this->setTemplatePath($userTemplatePath); |
|
333 | + return $userTemplatePath; |
|
334 | + } |
|
335 | + } |
|
336 | + |
|
337 | + $this->setTemplatePath($path ?? ''); |
|
338 | + return $this->getTemplatePath(); |
|
339 | + } catch (\Throwable $e) { |
|
340 | + $this->logger->error('Failed to initialize templates directory to user language ' . $userLang . ' for ' . $userId, ['app' => 'files_templates', 'exception' => $e]); |
|
341 | + } |
|
342 | + $this->setTemplatePath(''); |
|
343 | + return $this->getTemplatePath(); |
|
344 | + } |
|
345 | + |
|
346 | + private function getLocalizedTemplatePath(string $skeletonTemplatePath, string $userLang) { |
|
347 | + $localizedSkeletonTemplatePath = str_replace('{lang}', $userLang, $skeletonTemplatePath); |
|
348 | + |
|
349 | + if (!file_exists($localizedSkeletonTemplatePath)) { |
|
350 | + $dialectStart = strpos($userLang, '_'); |
|
351 | + if ($dialectStart !== false) { |
|
352 | + $localizedSkeletonTemplatePath = str_replace('{lang}', substr($userLang, 0, $dialectStart), $skeletonTemplatePath); |
|
353 | + } |
|
354 | + if ($dialectStart === false || !file_exists($localizedSkeletonTemplatePath)) { |
|
355 | + $localizedSkeletonTemplatePath = str_replace('{lang}', 'default', $skeletonTemplatePath); |
|
356 | + } |
|
357 | + } |
|
358 | + |
|
359 | + return $localizedSkeletonTemplatePath; |
|
360 | + } |
|
361 | 361 | } |
@@ -127,14 +127,14 @@ discard block |
||
127 | 127 | |
128 | 128 | public function listCreators(): array { |
129 | 129 | $types = $this->getTypes(); |
130 | - usort($types, function (TemplateFileCreator $a, TemplateFileCreator $b) { |
|
130 | + usort($types, function(TemplateFileCreator $a, TemplateFileCreator $b) { |
|
131 | 131 | return $a->getOrder() - $b->getOrder(); |
132 | 132 | }); |
133 | 133 | return $types; |
134 | 134 | } |
135 | 135 | |
136 | 136 | public function listTemplates(): array { |
137 | - return array_map(function (TemplateFileCreator $entry) { |
|
137 | + return array_map(function(TemplateFileCreator $entry) { |
|
138 | 138 | return array_merge($entry->jsonSerialize(), [ |
139 | 139 | 'templates' => $this->getTemplateFiles($entry) |
140 | 140 | ]); |
@@ -165,7 +165,7 @@ discard block |
||
165 | 165 | $template = $userFolder->get($templateId); |
166 | 166 | $template->copy($targetFile->getPath()); |
167 | 167 | } else { |
168 | - $matchingProvider = array_filter($this->getRegisteredProviders(), function (ICustomTemplateProvider $provider) use ($templateType) { |
|
168 | + $matchingProvider = array_filter($this->getRegisteredProviders(), function(ICustomTemplateProvider $provider) use ($templateType) { |
|
169 | 169 | return $templateType === get_class($provider); |
170 | 170 | }); |
171 | 171 | $provider = array_shift($matchingProvider); |
@@ -266,8 +266,8 @@ discard block |
||
266 | 266 | $this->userId = $userId; |
267 | 267 | } |
268 | 268 | |
269 | - $defaultSkeletonDirectory = \OC::$SERVERROOT . '/core/skeleton'; |
|
270 | - $defaultTemplateDirectory = \OC::$SERVERROOT . '/core/skeleton/Templates'; |
|
269 | + $defaultSkeletonDirectory = \OC::$SERVERROOT.'/core/skeleton'; |
|
270 | + $defaultTemplateDirectory = \OC::$SERVERROOT.'/core/skeleton/Templates'; |
|
271 | 271 | $skeletonPath = $this->config->getSystemValue('skeletondirectory', $defaultSkeletonDirectory); |
272 | 272 | $skeletonTemplatePath = $this->config->getSystemValue('templatedirectory', $defaultTemplateDirectory); |
273 | 273 | $isDefaultSkeleton = $skeletonPath === $defaultSkeletonDirectory; |
@@ -277,7 +277,7 @@ discard block |
||
277 | 277 | try { |
278 | 278 | $l10n = $this->l10nFactory->get('lib', $userLang); |
279 | 279 | $userFolder = $this->rootFolder->getUserFolder($this->userId); |
280 | - $userTemplatePath = $path ?? $this->config->getAppValue('core', 'defaultTemplateDirectory', $l10n->t('Templates')) . '/'; |
|
280 | + $userTemplatePath = $path ?? $this->config->getAppValue('core', 'defaultTemplateDirectory', $l10n->t('Templates')).'/'; |
|
281 | 281 | |
282 | 282 | // Initial user setup without a provided path |
283 | 283 | if ($path === null) { |
@@ -286,7 +286,7 @@ discard block |
||
286 | 286 | if (!$userFolder->nodeExists('Templates')) { |
287 | 287 | return ''; |
288 | 288 | } |
289 | - $newPath = Filesystem::normalizePath($userFolder->getPath() . '/' . $userTemplatePath); |
|
289 | + $newPath = Filesystem::normalizePath($userFolder->getPath().'/'.$userTemplatePath); |
|
290 | 290 | if ($newPath !== $userFolder->get('Templates')->getPath()) { |
291 | 291 | $userFolder->get('Templates')->move($newPath); |
292 | 292 | } |
@@ -325,7 +325,7 @@ discard block |
||
325 | 325 | } |
326 | 326 | |
327 | 327 | if ($path !== null && $isDefaultSkeleton && $isDefaultTemplates && $folderIsEmpty) { |
328 | - $localizedSkeletonPath = $this->getLocalizedTemplatePath($skeletonPath . '/Templates', $userLang); |
|
328 | + $localizedSkeletonPath = $this->getLocalizedTemplatePath($skeletonPath.'/Templates', $userLang); |
|
329 | 329 | if (!empty($localizedSkeletonPath) && file_exists($localizedSkeletonPath)) { |
330 | 330 | \OC_Util::copyr($localizedSkeletonPath, $folder); |
331 | 331 | $userFolder->getStorage()->getScanner()->scan($folder->getInternalPath(), Scanner::SCAN_RECURSIVE); |
@@ -337,7 +337,7 @@ discard block |
||
337 | 337 | $this->setTemplatePath($path ?? ''); |
338 | 338 | return $this->getTemplatePath(); |
339 | 339 | } catch (\Throwable $e) { |
340 | - $this->logger->error('Failed to initialize templates directory to user language ' . $userLang . ' for ' . $userId, ['app' => 'files_templates', 'exception' => $e]); |
|
340 | + $this->logger->error('Failed to initialize templates directory to user language '.$userLang.' for '.$userId, ['app' => 'files_templates', 'exception' => $e]); |
|
341 | 341 | } |
342 | 342 | $this->setTemplatePath(''); |
343 | 343 | return $this->getTemplatePath(); |