Skip to content

Commit 6bb9e42

Browse files
committed
add multer to add files to the app
1 parent 46191eb commit 6bb9e42

File tree

5 files changed

+83
-13
lines changed

5 files changed

+83
-13
lines changed

controllers/users_controller.js

+54-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
const User=require('../models/user')
1+
const User=require('../models/user');
2+
const fs=require('fs');
3+
const path=require('path');
4+
5+
26

37
module.exports.profile = function(req, res){
48
User.findById(req.params.id,function(err,user){
@@ -11,17 +15,59 @@ module.exports.profile = function(req, res){
1115
};
1216

1317

14-
module.exports.update=function(req,res){
18+
module.exports.update=async function(req,res){
1519

16-
if(req.user.id==req.params.id)
17-
{
18-
User.findByIdAndUpdate(req.params.id,req.body,function(error,user){
20+
// if(req.user.id==req.params.id)
21+
// {
22+
// User.findByIdAndUpdate(req.params.id,req.body,function(error,user){
1923

20-
return res.redirect('back');
24+
// return res.redirect('back');
25+
26+
// });
27+
// }else{
28+
// return res.status(401).send('Unauthroized');
29+
// }
30+
31+
32+
33+
if(req.user.id==req.params.id)
34+
{
35+
try{
36+
37+
let user= await User.findById(req.params.id);
38+
User.uploadedAvatar(req,res,function(err){
39+
40+
if(err){
41+
console.log('****Multer error !',err);
42+
}
43+
44+
user.name=req.body.name;
45+
user.email=req.body.email;
46+
47+
if(req.file){
48+
49+
50+
if(user.avatar){
51+
52+
fs.unlinkSync(path.join(__dirname,'..',user.avatar));
53+
54+
}
55+
56+
// this is saving the path of the uploaded file into the avatar field in the user
57+
user.avatar=User.avatarPath+'/'+req.file.filename;
58+
}
59+
user.save();
60+
return res.redirect('back');
61+
});
2162

22-
});
63+
} catch(err){
64+
req.flash('error',err);
65+
return res.redirect('back');
66+
}
67+
2368
}else{
24-
return res.status(401).send('Unauthroized');
69+
req.flash('error','Unauthorized!');
70+
return res.status(401).send('Unauthroized');
2571
}
2672

2773
}

index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const MongoStore=require('connect-mongo')(session);
1212
const sassMiddleware=require('node-sass-middleware');
1313
const flash = require('connect-flash');
1414
const customMware=require('./config/middleware');
15-
const multer=require('multer');
15+
// const multer=require('multer');
1616

1717

1818
app.use(sassMiddleware({
@@ -30,6 +30,8 @@ app.use(express.urlencoded());
3030
app.use(cookieParser());
3131

3232
app.use(express.static('./assets'));
33+
// make the uploads path availaible to the browser
34+
app.use('/uploads',express.static(__dirname+'/uploads'));
3335

3436
app.use(expressLayouts);
3537
// extract style and scripts from sub pages into the layout

models/user.js

+22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
const mongoose=require('mongoose');
22

3+
// import multer
4+
const multer=require('multer');
5+
const path=require('path');
6+
const AVATAR_PATH=path.join('/uploads/users/avatars');
7+
38
const userSchema=new mongoose.Schema({
49

510
email:{
@@ -16,6 +21,9 @@ const userSchema=new mongoose.Schema({
1621

1722
type:String,
1823
required:true
24+
},
25+
avatar:{
26+
type:String
1927
}
2028

2129
}, {
@@ -24,6 +32,20 @@ const userSchema=new mongoose.Schema({
2432

2533
});
2634

35+
var storage = multer.diskStorage({
36+
destination: function (req, file, cb) {
37+
cb(null,path.join(__dirname,'..',AVATAR_PATH));
38+
},
39+
filename: function (req, file, cb) {
40+
cb(null, file.fieldname + '-' + Date.now());
41+
}
42+
});
43+
44+
// static functions
45+
userSchema.statics.uploadedAvatar=multer({storage: storage}).single('avatar');
46+
userSchema.statics.avatarPath=AVATAR_PATH;
47+
48+
2749
const User=mongoose.model('User',userSchema);
2850

2951
module.exports=User;
217 KB
Binary file not shown.

views/user_profile.ejs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
<link rel="stylesheet" href="/css/user_profile.css">
2-
<h1>
3-
Codeial / Profile Page
4-
</h1>
2+
3+
<img src="<%= user.avatar %>" alt="<%= user.name %>" width="100">
54

65

76

87
<% if(user.id==profile_user.id) {%>
98
109
<!-- if user matches show the form -->
11-
<form action="/users/update/<%= profile_user.id %>" method="POST">
10+
<form action="/users/update/<%= profile_user.id %>" enctype="multipart/form-data" method="POST">
1211
1312
<input type="text" name="name" placeholder="Your name" value="<%= profile_user.name %>" required>
1413
<input type="email" name="email" placeholder="Your email" value="<%= profile_user.email %>" required>
14+
<input type="file" name="avatar" placeholder="Profile Picture">
1515
<input type="submit" value="update">
1616
</form>
1717

0 commit comments

Comments
 (0)