Skip to content

Beautiful Open-Source Form Builder

Refactor Unsplash download functionality to use photo ID

JhumanJ09b0d4eb9bb9
diff --git a/api/app/Http/Controllers/Content/UnsplashController.php b/api/app/Http/Controllers/Content/UnsplashController.php
index 771aa1be1..02c438675 100644
--- a/api/app/Http/Controllers/Content/UnsplashController.php
+++ b/api/app/Http/Controllers/Content/UnsplashController.php
@@ -50,18 +50,33 @@ class UnsplashController extends Controller
public function download(Request $request)
{
+ $request->validate([
+ 'photo_id' => 'required|string|min:1|max:255',
+ ]);
+
$accessKey = config('services.unsplash.access_key');
- $downloadLocation = $request->get('download_location');
- if (!$accessKey || !$downloadLocation) {
+ if (!$accessKey) {
return response()->json(['error' => 'Invalid request'], 400);
}
- // Trigger download tracking by calling Unsplash download endpoint
- Http::get($downloadLocation, [
+ $photoId = $request->input('photo_id');
+
+ // Construct the URL on the backend - only one place to maintain it
+ $downloadLocation = "https://api.unsplash.com/photos/{$photoId}/download";
+
+ // Trigger download tracking with proper headers
+ $response = Http::withHeaders([
+ 'Accept-Version' => 'v1',
+ 'User-Agent' => 'OpnForm (https://opnform.com)',
+ ])->get($downloadLocation, [
'client_id' => $accessKey,
]);
+ if (!$response->successful()) {
+ return response()->json(['error' => 'Failed to track download'], 500);
+ }
+
return response()->json(['success' => true]);
}
diff --git a/client/api/content.js b/client/api/content.js
index 640bd0e12..afca43efe 100644
--- a/client/api/content.js
+++ b/client/api/content.js
@@ -14,6 +14,6 @@ export const contentApi = {
// Unsplash
unsplash: {
list: (options) => apiService.get('/unsplash', options),
- download: (downloadLocation) => apiService.post('/unsplash/download', { download_location: downloadLocation })
+ download: (photoId) => apiService.post('/unsplash/download', { photo_id: photoId })
}
}
\ No newline at end of file
diff --git a/client/components/forms/heavy/components/UnsplashImages.vue b/client/components/forms/heavy/components/UnsplashImages.vue
index b902e129a..624f4d91f 100644
--- a/client/components/forms/heavy/components/UnsplashImages.vue
+++ b/client/components/forms/heavy/components/UnsplashImages.vue
@@ -2,6 +2,7 @@
<div class="unsplash-images-container">
<div class="py-2">
<UInput
+ ref="searchInput"
v-model="searchTerm"
variant="outline"
class="w-full mb-1"
@@ -9,6 +10,7 @@
icon="i-heroicons-magnifying-glass-solid"
:disabled="!isFeatureEnabled"
:ui="{ trailing: 'pe-1' }"
+ autofocus
>
<template v-if="searchTerm?.length" #trailing>
<UButton
@@ -93,10 +95,10 @@ const { data: images = [], isLoading: loading } = unsplash.list(debouncedTerm, {
const selectImage = async (image) => {
// Trigger download tracking as per Unsplash API guidelines
- if (image.download_location) {
+ if (image.id) {
try {
- // Fire-and-forget - no need for mutation wrapper
- contentApi.unsplash.download(image.download_location)
+ // Fire-and-forget - pass only the photo ID, backend constructs the URL
+ contentApi.unsplash.download(image.id)
} catch (error) {
// Silently fail - don't block image selection if download tracking fails
console.warn('Failed to track Unsplash download:', error)