import path from 'path';
import fs from 'fs/promises';

export async function GET(
  request: Request,
  { params }: { params: Promise<{ symbol: string }> }
) {
  const { symbol } = await params;
  const basePath = path.resolve('./public/icons/128/color');
  let filePath = path.join(basePath, `${symbol}.png`);

  try {
    await fs.access(filePath);
  } catch {
    console.log("symbol", symbol)
    filePath = path.join(basePath, `_no_image_.png`);
  }

  try {
    const imageBuffer = await fs.readFile(filePath);
    return new Response(new Uint8Array(imageBuffer), {
      status: 200,
      headers: {
        'Content-Type': 'image/png',
        'Content-Disposition': `inline; filename=${symbol}.png`,
      },
    });
  } catch {
    return new Response('error', { status: 404 });
  }
}
