This repository has been archived on 2026-02-11. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
Memento-old/src/pages/api/image/[id].ts
2026-01-12 18:59:02 +13:00

37 lines
996 B
TypeScript

import type { APIContext } from 'astro';
import { promises as fs } from 'fs';
import { join } from 'path';
import 'dotenv/config'
import { httpResponse } from '../../../utils/response';
const uploadDir = process.env.UPLOAD_DIR!;
export async function GET({ params }: APIContext) {
try {
const { id } = params
return readImage(id!)
} catch (error) {
return httpResponse({ error: `Failed to retrieve image: ${error}` }, 500);
}
}
async function readImage(id: string) {
const filepath = join(uploadDir, id);
try {
await fs.access(filepath);
} catch {
return httpResponse({ error: 'Image not found' }, 404);
}
const image = await fs.readFile(filepath);
const ext = id.split('.').pop()?.toLowerCase();
const contentType = ext === 'png' ? 'image/png' : 'image/jpeg';
return new Response(image, {
status: 200,
headers: {
'Content-Type': contentType,
}
});
}