Skip to content

Commit

Permalink
Merge branch 'develop' into origin/master
Browse files Browse the repository at this point in the history
  • Loading branch information
Walid LARABI committed Feb 3, 2020
2 parents a35072d + 1e4540a commit 22af185
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 30 deletions.
51 changes: 22 additions & 29 deletions back-end/server/controller/UploadController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const multer = require('multer');
const GridFsStorage = require('multer-gridfs-storage');
const jwt = require('jsonwebtoken');
const config = require('../config');
const ObjectID = require('mongodb').ObjectID;

let storage = new GridFsStorage({
url: config.DbConfig.DBURL,
file: (req, file) => {
Expand Down Expand Up @@ -43,43 +45,34 @@ module.exports.getFile = (req, res) => {
//never be allowed in a production app. Sanitize the input.
let fileName = req.body.text1;
//Connect to the MongoDB client
MongoClient.connect(config.DbConfig.DBURL, function(err, client){
MongoClient.connect(config.DbConfig.DBURL, async function(err, client){
if(err){
return res.status(500).send({title: 'Uploaded Error', message: 'MongoClient Connection error', error: err.errMsg});
}
const db = client.db("pws");

const collection = db.collection('plugins.files');
const collectionChunks = db.collection('plugins.chunks');
collection.find({filename: fileName}).toArray(function(err, docs){
if(err){
return res.status(200).send({title: 'File error', message: 'Error finding file', error: err.errMsg});
}
if(!docs || docs.length === 0){
return res.status(200).send({title: 'Download Error', message: 'No file found'});
}else{
//Retrieving the chunks from the db
collectionChunks.find({files_id : docs[0]._id}).sort({n: 1}).toArray(function(err, chunks){
if(err){
return res.status(500).send({title: 'Download Error', message: 'Error retrieving chunks', error: err.errmsg});
}
if(!chunks || chunks.length === 0){
//No data found
return res.status(500).send({title: 'Download Error', message: 'No data found'});
}
//Append Chunks
let fileData = [];
for(let i=0; i<chunks.length;i++){
//This is in Binary JSON or BSON format, which is stored
//in fileData array in base64 endocoded string format
fileData.push(chunks[i].data.toString('base64'));
}
//Display the chunks using the data URI format
let finalFile = 'data:' + docs[0].contentType + ';base64,' + fileData.join('');
res.status(200).send({title: 'Image File', message: 'Image loaded from MongoDB GridFS', imgurl: finalFile});
});
}


let result = await collection.findOne({filename: fileName});
console.log(result);

let bucket = new MongoClient.GridFSBucket(
db, {bucketName: "plugins"}
);
let downloadStream = bucket.openDownloadStream(ObjectID(result._id));

downloadStream.on('data', (chunk) => {
res.write(chunk);
});

downloadStream.on('error', () => {
res.sendStatus(404);
});

downloadStream.on('end', () => {
res.end();
});
});
};
2 changes: 1 addition & 1 deletion back-end/server/controller/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
module.exports = {
UserController : require("./UserController"),
PluginController : require("./PluginController"),
UploadController : require("./UploadController")
UploadController : require("./UploadController"),
};

0 comments on commit 22af185

Please sign in to comment.