Réorganisation des routes API et ajout de la gestion des téléchargements d'images
This commit is contained in:
89
routes/imageRoutes.js
Normal file
89
routes/imageRoutes.js
Normal file
@@ -0,0 +1,89 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const dbTester = require('../test/tester');
|
||||
const db = require('../db');
|
||||
const serverError = require('../utils/serverError');
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /smile:
|
||||
* get:
|
||||
* summary: Retrieve a smile image
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Smile image retrieved successfully
|
||||
* content:
|
||||
* image/jpeg:
|
||||
* schema:
|
||||
* type: string
|
||||
* format: binary
|
||||
* 404:
|
||||
* description: Image not found
|
||||
*/
|
||||
router.get('/smile', (req, res) => {
|
||||
const imagePath = dbTester.getSmileImage();
|
||||
fs.access(imagePath, fs.constants.F_OK, (err) => {
|
||||
if (err) {
|
||||
console.error('Image not found:', err);
|
||||
return res.status(404).json({ error: 'Image not found' });
|
||||
}
|
||||
res.sendFile(imagePath);
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /images/{projectId}/{orderId}:
|
||||
* get:
|
||||
* summary: Retrieve an image by project ID and order ID
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: projectId
|
||||
* required: true
|
||||
* schema:
|
||||
* type: string
|
||||
* description: The ID of the project
|
||||
* - in: path
|
||||
* name: orderId
|
||||
* required: true
|
||||
* schema:
|
||||
* type: string
|
||||
* description: The ID of the order
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Image retrieved successfully
|
||||
* content:
|
||||
* image/jpeg:
|
||||
* schema:
|
||||
* type: string
|
||||
* format: binary
|
||||
* 404:
|
||||
* description: Image not found
|
||||
* 500:
|
||||
* description: Internal server error
|
||||
*/
|
||||
router.get('/images/:projectId/:orderId', (req, res) => {
|
||||
const projectId = req.params.projectId;
|
||||
const orderId = req.params.orderId;
|
||||
const query = 'SELECT path FROM public.measurements WHERE project_id = $1 AND order_id = $2';
|
||||
db.query(query, [projectId, orderId], (err, results) => {
|
||||
if (err) {
|
||||
return serverError.sendError('Error getting image:', res, err);
|
||||
}
|
||||
if (results.rows.length === 0) {
|
||||
return res.status(404).json({ error: 'Image not found' });
|
||||
}
|
||||
const imagePath = results.rows[0].path;
|
||||
fs.access(imagePath, fs.constants.F_OK, (err) => {
|
||||
if (err) {
|
||||
console.error('Image not found:', err);
|
||||
return res.status(404).json({ error: 'Image not found' });
|
||||
}
|
||||
res.download(imagePath);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user