Ajoute la commande de téléchargement des images

Fix #2
This commit is contained in:
Clement 2020-07-23 14:54:02 +02:00
parent 2de65bb4aa
commit 6119e6c6d7
6 changed files with 160 additions and 3 deletions

View File

@ -0,0 +1,75 @@
<?php
namespace App\Console\Commands;
use App\Home;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
class DownloadImageCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:downloadimage {id}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Download pictures of a given home';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$id = $this->argument('id');
$this->info(sprintf('Downloading images from home id = %s', $id));
$home = Home::where('id', $id)->first();
if (!$home instanceof Home) {
$this->error(sprintf('No home with id «%s»', $id));
return 1;
}
if ($home->pictures_downloaded) {
$this->error(sprintf('Pictures already downloaded for «%s»', $id));
return 1;
}
$pictures = $home->pictures;
$newPictures = [];
$countPictures = count($pictures);
$this->info(sprintf('Downloading %u images', $countPictures));
foreach ($pictures as $picture) {
$picture_content = file_get_contents($picture);
$newPicture = sprintf('%s/%s', $id, basename($picture));
if (false === Storage::put($newPicture, $picture_content, 'public')) {
$this->error(sprintf('Can\'t store «%s» into «%s»', $picture, $newPicture));
return 1;
}
$newPictures[] = sprintf('/storage/%s', $newPicture);
sleep(1);
}
$home->pictures = $newPictures;
$home->pictures_downloaded = true;
$home->save();
$this->info('Download finished');
return 0;
}
}

View File

@ -0,0 +1,50 @@
<?php
namespace App\Console\Commands;
use App\Home;
use Illuminate\Console\Command;
class DownloadImagesCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:downloadimages';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$homes = Home::where('pictures_downloaded', false)->get();
foreach ($homes as $home) {
$this->call('app:downloadimage', [
'id' => $home->id
]);
}
return 0;
}
}

View File

@ -24,7 +24,7 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')->hourly();
$schedule->command('app:downloadimages')->daily();
}
/**

View File

@ -9,7 +9,7 @@ class Home extends Model
{
protected $fillable = [
'title', 'price', 'surface', 'garden_surface', 'rooms', 'energy',
'ges', 'description', 'city', 'pictures', 'map', 'url',
'ges', 'description', 'city', 'pictures', 'map', 'url', 'pictures_downloaded',
];
protected $casts = [

View File

@ -13,7 +13,7 @@ return [
|
*/
'default' => env('FILESYSTEM_DRIVER', 'local'),
'default' => env('FILESYSTEM_DRIVER', 'public'),
/*
|--------------------------------------------------------------------------

View File

@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class UpdateHomesImagesDownloaded extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('homes', static function (Blueprint $table) {
$table->boolean('pictures_downloaded')->default(false);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('homes', static function (Blueprint $table) {
$table->dropColumn('pictures_downloaded');
});
}
}