CarGallerySeeder.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace Database\Seeders;
  3. /*
  4. * CarGallerySeeder — скачивает 3 дополнительных фото на тип кузова
  5. * и заполняет поле photos_gallery для всех автомобилей.
  6. */
  7. use App\Models\Car;
  8. use Illuminate\Database\Seeder;
  9. use Illuminate\Support\Facades\Http;
  10. use Illuminate\Support\Facades\Storage;
  11. class CarGallerySeeder extends Seeder
  12. {
  13. // 3 фото на тип кузова для галереи
  14. private const GALLERY = [
  15. 'suv' => [
  16. 'https://images.unsplash.com/photo-1503376780353-7e6692767b70?w=800&q=75',
  17. 'https://images.unsplash.com/photo-1549317661-bd32c8ce0db2?w=800&q=75',
  18. 'https://images.unsplash.com/photo-1544636331-e26879cd4d9b?w=800&q=75',
  19. ],
  20. 'sedan' => [
  21. 'https://images.unsplash.com/photo-1494976388531-d1058494cdd8?w=800&q=75',
  22. 'https://images.unsplash.com/photo-1492144534655-ae79c964c9d7?w=800&q=75',
  23. 'https://images.unsplash.com/photo-1549399542-7e3f8b79c341?w=800&q=75',
  24. ],
  25. 'hatchback' => [
  26. 'https://images.unsplash.com/photo-1541899481282-d53bffe3c35d?w=800&q=75',
  27. 'https://images.unsplash.com/photo-1549317661-bd32c8ce0db2?w=800&q=75',
  28. 'https://images.unsplash.com/photo-1503376780353-7e6692767b70?w=800&q=75',
  29. ],
  30. 'wagon' => [
  31. 'https://images.unsplash.com/photo-1544636331-e26879cd4d9b?w=800&q=75',
  32. 'https://images.unsplash.com/photo-1503376780353-7e6692767b70?w=800&q=75',
  33. 'https://images.unsplash.com/photo-1492144534655-ae79c964c9d7?w=800&q=75',
  34. ],
  35. 'pickup' => [
  36. 'https://images.unsplash.com/photo-1558618666-fcd25c85cd64?w=800&q=75',
  37. 'https://images.unsplash.com/photo-1503376780353-7e6692767b70?w=800&q=75',
  38. 'https://images.unsplash.com/photo-1494976388531-d1058494cdd8?w=800&q=75',
  39. ],
  40. 'van' => [
  41. 'https://images.unsplash.com/photo-1519641471654-76ce0107ad1b?w=800&q=75',
  42. 'https://images.unsplash.com/photo-1503376780353-7e6692767b70?w=800&q=75',
  43. 'https://images.unsplash.com/photo-1544636331-e26879cd4d9b?w=800&q=75',
  44. ],
  45. 'minivan' => [
  46. 'https://images.unsplash.com/photo-1449965408869-eaa3f722e40d?w=800&q=75',
  47. 'https://images.unsplash.com/photo-1544636331-e26879cd4d9b?w=800&q=75',
  48. 'https://images.unsplash.com/photo-1503376780353-7e6692767b70?w=800&q=75',
  49. ],
  50. 'default' => [
  51. 'https://images.unsplash.com/photo-1494976388531-d1058494cdd8?w=800&q=75',
  52. 'https://images.unsplash.com/photo-1503376780353-7e6692767b70?w=800&q=75',
  53. 'https://images.unsplash.com/photo-1549317661-bd32c8ce0db2?w=800&q=75',
  54. ],
  55. ];
  56. public function run(): void
  57. {
  58. $dir = 'cars/demo';
  59. Storage::disk('public')->makeDirectory($dir);
  60. // Собираем все уникальные URL
  61. $allUrls = [];
  62. foreach (self::GALLERY as $pool) {
  63. foreach ($pool as $url) {
  64. $allUrls[$url] = true;
  65. }
  66. }
  67. $allUrls = array_keys($allUrls);
  68. // Скачиваем
  69. $downloaded = [];
  70. foreach ($allUrls as $url) {
  71. $hash = md5($url);
  72. $file = $dir . '/' . $hash . '.jpg';
  73. if (Storage::disk('public')->exists($file)) {
  74. $downloaded[$url] = $file;
  75. $this->command->line(" [skip] {$hash}.jpg");
  76. continue;
  77. }
  78. $this->command->line(" [download] {$hash}.jpg...");
  79. try {
  80. $response = Http::timeout(25)->get($url);
  81. if ($response->successful()) {
  82. Storage::disk('public')->put($file, $response->body());
  83. $downloaded[$url] = $file;
  84. $this->command->line(' ✓ ' . round(strlen($response->body()) / 1024) . ' KB');
  85. } else {
  86. $this->command->warn(' ✗ HTTP ' . $response->status());
  87. }
  88. } catch (\Exception $e) {
  89. $this->command->warn(' ✗ ' . $e->getMessage());
  90. }
  91. }
  92. // Присваиваем галереи
  93. $updated = 0;
  94. foreach (Car::all() as $car) {
  95. $pool = self::GALLERY[$car->body_type] ?? self::GALLERY['default'];
  96. $gallery = [];
  97. foreach ($pool as $url) {
  98. if (isset($downloaded[$url])) {
  99. $gallery[] = $downloaded[$url];
  100. }
  101. }
  102. if (!empty($gallery)) {
  103. $car->update(['photos_gallery' => $gallery]);
  104. $updated++;
  105. }
  106. }
  107. $this->command->info("Галерея обновлена: {$updated} авто.");
  108. }
  109. }