Skip to content
Permalink
Browse files
Fixed issue with downloading a file
  • Loading branch information
hortonr6 committed Nov 25, 2019
1 parent 85a76c6 commit f6391daf8820c7da049a835926518627fb661496
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 2 deletions.
@@ -4,6 +4,7 @@
'use strict'

// Module Imports
const fs = require('fs')
const Koa = require('koa')
const Router = require('koa-router')
const views = require('koa-views')
@@ -210,13 +211,16 @@ router.get('/file', async ctx => {
const download = await new Download(dbName)
const [sourceUser, hash] = [ctx.query.u, ctx.query.h]
const filePath = await download.getFilePath(ctx.session.username, sourceUser, hash) // Throws if cannot access
ctx.attachment(filePath) // Lets the user donwload the file
const fileName = await download.getFileName(sourceUser, hash)
ctx.statusCode = 200
ctx.body = fs.createReadStream(filePath)
ctx.set('Content-disposition', `attachment; filename=${fileName}`) // Lets the user download the file
const remover = await new Remove(dbName)
const timer = 500000 // Sets timer amount
setTimeout(() => {
remover.removeFile(sourceUser, hash)
}, timer) // Delete the file after approx. 5 minutes to allow user time to download it
await ctx.render('download')
//await ctx.render('download')
} catch (err) {
await ctx.render('error', { message: err.message })
}
@@ -252,4 +252,26 @@ module.exports = class Download {
return fileList
}
}

/**
* Generates an array of objects which contain information about each available file
* File info objects contain the following fields: fileName, uploader, fileType, fileSize, fileCat, timeTillDelete, dateUploaded, url
* @async
* @memberof module:download
* @param {string} source - Username of the user who uploaded the file.
* @param {string} hash - Hash id of the file.
* @returns {string} returns the file name or untitled (if the name could not be acquired).
*/
async getFileName(source, hash) {
try {
if (source === undefined || source === '') throw new Error('No username given, file cannot be located')
if (hash === undefined || hash === '') throw new Error('No file name given, file cannot be located')
const sql = 'SELECT * FROM files WHERE user_upload = ? AND hash_id = ?;'
const record = await this.db.get(sql, source, hash) // Runs sql to find stored file name
const fileName = record.file_name
return fileName
} catch (err) {
return 'untitled'
}
}
}
@@ -787,3 +787,63 @@ describe('getFileSize()', () => {
done()
})
})

describe('getFileName()', () => {

test('gets the file name correctly', async done => {
expect.assertions(1)
const download = await new Download()
// Upload files to test with
const sql = 'INSERT INTO files (hash_id, file_name, extension, user_upload, target_user) VALUES(?, ?, ?, ?, ?)'
await download.db.run(sql, 'a94a8fe5', 'test.txt', 'txt', 'tester', 'testTarget')
// Get path to the file
const returnVal = await download.getFileName('tester', 'a94a8fe5')

expect(returnVal).toBe('test.txt')

done() // Finish the test
})

test('returns untitled when there is no source username given', async done => {
expect.assertions(1)
const download = await new Download()
// Run function with no source username
// Upload file
const sql = 'INSERT INTO files (hash_id, file_name, extension, user_upload, target_user) VALUES(?, ?, ?, ?, ?)'
await download.db.run(sql, 'a94a8fe5', 'test.txt', 'txt', 'tester', 'testTarget')
// Attempt to get path to the file
const returnVal = await download.getFileName(undefined, 'a94a8fe5')

expect(returnVal).toBe('untitled')
done()
})

test('retuns untitled when there is no hash name given', async done => {
expect.assertions(1)
const download = await new Download()
// Run function with no hash name
// Upload file
const sql = 'INSERT INTO files (hash_id, file_name, extension, user_upload, target_user) VALUES(?, ?, ?, ?, ?)'
await download.db.run(sql, 'a94a8fe5', 'test.txt', 'txt', 'tester', 'testTarget')
// Attempt to get path to the file
const returnVal = await download.getFileName('tester', undefined)

expect(returnVal).toBe('untitled')
done()
})

test('returns untitled when there are no arguments given', async done => {
expect.assertions(1)
const download = await new Download()
// Run function with no arguments
// Upload file
const sql = 'INSERT INTO files (hash_id, file_name, extension, user_upload, target_user) VALUES(?, ?, ?, ?, ?)'
await download.db.run(sql, 'a94a8fe5', 'test.txt', 'txt', 'tester', 'testTarget')
// Attempt to get path to the file
const returnVal = await download.getFileName()

expect(returnVal).toBe('untitled')

done()
})
})

0 comments on commit f6391da

Please sign in to comment.