BVB Source Codes

mongo-express Show gridfs.js Source code

Return Download mongo-express: download gridfs.js Source code - Download mongo-express Source code - Type:.js
  1. 'use strict';
  2.  
  3. var _             = require('lodash');
  4. var Busboy        = require('busboy');
  5. var GridFSStream  = require('gridfs-stream');
  6. var mongo         = require('mongodb');
  7. var utils         = require('../utils');
  8.  
  9. // var routes = function(config) {
  10. var routes = function () {
  11.   var exp = {};
  12.  
  13.   //view all files in a bucket
  14.   exp.viewBucket = function (req, res) {
  15.  
  16.     var files   = req.files;
  17.     var columns = ['filename', 'length']; // putting these here keeps them at the front/left
  18.  
  19.     var statsAvgChunk  = utils.bytesToSize(files.reduce((prev, curr) => prev + curr.chunkSize, 0) / files.length);
  20.     var statsTotalSize = utils.bytesToSize(files.reduce((prev, curr) => prev + curr.length, 0));
  21.  
  22.     // Iterate through files for a cleanup
  23.     for (var f in files) {
  24.       columns.push(Object.keys(files[f]));                        // Generate an array of columns used by all documents visible on this page
  25.       files[f].length     = utils.bytesToSize(files[f].length);   // Filesizes to something more readable
  26.       delete files[f].chunkSize;                                   // Already taken the average above, no need;
  27.     }
  28.  
  29.     columns = _.uniq(_.flatten(columns));
  30.     columns.splice(columns.indexOf('_id'), 1);
  31.     columns.splice(columns.indexOf('chunkSize'), 1);
  32.  
  33.     var ctx = {
  34.       buckets: res.locals.gridFSBuckets[req.dbName],
  35.       columns: columns,
  36.       files: req.files,
  37.       title: 'Viewing Bucket: ' + req.bucketName,
  38.       stats: {
  39.         avgChunk:  statsAvgChunk,
  40.         totalSize: statsTotalSize,
  41.       },
  42.     };
  43.  
  44.     res.render('gridfs', ctx);
  45.  
  46.   };
  47.  
  48.   // upload a file
  49.   exp.addFile = function (req, res) {
  50.     var busboy      = new Busboy({ headers: req.headers });
  51.     var newFileID   = new mongo.ObjectId();
  52.  
  53.     // Override the bucket name with what is currently selected
  54.     // https://github.com/aheckmann/gridfs-stream/blob/a3b7c4e48a08ac625cf7564304c83e56d6b93821/lib/index.js#L31
  55.     mongo.GridStore.DEFAULT_ROOT_COLLECTION = req.bucketName;
  56.  
  57.     var gfs         = new GridFSStream(req.db, mongo);
  58.  
  59.     busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
  60.  
  61.       if (!filename) {
  62.         req.session.error = 'No filename.';
  63.         return res.redirect('back');
  64.       }
  65.  
  66.       var writeStream = gfs.createWriteStream({
  67.         _id: newFileID,
  68.         filename: filename,
  69.         mode: 'w',
  70.         content_type: mimetype,
  71.       });
  72.       file.pipe(writeStream);
  73.     }).on('finish', function () {
  74.       req.session.success = 'File uploaded!';
  75.  
  76.       setTimeout(function () {
  77.         // short delay to allow Mongo to finish syncing
  78.         return res.redirect('back');
  79.       }, 500);
  80.     });
  81.  
  82.     req.pipe(busboy);
  83.  
  84.   };
  85.  
  86.   // download a file
  87.   exp.getFile = function (req, res) {
  88.  
  89.     // Override the bucket name with what is currently selected
  90.     // https://github.com/aheckmann/gridfs-stream/blob/a3b7c4e48a08ac625cf7564304c83e56d6b93821/lib/index.js#L31
  91.     mongo.GridStore.DEFAULT_ROOT_COLLECTION = req.bucketName;
  92.  
  93.     var gfs = new GridFSStream(req.db, mongo);
  94.  
  95.     gfs.findOne({ _id: req.fileID }, function (err, file) {
  96.       if (err) {
  97.         console.error(err);
  98.         req.session.error = 'Error: ' + err;
  99.         return res.redirect('back');
  100.       }
  101.  
  102.       if (!file) {
  103.         console.error('No file');
  104.         req.session.error = 'File not found!';
  105.         return res.redirect('back');
  106.       }
  107.  
  108.       res.set('Content-Type', file.contentType);
  109.       res.set('Content-Disposition', 'attachment; filename="' + encodeURI(file.filename) + '"');
  110.  
  111.       var readstream = gfs.createReadStream({
  112.         _id: file._id,
  113.       });
  114.  
  115.       readstream.on('error', function (err) {
  116.         console.error('Got error while processing stream ' + err.message);
  117.         req.session.error = 'Error: ' + err;
  118.         res.end();
  119.       });
  120.  
  121.       readstream.pipe(res);
  122.     });
  123.   };
  124.  
  125.   // delete a file
  126.   exp.deleteFile = function (req, res) {
  127.     // Override the bucket name with what is currently selected
  128.     // https://github.com/aheckmann/gridfs-stream/blob/a3b7c4e48a08ac625cf7564304c83e56d6b93821/lib/index.js#L31
  129.     mongo.GridStore.DEFAULT_ROOT_COLLECTION = req.bucketName;
  130.  
  131.     var gfs = new GridFSStream(req.db, mongo);
  132.  
  133.     gfs.remove({ _id: req.fileID }, function (err) {
  134.       if (err) {
  135.         req.session.error = 'Error: ' + err;
  136.         return res.redirect('back');
  137.       }
  138.  
  139.       req.session.success = 'File _id: "' + req.fileID + '" deleted! ';
  140.       setTimeout(function () {
  141.         // short delay to allow Mongo to finish syncing
  142.         return res.redirect('back');
  143.       }, 500);
  144.     });
  145.   };
  146.  
  147.   // add bucket
  148.   exp.addBucket = function (req, res) {
  149.     req.session.error('addBucket not implemented yet');
  150.     res.redirect('back');
  151.  
  152.     // req.session.success = 'Bucket created!';
  153.   };
  154.  
  155.   // delete bucket
  156.   exp.deleteBucket = function (req, res) {
  157.     req.session.error('deleteBucket not implemented yet');
  158.     res.redirect('back');
  159.  
  160.     // req.session.success = 'Bucket deleted!';
  161.   };
  162.  
  163.   exp.renameBucket = function (req, res) {
  164.     req.session.error('renameBucket not implemented yet');
  165.     res.redirect('back');
  166.   };
  167.  
  168.   return exp;
  169. };
  170.  
  171. module.exports = routes;
  172.  
downloadgridfs.js Source code - Download mongo-express Source code
Related Source Codes/Software:
XCL-Charts - Android charting libraries (XCL-Charts is a free c... 2017-05-17
scrollMonitor - A simple and fast API to monitor elements as you s... 2017-05-16
XCDYouTubeKit - YouTube video player for iOS, tvOS and macOS 2017-05-16
rails-erd - Generate Entity-Relationship Diagrams for Rails ap... 2017-05-16
np - A better `npm publish` 2017-05-16
kaminari - A Scope & Engine based, clean, powerful, custo... 2017-06-04
python-fire - Python Fire is a library for automatically generat... 2017-06-04
Dapper - Dapper - a simple object mapper for .Net 2017-06-05
pkg - Package your Node.js project into an executable ... 2017-06-05
packer - Packer is a tool for creating identical machine im... 2017-06-05
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top