-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
142 lines (125 loc) · 3.34 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
const express = require('express');
const bodyParser = require('body-parser')
const { listenerCount } = require('multer-gridfs-storage');
const path = require('path');
const crypto = require('crypto')
const mongoose = require('mongoose');
const multer = require('multer')
const GridFsStorage = require('multer-gridfs-storage')
const Grid = require('gridfs-stream')
const methodOverride = require('method-override');
const { response } = require('express');
const app = express();
//middleWare
app.use(bodyParser.json());
app.use(methodOverride('_method'))
app.set('view engine', 'ejs');
//Mongo URI
const mongoURL = "mongodb://localhost:27017/test"
//create mongo connection
const connection = mongoose.createConnection(mongoURL, {
useUnifiedTopology: true,
useNewUrlParser: true,
});
//init gfs
let gfs;
connection.once('open', () => {
// Init stream
gfs = Grid(connection.db,mongoose.mongo);
gfs.collection('uploads');
})
//create storage engine
const storage = new GridFsStorage({
url: mongoURL,
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 });
//@route POST /upload
//@desc Loads form
app.get('/',(req,res)=>{
gfs.files.find().toArray((err,files)=>{
// Check if files
if(!files || files.length === 0){
res.render('index',{files : false});
}else{
files.map(files=>{
if(files.contentType==='image/jpeg' || files.contentType === 'image/png'){
files.isImage = true;
}else{
files.isImage = false;
}
})
res.render('index',{files : files});
}
})
});
//@route POST /upload
//@desc Uploads file to DB
app.post('/upload',upload.single('file'),(req,res)=>{
// res.json({ file: req.file });
res.redirect('/');
})
// @route GET /files
// @desc Display all files in JSON
app.get('/files',(req,res)=>{
gfs.files.find().toArray((err,files)=>{
// Check if files
if(!files || files.length === 0){
return res.status(404).json({
err:'No files exist'
})
}
// Files exist
return res.json(files);
})
})
// @route GET /image/:filename
// @desc Display single file
app.get('/image/:filename',(req,res)=>{
gfs.files.findOne({filename : req.params.filename},(err,files)=>{
// Check if files
if(!files || files.length === 0){
return res.status(404).json({
err:'No files exist'
})
}
// Check if image
if(files.contentType === 'image/jpeg' || files.contentType === 'image/png'){
// Read output to browser
const readstream = gfs.createReadStream(files.filename);
readstream.pipe(res);
}else{
res.status(404).json({
err:'Not an image'
})
}
})
})
//@route DELETE /files/:id
// DESC Delete file
app.delete('/files/:id',(req,res)=>{
gfs.remove({_id:req.params.id,root:'uploads'},(err,gridStore)=>{
if(err){
return res.status(404).json({err:err})
}else{
res.redirect('/')
}
});
})
const port = 5000
app.listen(port,()=>
console.log(`Server started on port ${port}`));