|
46 | 46 | use OCP\AppFramework\OCS\OCSNotFoundException; |
47 | 47 | use OCP\AppFramework\OCSController; |
48 | 48 | use OCP\BackgroundJob\IJobList; |
| 49 | +use OCP\Files\Folder; |
49 | 50 | use OCP\Files\IMimeTypeDetector; |
50 | 51 | use OCP\Files\IRootFolder; |
| 52 | +use OCP\Files\NotFoundException; |
51 | 53 | use OCP\IL10N; |
52 | 54 | use OCP\IRequest; |
53 | 55 | use OCP\IUser; |
@@ -742,6 +744,10 @@ public function deleteQuestion(int $formId, int $questionId): DataResponse { |
742 | 744 | $question->setOrder(0); |
743 | 745 | $this->questionMapper->update($question); |
744 | 746 |
|
| 747 | + if ($question->getType() === Constants::ANSWER_TYPE_FILE) { |
| 748 | + $this->deleteQuestionFolder($form, $question); |
| 749 | + } |
| 750 | + |
745 | 751 | // Update all question-order > deleted order. |
746 | 752 | $formQuestions = $this->questionMapper->findByForm($formId); |
747 | 753 | foreach ($formQuestions as $question) { |
@@ -1589,7 +1595,7 @@ public function deleteSubmission(int $formId, int $submissionId): DataResponse { |
1589 | 1595 | } |
1590 | 1596 |
|
1591 | 1597 | // Delete submission (incl. Answers) |
1592 | | - $this->submissionMapper->deleteById($submissionId); |
| 1598 | + $this->submissionMapper->deleteById($form, $submissionId); |
1593 | 1599 | $this->formMapper->update($form); |
1594 | 1600 |
|
1595 | 1601 | return new DataResponse($submissionId); |
@@ -1743,8 +1749,7 @@ public function uploadFiles(int $formId, int $questionId, string $shareHash = '' |
1743 | 1749 | } else { |
1744 | 1750 | $folder = $userFolder->newFolder($path); |
1745 | 1751 | } |
1746 | | - /** @var \OCP\Files\Folder $folder */ |
1747 | | - |
| 1752 | + /** @var Folder $folder */ |
1748 | 1753 | $fileName = $folder->getNonExistingName($uploadedFile['name']); |
1749 | 1754 | $file = $folder->newFile($fileName, file_get_contents($uploadedFile['tmp_name'])); |
1750 | 1755 |
|
@@ -1819,8 +1824,7 @@ private function storeAnswersForQuestion(Form $form, $submissionId, array $quest |
1819 | 1824 | } else { |
1820 | 1825 | $folder = $userFolder->newFolder($path); |
1821 | 1826 | } |
1822 | | - /** @var \OCP\Files\Folder $folder */ |
1823 | | - |
| 1827 | + /** @var Folder $folder */ |
1824 | 1828 | $file = $userFolder->getById($uploadedFile->getFileId())[0]; |
1825 | 1829 | $name = $folder->getNonExistingName($file->getName()); |
1826 | 1830 | $file->move($folder->getPath() . '/' . $name); |
@@ -1992,4 +1996,43 @@ private function handleOwnerTransfer(Form $form, int $formId, string $currentUse |
1992 | 1996 | $this->formMapper->update($form); |
1993 | 1997 | return new DataResponse($form->getOwnerId()); |
1994 | 1998 | } |
| 1999 | + |
| 2000 | + |
| 2001 | + |
| 2002 | + /** |
| 2003 | + * Delete question folders from all submissions |
| 2004 | + * @param Form $form The form |
| 2005 | + * @param Question $question The question |
| 2006 | + */ |
| 2007 | + private function deleteQuestionFolder(Form $form, Question $question): void { |
| 2008 | + try { |
| 2009 | + $userFolder = $this->rootFolder->getUserFolder($form->getOwnerId()); |
| 2010 | + $formFolderPath = $this->formsService->getFormUploadedFilesFolderPath($form); |
| 2011 | + |
| 2012 | + $formFolder = $userFolder->get($formFolderPath); |
| 2013 | + if (!$formFolder instanceof Folder) { |
| 2014 | + return; |
| 2015 | + } |
| 2016 | + $questionFolderPrefix = $question->getId() . ' - '; |
| 2017 | + |
| 2018 | + // Iterate through submission folders and delete matching question folders |
| 2019 | + foreach ($formFolder->getDirectoryListing() as $submissionFolder) { |
| 2020 | + if (!$submissionFolder instanceof Folder) { |
| 2021 | + continue; |
| 2022 | + } |
| 2023 | + foreach ($submissionFolder->getDirectoryListing() as $node) { |
| 2024 | + if (str_starts_with($node->getName(), $questionFolderPrefix)) { |
| 2025 | + $node->delete(); |
| 2026 | + } |
| 2027 | + } |
| 2028 | + } |
| 2029 | + } catch (NotFoundException) { |
| 2030 | + // Do nothing |
| 2031 | + } catch (\Throwable $e) { |
| 2032 | + $this->logger->warning('Failed to delete question folders: {error}', [ |
| 2033 | + 'error' => $e->getMessage(), |
| 2034 | + 'questionId' => $question->getId(), |
| 2035 | + ]); |
| 2036 | + } |
| 2037 | + } |
1995 | 2038 | } |
0 commit comments