|
| 1 | +const express = require('express'); |
| 2 | +const bodyParser = require('body-parser'); |
| 3 | +const path = require('path'); |
| 4 | +const crypto = require('crypto'); |
| 5 | +const mongoose = require('mongoose'); |
| 6 | +const multer = require ('multer'); |
| 7 | +const GridfsStorage = require ('multer-gridfs-storage'); |
| 8 | +const Grid = require ('gridfs-stream'); |
| 9 | +const methodeOverride = require ('method-override'); |
| 10 | + |
| 11 | +const app = express(); |
| 12 | + |
| 13 | +//MIDDLE WARE FUNCTIONS |
| 14 | +app.use(bodyParser.json()); //https://stackoverflow.com/questions/38306569/what-does-body-parser-do-with-express |
| 15 | +app.use(methodeOverride('_method')); //https://stackoverflow.com/questions/23643694/whats-the-role-of-the-method-override-middleware-in-express-4 |
| 16 | +app.set('view engine','ejs'); |
| 17 | + |
| 18 | +//MONGODB Uniform Resource Identifier |
| 19 | +const mongoURI = "mongodb://localhost:27017/folder"; |
| 20 | + |
| 21 | +//Creating Mongo Connection |
| 22 | +const conn = mongoose.createConnection(mongoURI); |
| 23 | + |
| 24 | +// declaring gfs globally |
| 25 | +let gfs; |
| 26 | +//Using gridfs along with mongo |
| 27 | +conn.once('open', () => { |
| 28 | + // init stream |
| 29 | + gfs = Grid(conn.db, mongoose.mongo); |
| 30 | + gfs.collection('uploads'); |
| 31 | + }) |
| 32 | + |
| 33 | + |
| 34 | +// Create storage engine |
| 35 | +const storage = new GridfsStorage({ |
| 36 | + url: 'mongodb://localhost:27017/folder', |
| 37 | + file: (req, file) => { |
| 38 | + return new Promise((resolve, reject) => { |
| 39 | + crypto.randomBytes(16, (err, buf) => { // check module_used.js for details |
| 40 | + if (err) { |
| 41 | + return reject(err); |
| 42 | + } |
| 43 | + const filename = buf.toString('hex') + path.extname(file.originalname); |
| 44 | + const fileInfo = { |
| 45 | + filename: filename, |
| 46 | + bucketName: 'uploads'//name of bucket list and collection must be same |
| 47 | + }; |
| 48 | + resolve(fileInfo); |
| 49 | + }); |
| 50 | + }); |
| 51 | + } |
| 52 | + }); |
| 53 | + const upload = multer({ storage }); |
| 54 | + |
| 55 | +// GET / ---> LOAD FORM TO DISPLAY ALL THE FILES PRESENT |
| 56 | + |
| 57 | +app.get('/',(req,res)=>{ |
| 58 | + gfs.files.find().toArray((err,files)=>{ |
| 59 | + if(!files || files.length==0){ |
| 60 | + res.render('index',{files:false}); |
| 61 | + } |
| 62 | + else{ |
| 63 | + files.map(file=>{ //map is a strong function mainly to desmostrate for image objects to other files |
| 64 | + if(file.contentType === 'image/jpeg' || file.contentType === 'image/png' ) |
| 65 | + { |
| 66 | + file.isImage = true; |
| 67 | + } |
| 68 | + else |
| 69 | + { |
| 70 | + file.isImage = false; |
| 71 | + } |
| 72 | + |
| 73 | + }); |
| 74 | + res.render('index',{files:files}); |
| 75 | + } |
| 76 | + |
| 77 | + }); |
| 78 | +}); |
| 79 | + |
| 80 | + |
| 81 | +// route GET / i.e. loads form |
| 82 | +app.get('/',(req,res)=>{ |
| 83 | + var retVal = prompt("FILE UPLOADED!!"); |
| 84 | + res.render('index'); |
| 85 | +}); |
| 86 | + |
| 87 | +// route POST/upload i.e. Uploads file to db |
| 88 | +app.post('/upload', upload.single('file'), (req,res)=>{ // upload is middle ware used along with single as we are uploading only one file at a time and also in the bracket we have written the name of the file name of the command used in index.ejs page line no.17 |
| 89 | + //res.json({file:req.file}); --> this wsa to to just check the uploaded file's json text |
| 90 | + res.redirect('/'); // redirects immediately to the home page after the upload is over |
| 91 | + |
| 92 | +}); |
| 93 | + |
| 94 | +// GET/files --> DISPLAY ALL THE FILES PRESENT IN THE JSON |
| 95 | +app.get('/files',(req,res)=>{ |
| 96 | + gfs.files.find().toArray((err,files) =>{ |
| 97 | + // Check if files exists |
| 98 | + if(!files || files.length===0){ |
| 99 | + return res.status(404).json({ |
| 100 | + err: 'No files Found' |
| 101 | + }); |
| 102 | + } |
| 103 | + //File Exists |
| 104 | + return res.json(files); |
| 105 | + }); |
| 106 | +}); |
| 107 | + |
| 108 | +//GET/files/:filename --->SingleFile --> Display a single file in the JSon format |
| 109 | +app.get('/files/:filename',(req,res)=>{ |
| 110 | + gfs.files.findOne({ filename: req.params.filename},(err,file) =>{ |
| 111 | + // Check if files exists |
| 112 | + if(!file || file.length===0){ |
| 113 | + return res.status(404).json({ |
| 114 | + err: 'No files Found' |
| 115 | + }); |
| 116 | + } |
| 117 | + //File Exists |
| 118 | + return res.json(file); |
| 119 | + }); |
| 120 | +}); |
| 121 | +//GET/image/:filename --->SingleFile --> Display a IMAGE AFTER CHECKING FORMAT |
| 122 | +app.get('/image/:filename',(req,res)=>{ |
| 123 | + gfs.files.findOne({ filename: req.params.filename},(err,file) =>{ |
| 124 | + // Check if files exists |
| 125 | + if(!file || file.length===0){ |
| 126 | + return res.status(404).json({ |
| 127 | + err: 'No files Found' |
| 128 | + }); |
| 129 | + } |
| 130 | + //File Exists |
| 131 | + // CHECK IF ITS AN IMAGE |
| 132 | + if(file.contentType === 'image/jpeg' || file.contentType === 'image/png' ){ |
| 133 | + // READ o/p to browser |
| 134 | + const readstream = gfs.createReadStream(file); |
| 135 | + readstream.pipe(res); |
| 136 | + } |
| 137 | + else{ |
| 138 | + res.status(404).json({ |
| 139 | + err : 'NOT AN IMAGE' |
| 140 | + }); |
| 141 | + } |
| 142 | + }); |
| 143 | +}); |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | +const PORT = process.env.PORT || 5000; |
| 148 | + |
| 149 | +app.listen(PORT, ()=>console.log(`SERVER RUNNING AT PORT ${PORT}`)); |
0 commit comments