-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
118 lines (85 loc) · 2.74 KB
/
server.js
File metadata and controls
118 lines (85 loc) · 2.74 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
require("dotenv").config();
const mongoose = require("mongoose");
const multer = require("multer");
const express = require("express");
const bcrypt = require("bcrypt");
const TinyURL = require('tinyurl');
const File = require("./models/Files");
const app = express();
app.use(express.urlencoded({extended: true}))
app.use(express.static('views'))
const upload = multer({dest: "upload"})
// console.log(process.env.DATABASE_URL);
mongoose.connect(process.env.DATABASE_URL);
app.set("view engine","ejs");
app.get("/", (req,res)=>{
res.render("landing");
})
app.get("/aboutus", (req,res)=>{
res.render("aboutus");
})
// contactus.ejs
app.get("/contactus", (req,res)=>{
res.render("contactus");
})
app.get("/index", (req,res) => {
res.render("index");
})
app.get("/privacyPolicy", (req,res) => {
res.render("privacyPolicy");
})
// termsandServices.ejs
app.get("/termsandServices", (req,res) => {
res.render("termsandServices");
})
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);
// console.log(file);
// res.send(file.originalName)
// res.render("index",{fileLink: `${req.headers.origin}/file/${file.id}`})
res.render("index",{fileLink: `${await shortenUrl(`${req.headers.origin}/file/${file.id}`)}`})
})
// console.log(Number(process.env.PORT));
// console.log(process.env.DATABASE_URL);
// Tiny url code
async function shortenUrl(url) {
try{
const shortUrl = await TinyURL.shorten(url);
console.log(`original URL: ${url}`);
console.log(`Shorten URL: ${shortUrl}`);
return shortUrl;
}catch(error){
console.log(`Error shortening ${url}:`,error);
throw error;
}
}
// app.get("/file/:id",handleDownload);
// app.post("/file/:id",handleDownload);
// we can write the above two lines in a shorter format like below
app.route("/file/:id").get(handleDownload).post(handleDownload);
async function handleDownload(req,res){
const file = await File.findById(req.params.id);
// console.log(req.body);
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();
console.log(file.downloadCount);
res.download(file.path, file.originalName);
}
app.listen(Number(process.env.PORT));