Fix form errors during creation of product remove all product images

This commit is contained in:
Mateusz 2025-10-09 15:08:10 +02:00
parent f8a27841f2
commit 40f31b4a22
4 changed files with 149 additions and 0 deletions

View file

@ -42,6 +42,8 @@
{{ form_row(image_form.position, sylius_test_form_attribute('position')) }}
</div>
{% endif %}
{{ form_widget(image_form.cachedFilePath) }}
{{ form_widget(image_form.cachedFileName) }}
</div>
</div>
{% endfor %}

View file

@ -0,0 +1,80 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Sylius\Bundle\CoreBundle\Console\Command;
use Sylius\Bundle\CoreBundle\Form\Type\ImageType;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
#[AsCommand(
name: 'sylius:cleanup-cached-uploads',
description: 'Removes old cached upload files from temporary storage.',
)]
final class CleanupCachedUploadsCommand extends Command
{
protected function configure(): void
{
$this
->addOption(
'max-age',
null,
InputOption::VALUE_OPTIONAL,
'Maximum age of cached files in seconds',
86400,
)
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$maxAge = (int) $input->getOption('max-age');
$cachedUploadsDir = ImageType::getCachedUploadsDirPath();
if (!is_dir($cachedUploadsDir)) {
$output->writeln('<info>No cached uploads directory found. Nothing to clean up.</info>');
return Command::SUCCESS;
}
$now = time();
$deletedCount = 0;
$failedCount = 0;
$files = new \FilesystemIterator($cachedUploadsDir);
foreach ($files as $file) {
if (!$file instanceof \SplFileInfo || !$file->isFile() || ($now - $file->getMTime()) <= $maxAge) {
continue;
}
if (@unlink($file->getPathname())) {
++$deletedCount;
} else {
++$failedCount;
$output->writeln(sprintf('<comment>Could not delete: %s</comment>', $file->getPathname()));
}
}
$output->writeln(sprintf(
'<info>Cleanup complete. Removed %d cached file(s) older than %d seconds.</info>',
$deletedCount,
$maxAge,
));
return $failedCount > 0 ? Command::FAILURE : Command::SUCCESS;
}
}

View file

@ -15,11 +15,17 @@ namespace Sylius\Bundle\CoreBundle\Form\Type;
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\HttpFoundation\File\UploadedFile;
abstract class ImageType extends AbstractResourceType
{
private const CACHED_UPLOADS_SUBDIR = 'sylius_cached_uploads';
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
@ -30,11 +36,68 @@ abstract class ImageType extends AbstractResourceType
->add('file', FileType::class, [
'label' => 'sylius.form.image.file',
])
->add('cachedFilePath', HiddenType::class, [
'mapped' => false,
'required' => false,
])
->add('cachedFileName', HiddenType::class, [
'mapped' => false,
'required' => false,
])
;
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event): void {
$data = $event->getData();
if (isset($data['file']) && $data['file'] instanceof UploadedFile) {
$uploadedFile = $data['file'];
$tempDir = $this->getCachedUploadsDir();
if (!is_dir($tempDir)) {
mkdir($tempDir, 0755, true);
}
$cachedFileName = uniqid('cached_', true) . '_' . $uploadedFile->getClientOriginalName();
copy($uploadedFile->getPathname(), $tempDir . '/' . $cachedFileName);
$data['cachedFilePath'] = $cachedFileName;
$data['cachedFileName'] = $uploadedFile->getClientOriginalName();
$event->setData($data);
return;
}
if (empty($data['file']) && !empty($data['cachedFilePath']) && !empty($data['cachedFileName'])) {
$tempDir = $this->getCachedUploadsDir();
$cachedFilePath = $tempDir . '/' . basename($data['cachedFilePath']);
if (file_exists($cachedFilePath)) {
$data['file'] = new UploadedFile(
$cachedFilePath,
$data['cachedFileName'],
null,
null,
true,
);
$event->setData($data);
}
}
});
}
public function getBlockPrefix(): string
{
return 'sylius_image';
}
public static function getCachedUploadsDirPath(): string
{
return sys_get_temp_dir() . '/' . self::CACHED_UPLOADS_SUBDIR;
}
private function getCachedUploadsDir(): string
{
return self::getCachedUploadsDirPath();
}
}

View file

@ -84,5 +84,9 @@
<service id="sylius.console.command.show_plus_info" class="Sylius\Bundle\CoreBundle\Console\Command\ShowPlusInfoCommand">
<tag name="console.command" />
</service>
<service id="sylius.console.command.cleanup_cached_uploads" class="Sylius\Bundle\CoreBundle\Console\Command\CleanupCachedUploadsCommand">
<tag name="console.command" />
</service>
</services>
</container>