-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
81 lines (62 loc) · 1.97 KB
/
server.js
File metadata and controls
81 lines (62 loc) · 1.97 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
require("dotenv").config()
const express = require("express")
const app = express();
const multer = require("multer")
const File = require("./models/File")
const upload = multer({dest:"uploads"})
const mongoose = require("mongoose")
const bcrypt = require("bcrypt")
mongoose.connect(process.env.DATABASE_URLMDB)
app.use(express.static("public"))
app.set("view engine","ejs")
app.use(express.urlencoded({extended:true}))
app.get("/",(req,res)=>{
res.render("index")
})
app.route("/file/:id").get(handleDownload).post(handleDownload)
app.route("/fileGet/forceGetAll").get(async (req,res)=>{
const files = await File.find({})
const paths = files.map(e => {
return e.path
})
res.render("image",{image: paths})
})
app.route("/fileGet/:id").get(async (req,res)=>{
const file = await File.findById(req.params.id)
res.render("image",{image: file.path})
})
app.post("/upload", upload.single("file"),async (req,res)=>{
const filename = {
path: req.file.path,
originalName: req.file.originalname
}
if(req.body.password !=null && req.body.password !==""){
filename.password=await bcrypt.hash(req.body.password, 10)
}
const file = await File.create(filename)
console.log(file)
res.render("index",{fileLink:`${req.headers.origin}/file/${file.id}`})
})
app.get('/newFile',async (req,res)=>{
const data = await File.findById('632ec2228e61d460c74ab8b3')
res.json(data)
})
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})
return
}
file.downloadCount++
await file.save()
res.download(file.path, file.originalName)
}
app.listen(process.env.PORT,()=>{
console.log('Listening At:',process.env.PORT)
})