File size: 2,276 Bytes
5de7420
 
 
 
 
 
26cefad
5de7420
 
 
 
 
 
 
 
26cefad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5de7420
 
 
 
26cefad
 
 
 
5de7420
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26cefad
 
 
 
 
 
 
 
 
 
 
 
5de7420
26cefad
 
5de7420
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { NextResponse } from 'next/server';
import axios from 'axios';

export async function GET(request) {
  try {
    const { searchParams } = new URL(request.url);
    let imageUrl = searchParams.get('url');

    if (!imageUrl) {
      return NextResponse.json(
        { error: 'Image URL is required' },
        { status: 400 }
      );
    }

    // Decode the URL properly and clean it
    imageUrl = decodeURIComponent(imageUrl);

    // Remove any extra encoded characters or HTML entities that might have been appended
    // Clean up common issues with malformed URLs
    imageUrl = imageUrl.split('&quot')[0].split('&#')[0].split('"')[0].split("'")[0];

    // Validate that it's a proper image URL
    if (!imageUrl.match(/^https?:\/\/.+\.(png|jpg|jpeg|gif|webp|svg)$/i)) {
      console.error('Invalid image URL format:', imageUrl);
      return NextResponse.json(
        { error: 'Invalid image URL format' },
        { status: 400 }
      );
    }

    // Fetch the image
    const response = await axios.get(imageUrl, {
      responseType: 'arraybuffer',
      headers: {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
        'Accept': 'image/*'
      },
      timeout: 10000 // 10 second timeout
    });

    // Get content type from response
    const contentType = response.headers['content-type'] || 'image/png';

    // Return the image with proper headers
    return new NextResponse(response.data, {
      status: 200,
      headers: {
        'Content-Type': contentType,
        'Cache-Control': 'public, max-age=3600',
        'Access-Control-Allow-Origin': '*'
      }
    });

  } catch (error) {
    console.error('Error proxying image:', {
      message: error.message,
      status: error.response?.status,
      url: error.config?.url
    });

    // If it's a 403, try to return a default avatar
    if (error.response?.status === 403 || error.response?.status === 404) {
      // Return a redirect to the default Hugging Face logo
      return NextResponse.redirect('https://huggingface.co/front/assets/huggingface_logo-noborder.svg');
    }

    return NextResponse.json(
      { error: 'Failed to fetch image', details: error.message },
      { status: error.response?.status || 500 }
    );
  }
}