From fc50901dbac31e2e415e8a75a21358473e6224e7 Mon Sep 17 00:00:00 2001 From: Otse Obande Date: Wed, 4 Sep 2019 07:05:04 +0100 Subject: [PATCH] replace mongoService in appsettings and cloudfile --- api/app/AppSettings.js | 52 ++++++++++++++++++++++++------------------ api/file/CloudFiles.js | 8 +++++-- config/setup.js | 1 - 3 files changed, 36 insertions(+), 25 deletions(-) diff --git a/api/app/AppSettings.js b/api/app/AppSettings.js index f6470e24..7c2d9616 100644 --- a/api/app/AppSettings.js +++ b/api/app/AppSettings.js @@ -8,11 +8,11 @@ const q = require('q'); const _ = require('underscore'); const winston = require('winston'); const { Readable } = require('stream'); -const util = require('../../helpers/util.js'); +const { MongoAdapter } = require('mongo-adapter'); +const util = require('../../helpers/util.js'); const config = require('../../config/config'); const apiTracker = require('../../database-connect/apiTracker'); -const mongoService = require('../../databases/mongo'); const appService = require('../../services/app'); const keyService = require('../../database-connect/keyService'); @@ -126,10 +126,7 @@ module.exports = (app) => { res.status(401).send({ status: 'Unauthorized' }); } } catch (error) { - winston.error({ - error: String(error), - stack: new Error().stack, - }); + winston.error(error); res.status(500).send('Error.'); } }); @@ -174,7 +171,11 @@ module.exports = (app) => { // Delete from gridFs if (fileName) { - mongoService.document.deleteFileFromGridFs(appId, fileName); + MongoAdapter.deleteFileFromGridFs({ + client: config.dbc, + appId, + filename: fileName, + }); } } } @@ -183,9 +184,13 @@ module.exports = (app) => { if (category === 'general') { fileName = appId; } - const savedFile = await mongoService.document.saveFileStream( - appId, fileStream.fileStream, fileName, fileStream.contentType, - ); + const savedFile = await MongoAdapter.saveFileStream({ + client: config.dbc, + appId, + fileStream: fileStream.fileStream, + fileName, + contentType: fileStream.contentType, + }); let fileUri = null; fileUri = `${myUrl}/settings/${appId}/file/${savedFile.filename}`; if (category === 'general') { @@ -194,10 +199,7 @@ module.exports = (app) => { return res.status(200).send(fileUri); } catch (error) { - winston.error({ - error: String(error), - stack: new Error().stack, - }); + winston.error(error); return res.status(500).send(error); } }); @@ -216,15 +218,24 @@ module.exports = (app) => { if (!isMasterKey) { return res.status(401).send('Unauthorized'); } - const file = await mongoService.document.getFile(appId, fileName.split('.')[0]); + const file = await MongoAdapter.getFile({ + client: config.dbc, + appId, + filename: fileName.split('.')[0], + }); // eslint-disable-next-line no-underscore-dangle - const fileStream = mongoService.document.getFileStreamById(appId, file._id); + const fileStream = MongoAdapter.getFileStreamById({ + client: config.dbc, + appId, + fileId: file._id, + }); res.set('Content-Type', file.contentType); res.set('Content-Disposition', `attachment; filename="${file.filename}"`); - fileStream.on('error', (err) => { - res.send(500, `Got error while processing stream ${err.message}`); + fileStream.on('error', (error) => { + winston.error(error); + res.send(500, `Got error while processing stream ${error.message}`); res.end(); }); @@ -234,10 +245,7 @@ module.exports = (app) => { return fileStream.pipe(res); } catch (error) { - winston.error({ - error: String(error), - stack: new Error().stack, - }); + winston.error(error); return res.status(500).send(error); } }); diff --git a/api/file/CloudFiles.js b/api/file/CloudFiles.js index c7a9dc6a..933fd6e9 100644 --- a/api/file/CloudFiles.js +++ b/api/file/CloudFiles.js @@ -7,11 +7,11 @@ const q = require('q'); const winston = require('winston'); const { Readable } = require('stream'); +const { MongoAdapter } = require('mongo-adapter'); const customHelper = require('../../helpers/custom.js'); const apiTracker = require('../../database-connect/apiTracker'); const config = require('../../config/config'); -const mongoService = require('../../databases/mongo'); const customService = require('../../services/cloudObjects'); const appService = require('../../services/app'); const fileService = require('../../services/cloudFiles'); @@ -128,7 +128,11 @@ const getFile = async (req, res) => { && typeof rDegs === 'undefined' && typeof bSigma === 'undefined') { // eslint-disable-next-line - const fileStream = mongoService.document.getFileStreamById(appId, file._id); + const fileStream = MongoAdapter.getFileStreamById({ + client: config.dbc, + appId, + fileId: file._id, + }); res.set('Content-Type', file.contentType); res.set('Content-Disposition', `inline; filename="${file.filename}"`); diff --git a/config/setup.js b/config/setup.js index 2a352ba2..6832717d 100644 --- a/config/setup.js +++ b/config/setup.js @@ -2,7 +2,6 @@ const winston = require('winston'); const { MongoAdapter } = require('mongo-adapter'); const keyService = require('../database-connect/keyService'); const serverService = require('../services/server'); -// import mongoConnect from '../database-connect/mongoConnect.js'; const config = require('./config'); const getMongoConnectionString = require('./mongo');