Skip to content

Commit

Permalink
Merge pull request #6 from sensebox/development
Browse files Browse the repository at this point in the history
add image upload
  • Loading branch information
mariopesch authored Feb 21, 2022
2 parents d0a3d60 + b0cd332 commit 2b87f24
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 33 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
node_modules
data/
package-lock.json
upload/*.jpeg
upload/*.jpg
upload/*.png

28 changes: 16 additions & 12 deletions helper/imageUpload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
49 changes: 28 additions & 21 deletions routes/upload/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
module.exports = router;
Empty file added upload/.gitkeep
Empty file.

0 comments on commit 2b87f24

Please sign in to comment.