-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
61 lines (49 loc) · 1.58 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const express = require("express")
const app = express()
const multer = require("multer")
const PORT = 3000;
const mongoose = require("mongoose")
const bcrypt = require("bcrypt")
const File = require("./models/File")
require("dotenv").config()
const upload = multer({dest: "uploads"})
app.use(express.urlencoded({extended: true}))
app.set("view engine", "ejs")
app.get("/", (req,res)=>{
res.render("index")
})
app.get("/file/:id", handleDownload)
app.post("/file/:id", handleDownload)
app.post("/upload", upload.single("file"), async(req, res)=>{
const fileData = {
path: req.file.path,
originalname: req.file.originalname
}
if(req.body.password != null && req.body.password !== ""){
fileData.password = await bcrypt.hash(req.body.password, 10)
}
const file = await File.create(fileData)
res.render("index",{ fileLink: `${req.headers.origin}/file/${file.id}`})
})
async function handleDownload(req, res){
const file = await File.findById(req.params.id)
if(file.password != null){
if(req.body.password == null){
res.render("password")
return
}
if(!(await bcrypt.compare(req.body.password, file.password))){
res.render("password", {error: true})
console.log("2")
return
}
}
file.downloadCount++
await file.save()
console.log(file.downloadCount)
res.download(file.path, file.originalname)
}
app.listen(PORT ,async ()=>{
await mongoose.connect(process.env.MONGO_URI)
console.log(`Server running on PORT ${PORT} `)
})