RemoveArchiveS3.php 929 B

1234567891011121314151617181920212223242526272829303132333435
  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. * @param string $url
  11. * @return bool
  12. * @throws \Exception If the file as not found or if the deletion failed.
  13. */
  14. public function removeArchiveByUrl(string $url): bool
  15. {
  16. // Extrair o caminho relativo da URL completa
  17. $imagePath = parse_url($url, PHP_URL_PATH);
  18. $imagePath = ltrim($imagePath, '/'); // remover a barra inicial
  19. // Excluir a imagem antiga
  20. if (Storage::exists($imagePath)) {
  21. $success = Storage::delete($imagePath);
  22. if ($success) {
  23. return true;
  24. } else {
  25. throw new Exception("Deletion was not possible");
  26. }
  27. } else {
  28. throw new Exception("File not found");
  29. }
  30. }
  31. }