Ajouter une route pour prévisualiser une image redimensionnée par ID de projet et ID de commande
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const sharp = require('sharp');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const dbTester = require('../test/tester');
|
||||
@@ -129,4 +130,74 @@ router.get('/images/:measurementId', (req, res) => {
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /preview/{projectId}/{orderId}:
|
||||
* get:
|
||||
* summary: Retrieve a preview of an image by project and order ID
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: projectId
|
||||
* required: true
|
||||
* schema:
|
||||
* type: string
|
||||
* description: The project ID
|
||||
* - in: path
|
||||
* name: orderId
|
||||
* required: true
|
||||
* schema:
|
||||
* type: string
|
||||
* description: The order ID
|
||||
* responses:
|
||||
* 200:
|
||||
* description: A resized preview of the image
|
||||
* content:
|
||||
* image/jpeg:
|
||||
* schema:
|
||||
* type: string
|
||||
* format: binary
|
||||
* 404:
|
||||
* description: Image not found
|
||||
* 500:
|
||||
* description: Internal Server Error
|
||||
*/
|
||||
router.get('/preview/:projectId/:orderId', async (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';
|
||||
|
||||
try {
|
||||
const result = await db.query(query, [projectId, orderId]);
|
||||
if (result.rows.length === 0) {
|
||||
return res.status(404).json({ error: 'Image not found' });
|
||||
}
|
||||
|
||||
const imagePath = result.rows[0].path;
|
||||
|
||||
// Vérifier si l'image existe
|
||||
fs.access(imagePath, fs.constants.F_OK, async (err) => {
|
||||
if (err) {
|
||||
console.error('Image not found:', err);
|
||||
return res.status(404).json({ error: 'Image not found' });
|
||||
}
|
||||
|
||||
// Obtenir les dimensions originales de l'image
|
||||
const metadata = await sharp(imagePath).metadata();
|
||||
const width = Math.floor(metadata.width / 2);
|
||||
const height = Math.floor(metadata.height / 2);
|
||||
|
||||
// Redimensionner l'image à la moitié de ses dimensions d'origine
|
||||
const resizedImage = await sharp(imagePath)
|
||||
.resize(width, height)
|
||||
.toBuffer();
|
||||
|
||||
res.set('Content-Type', 'image/jpeg');
|
||||
res.send(resizedImage);
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('Error getting image preview:', err);
|
||||
return res.status(500).json({ error: 'Internal Server Error' });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user