RemoveArchiveS3.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace App\Traits;
  3. use Exception;
  4. use Illuminate\Support\Facades\Log;
  5. use Illuminate\Support\Facades\Storage;
  6. trait RemoveArchiveS3
  7. {
  8. /**
  9. * Delete a file and return a boolean if succeded.
  10. *
  11. * @throws \Exception If the file as not found or if the deletion failed.
  12. */
  13. public function removeArchiveByPath(string $path): bool
  14. {
  15. $exists = Storage::exists($path);
  16. Log::info('[avatar-upload] RemoveArchiveS3::removeArchiveByPath', [
  17. 'path' => $path,
  18. 'exists' => $exists,
  19. ]);
  20. if (! $exists) {
  21. Log::warning('[avatar-upload] Arquivo antigo NÃO encontrado no S3 para remoção', ['path' => $path]);
  22. return false;
  23. }
  24. $deleted = Storage::delete($path);
  25. Log::info('[avatar-upload] Resultado da remoção do arquivo antigo', [
  26. 'path' => $path,
  27. 'deleted' => $deleted,
  28. ]);
  29. if (! $deleted) {
  30. throw new Exception('Deletion was not possible');
  31. }
  32. return true;
  33. }
  34. }