Skip to content

Commit

Permalink
Merge pull request #95 from ijpatricio/fix-slow-seed
Browse files Browse the repository at this point in the history
Use local images for seeding.
  • Loading branch information
danharrin authored Feb 7, 2024
2 parents 4a9fa8c + ab7f4ec commit d246fb9
Show file tree
Hide file tree
Showing 53 changed files with 130 additions and 12 deletions.
100 changes: 100 additions & 0 deletions app/Console/Commands/GetRandomImages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace App\Console\Commands;

use Database\Seeders\LocalImages;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;

class GetRandomImages extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:get-random-images';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Get random images stored locally, so that the seed process can be fast.';

/**
* Execute the console command.
*/
public function handle()
{
// Use an empty string to get random images
// We could fine-tune with search terms, examples: 'nature', 'people', 'city', 'abstract', 'food', 'sports', 'technics', 'transport', 'animals'
// This needs some deeper research to get the best results

$schemas = [
['amount' => 40, 'size' => LocalImages::SIZE_200x200, 'terms' => ['']],
['amount' => 40, 'size' => LocalImages::SIZE_1280x720, 'terms' => ['']],
];

foreach ($schemas as $schema) {
$this->getRandomImages($schema);
}

foreach ($schemas as $schema) {
$this->removeDuplicates($schema);
}
}

protected function getRandomImages($schema)
{
['amount' => $amount, 'size' => $size, 'terms' => $terms] = $schema;

$this->comment("Getting $amount random images of size $size, of topic: " . implode(', ', $terms));

File::deleteDirectory(database_path('seeders/local_images/' . $size));

$progressBar = $this->output->createProgressBar($amount);
$progressBar->start();

foreach (range(1, $amount) as $i) {
$url = "https://source.unsplash.com/{$size}/?img=1," . implode(',', $terms);
$image = file_get_contents($url);

File::ensureDirectoryExists(database_path('seeders/local_images/' . $size));
$filename = Str::uuid() . '.jpg';

File::put(
database_path(
path: "seeders/local_images/{$size}/{$filename}"
),
contents: $image
);

$progressBar->advance();
}

$progressBar->finish();

$this->newLine();
$this->info('Done!');
}

protected function removeDuplicates($schema)
{
['size' => $size] = $schema;

$allFiles = fn () => collect(File::files(database_path('seeders/local_images/' . $size)));

$uniqueImageSet = $allFiles()
->mapWithKeys(fn ($file) => [md5_file($file->getPathname()) => $file->getPathname()])
->values();

$allFiles()
->map(fn ($file) => $file->getPathname())
->diff($uniqueImageSet)
->each(fn ($file) => File::delete($file));

$this->info('Kept ' . $uniqueImageSet->count() . " unique files from size $size");
}
}
3 changes: 2 additions & 1 deletion database/factories/Blog/LinkFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Database\Factories\Blog;

use Database\Factories\Concerns\CanCreateImages;
use Database\Seeders\LocalImages;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

Expand Down Expand Up @@ -33,7 +34,7 @@ public function definition(): array
'nl' => $this->faker->sentence(),
],
'color' => $this->faker->hexColor(),
'image' => $this->createImage('https://source.unsplash.com/random/1280x720/?img=1'),
'image' => $this->createImage(LocalImages::SIZE_1280x720),
];
}
}
12 changes: 5 additions & 7 deletions database/factories/Concerns/CanCreateImages.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@

namespace Database\Factories\Concerns;

use Database\Seeders\DatabaseSeeder;
use Database\Seeders\LocalImages;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

trait CanCreateImages
{
public function createImage(?string $url = null): ?string
public function createImage(?string $size = LocalImages::SIZE_200x200): ?string
{
try {
$image = file_get_contents($url ?? DatabaseSeeder::IMAGE_URL);
} catch (Throwable $exception) {
return null;
}
$randomImage = LocalImages::getRandomFile($size);

$image = file_get_contents($randomImage->getPathname());

$filename = Str::uuid() . '.jpg';

Expand Down
5 changes: 3 additions & 2 deletions database/factories/Shop/ProductFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Database\Factories\Shop;

use App\Models\Shop\Product;
use Database\Seeders\DatabaseSeeder;
use Database\Seeders\LocalImages;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
use Spatie\MediaLibrary\MediaCollections\Exceptions\UnreachableUrl;
Expand Down Expand Up @@ -42,7 +42,8 @@ public function configure(): ProductFactory
return $this->afterCreating(function (Product $product) {
try {
$product
->addMediaFromUrl(DatabaseSeeder::IMAGE_URL)
->addMedia(LocalImages::getRandomFile())
->preservingOriginal()
->toMediaCollection('product-images');
} catch (UnreachableUrl $exception) {
return;
Expand Down
2 changes: 0 additions & 2 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@

class DatabaseSeeder extends Seeder
{
const IMAGE_URL = 'https://source.unsplash.com/random/200x200/?img=1';

public function run(): void
{
DB::raw('SET time_zone=\'+00:00\'');
Expand Down
20 changes: 20 additions & 0 deletions database/seeders/LocalImages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Database\Seeders;

use Illuminate\Support\Facades\File;
use Symfony\Component\Finder\SplFileInfo;

class LocalImages
{
public const SIZE_200x200 = '200x200';

public const SIZE_1280x720 = '1280x720';

public static function getRandomFile(?string $size = LocalImages::SIZE_200x200): SplFileInfo
{
return collect(
File::files(database_path('seeders/local_images/' . $size))
)->random();
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d246fb9

Please sign in to comment.