@@ -11,13 +11,13 @@ const organizations = new Map();
1111
1212class CoCreateFileSystem {
1313 constructor ( crud , render ) {
14-
14+
1515 async function defaultFiles ( fileName ) {
16- let file = await crud . readDocument ( {
16+ let file = await crud . readDocument ( {
1717 collection : 'files' ,
1818 filter : {
1919 query : [
20- { name : "path" , value : fileName , operator : "$eq" }
20+ { name : "path" , value : fileName , operator : "$eq" }
2121 ]
2222 } ,
2323 organization_id : process . env . organization_id
@@ -41,68 +41,68 @@ class CoCreateFileSystem {
4141 signup = file
4242 } )
4343
44- this . router = router . get ( '/*' , async ( req , res ) => {
44+ this . router = router . get ( '/*' , async ( req , res ) => {
4545 let hostname = req . hostname ;
4646 let organization = organizations . get ( hostname ) ;
4747 if ( ! organization ) {
48- let org = await crud . readDocument ( {
48+ let org = await crud . readDocument ( {
4949 collection : 'organizations' ,
5050 filter : {
5151 query : [
52- { name : "hosts" , value : [ hostname ] , operator : "$in" }
52+ { name : "hosts" , value : [ hostname ] , operator : "$in" }
5353 ]
5454 } ,
5555 organization_id : process . env . organization_id
5656 } )
5757
5858 if ( ! org || ! org . document || ! org . document [ 0 ] ) {
59- hostNotFound = hostNotFound || 'Organization cannot be found using the host: ' + hostname + ' in platformDB: ' + process . env . organization_id
60- organization = { _id : process . env . organization_id , key : process . env . key }
61- if ( ! req . url . startsWith ( '/superadmin' ) )
62- return res . redirect ( 301 , 'https://' + hostname + '/superadmin/signup.html' )
59+ hostNotFound = hostNotFound || 'Organization cannot be found using the host: ' + hostname + ' in platformDB: ' + process . env . organization_id
60+ return res . end ( hostNotFound ) ;
6361 } else {
64- organization = { _id : org . document [ 0 ] . _id , key : org . document [ 0 ] . key }
62+ organization = { _id : org . document [ 0 ] . _id }
6563 organizations . set ( hostname , organization )
6664 }
6765 }
6866
6967 let organization_id = organization . _id
68+ res . set ( 'organization' , organization_id )
69+
7070 let [ url , parameters ] = req . url . split ( "?" ) ;
71- if ( parameters ) { }
71+ if ( parameters ) { }
7272 if ( url . endsWith ( '/' ) ) {
7373 url += "index.html" ;
7474 } else {
7575 let directory = url . split ( "/" ) . slice ( - 1 ) [ 0 ] ;
76- if ( ! directory . includes ( '.' ) ) {
76+ if ( ! directory . includes ( '.' ) ) {
7777 url += "/index.html" ;
7878 }
7979 }
80-
80+
8181 let data = {
8282 collection : 'files' ,
8383 filter : {
8484 query : [
85- { name : "hosts" , value : [ hostname , '*' ] , operator : "$in" } ,
86- { name : "path" , value : url , operator : "$eq" }
85+ { name : "hosts" , value : [ hostname , '*' ] , operator : "$in" } ,
86+ { name : "path" , value : url , operator : "$eq" }
8787 ]
8888 } ,
8989 organization_id
9090 }
9191
92- if ( url . startsWith ( '/superadmin' ) )
92+ if ( url . startsWith ( '/superadmin' ) )
9393 data . organization_id = process . env . organization_id
9494
9595 let file = await crud . readDocument ( data ) ;
96-
96+
9797 if ( ! file || ! file . document || ! file . document [ 0 ] ) {
9898 data . filter . query [ 1 ] . value = '/404.html'
9999 if ( data . organization_id !== organization_id )
100100 data . organization_id = organization_id
101101
102- let pageNotFound = await crud . readDocument ( data ) ;
102+ let pageNotFound = await crud . readDocument ( data ) ;
103103 if ( ! pageNotFound || ! pageNotFound . document || ! pageNotFound . document [ 0 ] )
104104 pageNotFound = default404 || `${ url } could not be found for ${ organization_id } `
105- else
105+ else
106106 pageNotFound = pageNotFound . document [ 0 ] . src
107107 return res . status ( 404 ) . send ( pageNotFound ) ;
108108 }
@@ -113,15 +113,15 @@ class CoCreateFileSystem {
113113 if ( data . organization_id !== organization_id )
114114 data . organization_id = organization_id
115115
116- let pageForbidden = await crud . readDocument ( data ) ;
116+ let pageForbidden = await crud . readDocument ( data ) ;
117117 if ( ! pageForbidden || ! pageForbidden . document || ! pageForbidden . document [ 0 ] )
118118 pageForbidden = default403 || `${ url } access not allowed for ${ organization_id } `
119- else
119+ else
120120 pageForbidden = pageForbidden . document [ 0 ] . src
121121
122122 return res . status ( 403 ) . send ( pageForbidden ) ;
123123 }
124-
124+
125125 let src ;
126126 if ( file [ 'src' ] )
127127 src = file [ 'src' ] ;
@@ -135,18 +135,18 @@ class CoCreateFileSystem {
135135 } ) ;
136136 src = fileSrc [ file [ 'name' ] ] ;
137137 }
138-
138+
139139 if ( ! src ) {
140140 data . filter . query [ 1 ] . value = '/404.html'
141141 if ( data . organization_id !== organization_id )
142142 data . organization_id = organization_id
143143
144- let pageNotFound = await crud . readDocument ( data ) ;
144+ let pageNotFound = await crud . readDocument ( data ) ;
145145 if ( ! pageNotFound || ! pageNotFound . document || ! pageNotFound . document [ 0 ] )
146- pageNotFound = `${ url } could not be found for ${ organization_id } `
146+ pageNotFound = `${ url } could not be found for ${ organization_id } `
147147 return res . status ( 404 ) . send ( pageNotFound ) ;
148148 }
149-
149+
150150 let contentType = file [ 'content-type' ] || mime . lookup ( url ) || 'text/html' ;
151151
152152 if ( contentType . startsWith ( 'image/' ) || contentType . startsWith ( 'audio/' ) || contentType . startsWith ( 'video/' ) ) {
@@ -158,18 +158,13 @@ class CoCreateFileSystem {
158158 } catch ( err ) {
159159 console . warn ( 'server-render: ' + err . message )
160160 }
161- }
162- if ( process . env . organization_id !== organization_id && url . startsWith ( '/superadmin' ) && contentType === 'text/html' && ! url . includes ( '/signup.html' ) ) {
163- src = src . replace ( process . env . organization_id , organization_id )
164- src = src . replace ( process . env . key , organization . key )
165161 }
166162
167163 return res . type ( contentType ) . send ( src ) ;
168-
164+
169165 } )
170-
171- }
166+
167+ }
172168}
173169
174170module . exports = CoCreateFileSystem ;
175-
0 commit comments