diff --git a/index.js b/index.js index 0edf3f1..f00d485 100644 --- a/index.js +++ b/index.js @@ -1,11 +1,9 @@ #!/usr/bin/env node - //Routes File 'use strict' /* MODULE IMPORTS */ -const bcrypt = require('bcrypt-promise') const Koa = require('koa') const Router = require('koa-router') const views = require('koa-views') @@ -13,11 +11,8 @@ const staticDir = require('koa-static') const bodyParser = require('koa-bodyparser') const koaBody = require('koa-body')({multipart: true, uploadDir: '.'}) const session = require('koa-session') -const sqlite = require('sqlite-async') -const fs = require('fs-extra') const mime = require('mime-types') const Database = require('sqlite-async') -//const jimp = require('jimp') /* IMPORT CUSTOM MODULES */ const User = require('./modules/user') @@ -55,7 +50,11 @@ router.get('/', async ctx => { await ctx.render('error', {message: err.message}) } }) - +/** + * The Users Interaction Page and Script + * @name User Page + * @route {GET} /myfiles + */ // eslint-disable-next-line max-lines-per-function router.get('/myfiles', async ctx => { @@ -140,7 +139,6 @@ router.post('/register', koaBody, async ctx => { try { // extract the data from the request const body = ctx.request.body - const {path, type} = ctx.request.files.avatar // call the functions in the module const user = await new User(dbName) await user.register(body.user, body.pass) @@ -208,7 +206,7 @@ router.post('/transfer', koaBody, async ctx => { //TODO: username passed in email //Send the link in email - // await Mailer.sendMail(email, link, userMessage) + await Mailer.sendMail(email, link, userMessage) ctx.redirect('/myfiles?msg=Your file has been transfered!') diff --git a/modules/upload.js b/modules/upload.js index d81151b..c3036d6 100644 --- a/modules/upload.js +++ b/modules/upload.js @@ -28,14 +28,16 @@ module.exports = class File { async upload(extension, _name,_size,_date, _userName, path) { try{ - + if (_name === '') throw new Error('String cannot be empty.') + if(_name.length >= 50) throw new Error('String size too big') + if(_size >= 1073741824) throw new Error('Size cannot be equal or greater than a GB') const size = _size/1000 _date = _date.toString().slice(0,24) const nameHash = Mailer.createHash(_name) - const nameExt = nameHash + '.' +extension + const nameExt = `${nameHash}.${extension}` let targetPath = `/${ _userName }/${ nameExt}` @@ -45,7 +47,7 @@ module.exports = class File { await this.db.run(sql) //save to directory on server - targetPath = 'private' + targetPath + targetPath = `private${targetPath}` await fs.copy(path, targetPath) console.log('File saved on server') @@ -79,12 +81,14 @@ module.exports = class File { async getFileInfo(_fileName, _userName) { try { + _userName = '' + console.log(_userName) //TODO: Only search table belonging to specific user const sql = `SELECT * FROM Files WHERE FileHash LIKE '${_fileName}'` const data = await this.db.all(sql) - + return data } catch (err) { diff --git a/public/login.css b/public/login.css index 9770369..1e906e9 100644 --- a/public/login.css +++ b/public/login.css @@ -104,4 +104,9 @@ body { font-family: sans-serif; color: white; font-size: 20px; +} + + +.Error{ + color:white; } \ No newline at end of file diff --git a/unit tests/mailer.spec.js b/unit tests/mailer.spec.js index 1fcfd75..3d010b1 100644 --- a/unit tests/mailer.spec.js +++ b/unit tests/mailer.spec.js @@ -1,6 +1,6 @@ 'use strict' -const hashes = require('../modules/hashes.js') +const hashes = require('../modules/mailer.js') describe('createHash()', () => { // block of tests diff --git a/unit tests/upload.spec.js b/unit tests/upload.spec.js new file mode 100644 index 0000000..b594a01 --- /dev/null +++ b/unit tests/upload.spec.js @@ -0,0 +1,32 @@ +'use strict' + +const Uploads = require('../modules/upload.js') + +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/unit tests/user.spec.js b/unit tests/user.spec.js index 886d344..a9da915 100644 --- a/unit tests/user.spec.js +++ b/unit tests/user.spec.js @@ -3,6 +3,7 @@ const Accounts = require('../modules/user.js') + describe('register()', () => { test('register a valid account', async done => { @@ -38,6 +39,7 @@ describe('register()', () => { done() }) + }) describe('uploadPicture()', () => { @@ -73,4 +75,5 @@ describe('login()', () => { done() }) + }) diff --git a/views/error.handlebars b/views/error.handlebars index c5539ba..ed6397b 100644 --- a/views/error.handlebars +++ b/views/error.handlebars @@ -8,7 +8,9 @@ +

An Error Has Occurred

{{message}}

+