diff --git a/.gitignore b/.gitignore index 163d100..8dee31f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ node_modules data/ package-lock.json +upload/*.jpeg +upload/*.jpg +upload/*.png + diff --git a/helper/imageUpload.js b/helper/imageUpload.js index 7ee6b76..6868ffd 100644 --- a/helper/imageUpload.js +++ b/helper/imageUpload.js @@ -2,34 +2,38 @@ // jshint node: true "use strict"; -const path = require('path'); -const multer = require('multer'); -const { v4: uuidv4 } = require('uuid'); +const path = require("path"); +const multer = require("multer"); +const { v4: uuidv4 } = require("uuid"); const storage = multer.diskStorage({ destination: (req, file, cb) => { - cb(null, path.join(__dirname, "..", '/routes/tutorial/images')); + cb(null, path.join(__dirname, "..", "/upload")); }, filename: (req, file, cb) => { console.log(file); - var extension = file.originalname.split('.'); - extension = extension[extension.length - 1] - cb(null, uuidv4()+"."+extension); - } + var extension = file.originalname.split("."); + extension = extension[extension.length - 1]; + cb(null, uuidv4() + "." + extension); + }, }); var upload = multer({ storage: storage, fileFilter: (req, file, cb) => { - if (file.mimetype == "image/png" || file.mimetype == "image/jpg" || file.mimetype == "image/jpeg") { + if ( + file.mimetype == "image/png" || + file.mimetype == "image/jpg" || + file.mimetype == "image/jpeg" + ) { cb(null, true); } else { cb(null, false); - return cb(new Error('Only .png, .jpg and .jpeg format allowed!')); + return cb(new Error("Only .png, .jpg and .jpeg format allowed!")); } - } + }, }); module.exports = { - upload + upload, }; diff --git a/routes/upload/index.js b/routes/upload/index.js index 203ba9a..cbee7b2 100644 --- a/routes/upload/index.js +++ b/routes/upload/index.js @@ -2,31 +2,38 @@ // jshint node: true "use strict"; -const express = require('express'); -const path = require('path'); +const express = require("express"); +const path = require("path"); const router = express.Router(); -const { userAuthorization } = require('../../helper/userAuthorization'); +const { userAuthorization } = require("../../helper/userAuthorization"); -const { upload } = require('../../helper/imageUpload'); +const { upload } = require("../../helper/imageUpload"); -router.post('/uploadImage', userAuthorization, upload.single('files'), (req, res) => { +router.post( + "/uploadImage", + userAuthorization, + upload.single("files"), + (req, res) => { + console.log(req); if (!req.file) { - console.log("No file is available!"); - return res.send({ - success: false - }); + console.log("No file is available!"); + return res.send({ + success: false, + }); + } else { + console.log(req.file); + return res.send({ + success: true, + filename: req.file.filename, + }); } - else { - console.log(req.file); - return res.send({ - success: true, - filename: req.file.filename - }) - } -}) + } +); -router.get('/:imageName', (req,res) => { - res.sendFile(path.join(__dirname, "..", '/tutorial/images/', req.params.imageName)) -}) +router.get("/:imageName", (req, res) => { + res.sendFile( + path.join(__dirname, "..", "..", "/upload", req.params.imageName) + ); +}); -module.exports = router; \ No newline at end of file +module.exports = router; diff --git a/upload/.gitkeep b/upload/.gitkeep new file mode 100644 index 0000000..e69de29