Skip to content

Commit 56e2f91

Browse files
committed
WIP
1 parent b83f4ff commit 56e2f91

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

app/Console/Commands/SyncArticleImages.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Models\Article;
66
use Illuminate\Console\Command;
7+
use Illuminate\Support\Facades\Http;
78

89
final class SyncArticleImages extends Command
910
{
@@ -30,10 +31,28 @@ public function handle(): void
3031

3132
Article::published()->chunk(100, function ($articles) {
3233
$articles->each(function ($article) {
33-
if (! $article->hero_image) {
34-
// Update Unsplash image URL
34+
if ($article->hasHeroImage()) {
35+
$article->hero_image_url = $this->getUnsplashImageUrlFromStringId($article->hero_image);
36+
$article->save();
3537
}
3638
});
3739
});
3840
}
41+
42+
protected function getUnsplashImageUrlFromStringId(string $imageId): ?string
43+
{
44+
$response = Http::retry(3, 100, null, false)->withToken($this->accessKey, 'Client-ID')
45+
->get("https://api.unsplash.com/photos/{$imageId}");
46+
47+
if ($response->failed()) {
48+
logger()->error('Failed to get raw image url from unsplash for', [
49+
'imageId' => $imageId,
50+
'response' => $response->json(),
51+
]);
52+
53+
return null;
54+
}
55+
56+
return (string) $response->json('urls.raw');
57+
}
3958
}

config/services.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
],
4242

4343
'unsplash' => [
44-
'acccess_key' => env('UNSPLASH_ACCESS_KEY'),
44+
'access_key' => env('UNSPLASH_ACCESS_KEY'),
4545
],
4646

4747
];

0 commit comments

Comments
 (0)