Skip to content

feat: Implement proxyImage method in HeritageImageController #471

@zigzagdev

Description

@zigzagdev

Parent issue

#475

Overview

Create (or update) app/Http/Controllers/HeritageImageController.php with a proxyImage method that fetches the image from UNESCO on behalf of the browser and streams it back.

Implementation

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\DB;

class HeritageImageController extends Controller
{
    public function proxyImage($id)
    {
        $image = DB::table('world_heritage_site_images')->find($id);

        if (!$image) {
            return response()->json(['error' => 'Image record not found'], 404);
        }

        $response = Http::withHeaders([
            'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
        ])->get($image->url);

        if ($response->failed()) {
            return response()->json(['error' => 'Failed to fetch image from UNESCO'], 500);
        }

        return response($response->body(), 200)
            ->header('Content-Type', $response->header('Content-Type'))
            ->header('Access-Control-Allow-Origin', '*');
    }
}

Acceptance criteria

  • Returns the image binary with correct Content-Type when a valid id is supplied
  • Returns 404 when no record exists for the given id
  • Returns 500 when the upstream UNESCO request fails
  • Includes Access-Control-Allow-Origin: * header

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions