├── .gitignore ├── .github └── dependabot.yml ├── package.json ├── LICENSE ├── README.md ├── views └── index.ejs └── app.js /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: npm 4 | directory: "/" 5 | schedule: 6 | interval: monthly 7 | time: "23:30" 8 | open-pull-requests-limit: 10 9 | ignore: 10 | - dependency-name: mongoose 11 | versions: 12 | - 5.11.12 13 | - 5.11.14 14 | - 5.11.18 15 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nodejs-file-upload", 3 | "version": "1.1.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "start": "node app.js", 8 | "dev": "nodemon app.js" 9 | }, 10 | "private": true, 11 | "keywords": [], 12 | "author": "shubhambattoo", 13 | "license": "MIT", 14 | "dependencies": { 15 | "ejs": "^3.1.6", 16 | "express": "^4.18.2", 17 | "mongoose": "^5.12.12", 18 | "multer": "^1.4.2", 19 | "multer-gridfs-storage": "^4.2.0" 20 | }, 21 | "devDependencies": { 22 | "nodemon": "^2.0.7" 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Shubham Battoo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NodeJS GridFS Upload 2 | 3 | ## Using new gridfs bucket 4 | 5 | About GridFS [read more](https://docs.mongodb.com/manual/core/gridfs/) 6 | 7 | To read more about the GridFS bucket [read more](https://mongodb.github.io/node-mongodb-native/3.2/api/GridFSBucket.html) 8 | 9 | ## Methods working 10 | 15 | 16 | ## Author 17 | 18 | 👤 **Shubham Battoo** 19 | 20 | * Twitter: [@Shubham_batt](https://twitter.com/Shubham_batt) 21 | * Github: [@shubhambattoo](https://github.com/shubhambattoo) 22 | 23 | ## Usage 24 | 25 | Download or Clone the project, ``` cd node-js-file-upload ``` 26 | 27 | then install all the required dev and depenedencies by running 28 | 29 | ``` 30 | npm install 31 | ``` 32 | 33 | Serve the project 34 | 35 | ``` 36 | npm start 37 | ``` 38 | 39 | Server starts on the port [http://localhost:5001](http://localhost:5001) 40 | 41 | ### You can follow the following tutorial which describes how all the code works on this repo 42 | [https://dev.to/shubhambattoo/uploading-files-to-mongodb-with-gridfs-and-multer-using-nodejs-5aed](https://dev.to/shubhambattoo/uploading-files-to-mongodb-with-gridfs-and-multer-using-nodejs-5aed) 43 | 44 | 45 | 46 | Give a ⭐️ if this project helped you! 47 | -------------------------------------------------------------------------------- /views/index.ejs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Mongo File Upload 10 | 11 | 12 | 13 |
14 |
15 |
16 |

Lets upload some stuff

17 |
18 |
19 | 20 | 21 |
22 | 23 |
24 |
25 | <% if(files) { %> 26 | <% files.forEach(function(file) {%> 27 |
28 |
29 |
30 | <%= file.filename %> 31 |
32 |
33 |
34 | <% if (file.isImage) { %> 35 | 36 | <%} else { %> 37 | <%= file.filename %> 38 | <% } %> 39 |
40 | 45 |
46 | <%}) %> 47 | <% } else { %> 48 |

No files to show

49 | <% } %> 50 |
51 |
52 |
53 | 54 | 55 | 56 | 57 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | const express = require("express"); 2 | const app = express(); 3 | 4 | const crypto = require("crypto"); 5 | const path = require("path"); 6 | const mongoose = require("mongoose"); 7 | const multer = require("multer"); 8 | const GridFsStorage = require("multer-gridfs-storage"); 9 | 10 | // Middlewares 11 | app.use(express.json()); 12 | app.set("view engine", "ejs"); 13 | 14 | // DB 15 | const mongoURI = "mongodb://localhost:27017/node-file-upl"; 16 | 17 | // connection 18 | const conn = mongoose.createConnection(mongoURI, { 19 | useNewUrlParser: true, 20 | useUnifiedTopology: true 21 | }); 22 | 23 | // init gfs 24 | let gfs; 25 | conn.once("open", () => { 26 | // init stream 27 | gfs = new mongoose.mongo.GridFSBucket(conn.db, { 28 | bucketName: "uploads" 29 | }); 30 | }); 31 | 32 | // Storage 33 | const storage = new GridFsStorage({ 34 | url: mongoURI, 35 | file: (req, file) => { 36 | return new Promise((resolve, reject) => { 37 | crypto.randomBytes(16, (err, buf) => { 38 | if (err) { 39 | return reject(err); 40 | } 41 | const filename = buf.toString("hex") + path.extname(file.originalname); 42 | const fileInfo = { 43 | filename: filename, 44 | bucketName: "uploads" 45 | }; 46 | resolve(fileInfo); 47 | }); 48 | }); 49 | } 50 | }); 51 | 52 | const upload = multer({ 53 | storage 54 | }); 55 | 56 | // get / page 57 | app.get("/", (req, res) => { 58 | if(!gfs) { 59 | console.log("some error occured, check connection to db"); 60 | res.send("some error occured, check connection to db"); 61 | process.exit(0); 62 | } 63 | gfs.find().toArray((err, files) => { 64 | // check if files 65 | if (!files || files.length === 0) { 66 | return res.render("index", { 67 | files: false 68 | }); 69 | } else { 70 | const f = files 71 | .map(file => { 72 | if ( 73 | file.contentType === "image/png" || 74 | file.contentType === "image/jpeg" 75 | ) { 76 | file.isImage = true; 77 | } else { 78 | file.isImage = false; 79 | } 80 | return file; 81 | }) 82 | .sort((a, b) => { 83 | return ( 84 | new Date(b["uploadDate"]).getTime() - 85 | new Date(a["uploadDate"]).getTime() 86 | ); 87 | }); 88 | 89 | return res.render("index", { 90 | files: f 91 | }); 92 | } 93 | 94 | // return res.json(files); 95 | }); 96 | }); 97 | 98 | app.post("/upload", upload.single("file"), (req, res) => { 99 | // res.json({file : req.file}) 100 | res.redirect("/"); 101 | }); 102 | 103 | app.get("/files", (req, res) => { 104 | gfs.find().toArray((err, files) => { 105 | // check if files 106 | if (!files || files.length === 0) { 107 | return res.status(404).json({ 108 | err: "no files exist" 109 | }); 110 | } 111 | 112 | return res.json(files); 113 | }); 114 | }); 115 | 116 | app.get("/files/:filename", (req, res) => { 117 | gfs.find( 118 | { 119 | filename: req.params.filename 120 | }, 121 | (err, file) => { 122 | if (!file) { 123 | return res.status(404).json({ 124 | err: "no files exist" 125 | }); 126 | } 127 | 128 | return res.json(file); 129 | } 130 | ); 131 | }); 132 | 133 | app.get("/image/:filename", (req, res) => { 134 | // console.log('id', req.params.id) 135 | const file = gfs 136 | .find({ 137 | filename: req.params.filename 138 | }) 139 | .toArray((err, files) => { 140 | if (!files || files.length === 0) { 141 | return res.status(404).json({ 142 | err: "no files exist" 143 | }); 144 | } 145 | gfs.openDownloadStreamByName(req.params.filename).pipe(res); 146 | }); 147 | }); 148 | 149 | // files/del/:id 150 | // Delete chunks from the db 151 | app.post("/files/del/:id", (req, res) => { 152 | gfs.delete(new mongoose.Types.ObjectId(req.params.id), (err, data) => { 153 | if (err) return res.status(404).json({ err: err.message }); 154 | res.redirect("/"); 155 | }); 156 | }); 157 | 158 | const port = 5001; 159 | 160 | app.listen(port, () => { 161 | console.log("server started on " + port); 162 | }); 163 | --------------------------------------------------------------------------------