Skip to content
Permalink
Browse files
Merge pull request #13 from georgiak/delete-after-download
Delete file after downloaded
  • Loading branch information
czarniek committed Nov 29, 2019
2 parents 835efbc + e5af0d4 commit 30bc63b3b35791572e5c1f52c03a4018a3eb82b7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
@@ -65,7 +65,7 @@ router.get('/myfiles', async ctx => {

file = await new File(dbName)

await file.deleteFiles()
await file.deleteExpired()

user = await new User(dbName)

@@ -229,25 +229,42 @@ router.post('/transfer', koaBody, async ctx => {

router.get('/download/:usrName/:name', async ctx => {
try {
let fileName = ctx.params.name
const fileName = ctx.params.name
const userName = ctx.params.usrName

file = await new File(dbName)

const fileData = await file.getFileInfo(fileName, userName)
console.log(fileData)
//generate link with a direct path to file
const link = await file.generateLink(fileName, userName, false)

fileName = `${fileData[0]['FileName']}.${fileData[0]['FileType']}`
const data = {}
data.fileName = `${fileData[0]['FileName']}.${fileData[0]['FileType']}`
data.fileHash = fileData[0]['FileHash']

await ctx.render('download', { link: link, file: fileName })
await ctx.render('download', { link: link, file: data })
} catch (err) {
await ctx.render('error', { message: err.message })
} finally {
file.tearDown()
}
})

router.post('/download', async ctx => {
try {
const downloaded = ctx.request.body.downloaded
const fileHash = ctx.request.body.fileHash

if (downloaded) {
file = await new File(dbName)
await file.updateValue(fileHash)
}

return ctx.redirect('/')
} catch (err) {
await ctx.render('error', { message: err.message })
}
})

app.use(router.routes())
module.exports = app.listen(port, async() => console.log(`listening on port ${port}`))
@@ -24,7 +24,7 @@ module.exports = class File {
const sql = `CREATE TABLE IF NOT EXISTS Files (
id INTEGER PRIMARY KEY AUTOINCREMENT, FileName TEXT, FileType BLOB,
FilePath TEXT, Size INT, Date DATE NOT NULL DEFAULT (date('now')),
FileHash TEXT, ExpDate TEXT);`
FileHash TEXT, ExpDate TEXT, Downloaded TEXT DEFAULT false);`
try{
await this.db.run(sql)
// CURRENT_TIMESTAMP
@@ -74,10 +74,11 @@ module.exports = class File {
}

}
async deleteFiles() {
async deleteExpired() {
try{
// eslint-disable-next-line quotes
let sql = `SELECT FileHash, FilePath FROM Files WHERE date('now') > date(Date, '+3 days')`
let sql = `SELECT FileHash, FilePath FROM Files WHERE date('now') > date(Date, '+3 days')
OR Downloaded LIKE true`
const data = await this.db.all(sql)

for (const key in data) {
@@ -96,6 +97,20 @@ module.exports = class File {

}

async updateValue(_fileHash) {
try {
// eslint-disable-next-line quotes
const sql = `UPDATE Files SET Downloaded=true WHERE FileHash LIKE '${_fileHash}'`

await this.db.run(sql)

return true
} catch (err) {
throw err
}

}


async generateLink(_fileName, username, fullLink) {
try {
@@ -28,8 +28,12 @@
<h1>Download your file</h1>

{{#if link}}
<p id='filename'>{{file}}</p>
<a href='{{link}}' class='bigButton' id='downloadButton' download="{{file}}">Download</a>
<p id='filename'>{{file.fileName}}</p>
<form action='/download' id='form-id' method="post">
<input type='hidden' name='downloaded' value='true'>
<input type='hidden' name='fileHash' value='{{file.fileHash}}'>
<a href='{{link}}' onclick="document.getElementById('form-id').submit();" class='bigButton' id='downloadButton' download="{{file.fileName}}">Download</a>
</form>
{{else}}
<p>Something went wrong. Sorry!</p>
{{/if}}

0 comments on commit 30bc63b

Please sign in to comment.