diff --git a/dblisteners/file-upload.js b/dblisteners/file-upload.js index d380e0d..5f83121 100644 --- a/dblisteners/file-upload.js +++ b/dblisteners/file-upload.js @@ -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) { @@ -18,10 +20,67 @@ 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 { @@ -29,41 +88,37 @@ module.exports = function(change, maindb, config) { 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); - } - }); }); }); }); diff --git a/index.js b/index.js index f0b6138..7699309 100644 --- a/index.js +++ b/index.js @@ -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, @@ -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'); + } + }; diff --git a/package.json b/package.json index 34b52f7..37eacee 100644 --- a/package.json +++ b/package.json @@ -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",