-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathFlysystem.php
More file actions
55 lines (40 loc) · 1.8 KB
/
Flysystem.php
File metadata and controls
55 lines (40 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);
namespace Ibexa\Core\IO\FilePathNormalizer;
use const DIRECTORY_SEPARATOR;
use Ibexa\Core\IO\FilePathNormalizerInterface;
use Ibexa\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter;
use League\Flysystem\PathNormalizer;
final class Flysystem implements FilePathNormalizerInterface
{
private const HASH_PATTERN = '/^[0-9a-f]{12}-/';
private SlugConverter $slugConverter;
private PathNormalizer $pathNormalizer;
public function __construct(SlugConverter $slugConverter, PathNormalizer $pathNormalizer)
{
$this->slugConverter = $slugConverter;
$this->pathNormalizer = $pathNormalizer;
}
public function normalizePath(string $filePath, bool $doHash = true, ?string $realFilePath = null): string
{
$fileName = pathinfo($filePath, PATHINFO_BASENAME);
$directory = pathinfo($filePath, PATHINFO_DIRNAME);
$fileName = $this->slugConverter->convert($fileName, '_1', 'urlalias');
$hash = $doHash && !preg_match(self::HASH_PATTERN, $fileName)
? $this->generateFilePathHash($realFilePath)
: '';
$filePath = $directory . DIRECTORY_SEPARATOR . $hash;
$normalizedFileName = $this->pathNormalizer->normalizePath($fileName);
return $filePath . $normalizedFileName;
}
private function generateFilePathHash(?string $realFilePath = null): string
{
$hash = $realFilePath !== null ? md5_file($realFilePath) : false;
return ($hash !== false ? substr($hash, 0, 12) : bin2hex(random_bytes(6))) . '-';
}
}
class_alias(Flysystem::class, 'eZ\Publish\Core\IO\FilePathNormalizer\Flysystem');