Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
API Toucan
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Damien Armillon
API Toucan
Commits
9e0bbb40
Commit
9e0bbb40
authored
6 years ago
by
Damien
Browse files
Options
Downloads
Patches
Plain Diff
On peut transmettre le toucan en pdf et sa cover
parent
e2b24b7d
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
.gitignore
+1
-0
1 addition, 0 deletions
.gitignore
backend/.gitignore
+2
-1
2 additions, 1 deletion
backend/.gitignore
backend/routes/routesToucan.js
+17
-1
17 additions, 1 deletion
backend/routes/routesToucan.js
backend/utils/fileSaver.js
+54
-0
54 additions, 0 deletions
backend/utils/fileSaver.js
with
74 additions
and
2 deletions
.gitignore
0 → 100644
+
1
−
0
View file @
9e0bbb40
.vscode
\ No newline at end of file
This diff is collapsed.
Click to expand it.
backend/.gitignore
+
2
−
1
View file @
9e0bbb40
/node_modules
/node_modules
.env.json
\ No newline at end of file
This diff is collapsed.
Click to expand it.
backend/routes/routesToucan.js
+
17
−
1
View file @
9e0bbb40
var
express
=
require
(
"
express
"
);
var
express
=
require
(
"
express
"
);
var
fs
=
require
(
"
fs
"
);
var
path
=
require
(
"
path
"
);
var
upload
=
require
(
"
../utils/fileSaver
"
);
var
Toucan
=
require
(
"
../models/modelToucan
"
);
var
Toucan
=
require
(
"
../models/modelToucan
"
);
var
router
=
express
.
Router
();
var
router
=
express
.
Router
();
...
@@ -16,8 +19,21 @@ router.route("/toucans")
...
@@ -16,8 +19,21 @@ router.route("/toucans")
});
});
})
})
// Une route pour créer un toucan
// Une route pour créer un toucan
.
post
(
function
(
req
,
res
)
{
.
post
(
upload
.
fields
([{
name
:
"
toucan
"
,
maxCount
:
1
},
{
name
:
"
cover
"
,
maxCount
:
1
}]),
function
(
req
,
res
)
{
var
toucan
=
new
Toucan
(
req
.
body
);
var
toucan
=
new
Toucan
(
req
.
body
);
var
id
=
(
toucan
.
_id
).
toString
();
var
index
;
// On renome les fichier avec la clé de l'entrée dans la database
for
(
index
in
req
.
files
)
{
var
file
=
req
.
files
[
index
][
0
];
var
extension
=
path
.
extname
(
file
.
path
);
var
newPath
=
file
.
destination
+
"
/
"
+
id
+
extension
;
fs
.
rename
(
file
.
path
,
newPath
,
err
=>
{
if
(
err
)
{
res
.
err
(
err
);
}
});
}
toucan
.
save
(
function
(
err
)
{
toucan
.
save
(
function
(
err
)
{
if
(
err
)
{
if
(
err
)
{
res
.
send
(
err
);
res
.
send
(
err
);
...
...
This diff is collapsed.
Click to expand it.
backend/utils/fileSaver.js
0 → 100644
+
54
−
0
View file @
9e0bbb40
var
multer
=
require
(
"
multer
"
);
var
env
=
require
(
"
../.env
"
);
//Renvoie le nom de l'extension de file si son mime est autorisé, null sinon
var
isAllowedMime
=
function
(
file
)
{
var
mime
=
file
.
mimetype
;
var
ext
=
null
;
env
.
savedExtensions
.
forEach
(
extension
=>
{
var
index
=
extension
.
knownMime
.
indexOf
(
mime
);
if
(
index
>=
0
)
{
var
theMime
=
extension
.
knownMime
[
index
].
split
(
"
/
"
);
ext
=
theMime
[
theMime
.
length
-
1
];
}
});
return
ext
;
};
// On ne sauvegarde que si on connait l'extension
var
fileFilter
=
function
(
req
,
file
,
cb
)
{
var
shouldPass
=
isAllowedMime
(
file
);
cb
(
!
shouldPass
?
new
Error
(
"
Extension inconnue
"
):
null
,
shouldPass
);
};
// On envoie dans le dossier de destination en fonction de l'extension
var
saveDestination
=
function
(
req
,
file
,
cb
)
{
var
mime
=
file
.
mimetype
;
var
hasPassed
=
false
;
env
.
savedExtensions
.
forEach
(
extension
=>
{
if
(
extension
.
knownMime
.
includes
(
mime
))
{
hasPassed
=
true
;
cb
(
null
,
extension
.
path
);
}
});
if
(
!
hasPassed
)
{
cb
(
new
Error
(
"
Impossible d'enregistrer le fichier
"
));
}
};
//Les toucans sont només par leur date de parution
var
aFileNeedAName
=
function
(
req
,
file
,
cb
)
{
var
extension
=
isAllowedMime
(
file
);
cb
(
null
,
"
defaultName.
"
+
extension
);
};
var
storage
=
multer
.
diskStorage
({
destination
:
saveDestination
,
filename
:
aFileNeedAName
,
});
var
upload
=
multer
({
storage
:
storage
,
fileFilter
:
fileFilter
});
module
.
exports
=
upload
;
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment