Skip to content
This repository has been archived by the owner on Sep 13, 2019. It is now read-only.

Multitenancy option for dblisteners #11

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 79 additions & 24 deletions dblisteners/file-upload.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var fs = require('fs');
var mkdirp = require('mkdirp');
var path = require('path');
var url = require('url');
var AWS = require('aws-sdk');

function getFilePaths(type, fileName, config) {
switch (type) {
Expand All @@ -18,52 +20,105 @@ function getFilePaths(type, fileName, config) {
}
}
}

function storeFile(config, body, filePath, cb) {

console.log(filePath);
if (filePath.startsWith('s3://')) {

var q = url.parse(filePath, false, true);
console.log(q.hostname);
console.log(q.path);

var paths = q.path.split('/');
var bucket = paths[1];
paths.splice(0, 2);
var s3path = paths.join('/');

console.log([bucket, s3path]);
var ep = new AWS.Endpoint(q.hostname);
var s3 = new AWS.S3({
accessKeyId: config.awsAccessKeyId,
secretAccessKey: config.awsSecretAccessKey,
endpoint: ep
});

var params = {
Key: s3path,
Bucket: bucket,
Body: body
};
s3.upload(params, function (err) {
cb(err);
});

} else {

// Make the directory to the file if it doesn't exist
mkdirp(path.dirname(filePath), function (err) {
if (err) {
console.log('Error mkdirp for ' + path.dirname(filePath), err);
cb(err);
return;
}

// Write the file to the filesystem
fs.writeFile(filePaths.filePath, body, function (err) {
if (err) {
console.log('Error writing file: ' + filePath, err);
cb(err);
return;
}

cb(null);
});
});
}
}

/**
* Upload file that is temporarily stored as attachment.
*/
module.exports = function(change, maindb, config) {

if (change.doc && change.doc.data && change.doc.data.localFile && change.doc.data.localFile === true && change.doc._attachments) {
try {

var currentDoc = change.doc;
var docType = currentDoc._id.substring(0, currentDoc._id.indexOf('_'));
var filePaths = getFilePaths(docType, currentDoc.data.fileName, config);

// Make the directory to the file if it doesn't exist
mkdirp(path.dirname(filePaths.filePath), function(err) {
console.log([currentDoc._id, filePaths.fileName, filePaths.filePath]);

// Get the file from the couchdb attachment
maindb.attachment.get(currentDoc._id, 'file', function (err, body) {
if (err) {
console.log('Error mkdirp for ' + path.dirname(filePaths.filePath), err);
console.log('Error getting file attachment for: ', currentDoc, err);
return;
}
// Get the file from the couchdb attachment
maindb.attachment.get(currentDoc._id, 'file', function(err, body) {

storeFile(config, body, filePaths.filePath, function (err) {
if (err) {
console.log('Error getting file attachment for: ', currentDoc);
console.log('Error saving file attachment to: ', filePaths.filePath, err);
return;
}
// Write the file to the filesystem
fs.writeFile(filePaths.filePath, body, function(err) {

// Remove the attachment from the document
maindb.attachment.destroy(currentDoc._id, 'file', { rev: currentDoc._rev }, function (err, body) {
if (err) {
console.log('Error writing file: ' + filePaths.filePath, err);
console.log('Error deleting attachment on ' + currentDoc._id + ', rev:' + currentDoc._rev, err);
return;
}
// Remove the attachment from the document
maindb.attachment.destroy(currentDoc._id, 'file', {rev: currentDoc._rev}, function(err, body) {
currentDoc._rev = body.rev;
currentDoc.data.url = filePaths.fileName;
currentDoc.data.localFile = false;
currentDoc.data.files = [];
delete currentDoc._attachments;
// Update the url
maindb.insert(currentDoc, currentDoc._id, function (err) {
if (err) {
console.log('Error deleting attachment on ' + currentDoc._id + ', rev:' + currentDoc._rev, err);
return;
console.log('Error updating url for file:' + currentDoc.id, err);
}
currentDoc._rev = body.rev;
currentDoc.data.url = filePaths.fileName;
currentDoc.data.localFile = false;
currentDoc.data.files = [];
delete currentDoc._attachments;
// Update the url
maindb.insert(currentDoc, currentDoc._id, function(err) {
if (err) {
console.log('Error updating url for file:' + currentDoc.id, err);
}
});
});
});
});
Expand Down
31 changes: 26 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var globSync = require('glob').sync;

function setupFollow(config, dbName, listenersDir) {
var nano = require('nano')(config.couchAuthDbURL);
var maindb = nano.use(dbName);
var db = nano.use(dbName);
var couchFollowOpts = {
db: config.couchAuthDbURL + '/' + dbName,
include_docs: true,
Expand All @@ -16,13 +16,34 @@ function setupFollow(config, dbName, listenersDir) {
follow(couchFollowOpts, function(error, change) {
if (!error) {
dbListeners.forEach(function(listener) {
listener(change, maindb, config);
listener(change, db, config);
});
}
});
}

module.exports = function(config) {
setupFollow(config, 'main', 'dblisteners');
setupFollow(config, '_users', 'userdb-listeners');
module.exports = function (config) {

if (config.isMultitenancy) {

var nano = require('nano')(config.couchAuthDbURL);
nano.db.list()
.then((dbNames) => {

var filteredDbs = dbNames.filter((value) => {
return !value.startsWith('_') && value !== 'pushinfo' && value !== 'config';
});

filteredDbs.forEach(function (dbName) {
setupFollow(config, dbName, 'dblisteners');
});

setupFollow(config, '_users', 'userdb-listeners');
});

} else {
setupFollow(config, 'main', 'dblisteners');
setupFollow(config, '_users', 'userdb-listeners');
}

};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
},
"homepage": "http://hospitalrun.io",
"dependencies": {
"aws-sdk": "^2.331.0",
"follow": "^1.0.0",
"glob": "^7.0.0",
"mkdirp": "^0.5.1",
Expand Down