File manager - Edit - /var/www/html/portalHomolog/app/Http/Resources/ParqueTecnologico/ParqueTecResource.php
Back
<?php namespace App\Http\Resources\ParqueTecnologico; use App\Http\Controllers\Controller; use App\Models\ParqueTecnologico\DecretoLei; use App\Models\ParqueTecnologico\Edital; use App\Models\ParqueTecnologico\Startup; use Illuminate\Http\Request; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; class ParqueTecResource extends Controller { /** * Gera slug único para Editais. */ public static function generateUniqueSlugEdital(string $seed): string { $base = trim($seed) !== '' ? Str::slug(mb_strtolower($seed)) : 'edital'; $slug = $base; $i = 2; while (Edital::where('slug', $slug)->exists()) { $slug = $base . '-' . $i; $i++; } return $slug; } /** * Gera slug único para Decretos/Leis. */ public static function generateUniqueSlugDecretoLei(string $seed): string { $base = trim($seed) !== '' ? Str::slug(mb_strtolower($seed)) : 'decreto-lei'; $slug = $base; $i = 2; while (DecretoLei::where('slug', $slug)->exists()) { $slug = $base . '-' . $i; $i++; } return $slug; } /** * Retorna path da foto para Edital (upload ou mantém atual). */ public static function getFotoPathEdital(Request $request, ?Edital $edital = null): ?string { if ($request->hasFile('foto') && $request->file('foto')->isValid()) { $file = $request->file('foto'); $path = \App\Helpers\FileUploadHelper::processSafeImageUpload($file, self::EDITAIS_PTMAR_IMAGENS); if ($path) { return $path; } } return $edital?->foto; } /** Pasta no storage: imagens (fotos) dos editais. Link: pmar/assets/img/editais-ptmar-imagens */ public const EDITAIS_PTMAR_IMAGENS = 'editais-ptmar-imagens'; /** Pasta no storage: documentos (PDF) dos editais. Link: pmar/assets/files/editais-ptmar-documentos */ public const EDITAIS_PTMAR_DOCUMENTOS = 'editais-ptmar-documentos'; /** Pasta no storage: documentos (PDF) dos decretos e leis. Link: pmar/assets/files/decretos-leis-ptmar */ public const DECRETOS_LEIS_PTMAR_FOLDER = 'decretos-leis-ptmar'; /** Pasta no storage: logos das startups. Link: pmar/assets/img/startups-ptmar-logos */ public const STARTUPS_PTMAR_LOGOS = 'startups-ptmar-logos'; /** Pasta no storage: imagens de fundo das startups. Link: pmar/assets/img/startups-ptmar-backgrounds */ public const STARTUPS_PTMAR_BACKGROUNDS = 'startups-ptmar-backgrounds'; /** * Retorna path do arquivo para Edital (upload em storage/app/public/editais-ptmar-documentos, link público). */ public static function getArquivoPathEdital(Request $request, ?Edital $edital = null): ?string { if ($request->hasFile('arquivo') && $request->file('arquivo')->isValid()) { $file = $request->file('arquivo'); $path = \App\Helpers\FileUploadHelper::processSafeDocumentUploadStorageLink($file, self::EDITAIS_PTMAR_DOCUMENTOS); if ($path) { return $path; } } return $edital?->arquivo; } /** * Retorna path do arquivo para DecretoLei (upload em storage/app/public/decretos-leis-ptmar, link público). */ public static function getArquivoPathDecretoLei(Request $request, ?DecretoLei $decretoLei = null): ?string { if ($request->hasFile('arquivo') && $request->file('arquivo')->isValid()) { $file = $request->file('arquivo'); $path = \App\Helpers\FileUploadHelper::processSafeDocumentUploadStorageLink($file, self::DECRETOS_LEIS_PTMAR_FOLDER); if ($path) { return $path; } } return $decretoLei?->arquivo; } /** * Gera slug único para Startups. */ public static function generateUniqueSlugStartup(string $seed): string { $base = trim($seed) !== '' ? Str::slug(mb_strtolower($seed)) : 'startup'; $slug = $base; $i = 2; while (Startup::where('slug', $slug)->exists()) { $slug = $base . '-' . $i; $i++; } return $slug; } /** * Retorna path do logo da Startup (upload ou mantém atual). */ public static function getLogoPathStartup(Request $request, ?Startup $startup = null): ?string { if ($request->hasFile('logo') && $request->file('logo')->isValid()) { $file = $request->file('logo'); $path = \App\Helpers\FileUploadHelper::processSafeImageUpload($file, self::STARTUPS_PTMAR_LOGOS); if ($path) { // Normaliza para armazenar somente o path relativo ao storage público (ex.: startups-ptmar-logos/arquivo.png) $normalized = str_replace('\\', '/', $path); $prefix = 'pmar/assets/img/'; if (str_starts_with($normalized, $prefix)) { $normalized = substr($normalized, strlen($prefix)); } return ltrim($normalized, '/'); } } return $startup?->logo; } /** * Retorna path do background da Startup (upload ou mantém atual). */ public static function getBackgroundPathStartup(Request $request, ?Startup $startup = null): ?string { if ($request->hasFile('background') && $request->file('background')->isValid()) { $file = $request->file('background'); $path = \App\Helpers\FileUploadHelper::processSafeImageUpload($file, self::STARTUPS_PTMAR_BACKGROUNDS); if ($path) { $normalized = str_replace('\\', '/', $path); $prefix = 'pmar/assets/img/'; if (str_starts_with($normalized, $prefix)) { $normalized = substr($normalized, strlen($prefix)); } return ltrim($normalized, '/'); } } return $startup?->background; } /** * Remove do storage o arquivo de foto do edital (path: editais-ptmar-imagens/... ou legado parque_tec_editais/...). */ public static function deleteFotoEditalStorage(?string $fotoPath): void { if (empty($fotoPath)) { return; } $path = str_replace(['\\', '/'], '/', $fotoPath); foreach ([self::EDITAIS_PTMAR_IMAGENS, 'parque_tec_editais'] as $folder) { if (str_contains($path, $folder . '/') || str_contains($path, $folder . '\\')) { Storage::disk('public')->delete($folder . '/' . basename($fotoPath)); return; } } } /** * Remove do storage o arquivo do edital (path: editais-ptmar-documentos/... ou legado editais-ptmar/...). */ public static function deleteArquivoEditalStorage(?string $arquivoPath): void { if (empty($arquivoPath)) { return; } $path = str_replace('\\', '/', $arquivoPath); if (str_starts_with($path, self::EDITAIS_PTMAR_DOCUMENTOS . '/') || str_starts_with($path, 'editais-ptmar/')) { Storage::disk('public')->delete($arquivoPath); } } /** * Remove do storage o arquivo do decreto/lei (path: decretos-leis-ptmar/...). */ public static function deleteArquivoDecretoLeiStorage(?string $arquivoPath): void { if (empty($arquivoPath)) { return; } if (str_starts_with($arquivoPath, self::DECRETOS_LEIS_PTMAR_FOLDER . '/') || str_starts_with(str_replace('\\', '/', $arquivoPath), self::DECRETOS_LEIS_PTMAR_FOLDER . '/')) { Storage::disk('public')->delete($arquivoPath); } } /** * Remove do storage o logo da startup. */ public static function deleteLogoStartupStorage(?string $logoPath): void { if (empty($logoPath)) { return; } $path = str_replace(['\\', '/'], '/', $logoPath); if (str_contains($path, self::STARTUPS_PTMAR_LOGOS . '/')) { Storage::disk('public')->delete(self::STARTUPS_PTMAR_LOGOS . '/' . basename($logoPath)); } } /** * Remove do storage o background da startup. */ public static function deleteBackgroundStartupStorage(?string $backgroundPath): void { if (empty($backgroundPath)) { return; } $path = str_replace(['\\', '/'], '/', $backgroundPath); if (str_contains($path, self::STARTUPS_PTMAR_BACKGROUNDS . '/')) { Storage::disk('public')->delete(self::STARTUPS_PTMAR_BACKGROUNDS . '/' . basename($backgroundPath)); } } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.02 |
proxy
|
phpinfo
|
Settings