Skip to content

Commit dca7d6d

Browse files
committed
WIP
1 parent 1ebb625 commit dca7d6d

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

app/Console/Commands/SyncArticleImages.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,20 @@ public function handle(): void
3232
Article::published()->chunk(100, function ($articles) {
3333
$articles->each(function ($article) {
3434
if ($article->hasHeroImage()) {
35-
$article->hero_image_url = $this->generateUnsplashImageUrlFromId($article->hero_image);
36-
$article->save();
35+
$imageData = $this->fetchUnsplashImageDataFromId($article->hero_image);
36+
37+
if (!is_null($imageData)) {
38+
$article->hero_image_url = $imageData['image_url'];
39+
$article->hero_image_author_name = $imageData['author_name'];
40+
$article->hero_image_author_url = $imageData['author_url'];
41+
$article->save();
42+
}
3743
}
3844
});
3945
});
4046
}
4147

42-
protected function generateUnsplashImageUrlFromId(string $imageId): ?string
48+
protected function fetchUnsplashImageDataFromId(string $imageId): ?array
4349
{
4450
$response = Http::retry(3, 100, null, false)->withToken($this->accessKey, 'Client-ID')
4551
->get("https://api.unsplash.com/photos/{$imageId}");
@@ -53,6 +59,12 @@ protected function generateUnsplashImageUrlFromId(string $imageId): ?string
5359
return null;
5460
}
5561

56-
return (string) $response->json('urls.raw');
62+
$response = $response->json();
63+
64+
return [
65+
'image_url' => $response['urls']['raw'],
66+
'author_name' => $response['user']['name'],
67+
'author_url' => $response['user']['links']['html']
68+
];
5769
}
5870
}

app/Models/Article.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ final class Article extends Model implements Feedable
4646
'slug',
4747
'hero_image',
4848
'hero_image_url',
49+
'hero_image_author_name',
50+
'hero_image_author_url',
4951
'is_pinned',
5052
'view_count',
5153
'tweet_id',

database/migrations/2024_09_27_095949_add_hero_image_url_column_to_articles.php renamed to database/migrations/2024_09_27_095949_add_hero_image_additional_columns_to_articles.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
public function up(): void
1313
{
1414
Schema::table('articles', function (Blueprint $table) {
15-
$table->string('hero_image_url')->nullable()->after('hero_image');
15+
$table->after('hero_image', function () use ($table) {
16+
$table->string('hero_image_url')->nullable();
17+
$table->string('hero_image_author_name')->nullable();
18+
$table->string('hero_image_author_url')->nullable();
19+
});
1620
});
1721
}
1822

@@ -22,7 +26,11 @@ public function up(): void
2226
public function down(): void
2327
{
2428
Schema::table('articles', function (Blueprint $table) {
25-
$table->dropColumn('hero_image_url');
29+
$table->dropColumn([
30+
'hero_image_url',
31+
'hero_image_author_name',
32+
'hero_image_author_url',
33+
]);
2634
});
2735
}
2836
};

resources/views/articles/show.blade.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class="w-full bg-center {{ $article->hasHeroImage() ? 'bg-cover' : '' }} bg-gray
8585
</div>
8686
</div>
8787
</div>
88+
<p class="text-xs text-black/50 text-center mt-2">Photo by <a class="underline font-medium" href="{{ $article->hero_image_author_url }}?ref=laravel.io" target="_blank">{{ $article->hero_image_author_name }} </a> on <a class="underline font-medium" href="https://unsplash.com/?ref=laravel.io" target="_blank">Unsplash</a></p>
8889
</div>
8990
</div>
9091

@@ -250,4 +251,4 @@ class="prose prose-lg text-gray-800 prose-lio"
250251
@endif
251252
</x-modal>
252253
@endcan
253-
@endsection
254+
@endsection

0 commit comments

Comments
 (0)