Commit 63e88e36 authored by François Lorne's avatar François Lorne
Browse files

Merge branch 'feature/change-pdf-endpoint' into 'master'

(feature): change pdf endpoint

See merge request !1
parents 481e3e77 79ab868b
/node_modules
.env.json
/private
pdf/*
!pdf/.gitkeep
img/*
!img/.gitkeep
......@@ -9,13 +9,13 @@
{
"type": "image",
"path": "/home/edsheebran/Bureau/imgToucan",
"path": "./img",
"knownMime" : ["image/png","image/jpeg"],
"extensions" : [".png",".jpeg"]
},
{
"type": "pdf",
"path": "/home/edsheebran/Bureau/pdfToucan",
"path": "./pdf",
"knownMime" : ["application/pdf"],
"extensions" : ["pdf"]
}
......
......@@ -6,13 +6,15 @@
"author": "Damien <damien.armillon@gmail.com>",
"license": "MIT",
"scripts": {
"lint": "yarn eslint ."
"start": "yarn nodemon app.js",
"lint": "yarn eslint ."
},
"dependencies": {
"axios": "^0.18.0",
"body-parser": "^1.18.3",
"celebrate": "^9.1.0",
"connect-mongo": "^2.0.3",
"cookie-parser": "^1.4.4",
"cors": "^2.8.5",
"express": "^4.16.4",
"express-session": "^1.16.1",
......@@ -22,6 +24,7 @@
"querystring": "^0.2.0"
},
"devDependencies": {
"eslint": "^6.6.0"
"eslint": "^6.6.0",
"nodemon": "^2.0.4"
}
}
......@@ -3,7 +3,7 @@ var fs = require("fs");
var path = require("path");
var { celebrate } = require("celebrate");
var { newToucan, validId, validGet } = require("../utils/schema");
var { newToucan, validId, validGet, validTitle } = require("../utils/schema");
var env = require("../.env");
var isLogged = require("../utils/authentification");
var upload = require("../utils/fileSaver");
......@@ -109,7 +109,7 @@ router.route("/delete/:id")
// Renvoie la cover du toucan avec l'id donné
router.route("/img/:id")
.get(celebrate({params: validId}),function(req,res) {
var imgPath = path.join(env.savedExtensions[0].path,"/",req.params.id);
var imgPath = path.resolve(env.savedExtensions[0].path,req.params.id);
var fileKnown = false;
env.savedExtensions[0].extensions.forEach(ext => {
if (fs.existsSync(imgPath+ext)){
......@@ -124,6 +124,19 @@ router.route("/img/:id")
}
});
router.use("/pdf",express.static(env.savedExtensions[1].path));
router.route("/pdf/:title")
.get(celebrate({params: validTitle}), function(req,res) {
Toucan.findOne({title: req.params.title}, (err,data) => {
if (err) {
res.end(500);
return;
} else if (!data) {
res.status(404).send("Toucan non trouvée");
return;
}
const id = data._id;
res.sendFile(path.resolve(env.savedExtensions[1].path,id+".pdf"));
}
);});
module.exports = router;
\ No newline at end of file
......@@ -7,6 +7,12 @@ var newToucan = Joi.object({
date: Joi.date().required()
});
var validTitle = Joi.object({
title: Joi.string()
.regex(/^[a-zA-Z0-9áàâäãåçéèêëíìîïñóòôöõúùûüýÿæœÁÀÂÄÃÅÇÉÈÊËÍÌÎÏÑÓÒÔÖÕÚÙÛÜÝŸÆŒ._\s'-]{1,60}$/) //Autorise la plupart des acents
.required(),
});
var validId = Joi.object({id: Joi.string().hex().length(24).required()});
var validGet = Joi.object({
limit: Joi.number().integer(),
......@@ -14,4 +20,4 @@ var validGet = Joi.object({
after: Joi.date().timestamp()
});
module.exports = { newToucan, validId, validGet };
\ No newline at end of file
module.exports = { newToucan, validTitle, validId, validGet };
\ No newline at end of file
This diff is collapsed.
......@@ -22,9 +22,9 @@ L'api possède les routes :
Si l'oppération est un succès, on renvoie un message de succès et l'id du toucan enregistré.
2. `toucan/pdf/:id` :
2. `toucan/pdf/:title` :
* `GET` Renvoie le pdf du toucan avec id comme _id dans la base de donnée.
* `GET` Renvoie le pdf du toucan avec title comme titre (title) dans la base de donnée.
3. `toucan/img/:id` :
......
......@@ -49,7 +49,7 @@ function AllToucan(props){
{toucans.map( toucan => {
return <ToucanCard
image={`${props.backURL}/toucan/img/${toucan["_id"]}`}
link={`${props.backURL}/toucan/pdf/${toucan["_id"]}.pdf`}
link={`${props.backURL}/toucan/pdf/${toucan.title}`}
header={toucan.title}
toucanId={toucan._id}
isAdmin={props.isAdmin}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment