diff --git a/index.js b/index.js index 8397ec0..b19f178 100644 --- a/index.js +++ b/index.js @@ -11,7 +11,6 @@ const staticDir = require('koa-static') const bodyParser = require('koa-bodyparser') const koaBody = require('koa-body')({multipart: true, uploadDir: '.'}) const session = require('koa-session') -const mime = require('mime-types') const Database = require('sqlite-async') /* IMPORT CUSTOM MODULES */ @@ -62,27 +61,22 @@ router.get('/', async ctx => { router.get('/myfiles', async ctx => { try { if(ctx.session.authorised !== true) return ctx.redirect('/login?msg=you need to log in') - - const file = await new File(dbName) - const data = {} - - if(ctx.query.del) { - data.name = ctx.query.name - data.ex = ctx.query.ex - const fileName = data['name'] + file = await new File(dbName) - await file.delFromDir(path) - await file.deleteRecord(fileName) + //delete button + if(ctx.query.del) { + let fileName = ctx.query.name + const userName = ctx.session.userName + const data = await file.getFileInfo(fileName, userName) + fileName = data[0]['FileHash'] + await file.updateValue(fileName) } - - file = await new File(dbName) await file.deleteExpired() + user = await new User(dbName) const username = ctx.session.userName - //console.log(username) await user.UserFileDisplay(ctx, dbName, Database,username) - // console.log(data) } catch (err) { await ctx.render('error', {message: err.message}) @@ -93,7 +87,6 @@ router.get('/myfiles', async ctx => { user.tearDown() } }) - router.post('/myfiles', koaBody, async ctx => { @@ -101,13 +94,11 @@ router.post('/myfiles', koaBody, async ctx => { const userName = ctx.session.userName // extract the data from the request const { path, type } = ctx.request.files._file - const extension = mime.extension(type) - let name = ctx.request.files._file.name - name = name.replace(`.${extension}`, '') + const name = ctx.request.files._file.name const size = ctx.request.files._file.size file = await new File(dbName) - await file.upload(extension, name, size, userName, path) + await file.upload(name, size, userName, path) ctx.redirect('/myfiles?msg=Your file has been uploaded!') } catch (err) { @@ -149,7 +140,7 @@ router.post('/ContactUs', koaBody, async ctx => { await Mailer.emailContact(fullName, email2, phone, userMessage) ctx.redirect('/ContactUs?msg=Your query has been sent') - } catch (err){ + } catch (err) { await ctx.render('error', { message: err.message }) } }) @@ -261,7 +252,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/mailer.js b/modules/mailer.js index 75ed164..d87b80a 100644 --- a/modules/mailer.js +++ b/modules/mailer.js @@ -8,7 +8,7 @@ https://www.youtube.com/watch?v=FbDRGpB2eJ8 const nodemailer = require('nodemailer') const crypto = require('crypto') -process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; +process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0' const createHash = function(userString) { if (userString === '' || userString === undefined) throw new Error('String cannot be empty.') @@ -69,7 +69,7 @@ const emailContact = function(fullName, email2, phone, userMessage) { from: 'filesharingproject2@gmail.com', to: 'filesharingproject3@gmail.com', subject: 'Customer Query', - text: (mailText) + text: mailText } transporter.sendMail(mailOptions, (error, info) => { @@ -86,4 +86,4 @@ module.exports = { sendMail, createHash, emailContact, - } +} diff --git a/modules/upload.js b/modules/upload.js index ee4dfdc..9df54b5 100644 --- a/modules/upload.js +++ b/modules/upload.js @@ -41,7 +41,7 @@ module.exports = class File { } // eslint-disable-next-line max-statements - async upload(extension, _name,_size, _userName, path) { + async upload(_name,_size, _userName, path) { try{ if (_name === '') throw new Error('String cannot be empty.') const nameLimit = 50 @@ -50,10 +50,14 @@ module.exports = class File { if (_size >= oneGig) throw new Error('Size cannot be equal or greater than a GB') const toKB = 1000 const size = _size/toKB + const UserName = _userName const nameHash = Mailer.createHash(_name) + const extension = _name.split('.').pop() const nameExt = `${nameHash}.${extension}` + _name = _name.replace(`.${extension}`, '') + let targetPath = `/${ _userName }/${ nameExt}` @@ -144,7 +148,7 @@ module.exports = class File { async delFromDir(path) { try { // eslint-disable-next-line prefer-template - targetPath = 'private/' + targetPath + // targetPath = 'private/' + targetPath fs.unlinkSync(path) console.log('File deleted from server') } catch(err) { diff --git a/testingFiles/Cat03.jpg b/testingFiles/Cat03.jpg new file mode 100644 index 0000000..9b95fef Binary files /dev/null and b/testingFiles/Cat03.jpg differ diff --git a/testingFiles/cool.png b/testingFiles/cool.png new file mode 100644 index 0000000..af9e44c Binary files /dev/null and b/testingFiles/cool.png differ diff --git a/testingFiles/excel.xlsx b/testingFiles/excel.xlsx new file mode 100644 index 0000000..7a87b45 Binary files /dev/null and b/testingFiles/excel.xlsx differ diff --git a/testingFiles/hello.docx b/testingFiles/hello.docx new file mode 100644 index 0000000..e69de29 diff --git a/testingFiles/pdftest.pdf b/testingFiles/pdftest.pdf new file mode 100644 index 0000000..9202d66 Binary files /dev/null and b/testingFiles/pdftest.pdf differ diff --git a/testingFiles/sign.gif b/testingFiles/sign.gif new file mode 100644 index 0000000..4351600 Binary files /dev/null and b/testingFiles/sign.gif differ diff --git a/testingFiles/sign.mov b/testingFiles/sign.mov new file mode 100644 index 0000000..ec27290 Binary files /dev/null and b/testingFiles/sign.mov differ diff --git a/public/testingDownloads/testFile.txt b/testingFiles/testFile.txt similarity index 100% rename from public/testingDownloads/testFile.txt rename to testingFiles/testFile.txt diff --git a/unit tests/upload.spec.js b/unit tests/upload.spec.js index b5a1fe7..d162c54 100644 --- a/unit tests/upload.spec.js +++ b/unit tests/upload.spec.js @@ -2,16 +2,16 @@ 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('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()', () => { diff --git a/views/account.handlebars b/views/account.handlebars index 64e43c9..2452144 100644 --- a/views/account.handlebars +++ b/views/account.handlebars @@ -11,7 +11,7 @@ diff --git a/views/myfiles.handlebars b/views/myfiles.handlebars index 0e15de8..eacff19 100644 --- a/views/myfiles.handlebars +++ b/views/myfiles.handlebars @@ -13,12 +13,12 @@
- +
{{!-- TODO: button that clears search --}} @@ -28,10 +28,10 @@ @@ -41,7 +41,6 @@ @@ -63,7 +62,7 @@