Skip to content

Commit

Permalink
Profile image upload functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
CaptainFreak authored and J12934 committed Aug 1, 2018
1 parent baf6e6e commit 960857d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion frontend/src/app/navbar/navbar.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

<mat-toolbar-row *ngIf="isLoggedIn()" class="second-toolbar-row">
<span matTooltip="{{userEmail}}" matTooltipPosition="right">
<a mat-button routerLink="/profile"><i class="user-info" class="fas fa-user-circle fa-lg"></i></a>
<a mat-button href="/profile"><i class="user-info" class="fas fa-user-circle fa-lg"></i></a>
</span>
<a mat-button routerLink="/basket"><i class="fas fa-shopping-cart fa-lg"></i> Your Basket</a>
<a mat-button routerLink="/change-password"><i class="fas fa-user-secret fa-lg"></i> Change Password</a>
Expand Down
33 changes: 33 additions & 0 deletions routes/fileUpload.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const vm = require('vm')
const fs = require('fs')
const unzipper = require('unzipper')
const path = require('path')
const models = require('../models/index')
const insecurity = require('../lib/insecurity')

module.exports = function fileUpload () {
return (req, res, next) => {
Expand Down Expand Up @@ -76,6 +78,37 @@ module.exports = function fileUpload () {
}
}
}
if (utils.endsWith(file.originalname.toLowerCase(), '.jpg')) {
const loggedInUser = insecurity.authenticatedUsers.get(req.cookies.token)
if (loggedInUser) {
const buffer = file.buffer
const filename = file.originalname.toLowerCase()
fs.open('frontend/dist/frontend/assets/public/images/uploads/' + loggedInUser.data.id + '.jpg', 'w', function (err, fd) {
if (err) {
console.log('error opening file: ' + err)
}
fs.write(fd, buffer, 0, buffer.length, null, function (err) {
if (err) console.log('error opening file: ' + err)
fs.close(fd, function () {
})
})
})
models.User.findById(loggedInUser.data.id).then(user => {
user.updateAttributes({ profileImage: loggedInUser.data.id + '.jpg'}).then(user => {
console.log('profile Image updated succesfully.')
console.log(user.dataValues)
}).catch(error => {
next(error)
})
}).catch(error => {
next(error)
})
} else {
next(new Error('Blocked illegal activity by ' + req.connection.remoteAddress))
}
res.location('/profile');
res.redirect('/profile');
}
res.status(204).end()
}

Expand Down
2 changes: 1 addition & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ app.use(cookieParser('kekse'))

/* File Upload */
app.post('/file-upload', upload.single('file'), fileUpload())
app.post('/profile/imageupload', upload.single('file'), imageUpload())
//app.post('/profile/imageupload', upload.single('file'), imageUpload())

app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.text({type: '*/*'}))
Expand Down
6 changes: 3 additions & 3 deletions views/userProfile.jade
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ html(lang='en')
.col-sm-4
img.img-rounded(src='assets/public/images/uploads/'+profileImage, alt='Cinque Terre', width='304', height='236')
p(style='padding-left:36%;margin-top:8%;') usrname
form(action='/profile/imageupload' , style='margin-top:10%;',method='post',enctype='multipart/form-data')
form(action='/file-upload' , style='margin-top:10%;',method='post',enctype='multipart/form-data')
.form-group
input(type='file', accept='image/*')
input(type='file', accept='image/*',name='file')
.form-group
label ------------------------- Or -------------------------
.form-group
label(for='url') Image Url:
input#url.form-control(type='text')
input#url.form-control(type='text',name='file')
button.btn.btn-default(type='submit') Submit


Expand Down

0 comments on commit 960857d

Please sign in to comment.