Загрузка данных


export default async function handler(req) {
  const chromecastUrls = 'https://githubusercontent.com';
  
  try {
    const randomSource = Math.floor(Math.random() * 2);
    let finalImageUrl = '';

    if (randomSource === 0) {
      const response = await fetch(chromecastUrls);
      const data = await response.json();
      const randomImage = data[Math.floor(Math.random() * data.length)];
      finalImageUrl = randomImage.url;
    } else {
      const randomEarthId = Math.floor(Math.random() * 5000) + 1000;
      finalImageUrl = `https://withgoogle.com{randomEarthId}.jpg`;
    }

    // Скачиваем картинку напрямую через fetch
    const imageResponse = await fetch(finalImageUrl);
    
    // Забираем её как чистый бинарный массив (Blob)
    const imageBlob = await imageResponse.blob();

    // Отдаем телефону чистый файл с правильным типом данных
    return new Response(imageBlob, {
      status: 200,
      headers: {
        'Content-Type': 'image/jpeg',
        'Cache-Control': 'no-store, max-age=0, must-revalidate',
      },
    });

  } catch (error) {
    // Если всё упало, отдаем одну железную рабочую картинку космоса из открытого репозитория
    const fallbackResponse = await fetch('https://githubusercontent.com');
    const fallbackBlob = await fallbackResponse.blob();
    return new Response(fallbackBlob, {
      status: 200,
      headers: { 'Content-Type': 'image/png' },
    });
  }
}