Separated laravel app to its own folder (#540)

This commit is contained in:
Julien Nahum
2024-08-26 18:24:56 +02:00
committed by GitHub
parent 39b8df5eed
commit 5bd1dda504
546 changed files with 124 additions and 143 deletions

View File

@@ -0,0 +1,54 @@
<?php
namespace App\Service\Storage;
class S3KeyCleaner
{
public static function sanitize($objectKey, $separator = '-')
{
return (new self())->sanitizeS3Key($objectKey, $separator);
}
public function replaceLatinCharacters($value)
{
// Load the latin map
$latinMap = json_decode(\File::get(resource_path('data/latin-map.json')), true);
$result = '';
$length = mb_strlen($value);
for ($i = 0; $i < $length; $i++) {
$char = mb_substr($value, $i, 1);
$result .= array_key_exists($char, $latinMap) ? $latinMap[$char] : $char;
}
return $result;
}
private function removeIllegalCharacters($value)
{
$SAFE_CHARACTERS = '/[^0-9a-zA-Z! _\\.\\*\'\\(\\)\\-\\/]/';
return preg_replace($SAFE_CHARACTERS, '', $value);
}
private function isValidSeparator($separator)
{
$SAFE_CHARACTERS = '/[^0-9a-zA-Z! _\\.\\*\'\\(\\)\\-\\/]/';
return $separator && !preg_match($SAFE_CHARACTERS, $separator);
}
public function sanitizeS3Key($objectKey, $separator = '-')
{
if (!$this->isValidSeparator($separator)) {
throw new \Exception("${separator} is not a valid separator");
}
if (!$objectKey || (!is_string($objectKey) && !is_numeric($objectKey))) {
throw new \Exception("Expected non-empty string or number, got ${objectKey}");
}
if (is_numeric($objectKey)) {
return strval($objectKey);
}
return str_replace(' ', $separator, $this->removeIllegalCharacters($this->replaceLatinCharacters(trim($objectKey))));
}
}

View File

@@ -0,0 +1,76 @@
<?php
namespace App\Service\Storage;
use Illuminate\Support\Str;
/**
* Used
* File can have 2 formats:
* - file_name-{uuid}.{ext}
* - {uuid}
*/
class StorageFileNameParser
{
public ?string $uuid = null;
public ?string $fileName = null;
public ?string $extension = null;
public function __construct(string $fileName)
{
$this->parseFileName($fileName);
}
/**
* If we have parsed a file name and an extension, we keep the same and append uuid to avoid collisions
* Otherwise we just return the uuid
*/
public function getMovedFileName(): ?string
{
if ($this->fileName && $this->extension) {
$fileName = substr($this->fileName, 0, 50) . '_' . $this->uuid . '.' . $this->extension;
$fileName = preg_replace('#\p{C}+#u', '', $fileName); // avoid CorruptedPathDetected exceptions
return S3KeyCleaner::sanitize($fileName);
}
return $this->uuid;
}
private function parseFileName(string $fileName)
{
if (Str::isUuid($fileName)) {
$this->uuid = $fileName;
return;
}
if (!str_contains($fileName, '_')) {
return;
}
$candidateString = substr($fileName, strrpos($fileName, '_') + 1);
if (
!str_contains($candidateString, '.')
|| !Str::isUuid(substr($candidateString, 0, strpos($candidateString, '.')))
) {
return;
}
try {
$this->uuid = substr($candidateString, 0, strpos($candidateString, '.'));
$this->fileName = substr($fileName, 0, strrpos($fileName, '_'));
// get everything after the last dot
$this->extension = substr($candidateString, strrpos($candidateString, '.') + 1);
} catch (\Exception $e) {
return;
}
}
public static function parse(string $fileName): self
{
return new self($fileName);
}
}