Skip to content

Beautiful Open-Source Form Builder

Remove file on submission delete (#846)

Chirag Chhatralabf317ef32010
diff --git a/api/app/Events/Models/FormSubmissionDeleting.php b/api/app/Events/Models/FormSubmissionDeleting.php
new file mode 100644
index 000000000..4712bc05e
--- /dev/null
+++ b/api/app/Events/Models/FormSubmissionDeleting.php
@@ -0,0 +1,17 @@
+<?php
+
+namespace App\Events\Models;
+
+use App\Models\Forms\FormSubmission;
+use Illuminate\Foundation\Events\Dispatchable;
+use Illuminate\Queue\SerializesModels;
+
+class FormSubmissionDeleting
+{
+ use Dispatchable;
+ use SerializesModels;
+
+ public function __construct(public FormSubmission $submission)
+ {
+ }
+}
diff --git a/api/app/Listeners/Forms/DeleteFormSubmissionFiles.php b/api/app/Listeners/Forms/DeleteFormSubmissionFiles.php
new file mode 100644
index 000000000..f3d2b01c7
--- /dev/null
+++ b/api/app/Listeners/Forms/DeleteFormSubmissionFiles.php
@@ -0,0 +1,53 @@
+<?php
+
+namespace App\Listeners\Forms;
+
+use App\Events\Models\FormSubmissionDeleting;
+use App\Http\Controllers\Forms\PublicFormController;
+use Illuminate\Support\Facades\Storage;
+use Illuminate\Support\Str;
+
+class DeleteFormSubmissionFiles
+{
+ public function handle(FormSubmissionDeleting $event): void
+ {
+ $submission = $event->submission;
+ $data = $submission->data;
+
+ if (empty($data)) {
+ return;
+ }
+
+ $fileFieldIds = collect($submission->form->properties)
+ ->filter(function ($property) {
+ return in_array($property['type'], ['files', 'signature']) ||
+ ($property['type'] === 'url' && ($property['file_upload'] ?? false));
+ })
+ ->pluck('id')
+ ->all();
+
+ foreach ($data as $key => $value) {
+ if (!$value || !in_array($key, $fileFieldIds)) {
+ continue;
+ }
+
+ if (is_array($value)) {
+ foreach ($value as $file) {
+ if (is_string($file)) {
+ $this->deleteFile($submission->form->id, $file);
+ }
+ }
+ } elseif (is_string($value)) {
+ $this->deleteFile($submission->form->id, $value);
+ }
+ }
+ }
+
+ private function deleteFile(int|string $formId, string $fileName): void
+ {
+ $path = Str::of(PublicFormController::FILE_UPLOAD_PATH)->replace('?', $formId) . '/' . urldecode($fileName);
+ if (Storage::exists($path)) {
+ Storage::delete($path);
+ }
+ }
+}
diff --git a/api/app/Models/Forms/Form.php b/api/app/Models/Forms/Form.php
index 79acfeffd..bd63ae049 100644
--- a/api/app/Models/Forms/Form.php
+++ b/api/app/Models/Forms/Form.php
@@ -331,8 +331,12 @@ class Form extends Model implements CachableAttributes
public static function booted(): void
{
- static::deleted(function (Form $form) {
- $form->integrations()->delete();
+ static::deleting(function (Form $form) {
+ $form->integrations()
+ ->lazyById()
+ ->each(function (FormIntegration $integration): void {
+ $integration->delete();
+ });
});
}
}
diff --git a/api/app/Models/Forms/FormSubmission.php b/api/app/Models/Forms/FormSubmission.php
index 57613560e..c087a754b 100644
--- a/api/app/Models/Forms/FormSubmission.php
+++ b/api/app/Models/Forms/FormSubmission.php
@@ -2,6 +2,7 @@
namespace App\Models\Forms;
+use App\Events\Models\FormSubmissionDeleting;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
@@ -26,6 +27,15 @@ class FormSubmission extends Model
];
}
+ /**
+ * The event map for the model.
+ *
+ * @var array
+ */
+ protected $dispatchesEvents = [
+ 'deleting' => FormSubmissionDeleting::class,
+ ];
+
/**
* RelationShips
*/
diff --git a/api/app/Models/Integration/FormIntegration.php b/api/app/Models/Integration/FormIntegration.php
index 0e3f1f0b2..806e5dbf5 100644
--- a/api/app/Models/Integration/FormIntegration.php
+++ b/api/app/Models/Integration/FormIntegration.php
@@ -62,4 +62,11 @@ class FormIntegration extends Model
{
return self::getAllIntegrations()[$key] ?? null;
}
+
+ public static function booted(): void
+ {
+ static::deleting(function (FormIntegration $integration) {
+ $integration->events()->delete();
+ });
+ }
}
diff --git a/api/app/Providers/EventServiceProvider.php b/api/app/Providers/EventServiceProvider.php
index 7d0069530..01ed6f73d 100644
--- a/api/app/Providers/EventServiceProvider.php
+++ b/api/app/Providers/EventServiceProvider.php
@@ -5,6 +5,7 @@ namespace App\Providers;
use App\Events\Billing\SubscriptionCreated;
use App\Events\Billing\SubscriptionUpdated;
use App\Events\Forms\FormSubmitted;
+use App\Events\Models\FormSubmissionDeleting;
use App\Events\Forms\FormSaved;
use App\Events\Models\FormCreated;
use App\Events\Models\FormIntegrationCreated;
@@ -15,6 +16,7 @@ use App\Listeners\Forms\FormCreationConfirmation;
use App\Listeners\Forms\FormIntegrationCreatedHandler;
use App\Listeners\Forms\FormIntegrationsEventListener;
use App\Listeners\Forms\NotifyFormSubmission;
+use App\Listeners\Forms\DeleteFormSubmissionFiles;
use App\Listeners\Forms\FormSpamCheckListener;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
@@ -40,6 +42,9 @@ class EventServiceProvider extends ServiceProvider
FormSubmitted::class => [
NotifyFormSubmission::class
],
+ FormSubmissionDeleting::class => [
+ DeleteFormSubmissionFiles::class,
+ ],
FormIntegrationCreated::class => [
FormIntegrationCreatedHandler::class,
],