Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Forms/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Storage;
use Statamic\Contracts\Forms\Submission;
use Statamic\Facades\Antlers;
use Statamic\Facades\Blueprint;
Expand Down Expand Up @@ -161,7 +162,9 @@ private function attachFiles($field)
: config('statamic.system.file_uploads_path', 'statamic/file-uploads');

foreach ($value as $file) {
$this->attachFromStorageDisk($disk, $basePath.'/'.$file);
if (Storage::disk($disk)->exists($path = "{$basePath}/{$file}")) {
$this->attachFromStorageDisk($disk, $path);
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/Forms/Submission.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@ public function delete()
$withEvents = $this->withEvents;
$this->withEvents = true;

if ($withEvents) {
DeleteTemporaryFiles::dispatchSync($this);
}

FormSubmission::delete($this);

if ($withEvents) {
Expand Down
8 changes: 1 addition & 7 deletions src/Jobs/DeletePartialFormSubmissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Statamic\Contracts\Forms\Submission;
use Statamic\Facades\FormSubmission;
use Statamic\Forms\DeleteTemporaryFiles;

class DeletePartialFormSubmissions implements ShouldQueue
{
Expand All @@ -26,10 +24,6 @@ public function handle(): void
->where('partial', true)
->where('date', '<', $threshold)
->get()
->each(function (Submission $submission): void {
DeleteTemporaryFiles::dispatchSync($submission);

$submission->delete();
});
->each->delete();
}
}
18 changes: 18 additions & 0 deletions tests/Forms/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,24 @@ public function it_attaches_files_from_files_field()
$this->assertTrue($email->hasAttachmentFromStorageDisk('local', 'statamic/file-uploads/'.$documentPath));
}

#[Test]
public function it_skips_attachments_whose_temporary_files_no_longer_exist()
{
Storage::fake('local');

$form = tap(Form::make('test')->formFields([
'fields' => [
['handle' => 'document', 'field' => ['type' => 'files', 'max_files' => 1]],
],
]))->save();

$submission = $form->makeSubmission()->data(['document' => now()->timestamp.'/resume.pdf']);

$email = tap(new Email($submission, ['to' => 'test@test.com', 'attachments' => true], Site::default()))->build();

$this->assertEmpty($email->attachments);
}

#[Test]
public function it_attaches_files_from_files_field_on_the_configured_disk_and_path()
{
Expand Down
27 changes: 27 additions & 0 deletions tests/Forms/SubmissionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Statamic\Facades\Form;
use Statamic\Facades\Site;
use Statamic\Forms\CreateAssetsFromFileUploads;
use Statamic\Forms\DeleteTemporaryFiles;
use Statamic\Forms\SendEmails;
use Tests\PreventSavingStacheItemsToDisk;
use Tests\TestCase;
Expand Down Expand Up @@ -272,6 +273,32 @@ public function it_deletes_quietly()
$this->assertTrue($return);
}

#[Test]
public function deleting_dispatches_delete_temporary_files()
{
Bus::fake();

$form = tap(Form::make('contact_us'))->save();
$submission = tap($form->makeSubmission())->save();

$submission->delete();

Bus::assertDispatchedSync(DeleteTemporaryFiles::class);
}

#[Test]
public function deleting_quietly_does_not_dispatch_delete_temporary_files()
{
Bus::fake();

$form = tap(Form::make('contact_us'))->save();
$submission = tap($form->makeSubmission())->save();

$submission->deleteQuietly();

Bus::assertNotDispatched(DeleteTemporaryFiles::class);
}

#[Test]
public function it_determines_its_status()
{
Expand Down
Loading