RemoveArchiveS3.php 883 B

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. namespace App\Traits;
  3. use Exception;
  4. use Illuminate\Support\Facades\Storage;
  5. trait RemoveArchiveS3
  6. {
  7. /**
  8. * Delete a file and return a boolean if succeded.
  9. *
  10. * @throws \Exception If the file as not found or if the deletion failed.
  11. */
  12. public function removeArchiveByUrl(string $url): bool
  13. {
  14. // Extrair o caminho relativo da URL completa
  15. $imagePath = parse_url($url, PHP_URL_PATH);
  16. $imagePath = ltrim($imagePath, '/'); // remover a barra inicial
  17. // Excluir a imagem antiga
  18. if (Storage::exists($imagePath)) {
  19. $success = Storage::delete($imagePath);
  20. if ($success) {
  21. return true;
  22. } else {
  23. throw new Exception('Deletion was not possible');
  24. }
  25. } else {
  26. throw new Exception('File not found');
  27. }
  28. }
  29. }