File manager - Edit - /var/www/html/portal/app/Http/Services/Observatorio/ObservatorioUploadHelper.php
Back
<?php namespace App\Http\Services\Observatorio; use App\Helpers\FileUploadHelper; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Storage; use Illuminate\Validation\ValidationException; /** * Upload e remoção de ícones dos temas do Observatório. * * Arquivos ficam em storage/app/public (disco public) e o banco * armazena o caminho relativo (ex.: observatorio/temas/arquivo.png). */ class ObservatorioUploadHelper { public const FOLDER_TEMA = 'observatorio/temas'; public const FOLDER_MAPA_TEMA = 'observatorio/mapa-temas'; private const LEGACY_PREFIX = 'pmar/assets/img/'; private const ALLOWED_EXTENSIONS = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'svg']; private const ALLOWED_MIMES = [ 'image/jpeg', 'image/png', 'image/gif', 'image/webp', 'image/bmp', 'image/x-ms-bmp', 'image/svg+xml', ]; /** * Processa upload opcional de ícone e grava o path relativo em $data['icone']. * * @param array<string, mixed> $data * * @throws ValidationException Quando o arquivo foi enviado mas não pôde ser salvo. */ public static function applyIconeUpload(array &$data, ?UploadedFile $file, ?string $currentPath, string $folder): void { if ($file === null) { return; } if (!$file->isValid()) { throw ValidationException::withMessages([ 'icone_arquivo' => __('Não foi possível enviar o ícone. Verifique se o arquivo não ultrapassa 2 MB.'), ]); } $newPath = self::storeIcone($file, $folder); if ($newPath === null || $newPath === '') { throw ValidationException::withMessages([ 'icone_arquivo' => __('Formato de ícone inválido. Use JPG, PNG, GIF, WEBP, BMP ou SVG.'), ]); } self::deleteIcone($currentPath); $data['icone'] = $newPath; } public static function storeIcone(?UploadedFile $file, string $folder): ?string { if (!$file || !$file->isValid()) { return null; } if (!FileUploadHelper::validateFile($file, self::ALLOWED_EXTENSIONS, self::ALLOWED_MIMES)) { return null; } try { $fileName = FileUploadHelper::generateSafeFileName($file, self::ALLOWED_EXTENSIONS); $dir = trim(str_replace('\\', '/', $folder), '/'); $path = Storage::disk('public')->putFileAs($dir, $file, $fileName); return $path ?: null; } catch (\Throwable $e) { Log::error('Erro ao enviar ícone do observatório: ' . $e->getMessage(), [ 'folder' => $folder, 'extension' => $file->getClientOriginalExtension(), 'size' => $file->getSize(), ]); return null; } } public static function deleteIcone(?string $path): void { $relative = self::normalizeStoragePath($path); if ($relative === null) { return; } if (Storage::disk('public')->exists($relative)) { Storage::disk('public')->delete($relative); } } public static function publicUrl(?string $icone): ?string { if ($icone === null || $icone === '') { return null; } if (str_starts_with($icone, 'http://') || str_starts_with($icone, 'https://')) { return $icone; } $relative = self::normalizeStoragePath($icone); if ($relative === null) { return null; } return url('storage/' . $relative); } /** * Converte caminhos legados (pmar/assets/img/...) para o path relativo do disco public. */ public static function normalizeStoragePath(?string $path): ?string { if ($path === null || $path === '') { return null; } $normalized = str_replace('\\', '/', trim($path)); if (str_starts_with($normalized, 'storage/')) { $normalized = substr($normalized, strlen('storage/')); } if (str_starts_with($normalized, self::LEGACY_PREFIX)) { $normalized = substr($normalized, strlen(self::LEGACY_PREFIX)); } return ltrim($normalized, '/'); } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.01 |
proxy
|
phpinfo
|
Settings