File size: 1,277 Bytes
5dfbe50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { Hono } from 'hono';
import { config } from '../config';
import { cache } from '../services/cache';

const fullImageApp = new Hono();

// Full image endpoint - matches Next.js /api/full-image
fullImageApp.get('/', async (c) => {
  try {
    const docId = c.req.query('docId');
    
    if (!docId) {
      return c.json({ error: 'docId is required' }, 400);
    }

    // Check cache
    const cacheKey = `fullimage:${docId}`;
    const cachedImage = cache.get<{ base64_image: string }>(cacheKey);
    
    if (cachedImage) {
      c.header('X-Cache', 'HIT');
      return c.json(cachedImage);
    }

    // Proxy to backend
    const imageUrl = `${config.backendUrl}/full_image?doc_id=${encodeURIComponent(docId)}`;
    const response = await fetch(imageUrl);

    if (!response.ok) {
      throw new Error(`Backend returned ${response.status}`);
    }

    const data = await response.json();
    
    // Cache for 24 hours
    cache.set(cacheKey, data, 86400);
    c.header('X-Cache', 'MISS');

    return c.json(data);
  } catch (error) {
    console.error('Full image error:', error);
    return c.json({ 
      error: 'Failed to fetch image', 
      message: error instanceof Error ? error.message : 'Unknown error' 
    }, 500);
  }
});

export { fullImageApp };