-
Notifications
You must be signed in to change notification settings - Fork 12
/
app.js
162 lines (143 loc) · 3.63 KB
/
app.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const express = require("express");
const app = express();
const crypto = require("crypto");
const path = require("path");
const mongoose = require("mongoose");
const multer = require("multer");
const GridFsStorage = require("multer-gridfs-storage");
// Middlewares
app.use(express.json());
app.set("view engine", "ejs");
// DB
const mongoURI = "mongodb://localhost:27017/node-file-upl";
// connection
const conn = mongoose.createConnection(mongoURI, {
useNewUrlParser: true,
useUnifiedTopology: true
});
// init gfs
let gfs;
conn.once("open", () => {
// init stream
gfs = new mongoose.mongo.GridFSBucket(conn.db, {
bucketName: "uploads"
});
});
// Storage
const storage = new GridFsStorage({
url: mongoURI,
file: (req, file) => {
return new Promise((resolve, reject) => {
crypto.randomBytes(16, (err, buf) => {
if (err) {
return reject(err);
}
const filename = buf.toString("hex") + path.extname(file.originalname);
const fileInfo = {
filename: filename,
bucketName: "uploads"
};
resolve(fileInfo);
});
});
}
});
const upload = multer({
storage
});
// get / page
app.get("/", (req, res) => {
if(!gfs) {
console.log("some error occured, check connection to db");
res.send("some error occured, check connection to db");
process.exit(0);
}
gfs.find().toArray((err, files) => {
// check if files
if (!files || files.length === 0) {
return res.render("index", {
files: false
});
} else {
const f = files
.map(file => {
if (
file.contentType === "image/png" ||
file.contentType === "image/jpeg"
) {
file.isImage = true;
} else {
file.isImage = false;
}
return file;
})
.sort((a, b) => {
return (
new Date(b["uploadDate"]).getTime() -
new Date(a["uploadDate"]).getTime()
);
});
return res.render("index", {
files: f
});
}
// return res.json(files);
});
});
app.post("/upload", upload.single("file"), (req, res) => {
// res.json({file : req.file})
res.redirect("/");
});
app.get("/files", (req, res) => {
gfs.find().toArray((err, files) => {
// check if files
if (!files || files.length === 0) {
return res.status(404).json({
err: "no files exist"
});
}
return res.json(files);
});
});
app.get("/files/:filename", (req, res) => {
gfs.find(
{
filename: req.params.filename
},
(err, file) => {
if (!file) {
return res.status(404).json({
err: "no files exist"
});
}
return res.json(file);
}
);
});
app.get("/image/:filename", (req, res) => {
// console.log('id', req.params.id)
const file = gfs
.find({
filename: req.params.filename
})
.toArray((err, files) => {
if (!files || files.length === 0) {
return res.status(404).json({
err: "no files exist"
});
}
gfs.openDownloadStreamByName(req.params.filename).pipe(res);
});
});
// files/del/:id
// Delete chunks from the db
app.post("/files/del/:id", (req, res) => {
gfs.delete(new mongoose.Types.ObjectId(req.params.id), (err, data) => {
if (err) return res.status(404).json({ err: err.message });
res.redirect("/");
});
});
const port = 5001;
app.listen(port, () => {
console.log("server started on " + port);
});