1
0
mirror of https://github.com/Kovah/LinkAce.git synced 2025-04-22 07:52:43 +02:00

Add command to update all link thumbnails

This commit is contained in:
Kovah 2021-04-18 12:57:06 +02:00
parent edce067d6e
commit ae0283ee8a
No known key found for this signature in database
GPG Key ID: AAAA031BA9830D7B
3 changed files with 54 additions and 1 deletions

View File

@ -8,7 +8,7 @@ use Venturecraft\Revisionable\Revision;
class CleanupLinkHistoriesCommand extends Command
{
protected $signature = 'link:cleanup-histories {field?}';
protected $signature = 'links:cleanup-histories {field?}';
protected $description = 'Removes all but the last 5 entries in the link histories.
{field : If provided, only history entries of that field are deleted}';

View File

@ -0,0 +1,51 @@
<?php
namespace App\Console\Commands;
use App\Helper\HtmlMeta;
use App\Models\Link;
use Illuminate\Console\Command;
class UpdateLinkThumbnails extends Command
{
protected $signature = 'links:update-thumbnails';
protected $description = 'Updates the thumbnails for all existing links, done in batches.';
public function handle()
{
$this->confirm('This command updates the thumbnail for all links with the status "ok". This can take a long time, depending on the amount of links you have saved. Do you want to proceed?');
$totalCount = Link::where('status', Link::STATUS_OK)->count();
$processedLinks = 0;
if ($totalCount === 0) {
$this->warn('No links with status "ok" found. Aborting');
}
$this->comment("Started processing of $totalCount links...");
Link::where('status', Link::STATUS_OK)->latest()
->chunk(100, function ($links) use ($processedLinks, $totalCount) {
foreach ($links as $link) {
$this->updateThumbnailForLink($link);
sleep(1); // Rate limiting of outgoing traffic
}
$processedLinks += count($links);
$this->comment("Processed $processedLinks of $totalCount links.");
});
$this->info('Finished processing all links.');
}
protected function updateThumbnailForLink(Link $link): void
{
$meta = (new HtmlMeta)->getFromUrl($link->url);
if ($meta['thumbnail'] !== null) {
$link->thumbnail = $meta['thumbnail'];
$link->save();
}
}
}

View File

@ -7,6 +7,7 @@ use App\Console\Commands\CleanupLinkHistoriesCommand;
use App\Console\Commands\RegisterUserCommand;
use App\Console\Commands\ResetPasswordCommand;
use App\Console\Commands\ImportCommand;
use App\Console\Commands\UpdateLinkThumbnails;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
@ -28,6 +29,7 @@ class Kernel extends ConsoleKernel
ResetPasswordCommand::class,
CleanupLinkHistoriesCommand::class,
ImportCommand::class,
UpdateLinkThumbnails::class,
];
/**