-
Notifications
You must be signed in to change notification settings - Fork 119
fix: Delete files on submission/question/form deletion #3335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Koc
wants to merge
3
commits into
main
Choose a base branch
from
feature/delete-files-on
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Forms\BackgroundJob; | ||
|
|
||
| use OCA\Forms\Helper\FilePathHelper; | ||
| use OCP\AppFramework\Utility\ITimeFactory; | ||
| use OCP\BackgroundJob\QueuedJob; | ||
| use OCP\Files\Folder; | ||
| use OCP\Files\NotFoundException; | ||
| use Psr\Log\LoggerInterface; | ||
|
|
||
| class DeleteQuestionFoldersJob extends QueuedJob { | ||
| public function __construct( | ||
| ITimeFactory $time, | ||
| private FilePathHelper $filePathHelper, | ||
| private LoggerInterface $logger, | ||
| ) { | ||
| parent::__construct($time); | ||
| } | ||
|
|
||
| /** | ||
| * @param array{formId: int, questionId: int, ownerId: string} $argument | ||
| */ | ||
| public function run($argument): void { | ||
| $formId = $argument['formId']; | ||
| $questionId = $argument['questionId']; | ||
| $ownerId = $argument['ownerId']; | ||
|
|
||
| try { | ||
| $this->logger->debug('Deleting question folders for question {questionId} in form {formId}', [ | ||
| 'questionId' => $questionId, | ||
| 'formId' => $formId, | ||
| ]); | ||
|
|
||
| $formFolders = $this->filePathHelper->getAllFormFoldersById($formId, $ownerId); | ||
| if (empty($formFolders)) { | ||
| $this->logger->notice('Form folder not found, nothing to delete', [ | ||
| 'formId' => $formId, | ||
| ]); | ||
| return; | ||
| } | ||
|
|
||
| $questionFolderPrefix = $questionId . ' - '; | ||
| $deletedCount = 0; | ||
|
|
||
| // Iterate through all form folders (handles form renames) | ||
| foreach ($formFolders as $formFolder) { | ||
| // Iterate through submission folders and delete matching question folders | ||
| foreach ($formFolder->getDirectoryListing() as $submissionFolder) { | ||
| if (!$submissionFolder instanceof Folder) { | ||
| continue; | ||
| } | ||
| foreach ($submissionFolder->getDirectoryListing() as $questionFolder) { | ||
| if (str_starts_with($questionFolder->getName(), $questionFolderPrefix)) { | ||
| $questionFolder->delete(); | ||
| $deletedCount++; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| $this->logger->info('Deleted {count} question folders for question {questionId}', [ | ||
| 'count' => $deletedCount, | ||
| 'questionId' => $questionId, | ||
| 'formId' => $formId, | ||
| ]); | ||
| } catch (NotFoundException) { | ||
| // Folder doesn't exist, do nothing | ||
| $this->logger->notice('Question folder not found, nothing to delete', [ | ||
| 'questionId' => $questionId, | ||
| 'formId' => $formId, | ||
| ]); | ||
| } catch (\Throwable $e) { | ||
| $this->logger->warning('Failed to delete question folders: {error}', [ | ||
| 'error' => $e->getMessage(), | ||
| 'questionId' => $questionId, | ||
| 'formId' => $formId, | ||
| ]); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,13 +8,16 @@ | |||||||
| namespace OCA\Forms\Db; | ||||||||
|
|
||||||||
| use OCA\Forms\Constants; | ||||||||
| use OCA\Forms\Helper\FilePathHelper; | ||||||||
| use OCA\Forms\Service\ConfigService; | ||||||||
| use OCP\AppFramework\Db\Entity; | ||||||||
| use OCP\AppFramework\Db\QBMapper; | ||||||||
| use OCP\Comments\ICommentsManager; | ||||||||
| use OCP\DB\QueryBuilder\IQueryBuilder; | ||||||||
| use OCP\Files\Folder; | ||||||||
| use OCP\IDBConnection; | ||||||||
| use OCP\Share\IShare; | ||||||||
| use Psr\Log\LoggerInterface; | ||||||||
|
|
||||||||
| /** | ||||||||
| * @extends QBMapper<Form> | ||||||||
|
|
@@ -32,6 +35,8 @@ public function __construct( | |||||||
| private SubmissionMapper $submissionMapper, | ||||||||
| private ConfigService $configService, | ||||||||
| private ICommentsManager $commentsManager, | ||||||||
| private FilePathHelper $filePathHelper, | ||||||||
| private LoggerInterface $logger, | ||||||||
| ) { | ||||||||
| parent::__construct($db, 'forms_v2_forms', Form::class); | ||||||||
| } | ||||||||
|
|
@@ -224,6 +229,33 @@ public function deleteForm(Form $form): void { | |||||||
| $this->shareMapper->deleteByForm($formId); | ||||||||
| $this->questionMapper->deleteByForm($formId); | ||||||||
| $this->commentsManager->deleteCommentsAtObject('forms', (string)$formId); | ||||||||
| $this->deleteFormFolder($form); | ||||||||
| $this->delete($form); | ||||||||
| } | ||||||||
|
|
||||||||
| /** | ||||||||
| * Delete the form folder from the file system | ||||||||
| * @param Form $form The form instance | ||||||||
| */ | ||||||||
| private function deleteFormFolder(Form $form): void { | ||||||||
| try { | ||||||||
| $formsFolder = $this->filePathHelper->getFormsFolder($form->getOwnerId()); | ||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| if ($formsFolder === null) { | ||||||||
| return; | ||||||||
| } | ||||||||
| $formFolderPrefix = $form->getId() . ' - '; | ||||||||
|
|
||||||||
| // Iterate through form folders and delete matching folders | ||||||||
| foreach ($formsFolder->getDirectoryListing() as $node) { | ||||||||
| if (str_starts_with($node->getName(), $formFolderPrefix)) { | ||||||||
| $node->delete(); | ||||||||
| } | ||||||||
| } | ||||||||
| } catch (\Throwable $e) { | ||||||||
| $this->logger->warning('Failed to delete form folder: {error}', [ | ||||||||
| 'error' => $e->getMessage(), | ||||||||
| 'formId' => $form->getId(), | ||||||||
| ]); | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,10 +7,13 @@ | |||||||
|
|
||||||||
| namespace OCA\Forms\Db; | ||||||||
|
|
||||||||
| use OCA\Forms\Helper\FilePathHelper; | ||||||||
| use OCP\AppFramework\Db\DoesNotExistException; | ||||||||
| use OCP\AppFramework\Db\QBMapper; | ||||||||
| use OCP\DB\QueryBuilder\IQueryBuilder; | ||||||||
| use OCP\Files\Folder; | ||||||||
| use OCP\IDBConnection; | ||||||||
| use Psr\Log\LoggerInterface; | ||||||||
|
|
||||||||
| /** | ||||||||
| * @extends QBMapper<Submission> | ||||||||
|
|
@@ -20,10 +23,14 @@ class SubmissionMapper extends QBMapper { | |||||||
| * SubmissionMapper constructor. | ||||||||
| * @param IDBConnection $db | ||||||||
| * @param AnswerMapper $answerMapper | ||||||||
| * @param FilePathHelper $filePathHelper | ||||||||
| * @param LoggerInterface $logger | ||||||||
| */ | ||||||||
| public function __construct( | ||||||||
| IDBConnection $db, | ||||||||
| private AnswerMapper $answerMapper, | ||||||||
| private FilePathHelper $filePathHelper, | ||||||||
| private LoggerInterface $logger, | ||||||||
| ) { | ||||||||
| parent::__construct($db, 'forms_v2_submissions', Submission::class); | ||||||||
| } | ||||||||
|
|
@@ -179,22 +186,17 @@ protected function countSubmissionsWithFilters(int $formId, ?string $userId = nu | |||||||
|
|
||||||||
| /** | ||||||||
| * Delete the Submission, including answers. | ||||||||
| * @param Form $form Form the submission belongs to. | ||||||||
| * @param int $id of the submission to delete | ||||||||
| */ | ||||||||
| public function deleteById(int $id): void { | ||||||||
| $qb = $this->db->getQueryBuilder(); | ||||||||
|
|
||||||||
| // First delete corresponding answers. | ||||||||
| public function deleteById(Form $form, int $id): void { | ||||||||
| $submissionEntity = $this->findById($id); | ||||||||
| $this->answerMapper->deleteBySubmission($submissionEntity->getId()); | ||||||||
|
|
||||||||
| //Delete Submission | ||||||||
| $qb->delete($this->getTableName()) | ||||||||
| ->where( | ||||||||
| $qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)) | ||||||||
| ); | ||||||||
| $this->deleteSubmissionFolder($form, $submissionEntity->getId()); | ||||||||
|
|
||||||||
| $qb->executeStatement(); | ||||||||
| $this->answerMapper->deleteBySubmission($submissionEntity->getId()); | ||||||||
|
|
||||||||
| $this->delete($submissionEntity); | ||||||||
| } | ||||||||
|
|
||||||||
| /** | ||||||||
|
|
@@ -218,4 +220,24 @@ public function deleteByForm(int $formId): void { | |||||||
|
|
||||||||
| $qb->executeStatement(); | ||||||||
| } | ||||||||
|
|
||||||||
| /** | ||||||||
| * Delete the submission folder from the file system | ||||||||
| * @param Form $form The form instance | ||||||||
| * @param int $submissionId The submission ID | ||||||||
| */ | ||||||||
| private function deleteSubmissionFolder(Form $form, int $submissionId): void { | ||||||||
| try { | ||||||||
| $submissionFolder = $this->filePathHelper->getSubmissionFolder($form, $submissionId); | ||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without the annotation the import is shown as unused
Suggested change
|
||||||||
| if ($submissionFolder !== null) { | ||||||||
| $submissionFolder->delete(); | ||||||||
| } | ||||||||
| } catch (\Throwable $e) { | ||||||||
| $this->logger->warning('Failed to delete submission folder: {error}', [ | ||||||||
| 'error' => $e->getMessage(), | ||||||||
| 'submissionId' => $submissionId, | ||||||||
| 'formId' => $form->getId(), | ||||||||
| ]); | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.