From 2a60504cea0153933300bd65b3652a1f762de2cf Mon Sep 17 00:00:00 2001 From: HRiggs Date: Sat, 25 Oct 2025 18:38:12 -0400 Subject: [PATCH] Add image to tabel --- src/db/client.ts | 6 +++--- src/public/index.html | 2 +- src/routes/api.ts | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/db/client.ts b/src/db/client.ts index 2f56734..b2e689a 100644 --- a/src/db/client.ts +++ b/src/db/client.ts @@ -65,7 +65,7 @@ export const db = { ); const insertId = (result as any).insertId as number; const [rows] = await pool.query( - `select id, manufacturer, model, sku, quantity, description, item_condition as cond, has_box as hasBox, image_path as imagePath, created_at as createdAt + `select id, manufacturer, model, sku, quantity, description, item_condition as cond, has_box as hasBox, image_path as imagePath, (image_data is not null) as hasImage, created_at as createdAt from items where id = ?`, [insertId] ); @@ -78,7 +78,7 @@ export const db = { const where = search ? 'where model like ? or sku like ?' : ''; const params = search ? [`%${search}%`, `%${search}%`] : []; const [rows] = await pool.query( - `select id, manufacturer, model, sku, quantity, description, item_condition as cond, has_box as hasBox, image_path as imagePath, created_at as createdAt + `select id, manufacturer, model, sku, quantity, description, item_condition as cond, has_box as hasBox, image_path as imagePath, (image_data is not null) as hasImage, created_at as createdAt from items ${where} order by id asc`, params ); @@ -89,7 +89,7 @@ export const db = { async getItem(id: number) { await getReady(); const [rows] = await pool.query( - `select id, manufacturer, model, sku, quantity, description, item_condition as cond, has_box as hasBox, image_path as imagePath, created_at as createdAt + `select id, manufacturer, model, sku, quantity, description, item_condition as cond, has_box as hasBox, image_path as imagePath, (image_data is not null) as hasImage, created_at as createdAt from items where id = ?`, [id] ); diff --git a/src/public/index.html b/src/public/index.html index f2486f3..b1cc2a9 100644 --- a/src/public/index.html +++ b/src/public/index.html @@ -102,7 +102,7 @@ ${it.condition} ${it.description} ${it.hasBox ? 'Yes' : 'No'} - ${it.imagePath ? 'Yes' : ''} + ${it.hasImage ? `img ${it.id}` : ''} `; tableBody.appendChild(tr); diff --git a/src/routes/api.ts b/src/routes/api.ts index 0e19dd1..f128830 100644 --- a/src/routes/api.ts +++ b/src/routes/api.ts @@ -59,6 +59,24 @@ router.get('/items', async (req, res) => { } }); +// Serve item image as bytes +router.get('/items/:id/image', async (req, res) => { + try { + const id = Number(req.params.id); + const item = await db.getItemWithImage(id); + if (!item || !(item as any).imageData) { + return res.status(404).json({ error: 'Image not found' }); + } + const mime: string = (item as any).imageMime || 'application/octet-stream'; + res.setHeader('Content-Type', mime); + res.setHeader('Cache-Control', 'public, max-age=31536000, immutable'); + res.send(Buffer.from((item as any).imageData)); + } catch (err: any) { + console.error(err); + res.status(500).json({ error: err.message || 'Failed to fetch image' }); + } +}); + router.get('/items/:id/pdf', async (req, res) => { try { const id = Number(req.params.id);