Skip to content

Commit

Permalink
Download from browser complete
Browse files Browse the repository at this point in the history
  • Loading branch information
BurhanRaja committed Jul 30, 2022
1 parent bf096aa commit 233bb7c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ app.set('view engine', 'ejs')
// routes
app.use('/api/files', require("./routes/files"))
app.use('/files', require("./routes/show"))
app.use('/file/download', require('./routes/download'))

app.listen(port, () => {
console.log(`Example app listening on port http://localhost:${port}`)
Expand Down
17 changes: 15 additions & 2 deletions routes/download.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
const router = require('express').Router()
const file = require('../models/File')
const File = require('../models/File')

router.get('/:uuid', async (req, res) => {

try {
const file = await File.findOne({ uuid: req.params.uuid })

if (!file) {
return res.render('download', { error: "Link is expired" })
}

const filePath = `${__dirname}/../${file.path}`
res.download(filePath)
}

catch (error) {
res.status(500).send({ error: error.message })
}
})

module.exports = router
Expand Down
2 changes: 1 addition & 1 deletion routes/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const path = require('path')
const multer = require('multer')
const File = require('../models/File')
const { v4: uuid4 } = require('uuid')
const config = require("../project.config")
const config = require("../config")

const storage = multer.diskStorage({
destination: (req, file, cb) => {
Expand Down
2 changes: 1 addition & 1 deletion routes/show.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const router = require('express').Router()
const File = require('../models/File')
const config = require('../project.config')
const config = require('../config')

router.get('/:uuid', async (req, res) => {
try {
Expand Down

0 comments on commit 233bb7c

Please sign in to comment.