|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* For licensing terms, see /license.txt */ |
|
6
|
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\Component\VichUploader; |
|
8
|
|
|
|
|
9
|
|
|
use Chamilo\CoreBundle\Entity\Asset; |
|
10
|
|
|
use Chamilo\CoreBundle\Repository\AssetRepository; |
|
11
|
|
|
use InvalidArgumentException; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
13
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
|
14
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
|
15
|
|
|
use Vich\UploaderBundle\Mapping\PropertyMapping; |
|
16
|
|
|
use Vich\UploaderBundle\Naming\ConfigurableInterface; |
|
17
|
|
|
use Vich\UploaderBundle\Naming\DirectoryNamerInterface; |
|
18
|
|
|
|
|
19
|
|
|
class AssetDirectoryNamer implements DirectoryNamerInterface, ConfigurableInterface |
|
20
|
|
|
{ |
|
21
|
|
|
protected PropertyAccessorInterface $propertyAccessor; |
|
22
|
|
|
private ?AssetRepository $assetRepository = null; |
|
23
|
|
|
private ?RequestStack $requestStack = null; |
|
24
|
|
|
private string $propertyPath; |
|
25
|
|
|
private int $charsPerDir = 2; |
|
26
|
|
|
private int $dirs = 1; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct(?PropertyAccessorInterface $propertyAccessor, ?AssetRepository $assetRepository = null, ?RequestStack $requestStack = null) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor(); |
|
31
|
|
|
$this->assetRepository = $assetRepository; |
|
32
|
|
|
$this->requestStack = $requestStack; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param array $options Options for this namer. The following options are accepted: |
|
37
|
|
|
* - chars_per_dir: how many chars use for each dir. |
|
38
|
|
|
* - dirs: how many dirs create |
|
39
|
|
|
*/ |
|
40
|
|
|
public function configure(array $options): void |
|
41
|
|
|
{ |
|
42
|
|
|
if (empty($options['property'])) { |
|
43
|
|
|
throw new InvalidArgumentException('Option "property" is missing or empty.'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$this->propertyPath = $options['property']; |
|
47
|
|
|
|
|
48
|
|
|
$options = array_merge([ |
|
49
|
|
|
'chars_per_dir' => $this->charsPerDir, |
|
50
|
|
|
'dirs' => $this->dirs, |
|
51
|
|
|
], $options); |
|
52
|
|
|
|
|
53
|
|
|
$this->charsPerDir = $options['chars_per_dir']; |
|
54
|
|
|
$this->dirs = $options['dirs']; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function directoryName($object, PropertyMapping $mapping): string |
|
58
|
|
|
{ |
|
59
|
|
|
$fileName = $mapping->getFileName($object); |
|
60
|
|
|
$category = $this->propertyAccessor->getValue($object, $this->propertyPath); |
|
61
|
|
|
|
|
62
|
|
|
if (Asset::SYSTEM_TEMPLATE === $object->getCategory()) { |
|
63
|
|
|
return 'system_templates'; |
|
64
|
|
|
} |
|
65
|
|
|
if (Asset::TEMPLATE === $object->getCategory()) { |
|
66
|
|
|
return 'doc_templates'; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$parts[] = $category; |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
if (Asset::EXTRA_FIELD === $category) { |
|
72
|
|
|
for ($i = 0, $start = 0; $i < $this->dirs; $i++, $start += $this->charsPerDir) { |
|
73
|
|
|
$parts[] = substr($fileName, $start, $this->charsPerDir); |
|
74
|
|
|
} |
|
75
|
|
|
} else { |
|
76
|
|
|
$parts[] = $fileName; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return implode('/', $parts); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|