diff --git a/src/helpers/index.backup.js b/src/helpers/index.backup.js new file mode 100644 index 0000000..5ad334e --- /dev/null +++ b/src/helpers/index.backup.js @@ -0,0 +1,108 @@ +var express = require('express'); +var router = express.Router(); +var path = require('path'); +var utils = require("../helpers/utils.js"); +var fs = require('fs'); +const mime = require('mime-types') +const render = require('./render'); + +/** + * + * + * 1. with hostname we query a master db in order to find the organization_id... + * with the org id we can now query for the uri in collection "files" and return value for fieldName "src" + * 2. in db we store path as /docs/dnd but need something more flexible so we can respond even if request /docs/dnd/ + * + * */ +const masterDB = '5ae0cfac6fb8c4e656fdaf92' +router.get('/*', async(req, res, next) => { + + + + let hostname = req.hostname; + let organization = await utils.organizationsfindOne({ domains: hostname }, masterDB) + if (!organization) + return res.send('Organization cannot be found using the domain:' + hostname); + + + let url = req.url; + if (url.endsWith('/')) + url = url.substring(0, url.length - 1); + + url = url.startsWith('/ws') ? url.substr(3) : url; // dev + + // console.log('>>>>>>>>>>>>>', url, organization) + let organization_id = organization._id.toString(); + + + let route_files = await utils.routesfindOne({ hostname: hostname, route_uri: url }, organization_id); + + + if (!route_files) + return res.status(404).send(`there is no ${url} in masterDb ${organization_id} `); + + // if (!route_files['public'] || route_files['public'] === "false") + // return res.status(404).send(`access not allowed`); + + + + + + let data; + if (route_files['src']) + data = route_files['src']; + else { + let route_export = await utils.getDocument({ + collection: route_files['collection'], + document_id: route_files['document_id'] + }, organization_id); + data = route_export[route_files['name']]; + + } + + if (!data) { + res.send('Document provided by routes could not be found and has no src '); + } + + + + let content_type = route_files['content_type'] || + mime.lookup(url) || + 'text/html'; + + + // console.log('(>>>>>>>>>>>>>>', content_type, route_files['content_type'], route_files['route']) + + if (content_type.startsWith('image/') || content_type.startsWith('audio/') || content_type.startsWith('video/')) { + // let content_type = route['content_type'] || "image/png"; + // todo: is there are better alternative or conevention not to process base64 + let file = Buffer.from(data, 'base64'); + res.set('Content-Type', content_type); + res.send(file); + } + else if (content_type === 'text/html') { + + + try { + + let fullHtml = await render(data, organization_id); + res.type(content_type); + res.send(fullHtml); + } + catch (err) { + if (err.message.startsWith('infinite loop:')) + return res.send('there is a infinite loop'); + else + return res.send('something is wrong') + } + + } + else { + res.type(content_type); + res.send(data); + } + + +}); + +module.exports = router; diff --git a/src/helpers/render.backup.js b/src/helpers/render.backup.js new file mode 100644 index 0000000..a5f57f8 --- /dev/null +++ b/src/helpers/render.backup.js @@ -0,0 +1,67 @@ +let { parse } = require("node-html-parser"); +let { getDocument } = require('../helpers/utils'); + +module.exports = async function renderHtml(html, organization_id) { + + let dep = []; + let dbCache = new Map(); + + async function render(html, lastKey) { + const dom = parse(html); + for (let el of dom.querySelectorAll( + "[data-collection][name][data-document_id]" + )) { + let meta = el.attributes; + let id = meta["data-document_id"], + coll = meta['data-collection'], + name = meta['name']; + let key = id + coll + name; + if(!id || !name || !coll) continue; + if (dep.includes(key)) + throw new Error( + `infinite loop: ${lastKey} ${id} ${coll} ${name} has been already rendered` + ); + else + dep.push(key) + + let cacheKey = id + coll; + let record; + if (dbCache.has(cacheKey)) + record = dbCache.get(cacheKey) + else { + + record = await + getDocument({ + collection: coll, + document_id: id + }, organization_id); + dbCache.set(cacheKey, record) + } + + + + if(!record || !record[name]) + { + dep.pop(); + continue; + } + let chunk = record[name]; + if (!chunk) { + + dep.pop(); + continue; + } + let dom = await render(chunk); + + el.innerHTML = ""; + el.appendChild(dom); + + + dep.pop(); + } + + return dom; + } + let result = (await render(html, 'root')).toString(); + return result; +} diff --git a/src/helpers/utils.js b/src/helpers/utils.js index eb3f7bf..862851a 100644 --- a/src/helpers/utils.js +++ b/src/helpers/utils.js @@ -1,7 +1,7 @@ const connection = require('./dbConnection.js'); var ObjectID = require('mongodb').ObjectID; module.exports = { - getDocument: async (data,dbName) => { + getDocument: async (data, dbName) => { try { const db = await connection(dbName); // obtenemos la conexión diff --git a/src/routes/index.js b/src/routes/index.js index 4c98f85..ba895c4 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -1,11 +1,21 @@ var express = require('express'); var router = express.Router(); var path = require('path'); -var utils = require("../helpers/utils.js"); +var utils = require("./utils.js"); var fs = require('fs'); const mime = require('mime-types') const render = require('./render'); +const { MongoClient } = require('mongodb'); +var config = require('../../config.json'); + +let dbClient = null; + +MongoClient.connect(config.db_url, { useNewUrlParser: true, poolSize: 10 }) + .then((client, err) => { + dbClient = client; + }); + /** * * @@ -17,10 +27,8 @@ const render = require('./render'); const masterDB = '5ae0cfac6fb8c4e656fdaf92' router.get('/*', async(req, res, next) => { - - let hostname = req.hostname; - let organization = await utils.organizationsfindOne({ domains: hostname }, masterDB) + let organization = await utils.organizationsfindOne(dbClient, { domains: hostname }, masterDB) if (!organization) return res.send('Organization cannot be found using the domain:' + hostname); @@ -35,14 +43,14 @@ router.get('/*', async(req, res, next) => { let organization_id = organization._id.toString(); - let route_files = await utils.routesfindOne({ hostname: hostname, route_uri: url }, organization_id); + let route_files = await utils.routesfindOne(dbClient,{ hostname: hostname, route_uri: url }, organization_id); if (!route_files) return res.status(404).send(`there is no ${url} in masterDb ${organization_id} `); - if (!route_files['public'] || route_files['public'] === "false") - return res.status(404).send(`access not allowed`); + // if (!route_files['public'] || route_files['public'] === "false") + // return res.status(404).send(`access not allowed`); @@ -52,10 +60,14 @@ router.get('/*', async(req, res, next) => { if (route_files['src']) data = route_files['src']; else { - let route_export = await utils.getDocument({ - collection: route_files['collection'], - document_id: route_files['document_id'] - }, organization_id); + let route_export = await utils.getDocument( + dbClient, + { + collection: route_files['collection'], + document_id: route_files['document_id'] + }, + organization_id + ); data = route_export[route_files['name']]; } @@ -85,7 +97,7 @@ router.get('/*', async(req, res, next) => { try { - let fullHtml = await render(data, organization_id); + let fullHtml = await render(db_client, data, organization_id); res.type(content_type); res.send(fullHtml); } diff --git a/src/routes/render.js b/src/routes/render.js index a5f57f8..5f1fbad 100644 --- a/src/routes/render.js +++ b/src/routes/render.js @@ -1,7 +1,7 @@ let { parse } = require("node-html-parser"); -let { getDocument } = require('../helpers/utils'); +let { getDocument } = require('./utils'); -module.exports = async function renderHtml(html, organization_id) { +module.exports = async function renderHtml(db_client, html, organization_id) { let dep = []; let dbCache = new Map(); @@ -31,7 +31,7 @@ module.exports = async function renderHtml(html, organization_id) { else { record = await - getDocument({ + getDocument(db_client, { collection: coll, document_id: id }, organization_id); diff --git a/src/routes/utils.js b/src/routes/utils.js new file mode 100644 index 0000000..5ef599b --- /dev/null +++ b/src/routes/utils.js @@ -0,0 +1,77 @@ +var ObjectID = require('mongodb').ObjectID; + +const select_db = (client, db_name) => { + if (client) { + return client.db(db_name) + } +} + +module.exports = { + getDocument: async (client, data, dbName) => { + + try { + const db = select_db(client, dbName); // obtenemos la conexión + //console.log(" Beginf Query") + + const collection = db.collection(data["collection"]); + + var query = { + "_id": new ObjectID(data["document_id"]) + }; + + return await collection.findOne(query); + //console.log(" END Query") + + } + catch(e){ + console.log(" Catch Error finOne ---",e) + return null; + } + /*finally { + //client.close(); + //console.log(" finlllay ") + }*/ + }, + getDocumentByQuery: async (client, data,dbName) => { + + try { + const db = select_db(client, dbName); // obtenemos la conexión + const collection = db.collection(data["collection"]); + delete data["collection"]; + var query = { + ...data + }; + return await collection.findOne(query); + } + catch(e){ + console.log(" Catch Error finOne --- By Query",e) + return null; + } + }, + organizationsfindOne : async (client, data, dbName) => { + try { + const db = select_db(client, dbName); // obtenemos la conexión + const collection = db.collection('organizations'); + var query ={ "domains": { $in: [data["domains"]] } }; + return await collection.findOne(query); + }catch(e){ + console.log(" Catch Error OrganizationfinOne ---",e) + return null; + } + }, + routesfindOne : async (client, data,dbName) => { + try { + const db = select_db(client, dbName); // obtenemos la conexión + const collection = db.collection('files'); + return await collection.findOne({domains: { $in: [data["hostname"]] } , "path" : data["route_uri"]}); + //return await collection.findOne({domains: { $in: [data["hostname"]] } , route : data["route_uri"]}); + }catch(e){ + console.log(" Catch Error ---",e) + return null; + } + /*finally { + console.log("Error FindOne") + return null; + }*/ + } +}