RemoveArchiveS3.php 823 B

1234567891011121314151617181920212223242526272829303132
  1. <?php
  2. namespace App\Traits;
  3. use Illuminate\Support\Facades\Storage;
  4. use Illuminate\Support\Str;
  5. trait RemoveArchiveS3
  6. {
  7. /**
  8. * Upload a base64 encoded image to S3 and return the URL.
  9. *
  10. * @param string $base64Image
  11. * @param string $s3Folder
  12. * @return string
  13. * @throws \Exception
  14. */
  15. public function removeArchiveByUrl(string $url): bool
  16. {
  17. // Extrair o caminho relativo do S3 da URL completa
  18. $oldImagePath = parse_url($url, PHP_URL_PATH);
  19. $oldImagePath = ltrim($oldImagePath, '/'); // remover a barra inicial
  20. // Excluir a imagem antiga do S3
  21. if (Storage::disk('s3')->exists($oldImagePath)) {
  22. Storage::disk('s3')->delete($oldImagePath);
  23. }
  24. // Return the URL of the uploaded image
  25. return true;
  26. }
  27. }