generated from lucien/api-template
22 lines
No EOL
686 B
JavaScript
22 lines
No EOL
686 B
JavaScript
const express = require('express');
|
|
const { getConnection, getAttachment } = require('../libs/mysql');
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
|
|
const router = express.Router();
|
|
|
|
router.get('/:file_name', async (req, res) => {
|
|
const { file_name } = req.params;
|
|
|
|
const connection = await getConnection();
|
|
const attachment = await getAttachment(connection, file_name);
|
|
connection.end();
|
|
|
|
if (!attachment[0]) {
|
|
return res.status(404).send({ error: 'File not found' });
|
|
}
|
|
|
|
res.sendFile(path.join(__dirname, `../data/attachments/${attachment[0].file_name}`), { headers: { 'Content-Type': 'image' } });
|
|
})
|
|
|
|
module.exports = router; |