Prevent image saving with FileUpload (v2)

Hi!

I have a FileUpload component in an action form with multiple(). When I submit the modal form I want to save only some of the files sent. I've tried to remove some from $data, but it doesn't work. Any idea?

Tables\Actions\Action::make('import_images')
    ->button()
    ->color('warning')
    ->form([
        Forms\Components\FileUpload::make('files')
            ->disk('private')
            ->directory('applicant_pictures')
            ->image()
            ->imageResizeMode('cover')
            ->imageResizeTargetWidth(768)
            ->imageResizeTargetHeight(1024)
            ->imageCropAspectRatio('3:4')
            ->imagePreviewHeight('40px')
            ->disablePreview()
            ->maxFiles(10)
            ->getUploadedFileNameForStorageUsing(function (TemporaryUploadedFile $file): string {
                $preffix = 'ap_';
                return (string) str($file->hashName())->prepend($preffix);
            })
            ->visibility('private')
            ->multiple(true)
            ->storeFileNamesIn('original_file_names')
            ->panelLayout('grid'),
        ])
        ->action(function ($data) {
            $fileIndex = 0;
            foreach ($data['original_file_names'] as $filepath => $originalName) {
                $dni = Str::upper(pathinfo($originalName, PATHINFO_FILENAME));
                $applicant = Applicant::where('dni', $dni)->first();
                $pictures = [];
                if ($applicant) {
                    $pictures[] = [
                        'path' => $filepath,
                    ];
                } else {
                    unset($data['original_file_names'][$filepath]);
                    unset($data['files'][$fileIndex]);
                }

                $fileIndex += 1;

                if (count($pictures) > 0) {
                    $applicant->pictures()->createMany($pictures);
                }
            }
        }),
Solution
I've solved using saveUploadedFilesUsing() . Example:

->saveUploadedFileUsing(function (TemporaryUploadedFile $file, $component) {
    $originalName = Str::upper(pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME));
    $applicant = Applicant::whereRaw('UPPER(dni) = ?', [$originalName])->first();

    if (empty($applicant)) {
        try {
            $file->delete();
        } catch (Exception $e) {
            return null;
        }

        return null;
    } else {
        if ($component->shouldMoveFile() && $component->getDiskName() == $file->disk) {
            $newPath = trim($component->getDirectory() . '/' . $component->getUploadedFileNameForStorage($file), '/');
            $component->getDisk()->move($file->path(), $newPath);

            return $newPath;
        }

        $storeMethod = $component->getVisibility() === 'public' ? 'storePubliclyAs' : 'storeAs';
        return $file->{$storeMethod}(
            $component->getDirectory(),
            $component->getUploadedFileNameForStorage($file),
            $component->getDiskName(),
        );
    }
})


The code inside else block has been taken from BaseFileUpload class.
Was this page helpful?