Skip to content
Permalink
Browse files
Delete files from server
Before, when using delete file button it was only deleted from the database and not physical location on server. Now its deleted from server as well. Also managed to fix the upload function not working for some datatypes.
  • Loading branch information
czarniek committed Dec 1, 2019
1 parent 3482742 commit 8e338e1e6b2267478d8346192a2dde88e636df8e
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 47 deletions.
@@ -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,21 +87,18 @@ router.get('/myfiles', async ctx => {
user.tearDown()
}
})



router.post('/myfiles', koaBody, async ctx => {
try {
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!')

@@ -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,
}
}
@@ -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) {
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN +218 KB testingFiles/cool.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Empty file.
Binary file not shown.
BIN +4.28 MB testingFiles/sign.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN +27.5 MB testingFiles/sign.mov
Binary file not shown.
File renamed without changes.
@@ -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()', () => {
@@ -11,7 +11,7 @@
<nav id='nav1'>
<a href=""><img class='avatar'src='images/user.png'/></a>
<ul>
<li id='nav1Top'><a href="#"><img class='navImg'src="images/profile.png"><span>Profile</span></a></li>
<li id='nav1Top'><a href="/account"><img class='navImg'src="images/profile.png"><span>Profile</span></a></li>
<li><a href="/logout"><img class='navImg' src="images/signout.png"><span>Sign out</span></a></li>
</ul>
</nav>
@@ -13,12 +13,12 @@
</head>
<body>
<header>
<a href='/'><img id='logo' src='images/logo.png' alt='Share Drive logo' /></a>
<a href='/'><img id='logo' src='/images/logo.png' alt='Share Drive logo' /></a>

<section>
<form class="search" action="" method="get">
<input class="search-txt" type="text" name="q" value="{{query}}" placeholder="Search Files"/>
<input class="search-btn" type="image" src='images/loupe.png' border='0' alt='Submit' value="Search">
<input class="search-btn" type="image" src='/images/loupe.png' border='0' alt='Submit' value="Search">
</form>

{{!-- TODO: button that clears search --}}
@@ -28,10 +28,10 @@


<nav id='nav1'>
<a href=""><img class='avatar'src='images/user.png'/></a>
<a href=""><img class='avatar'src='/images/user.png'/></a>
<ul>
<li id='nav1Top'><a href="#"><img class='navImg'src="images/profile.png"><span>Profile</span></a></li>
<li><a href="/logout"><img class='navImg' src="images/signout.png"><span>Sign out</span></a></li>
<li id='nav1Top'><a href="/account"><img class='navImg'src="/images/profile.png"><span>Profile</span></a></li>
<li><a href="/logout"><img class='navImg' src="/images/signout.png"><span>Sign out</span></a></li>
</ul>
</nav>

@@ -41,7 +41,6 @@
<nav id='nav2'>
<ul>
<li ><a class='active' href="/myfiles">My Files</a></li>
<li><a href="/account">Account</a></li>
</ul>
</nav>

@@ -63,7 +62,7 @@
<form id='uploadForm' action="/myfiles" enctype="multipart/form-data" method="post">
<input type="file" name="_file" id='upload' class='inputUpload' onchange="onSubmit()" required>
<label for="upload">
<img src='images/plus.png'>
<img src='/images/plus.png'>
<p id='uploaded'>Upload your file</p>
{{!-- <img src='images/plus.png'>
<p id='uploaded'>Add your file</p> --}}
@@ -127,14 +126,14 @@
<td class='delete'>
<a href='/myfiles/?name={{this.FileName}}&del=true'>
<button type="button" class='button' >
<img class='smlImg' src='images/bin.png' alt='delete'>
<img class='smlImg' src='/images/bin.png' alt='delete'>
</button>
</a>
</td>
<td class='share'>
<a href='/transfer/?name={{this.FileName}}&ex={{this.FileType}}'>
<button type="button" class='button'>
<img class='smlImg' src='images/share.png' alt='share' >
<img class='smlImg' src='/images/share.png' alt='share' >
</button>

</a>

0 comments on commit 8e338e1

Please sign in to comment.