Skip to content

Commit a0bd00c

Browse files
committedMar 28, 2025
Improve article sync
1 parent 8ae50f2 commit a0bd00c

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed
 

Diff for: ‎app/Console/Commands/SyncArticleImages.php

+10-8
Original file line numberDiff line numberDiff line change
@@ -22,36 +22,38 @@ public function handle(): void
2222

2323
Article::unsyncedImages()->chunk(100, function ($articles) {
2424
$articles->each(function ($article) {
25-
$imageData = $this->fetchUnsplashImageDataFromId($article->hero_image_id);
25+
$imageData = $this->fetchUnsplashImageDataFromId($article);
2626

2727
if (! is_null($imageData)) {
2828
$article->hero_image_url = $imageData['image_url'];
2929
$article->hero_image_author_name = $imageData['author_name'];
3030
$article->hero_image_author_url = $imageData['author_url'];
3131
$article->save();
32+
} else {
33+
$this->warn("Failed to fetch image data for image {$article->hero_image_id}");
3234
}
3335
});
3436
});
3537
}
3638

37-
protected function fetchUnsplashImageDataFromId(string $imageId): ?array
39+
protected function fetchUnsplashImageDataFromId(Article $article): ?array
3840
{
3941
$response = Http::retry(3, 100, throw: false)
4042
->withToken(config('services.unsplash.access_key'), 'Client-ID')
41-
->get("https://api.unsplash.com/photos/{$imageId}");
43+
->get("https://api.unsplash.com/photos/{$article->hero_image_id}");
4244

4345
if ($response->failed()) {
44-
logger()->error('Failed to get raw image url from unsplash for', [
45-
'imageId' => $imageId,
46-
'response' => $response->json(),
47-
]);
46+
$article->hero_image_id = null;
47+
$article->save();
48+
49+
$this->warn("Failed to fetch image data for image {$article->hero_image_id}");
4850

4951
return null;
5052
}
5153

5254
$response = $response->json();
5355

54-
// Trigger as download...
56+
// Trigger as Unsplash download...
5557
Http::retry(3, 100, throw: false)
5658
->withToken(config('services.unsplash.access_key'), 'Client-ID')
5759
->get($response['links']['download_location']);

0 commit comments

Comments
 (0)