From 6119e6c6d71dfdb8b73ec4a5775488a2f33b02af Mon Sep 17 00:00:00 2001 From: Clement Date: Thu, 23 Jul 2020 14:54:02 +0200 Subject: [PATCH] =?UTF-8?q?:sparkles:=20Ajoute=20la=20commande=20de=20t?= =?UTF-8?q?=C3=A9l=C3=A9chargement=20des=20images?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix #2 --- app/Console/Commands/DownloadImageCommand.php | 75 +++++++++++++++++++ .../Commands/DownloadImagesCommand.php | 50 +++++++++++++ app/Console/Kernel.php | 2 +- app/Home.php | 2 +- config/filesystems.php | 2 +- ..._111936_update_homes_images_downloaded.php | 32 ++++++++ 6 files changed, 160 insertions(+), 3 deletions(-) create mode 100644 app/Console/Commands/DownloadImageCommand.php create mode 100644 app/Console/Commands/DownloadImagesCommand.php create mode 100644 database/migrations/2020_07_23_111936_update_homes_images_downloaded.php diff --git a/app/Console/Commands/DownloadImageCommand.php b/app/Console/Commands/DownloadImageCommand.php new file mode 100644 index 0000000..0a5a00e --- /dev/null +++ b/app/Console/Commands/DownloadImageCommand.php @@ -0,0 +1,75 @@ +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; + } +} diff --git a/app/Console/Commands/DownloadImagesCommand.php b/app/Console/Commands/DownloadImagesCommand.php new file mode 100644 index 0000000..2f0b952 --- /dev/null +++ b/app/Console/Commands/DownloadImagesCommand.php @@ -0,0 +1,50 @@ +get(); + foreach ($homes as $home) { + $this->call('app:downloadimage', [ + 'id' => $home->id + ]); + } + + return 0; + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 69914e9..d201b42 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -24,7 +24,7 @@ class Kernel extends ConsoleKernel */ protected function schedule(Schedule $schedule) { - // $schedule->command('inspire')->hourly(); + $schedule->command('app:downloadimages')->daily(); } /** diff --git a/app/Home.php b/app/Home.php index ac0356d..ff8c1de 100644 --- a/app/Home.php +++ b/app/Home.php @@ -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 = [ diff --git a/config/filesystems.php b/config/filesystems.php index 94c8112..5f760c1 100644 --- a/config/filesystems.php +++ b/config/filesystems.php @@ -13,7 +13,7 @@ return [ | */ - 'default' => env('FILESYSTEM_DRIVER', 'local'), + 'default' => env('FILESYSTEM_DRIVER', 'public'), /* |-------------------------------------------------------------------------- diff --git a/database/migrations/2020_07_23_111936_update_homes_images_downloaded.php b/database/migrations/2020_07_23_111936_update_homes_images_downloaded.php new file mode 100644 index 0000000..817eaac --- /dev/null +++ b/database/migrations/2020_07_23_111936_update_homes_images_downloaded.php @@ -0,0 +1,32 @@ +boolean('pictures_downloaded')->default(false); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('homes', static function (Blueprint $table) { + $table->dropColumn('pictures_downloaded'); + }); + } +}