diff --git a/index.js b/index.js index e044f02..8397ec0 100644 --- a/index.js +++ b/index.js @@ -61,10 +61,22 @@ router.get('/', async ctx => { // eslint-disable-next-line max-lines-per-function router.get('/myfiles', async ctx => { try { - if(ctx.session.authorised !== true) return ctx.redirect('/login?msg=You need to log in') + if(ctx.session.authorised !== true) return ctx.redirect('/login?msg=you need to log in') + + const file = await new File(dbName) + const data = {} - file = await new File(dbName) + if(ctx.query.del) { + data.name = ctx.query.name + data.ex = ctx.query.ex + + const fileName = data['name'] + + await file.delFromDir(path) + await file.deleteRecord(fileName) + } + file = await new File(dbName) await file.deleteExpired() user = await new User(dbName) const username = ctx.session.userName @@ -72,7 +84,7 @@ router.get('/myfiles', async ctx => { await user.UserFileDisplay(ctx, dbName, Database,username) // console.log(data) - } catch(err) { + } catch (err) { await ctx.render('error', {message: err.message}) } finally { if(ctx.session.authorised !== true) return ctx.redirect('/login?msg=You need to log in') @@ -80,8 +92,9 @@ router.get('/myfiles', async ctx => { file.tearDown() user.tearDown() } - }) + + router.post('/myfiles', koaBody, async ctx => { try { @@ -113,14 +126,33 @@ router.get('/about', async ctx => { } }) -router.get('/contact', async ctx => { +router.get('/ContactUs', async ctx => { try { + const data= {} await ctx.render('ContactUs') } catch (err) { await ctx.render('error', { message: err.message }) + + } }) +router.post('/ContactUs', koaBody, async ctx => { + + try{ + //extracts data from request + const fullName = ctx.request.body.fullName + const email2 = ctx.request.body.email2 + const phone = ctx.request.body.phone + const userMessage = ctx.request.body.userMessage + + await Mailer.emailContact(fullName, email2, phone, userMessage) + ctx.redirect('/ContactUs?msg=Your query has been sent') + + } catch (err){ + await ctx.render('error', { message: err.message }) + } +}) /** * The user registration page. @@ -283,5 +315,6 @@ router.post('/download', async ctx => { } }) + app.use(router.routes()) module.exports = app.listen(port, async() => console.log(`listening on port ${port}`)) diff --git a/modules/mailer.js b/modules/mailer.js index 56163e6..75ed164 100644 --- a/modules/mailer.js +++ b/modules/mailer.js @@ -2,12 +2,13 @@ Sources: https://www.w3schools.com/nodejs/nodejs_email.asp https://www.youtube.com/watch?v=FbDRGpB2eJ8 - */ +*/ 'use strict' const nodemailer = require('nodemailer') const crypto = require('crypto') +process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; const createHash = function(userString) { if (userString === '' || userString === undefined) throw new Error('String cannot be empty.') @@ -54,8 +55,35 @@ const sendMail = function(toAddress, link, userMessage) { } -module.exports = { - sendMail, - createHash +const emailContact = function(fullName, email2, phone, userMessage) { + const mailText = (fullName, email2, phone, userMessage) + + const transporter = nodemailer.createTransport({ + service: 'gmail', + auth: { + user: 'filesharingproject2@gmail.com', + pass: 'passpasspass' + } + }) + const mailOptions = { + from: 'filesharingproject2@gmail.com', + to: 'filesharingproject3@gmail.com', + subject: 'Customer Query', + text: (mailText) + } + + transporter.sendMail(mailOptions, (error, info) => { + if (error) { + console.log(error) + } else { + console.log(`Email sent: ${info.response}`) + } + }) + } +module.exports = { + sendMail, + createHash, + emailContact, + } diff --git a/modules/upload.js b/modules/upload.js index d1bde3c..ee4dfdc 100644 --- a/modules/upload.js +++ b/modules/upload.js @@ -141,6 +141,16 @@ module.exports = class File { throw err } } + async delFromDir(path) { + try { + // eslint-disable-next-line prefer-template + targetPath = 'private/' + targetPath + fs.unlinkSync(path) + console.log('File deleted from server') + } catch(err) { + throw err + } + } async countItems() { try { @@ -172,6 +182,18 @@ module.exports = class File { throw err } } + + async deleteRecord(fileName) { + try { + const sql = `DELETE FROM Files + WHERE FileName = '${fileName}'` + const data = await this.db.run(sql) + + } catch (err) { + throw err + } + } + /* istanbul ignore next */ async tearDown() { await this.db.close() diff --git a/public/ContactAbout.css b/public/ContactAbout.css new file mode 100644 index 0000000..d3a7fa0 --- /dev/null +++ b/public/ContactAbout.css @@ -0,0 +1,82 @@ +body { + font-family: sans-serif; + margin: 0; + padding: 0; +} + +header { + position: relative; + border-bottom: 10px solid #11B8FF; + margin-bottom: 5px; + padding: 13px 0; + } + + h1 { + + text-align: center; + font-size: 60px; + margin-top: 50px; + margin-bottom: 25px; + padding: 13px 0; + } + +p { + /*float: left;*/ + text-align: center; + font-size: 20px; +} + +#contacttxt{ + text-align:left; + align-content: center; + margin:10px 0; + background: rgba(red, green, blue, alpha); + border-radius: 2px; + font-family: 'Permanent Marker', cursive; + font-size: 15px; + font-weight: bold; + width: 25% +} + + +.wrapper{ + width:1170px; + margin: auto; +} + +#logo { + position: absolute; + margin-top: 0px; + width: 200px; + height: auto; + +} + +.nav-bar{ + float:right; + list-style:none; + margin-top: 30px; +} + +.nav-bar li{ + display: inline-block; + +} + +.nav-bar li a{ + color:#11B8FF; + text-decoration: none; + padding: 5px 20px; + font-family:'Roboto', sans-serif; + font-size: 25px; + transition: 0.8s; + +} + + + +.nav-bar li a:hover{ + background:#11B8FF; + color:#333; + +} \ No newline at end of file diff --git a/public/home.css b/public/home.css index 7723463..8d51631 100644 --- a/public/home.css +++ b/public/home.css @@ -52,8 +52,8 @@ header{ width: 200px; float:left; height: auto; - -} + +} .welcome-text{ position: absolute; diff --git a/unit tests/upload.spec.js b/unit tests/upload.spec.js new file mode 100644 index 0000000..b5a1fe7 --- /dev/null +++ b/unit tests/upload.spec.js @@ -0,0 +1,44 @@ +'use strict' + +const Uploads = require('../modules/upload.js') + + describe('deleteRecord()', () => { + test('the deletd file does not still exist in db', async done=>{ + expect.assertions(1) + const todo = await new Uploads() + + await expect(todo.deletedRecord('lab5')).toBeNull() + done() + + }) + }) + + +describe('upload()', () => { + test('File Name missing', async done => { + expect.assertions(1) + const todo = await new Uploads() + await expect(todo.upload( + 'png','',200,'Wed 20/20/19 15:20:20','path','Userpath')).rejects.toEqual(Error('String cannot be empty.')) + done() + }) + + test('File Not bigger than a GB', async done => { + expect.assertions(1) + const todo = await new Uploads() + await expect(todo.upload( + 'png','name',1073741824,'Wed 20/20/19 15:20:20', + 'path','Userpath')).rejects.toEqual(Error('Size cannot be equal or greater than a GB')) + done() + }) + + test('File Name string too long', async done => { + expect.assertions(1) + const todo = await new Uploads() + await expect(todo.upload( + 'png','K8DbQxhva2qECr7urmCOVOayGnf6OUh2MCjYC9VaO04m9SXLui', + 10750,'Wed 20/20/19 15:20:20','path','Userpath')).rejects.toEqual(Error('String size too big')) + done() + }) + +}) diff --git a/views/AboutUs.handlebars b/views/AboutUs.handlebars index af21980..e7a3161 100644 --- a/views/AboutUs.handlebars +++ b/views/AboutUs.handlebars @@ -6,25 +6,30 @@ About Us | Safe Share - + -

About Safe Share

- -
-

Safe Share is a website which allows logged in users to upload files to the server.
You can email a link to these files to your friends who can use this to download them.

-
+
+ + +
- + +
+

About Safe Share

+
- \ No newline at end of file +
+

Safe Share is a website which allows logged in users to upload files to the server.
You can email a link to these files to your friends who can use this to download them.

+
+ + \ No newline at end of file diff --git a/views/ContactUs.handlebars b/views/ContactUs.handlebars index 6674822..1f80882 100644 --- a/views/ContactUs.handlebars +++ b/views/ContactUs.handlebars @@ -4,26 +4,52 @@ Contact Us | Safe Share - + - - + + + -\ + + +
+ +

Contact Safe Share

-

-
- - - \ No newline at end of file + +
+ + + +
+ +

+

+ +

+

+ +

+

+ +

+

+

+ +
+ + + + + \ No newline at end of file diff --git a/views/home.handlebars b/views/home.handlebars index 875566e..e28dcd5 100644 --- a/views/home.handlebars +++ b/views/home.handlebars @@ -20,7 +20,7 @@ diff --git a/views/myfiles.handlebars b/views/myfiles.handlebars index adfb6f5..0e15de8 100644 --- a/views/myfiles.handlebars +++ b/views/myfiles.handlebars @@ -5,7 +5,7 @@ Share Drive - + @@ -125,9 +125,11 @@ {{this.Size}}KB {{this.FileType}} - + + +