-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9af97a8
commit 4954fcc
Showing
6 changed files
with
280 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
|
||
/** | ||
* <write a short decription> | ||
* | ||
* 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}*/ | ||
} | ||
} |