System template: Improve asset file naming and directory structure - refs #3804

pull/5203/head
christianbeeznst 9 months ago
parent 21ed7a7290
commit e3bcf4c8b3
  1. 2
      config/packages/vich_uploader.yaml
  2. 14
      src/CoreBundle/Component/Utils/AssetDirectoryNamer.php
  3. 48
      src/CoreBundle/Component/Utils/AssetFileNameNamer.php
  4. 14
      src/CoreBundle/Resources/config/services.yml

@ -16,7 +16,7 @@ vich_uploader:
assets:
uri_prefix: ''
upload_destination: oneup_flysystem.asset_filesystem
namer: Vich\UploaderBundle\Naming\SmartUniqueNamer
namer: Chamilo\CoreBundle\Component\Utils\AssetFileNameNamer
directory_namer:
service: Chamilo\CoreBundle\Component\Utils\AssetDirectoryNamer
options: {property: 'category'}

@ -7,7 +7,9 @@ declare(strict_types=1);
namespace Chamilo\CoreBundle\Component\Utils;
use Chamilo\CoreBundle\Entity\Asset;
use Chamilo\CoreBundle\Repository\AssetRepository;
use InvalidArgumentException;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Vich\UploaderBundle\Mapping\PropertyMapping;
@ -17,13 +19,17 @@ use Vich\UploaderBundle\Naming\DirectoryNamerInterface;
class AssetDirectoryNamer implements DirectoryNamerInterface, ConfigurableInterface
{
protected PropertyAccessorInterface $propertyAccessor;
private ?AssetRepository $assetRepository = null;
private ?RequestStack $requestStack = null;
private string $propertyPath;
private int $charsPerDir = 2;
private int $dirs = 1;
public function __construct(?PropertyAccessorInterface $propertyAccessor)
public function __construct(?PropertyAccessorInterface $propertyAccessor, ?AssetRepository $assetRepository = null, ?RequestStack $requestStack= null)
{
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
$this->assetRepository = $assetRepository;
$this->requestStack = $requestStack;
}
/**
@ -53,6 +59,12 @@ class AssetDirectoryNamer implements DirectoryNamerInterface, ConfigurableInterf
$fileName = $mapping->getFileName($object);
$category = $this->propertyAccessor->getValue($object, $this->propertyPath);
if ($object->getCategory() === Asset::SYSTEM_TEMPLATE) {
return 'system_templates';
} elseif ($object->getCategory() === Asset::TEMPLATE) {
return 'doc_templates';
}
$parts[] = $category;
if (Asset::EXTRA_FIELD === $category) {

@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Component\Utils;
use Chamilo\CoreBundle\Entity\Asset;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Contracts\Translation\TranslatorInterface;
use Vich\UploaderBundle\Mapping\PropertyMapping;
use Vich\UploaderBundle\Naming\NamerInterface;
class AssetFileNameNamer implements NamerInterface
{
private RequestStack $requestStack;
private TranslatorInterface $translator;
public function __construct(RequestStack $requestStack, TranslatorInterface $translator)
{
$this->requestStack = $requestStack;
$this->translator = $translator;
}
public function name($object, PropertyMapping $mapping): string
{
$category = $object->getCategory();
if (in_array($category, [Asset::TEMPLATE, Asset::SYSTEM_TEMPLATE])) {
$request = $this->requestStack->getCurrentRequest();
if ($request) {
$templateId = $object->getId();
$templateTitle = $request->get('title', 'default-title');
$titleSlug = $this->slugify($templateTitle);
$extension = pathinfo($mapping->getFileName($object), PATHINFO_EXTENSION);
return sprintf('%s-%s.%s', $templateId, $titleSlug, $extension);
}
}
return $mapping->getFileName($object);
}
private function slugify(string $text): string
{
return strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $text), '-'));
}
}

@ -45,7 +45,19 @@ services:
Chamilo\CoreBundle\Component\Editor\CkEditor\CkEditor:
arguments: ['@translator', '@router']
Chamilo\CoreBundle\Component\Utils\AssetDirectoryNamer: ~
Chamilo\CoreBundle\Component\Utils\AssetDirectoryNamer:
arguments:
- '@property_accessor'
- '@Chamilo\CoreBundle\Repository\AssetRepository'
- '@request_stack'
Chamilo\CoreBundle\Component\Utils\AssetFileNameNamer:
arguments:
- '@request_stack'
- '@translator'
tags:
- { name: vich_uploader.namer, mapping: assets }
Chamilo\CoreBundle\Component\Utils\CreateDefaultPages: ~
Chamilo\CoreBundle\Component\Utils\NameConvention: ~
Chamilo\CoreBundle\Component\Utils\Glide:

Loading…
Cancel
Save