diff --git a/.gitignore b/.gitignore index 31a2339..163d100 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ node_modules data/ -upload/ package-lock.json diff --git a/api.js b/api.js index b0ab15f..749df6f 100644 --- a/api.js +++ b/api.js @@ -20,6 +20,10 @@ api.use(cors()); api.set('views', path.join(__dirname, 'views')); api.set('view engine', 'jade'); +// increase size limit for image upload +api.use(express.urlencoded({limit: '50mb'})); +api.use(express.json({limit: '50mb'})); + api.use(logger('dev')); api.use(express.json()); // api.use(express.urlencoded({ extended: true, limit: '10mb' })); @@ -35,12 +39,14 @@ var shareRouter = require('./routes/share/index'); var galleryRouter = require('./routes/gallery/index'); var projectRouter = require('./routes/project/index'); var userRouter = require('./routes/user/index'); +var uploadRouter = require('./routes/upload/index'); api.use('/tutorial', tutorialRouter); api.use('/share', shareRouter); api.use('/gallery', galleryRouter); api.use('/project', projectRouter); api.use('/user', userRouter); +api.use('/upload', uploadRouter); // catch 404 and forward to error handler api.use(function(req, res, next) { diff --git a/helper/imageUpload.js b/helper/imageUpload.js index 0399f80..7ee6b76 100644 --- a/helper/imageUpload.js +++ b/helper/imageUpload.js @@ -2,33 +2,30 @@ // jshint node: true "use strict"; +const path = require('path'); const multer = require('multer'); -const fs = require('fs'); +const { v4: uuidv4 } = require('uuid'); -var storage = multer.diskStorage({ - destination: (req, file, cb) => { - cb(null, './upload'); - }, - filename: (req, file, cb) => { - cb(null, /*${req.user.id}_*/`${Date.now()}_${file.originalname}`); - } +const storage = multer.diskStorage({ + destination: (req, file, cb) => { + cb(null, path.join(__dirname, "..", '/routes/tutorial/images')); + }, + filename: (req, file, cb) => { + console.log(file); + var extension = file.originalname.split('.'); + extension = extension[extension.length - 1] + cb(null, uuidv4()+"."+extension); + } }); var upload = multer({ storage: storage, - fileFilter: function (req, file, callback) { - var fields = req.files.filter(file => /^steps\[\d\]\[media\]\[picture\]$/.test(file.fieldname)); - var extensionType = file.mimetype.split('image/')[1]; - if(fields.length !== req.files.length){ - req.fileValidationError = "Files are only allowed at this specific field {steps: [{media: {picture: File}}]}]"; - return callback(null, false); - } - else if(extensionType !== 'png' && extensionType !== 'jpg' && extensionType !== 'gif' && extensionType !== 'jpeg') { - req.fileValidationError = "Only images with extension 'PNG', 'JPEG', 'JPG' and 'GIF' are allowed."; - return callback(null, false); - } - else { - callback(null, true); + fileFilter: (req, file, cb) => { + 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!')); } } }); diff --git a/models/tutorial.js b/models/tutorial.js index af987af..5c9a20d 100644 --- a/models/tutorial.js +++ b/models/tutorial.js @@ -4,26 +4,6 @@ const mongoose = require("mongoose"); -const MediaSchema = new mongoose.Schema({ - youtube: { - type: String, - }, - picture: { - path: { - type: String, - }, - size: { - type: Number, - }, - contentType: { - type: String, - }, - originalName: { - type: String, - }, - }, -}); - const StepSchema = new mongoose.Schema({ type: { type: String, @@ -47,9 +27,6 @@ const StepSchema = new mongoose.Schema({ type: [String], default: undefined, }, - media: { - type: MediaSchema, - }, xml: { type: String, }, diff --git a/package.json b/package.json index 8507467..a444c19 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,10 @@ "moment": "^2.29.1", "mongoose": "^5.10.16", "morgan": "~1.9.1", - "multer": "^1.4.2", - "request": "^2.88.2" + "multer": "^1.4.4", + "path": "^0.12.7", + "request": "^2.88.2", + "uuid": "^8.3.2", + "uuidv4": "^6.2.12" } } diff --git a/routes/upload/index.js b/routes/upload/index.js new file mode 100644 index 0000000..203ba9a --- /dev/null +++ b/routes/upload/index.js @@ -0,0 +1,32 @@ +// jshint esversion: 8 +// jshint node: true +"use strict"; + +const express = require('express'); +const path = require('path'); +const router = express.Router(); +const { userAuthorization } = require('../../helper/userAuthorization'); + +const { upload } = require('../../helper/imageUpload'); + +router.post('/uploadImage', userAuthorization, upload.single('files'), (req, res) => { + if (!req.file) { + 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 + }) + } +}) + +router.get('/:imageName', (req,res) => { + res.sendFile(path.join(__dirname, "..", '/tutorial/images/', req.params.imageName)) +}) + +module.exports = router; \ No newline at end of file