Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markdown file upload #4

Merged
merged 4 commits into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
node_modules
data/
upload/
package-lock.json
6 changes: 6 additions & 0 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' }));
Expand All @@ -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) {
Expand Down
39 changes: 18 additions & 21 deletions helper/imageUpload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!'));
}
}
});
Expand Down
23 changes: 0 additions & 23 deletions models/tutorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -47,9 +27,6 @@ const StepSchema = new mongoose.Schema({
type: [String],
default: undefined,
},
media: {
type: MediaSchema,
},
xml: {
type: String,
},
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
32 changes: 32 additions & 0 deletions routes/upload/index.js
Original file line number Diff line number Diff line change
@@ -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;