Skip to content

Commit

Permalink
replace mongoService in appsettings and cloudfile
Browse files Browse the repository at this point in the history
  • Loading branch information
otseobande committed Sep 4, 2019
1 parent 3375fa2 commit fc50901
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
52 changes: 30 additions & 22 deletions api/app/AppSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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.');
}
});
Expand Down Expand Up @@ -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,
});
}
}
}
Expand All @@ -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') {
Expand All @@ -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);
}
});
Expand All @@ -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();
});

Expand All @@ -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);
}
});
Expand Down
8 changes: 6 additions & 2 deletions api/file/CloudFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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}"`);
Expand Down
1 change: 0 additions & 1 deletion config/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down

0 comments on commit fc50901

Please sign in to comment.