|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 6 | + */ |
| 7 | + |
| 8 | +namespace OCA\Forms\Helper; |
| 9 | + |
| 10 | +use OCA\Forms\Constants; |
| 11 | +use OCA\Forms\Db\Form; |
| 12 | +use OCP\Files\Folder; |
| 13 | +use OCP\Files\IRootFolder; |
| 14 | +use OCP\Files\NotFoundException; |
| 15 | + |
| 16 | +class FilePathHelper { |
| 17 | + public function __construct( |
| 18 | + public IRootFolder $rootFolder, |
| 19 | + ) { |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * Normalize a filename by replacing invalid characters |
| 24 | + */ |
| 25 | + public function normalizeFileName(string $fileName): string { |
| 26 | + return trim(str_replace(Constants::FILENAME_INVALID_CHARS, '-', $fileName)); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Get the folder path for uploaded files of a form |
| 31 | + */ |
| 32 | + public function getFormUploadedFilesFolderPath(Form $form): string { |
| 33 | + return implode('/', [ |
| 34 | + Constants::FILES_FOLDER, |
| 35 | + $this->normalizeFileName($form->getId() . ' - ' . $form->getTitle()), |
| 36 | + ]); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Get the full file path for a specific uploaded file |
| 41 | + */ |
| 42 | + public function getUploadedFilePath(Form $form, int $submissionId, int $questionId, ?string $questionName, string $questionText): string { |
| 43 | + return implode('/', [ |
| 44 | + $this->getFormUploadedFilesFolderPath($form), |
| 45 | + $submissionId, |
| 46 | + $this->normalizeFileName($questionId . ' - ' . ($questionName ?: $questionText)) |
| 47 | + ]); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Get the form folder for a given form |
| 52 | + * Searches by prefix to handle form renames |
| 53 | + * Returns the first matching folder (most recent) |
| 54 | + */ |
| 55 | + public function getFormFolder(Form $form): ?Folder { |
| 56 | + $formsFolder = $this->getFormsFolder($form->getOwnerId()); |
| 57 | + if ($formsFolder === null) { |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + $formFolderPrefix = $form->getId() . ' - '; |
| 62 | + |
| 63 | + // Search for folder matching the form ID prefix |
| 64 | + foreach ($formsFolder->getDirectoryListing() as $node) { |
| 65 | + if (str_starts_with($node->getName(), $formFolderPrefix) && $node instanceof Folder) { |
| 66 | + return $node; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return null; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Get all form folders matching the form ID prefix |
| 75 | + * Useful for cleanup operations when form may have been renamed or deleted |
| 76 | + * @param int $formId The form ID |
| 77 | + * @param string $ownerId The owner user ID |
| 78 | + * @return Folder[] |
| 79 | + */ |
| 80 | + public function getAllFormFoldersById(int $formId, string $ownerId): array { |
| 81 | + $formsFolder = $this->getFormsFolder($ownerId); |
| 82 | + if ($formsFolder === null) { |
| 83 | + return []; |
| 84 | + } |
| 85 | + |
| 86 | + $formFolderPrefix = $formId . ' - '; |
| 87 | + $matchingFolders = []; |
| 88 | + |
| 89 | + // Collect all folders matching the form ID prefix |
| 90 | + foreach ($formsFolder->getDirectoryListing() as $node) { |
| 91 | + if (str_starts_with($node->getName(), $formFolderPrefix) && $node instanceof Folder) { |
| 92 | + $matchingFolders[] = $node; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return $matchingFolders; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Get the forms folder for a user |
| 101 | + */ |
| 102 | + public function getFormsFolder(string $ownerId): ?Folder { |
| 103 | + try { |
| 104 | + $userFolder = $this->rootFolder->getUserFolder($ownerId); |
| 105 | + $formsFolder = $userFolder->get(Constants::FILES_FOLDER); |
| 106 | + |
| 107 | + if (!$formsFolder instanceof Folder) { |
| 108 | + return null; |
| 109 | + } |
| 110 | + |
| 111 | + return $formsFolder; |
| 112 | + } catch (NotFoundException) { |
| 113 | + return null; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Get the submission folder for a specific submission |
| 119 | + * Searches across all form folders to handle form renames |
| 120 | + */ |
| 121 | + public function getSubmissionFolder(Form $form, int $submissionId): ?Folder { |
| 122 | + $formFolders = $this->getAllFormFoldersById($form->getId(), $form->getOwnerId()); |
| 123 | + |
| 124 | + // Search for submission folder in all matching form folders |
| 125 | + foreach ($formFolders as $formFolder) { |
| 126 | + try { |
| 127 | + $submissionFolder = $formFolder->get((string)$submissionId); |
| 128 | + if ($submissionFolder instanceof Folder) { |
| 129 | + return $submissionFolder; |
| 130 | + } |
| 131 | + } catch (NotFoundException) { |
| 132 | + // Continue to next form folder |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return null; |
| 137 | + } |
| 138 | +} |
0 commit comments