Skip to content

Commit

Permalink
Merge branch 'master' into new-project-page-components
Browse files Browse the repository at this point in the history
  • Loading branch information
dhovart authored Jun 8, 2022
2 parents 39d38fb + 3d80737 commit dd6104e
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 216 deletions.
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"--no-timeout"
],
"env": {
"LOCALSIGNIN": "true"
"LOCALSIGNIN": "true",
"MONGODB": "127.0.0.1:27017/brainbox_test"
}
}
]
Expand Down
61 changes: 31 additions & 30 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var compression = require('compression');
const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const tracer = require('tracer').console({format: '[{{file}}:{{line}}] {{message}}'});
const tracer = require('tracer').console({ format: '[{{file}}:{{line}}] {{message}}' });
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const mustacheExpress = require('mustache-express');
Expand All @@ -31,7 +31,7 @@ const DOCKER_DEVELOP = process.env.DEVELOP;
if (DOCKER_DB) {
MONGO_DB = DOCKER_DB.replace('tcp://', '') + '/brainbox';
} else {
MONGO_DB = 'localhost:27017/brainbox'; //process.env.MONGODB;
MONGO_DB = process.env.MONGODB || 'localhost:27017/brainbox';
}

/** @todo Handle the case when MongoDB is not installed */
Expand Down Expand Up @@ -62,8 +62,8 @@ if (DOCKER_DEVELOP === '1') {
const start = async function () {
const app = express();

app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));

/*
Use the NeuroWebLab (NWL) module for authentication.
Expand All @@ -85,7 +85,7 @@ const start = async function () {
//========================================================================================
// Allow CORS
//========================================================================================
app.use(function(req, res, next) {
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
next();
Expand Down Expand Up @@ -136,7 +136,7 @@ const start = async function () {
key: await fs.promises.readFile(Config.ssl_key),
cert: await fs.promises.readFile(Config.ssl_cert)
};
if(Config.ssl_chain) {
if (Config.ssl_chain) {
options.ca = await fs.promises.readFile(Config.ssl_chain);
}
atlasmakerServer.server = https.createServer(options, app);
Expand All @@ -163,33 +163,34 @@ const start = async function () {
//========================================================================================
// catch 404 and forward to error handler
app.use(function (req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
console.log('Not found URL requested: ' + req.url);
next();
});

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function (err, req, res) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function (err, req, res) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
// the following middlewares will not be used by express as we need to pass 4 arguments to
// the use method to handle express errors: https://expressjs.com/fr/guide/error-handling.html
// // development error handler
// // will print stacktrace
// if (app.get('env') === 'development') {
// app.use(function (err, req, res) {
// res.status(err.status || 500);
// res.render('error', {
// message: err.message,
// error: err
// });
// });
// }
// // production error handler
// // no stacktraces leaked to user
// app.use(function (err, req, res) {
// res.status(err.status || 500);
// res.render('error', {
// message: err.message,
// error: {}
// });
// });

return { app, server, atlasmakerServer };
};

module.exports = {start};
module.exports = { start };
34 changes: 20 additions & 14 deletions controller/mri/mri.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const dataSlices = require('../dataSlices/dataSlices.js');
const { AccessType, AccessLevel } = require('neuroweblab');
const BrainboxAccessControlService = require('../../services/BrainboxAccessControlService');
const _ = require('lodash');
const AsyncLock = require('async-lock');
const lock = new AsyncLock();

const downloadQueue = {};
let atlasmakerServer;
Expand Down Expand Up @@ -92,20 +94,20 @@ const validatorPost = function (req, res, next) {
--------------------- */
// @todo Change this function callback into a promise
// eslint-disable-next-line max-statements
const downloadMRI = async function(myurl, req) {
const downloadMRI = async function (myurl, req) {
console.log('downloadMRI');
const hash = crypto
.createHash('md5')
.update(myurl)
.digest('hex');

const mridb = await req.db.get('mri').findOne({source: myurl, backup: {$exists: 0}});
const mridb = await req.db.get('mri').findOne({ source: myurl, backup: { $exists: 0 } });
console.log('mridb:', mridb);
let filename;
if (!mridb || !mridb.filename) {
filename = sanitize(url.parse(myurl).pathname.split('/').pop());
} else {
({filename} = mridb);
({ filename } = mridb);
}
let dest = req.dirname + '/public/data/' + hash + '/' + filename;
console.log(' source:', myurl);
Expand All @@ -122,7 +124,7 @@ const downloadMRI = async function(myurl, req) {
let cur = 0;

return new Promise(function (resolve, reject) {
request({uri: myurl, followAllRedirects: true, rejectUnauthorized : false})
request({ uri: myurl, followAllRedirects: true, rejectUnauthorized: false })
.on('error', (err) => {
console.log('ERROR in downloadMRI', err);
reject(err);
Expand Down Expand Up @@ -162,7 +164,7 @@ const downloadMRI = async function(myurl, req) {
.then((mri) => {
// Create json file for new dataset
let ip = '';
if(typeof req.headers['x-forwarded-for'] !== 'undefined') {
if (typeof req.headers['x-forwarded-for'] !== 'undefined') {
ip = req.headers['x-forwarded-for'];
} else if (req.connection.remoteAddress !== 'undefined') {
ip = req.connection.remoteAddress;
Expand All @@ -173,8 +175,8 @@ const downloadMRI = async function(myurl, req) {
}

let username;
if(req.isAuthenticated()) {
({username} = req.user);
if (req.isAuthenticated()) {
({ username } = req.user);
} else {
username = ip;
}
Expand Down Expand Up @@ -278,9 +280,10 @@ const mri = async function (req, res) {
// also query projects that set this MRI as a source
projects.push(...await req.db.get('project').find({
$or: [
{ 'files.list': {$eq: myurl }},
{ 'files.list.source': {$eq: myurl }}
]}
{ 'files.list': { $eq: myurl } },
{ 'files.list.source': { $eq: myurl } }
]
}
));

// set access to volume annotations
Expand Down Expand Up @@ -324,7 +327,7 @@ const apiMriPost = async function (req, res) {
try {
// eslint-disable-next-line no-new
new URL(myurl);
} catch(err) {
} catch (err) {
res.send('Invalid URL!');

return;
Expand Down Expand Up @@ -412,7 +415,10 @@ const apiMriPost = async function (req, res) {
console.log('Download succeeded. Insert in DB, remove from queue');
obj.success = true;

return req.db.get('mri').insert(obj);
return lock.acquire('mri', async function () {
await req.db.get('mri').update({ source: myurl }, { $set: { backup: true } }, { multi: true });
await req.db.get('mri').insert(obj);
});
})
.then(() => {
// downloadQueue[myurl] = obj;
Expand Down Expand Up @@ -567,7 +573,7 @@ const apiMriGet = async function (req, res) {
};

// eslint-disable-next-line func-style
const reset = async function reset(req, res) {
const reset = async function reset (req, res) {
const myurl = req.query.url;
const hash = crypto.createHash('md5').update(myurl)
.digest('hex');
Expand All @@ -582,7 +588,7 @@ const reset = async function reset(req, res) {
});
console.log(mridb);
let filename;
if (mridb) { ({filename} = mridb); }
if (mridb) { ({ filename } = mridb); }
const mrires = await atlasmakerServer.getBrainAtPath('/data/' + hash + '/' + filename)
.catch((err) => {
console.log('ERROR:', err);
Expand Down
Loading

0 comments on commit dd6104e

Please sign in to comment.