UploadsFile.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace App\Traits;
  3. use Illuminate\Http\UploadedFile;
  4. use Illuminate\Support\Facades\Log;
  5. use Illuminate\Support\Facades\Storage;
  6. use Illuminate\Support\Str;
  7. trait UploadsFile
  8. {
  9. public function uploadFile(UploadedFile $file, string $folder, ?string $filename = null): string
  10. {
  11. $extension = $file->getClientOriginalExtension();
  12. $name = $filename ?? Str::random(20).'.'.$extension;
  13. $path = $folder.'/'.$name;
  14. Log::info('[avatar-upload] UploadsFile::uploadFile — chamando Storage::disk(s3)->put()', [
  15. 'path' => $path,
  16. 'disk' => 's3',
  17. 'file_size' => $file->getSize(),
  18. 'file_real_path' => $file->getRealPath(),
  19. 'content_length' => strlen(file_get_contents($file)),
  20. ]);
  21. $result = Storage::disk('s3')->put($path, file_get_contents($file));
  22. Log::info('[avatar-upload] UploadsFile::uploadFile — resultado do Storage::put()', [
  23. 'path' => $path,
  24. 'result' => $result,
  25. 'result_type' => gettype($result),
  26. 'upload_success' => $result !== false,
  27. ]);
  28. if ($result === false) {
  29. Log::error('[avatar-upload] FALHA NO UPLOAD S3 — Storage::put() retornou false', [
  30. 'path' => $path,
  31. 'disk' => 's3',
  32. 'bucket' => config('filesystems.disks.s3.bucket'),
  33. 'region' => config('filesystems.disks.s3.region'),
  34. 'endpoint' => config('filesystems.disks.s3.endpoint'),
  35. ]);
  36. }
  37. // Verificar se o arquivo realmente existe no S3 após o upload
  38. $existsAfterUpload = Storage::disk('s3')->exists($path);
  39. Log::info('[avatar-upload] Verificação pós-upload: arquivo existe no S3?', [
  40. 'path' => $path,
  41. 'exists' => $existsAfterUpload,
  42. ]);
  43. return $path;
  44. }
  45. }