-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
65 lines (46 loc) · 1.61 KB
/
server.js
File metadata and controls
65 lines (46 loc) · 1.61 KB
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
62
63
64
65
const express = require("express");
const mongoose = require("mongoose")
const multer = require("multer")
const path = require("path")
const File = require("./Model/File")
const bycrypt = require("bcrypt")
require("dotenv").config({path: "./config.env"})
const app = express()
app.use(express.json())
app.use(express.urlencoded({extended: true}))
const upload = multer({dest: "uploads"})
mongoose.connect(process.env.DATABASE.replace("<password>", process.env.DB_PASSWORD))
app.set("view engine", "ejs")
app.get("/", (req, res)=>{
res.render('index')
})
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 bycrypt.hash(req.body.password, 10)
}
const file = await File.create(fileData)
res.render("index", {fileLink: `${req.headers.origin}/file/${file.id}`})
} )
app.route("/file/:id").get(Download).post(Download)
async function Download(req, res){
const findFile = await File.findById(req.params.id)
if(findFile.password != null){
if(req.body.password == null){
res.render("password")
return
}
if(!(await bycrypt.compare(req.body.password, findFile.password))){
res.render("password", {err : true})
return
}
}
findFile.downloadCount++
await findFile.save()
res.download(findFile.path, findFile.originalName)
}
Port = process.env.PORT || 3000
app.listen(Port, ()=> console.log("port connected"))