Created
July 2, 2022 06:09
-
-
Save firecentaur/f853f258565b97844ee7f787c7ee80f8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
} | |
}); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment