diff --git a/index.js b/index.js index 8cde0a7..216eb25 100644 --- a/index.js +++ b/index.js @@ -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}`) diff --git a/routes/download.js b/routes/download.js index a8e5d1e..3c5229f 100644 --- a/routes/download.js +++ b/routes/download.js @@ -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 diff --git a/routes/files.js b/routes/files.js index 69fc743..773a378 100644 --- a/routes/files.js +++ b/routes/files.js @@ -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) => { diff --git a/routes/show.js b/routes/show.js index 3aae1e4..2a1139b 100644 --- a/routes/show.js +++ b/routes/show.js @@ -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 {