Upload beautify
This commit is contained in:
136
app/Services/Uploads/UploadStorageService.php
Normal file
136
app/Services/Uploads/UploadStorageService.php
Normal file
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\Uploads;
|
||||
|
||||
use App\DTOs\Uploads\UploadStoredFile;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Str;
|
||||
use RuntimeException;
|
||||
|
||||
final class UploadStorageService
|
||||
{
|
||||
public function sectionPath(string $section): string
|
||||
{
|
||||
$root = rtrim((string) config('uploads.storage_root'), DIRECTORY_SEPARATOR);
|
||||
$paths = (array) config('uploads.paths');
|
||||
|
||||
if (! array_key_exists($section, $paths)) {
|
||||
throw new RuntimeException('Unknown upload storage section: ' . $section);
|
||||
}
|
||||
|
||||
return $root . DIRECTORY_SEPARATOR . trim((string) $paths[$section], DIRECTORY_SEPARATOR);
|
||||
}
|
||||
|
||||
public function ensureSection(string $section): string
|
||||
{
|
||||
$path = $this->sectionPath($section);
|
||||
|
||||
if (! File::exists($path)) {
|
||||
File::makeDirectory($path, 0755, true);
|
||||
}
|
||||
|
||||
return $path;
|
||||
}
|
||||
|
||||
public function storeUploadedFile(UploadedFile $file, string $section): UploadStoredFile
|
||||
{
|
||||
$dir = $this->ensureSection($section);
|
||||
$extension = $this->safeExtension($file);
|
||||
$filename = Str::uuid()->toString() . ($extension !== '' ? '.' . $extension : '');
|
||||
|
||||
$file->move($dir, $filename);
|
||||
|
||||
$path = $dir . DIRECTORY_SEPARATOR . $filename;
|
||||
|
||||
return UploadStoredFile::fromPath($path);
|
||||
}
|
||||
|
||||
public function moveToSection(string $path, string $section): string
|
||||
{
|
||||
if (! is_file($path)) {
|
||||
throw new RuntimeException('Source file not found for move.');
|
||||
}
|
||||
|
||||
$dir = $this->ensureSection($section);
|
||||
$extension = (string) pathinfo($path, PATHINFO_EXTENSION);
|
||||
$filename = Str::uuid()->toString() . ($extension !== '' ? '.' . $extension : '');
|
||||
$target = $dir . DIRECTORY_SEPARATOR . $filename;
|
||||
|
||||
File::move($path, $target);
|
||||
|
||||
return $target;
|
||||
}
|
||||
|
||||
public function ensureHashDirectory(string $section, string $hash): string
|
||||
{
|
||||
$segments = $this->hashSegments($hash);
|
||||
$dir = $this->sectionPath($section) . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $segments);
|
||||
|
||||
if (! File::exists($dir)) {
|
||||
File::makeDirectory($dir, 0755, true);
|
||||
}
|
||||
|
||||
return $dir;
|
||||
}
|
||||
|
||||
public function publicHashDirectory(string $hash): string
|
||||
{
|
||||
$prefix = trim((string) config('uploads.public_img_prefix', 'img'), DIRECTORY_SEPARATOR);
|
||||
$base = $this->sectionPath('public') . DIRECTORY_SEPARATOR . $prefix;
|
||||
|
||||
if (! File::exists($base)) {
|
||||
File::makeDirectory($base, 0755, true);
|
||||
}
|
||||
|
||||
$segments = $this->hashSegments($hash);
|
||||
$dir = $base . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $segments);
|
||||
|
||||
if (! File::exists($dir)) {
|
||||
File::makeDirectory($dir, 0755, true);
|
||||
}
|
||||
|
||||
return $dir;
|
||||
}
|
||||
|
||||
public function publicRelativePath(string $hash, string $filename): string
|
||||
{
|
||||
$prefix = trim((string) config('uploads.public_img_prefix', 'img'), DIRECTORY_SEPARATOR);
|
||||
$segments = $this->hashSegments($hash);
|
||||
|
||||
return $prefix . '/' . implode('/', $segments) . '/' . ltrim($filename, '/');
|
||||
}
|
||||
|
||||
public function sectionRelativePath(string $section, string $hash, string $filename): string
|
||||
{
|
||||
$segments = $this->hashSegments($hash);
|
||||
$section = trim($section, DIRECTORY_SEPARATOR);
|
||||
|
||||
return $section . '/' . implode('/', $segments) . '/' . ltrim($filename, '/');
|
||||
}
|
||||
|
||||
private function safeExtension(UploadedFile $file): string
|
||||
{
|
||||
$extension = (string) $file->guessExtension();
|
||||
$extension = strtolower($extension);
|
||||
|
||||
return preg_match('/^[a-z0-9]+$/', $extension) ? $extension : '';
|
||||
}
|
||||
|
||||
private function hashSegments(string $hash): array
|
||||
{
|
||||
$hash = strtolower($hash);
|
||||
$hash = preg_replace('/[^a-z0-9]/', '', $hash) ?? '';
|
||||
$hash = str_pad($hash, 6, '0');
|
||||
|
||||
$segments = [
|
||||
substr($hash, 0, 2),
|
||||
substr($hash, 2, 2),
|
||||
substr($hash, 4, 2),
|
||||
];
|
||||
|
||||
return array_map(static fn (string $part): string => $part === '' ? '00' : $part, $segments);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user