Skip to content

Instantly share code, notes, and snippets.

@firecentaur
Created July 2, 2022 06:09
Show Gist options
  • Select an option

  • Save firecentaur/f853f258565b97844ee7f787c7ee80f8 to your computer and use it in GitHub Desktop.

Select an option

Save firecentaur/f853f258565b97844ee7f787c7ee80f8 to your computer and use it in GitHub Desktop.

Revisions

  1. firecentaur created this gist Jul 2, 2022.
    75 changes: 75 additions & 0 deletions LocationGalleryImageSeeder.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    <?php

    namespace Database\Seeders;

    use App\Models\Location;
    use App\Models\LocationGallery;
    use GuzzleHttp\Client;
    use GuzzleHttp\Exception\GuzzleException;
    use Illuminate\Database\Seeder;
    use Illuminate\Support\Collection;
    use Illuminate\Support\Facades\DB;
    use Illuminate\Support\Facades\Storage;

    class LocationGalleryImageSeeder extends Seeder
    {
    public $rewrite;
    /**
    * Run the database seeds.
    *
    * @return void
    */

    public function run()
    {

    $locations = Location::all();
    $numLocations = $locations->count();
    $this->command->info("There are $numLocations existing Locations");
    $numGalleries = LocationGallery::all()->count();
    $this->command->info("There are $numGalleries existing gallery images");

    /**
    * @var $locations Collection
    * var[]/class
    */
    $this->command->info("Importing images");
    $client = new Client( );
    $locations->each(function ($loc) use ($client){
    /**
    * @var $loc LocationGallery
    * var[]/class
    */

    $gallery = $loc->location_gallery;

    $gallery->each(function ($image) use ($loc,$client) {
    /**
    * @var $image LocationGallery
    * var[]/class
    */
    $filename = $image->location_image;
    $id = $loc->id;
    $url = 'https://dux.city/duxapi/locations/'.$filename;

    try {
    $path = storage_path('public/location/'.$id);
    echo "scraping $url, storing in $path/$filename";
    if (!Storage::exists('public/location/'.$id)){
    $result = Storage::makeDirectory("public/location/$id",'0777',true,true);
    }


    $client->request('GET', $url, [
    'sink' => storage_path("app/public/location/$id/$filename")
    ]);

    }catch (GuzzleException $exception){
    echo $exception->getMessage();
    }


    });
    });
    }
    }