Advertisement
Guest User

Untitled

a guest
Aug 15th, 2017
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. /** Setting up storage using multer-gridfs-storage */
  2. var storage = GridFsStorage({
  3. gfs : gfs,
  4. filename: function (req, file, cb) {
  5. var datetimestamp = Date.now();
  6. cb(null, file.fieldname + '-' + datetimestamp + '.' + file.originalname.split('.')[file.originalname.split('.').length -1]);
  7. },
  8. /** With gridfs we can store aditional meta-data along with the file */
  9. metadata: function(req, file, cb) {
  10. cb(null, { originalname: file.originalname });
  11. },
  12. root: 'ctFiles' //root name for collection to store files into
  13. });
  14.  
  15. var upload = multer({ //multer settings for single upload
  16. storage: storage
  17. }).single('file');
  18.  
  19. /** API path that will upload the files */
  20. app.post('/upload', function(req, res) {
  21. upload(req,res,function(err){
  22. if(err){
  23. res.json({error_code:1,err_desc:err});
  24. return;
  25. }
  26. console.log(res.file);
  27. console.log(res[0].file);
  28. res.json({error_code:0,err_desc:null});
  29. });
  30. });
  31.  
  32. var UserSchema = new Schema({
  33. name: String,
  34. email: {
  35. type: String,
  36. lowercase: true
  37. },
  38. filepath: String,
  39. });
  40.  
  41. {
  42. "_id" : ObjectId("58fb894111387b23a0bf2ccc"),
  43. "filename" : "file-1492879681306.PNG",
  44. "contentType" : "image/png",
  45. "length" : 67794,
  46. "chunkSize" : 261120,
  47. "uploadDate" : ISODate("2017-04-22T16:48:01.350Z"),
  48. "aliases" : null,
  49. "metadata" : {
  50. "originalname" : "Front.PNG"
  51. },
  52. "md5" : "404787a5534d0479bd55b2793f2a74b5"
  53. }
  54.  
  55. {
  56.  
  57. "name" :"asdf",
  58. "email" : "asdf@gmail.com",
  59. "filepath":"file/file-1492879681306.PNG"
  60.  
  61. }
  62.  
  63. {
  64. "_id" : ObjectId("58fb894111387b23a0bf2ccc"),
  65. "filename" : "file-1492879681306.PNG",
  66. "contentType" : "image/png",
  67. "length" : 67794,
  68. "chunkSize" : 261120,
  69. "uploadDate" : ISODate("2017-04-22T16:48:01.350Z"),
  70. "aliases" : null,
  71. "metadata" : {
  72. "originalname" : "Front.PNG",
  73. "name" :"asdf",
  74. "email" : "asdf@gmail.com",
  75. "filepath":"file/file-1492879681306.PNG"
  76. },
  77. "md5" : "404787a5534d0479bd55b2793f2a74b5"
  78. }
  79.  
  80. ....
  81. /** With gridfs we can store aditional meta-data along with the file */
  82. metadata: function(req, file, cb) {
  83. var metadata = {
  84. originalname: file.originalname,
  85. // get this information somehow
  86. name :"asdf",
  87. email : "asdf@gmail.com",
  88. filepath:"file/file-1492879681306.PNG"
  89. };
  90. cb(null, metadata);
  91. },
  92. ....
  93.  
  94. const mongodb = require('mongodb');
  95. const GridFSBucket = mongodb.GridFSBucket;
  96. const MongoClient = mongodb.MongoClient;
  97.  
  98. MongoClient.connect('mongodb://yourhost:27017/database').then((db) => {
  99. const bucket = new GridFSBucket(db, {bucketName: 'ctFiles'});
  100.  
  101. bucket
  102. .find({metadata: {'email' : 'asdf@gmail.com'}})
  103. .toArray()
  104. .then((fileInfoArr) => {
  105. console.log(fileInfoArr);
  106. });
  107. });
  108.  
  109. var UserSchema = new Schema({
  110. name: String,
  111. email: {
  112. type: String,
  113. lowercase: true
  114. },
  115. filepath: String,
  116. fileId: Schema.Types.ObjectId
  117. });
  118.  
  119. UserModel.findOne({'email' : 'asdf@gmail.com'}, function (err, user) {
  120. // deal with error
  121. // store and get the db object somehow
  122.  
  123. const bucket = new GridFSBucket(db, {bucketName: 'ctFiles'});
  124.  
  125. // A download stream reads the file from GridFs
  126. const readStream = bucket.openDownloadStream(user.fileId));
  127.  
  128. readStream.pipe(/* some writable stream */);
  129. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement