@cocreate/file-server 1.4.5 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -51,6 +51,10 @@ jobs:
51
51
  uses: actions/setup-node@v3
52
52
  with:
53
53
  node-version: 16
54
-
55
54
  - name: update documentation
56
55
  uses: CoCreate-app/CoCreate-docs@master
56
+ env:
57
+ organization_id: ${{ secrets.COCREATE_ORGANIZATION_ID }}
58
+ key: ${{ secrets.COCREATE_KEY }}
59
+ host: ${{ secrets.COCREATE_HOST }}
60
+
package/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ # [1.5.0](https://github.com/CoCreate-app/CoCreate-file-server/compare/v1.4.6...v1.5.0) (2023-06-07)
2
+
3
+
4
+ ### Features
5
+
6
+ * replaced express with http module. Refactor server code with better organization and readable code ([a723ba3](https://github.com/CoCreate-app/CoCreate-file-server/commit/a723ba327e39f5d3ab9e5afc8815440389d06853))
7
+
8
+ ## [1.4.6](https://github.com/CoCreate-app/CoCreate-file-server/compare/v1.4.5...v1.4.6) (2023-06-04)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * Refactor CoCreate.config.js to remove hard-coded credentials ([d580478](https://github.com/CoCreate-app/CoCreate-file-server/commit/d58047895652d997957e00cc39233807d1e5e27a))
14
+
1
15
  ## [1.4.5](https://github.com/CoCreate-app/CoCreate-file-server/compare/v1.4.4...v1.4.5) (2023-06-04)
2
16
 
3
17
 
package/docs/index.html CHANGED
@@ -1,238 +1,238 @@
1
- <!DOCTYPE html>
2
- <html lang="en">
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta http-equiv="X-UA-Compatible" content="IE=edge" />
6
- <meta name="viewport" content="width=device-width, initial-scale=1" />
7
- <title>CoCreate-file-server Documentation | CoCreateJS</title>
8
- <link
9
- rel="icon"
10
- type="image/png"
11
- sizes="32x32"
12
- href="https://cocreate.app/images/favicon.ico" />
13
- <meta
14
- name="description"
15
- content="A simple HTML5 and pure javascript component. Easy configuration using data-attributes and highly styleable." />
16
- <meta
17
- name="keywords"
18
- content="helper classes, utility classes, css framework, css library, inline style classes" />
19
- <meta name="robots" content="index,follow" />
20
-
21
- <!-- CoCreate CSS CDN -->
22
- <link
23
- rel="stylesheet"
24
- href="https://cdn.cocreate.app/latest/CoCreate.min.css"
25
- type="text/css" />
26
- </head>
27
-
28
- <body>
29
- <div collection="" document_id="" name="" id="cocreate-file-server">
30
- <div
31
- class="display:flex flex-wrap:wrap justify-content:space-between margin:10px">
32
- <div class="display:flex align-items:center">
33
- <h2>CoCreate-file-server</h2>
34
- </div>
35
- <div
36
- class="display:flex align-items:center font-size:20px"
37
- share-height="600"
38
- share-width="500"
39
- share-media="https://via.placeholder.com/300/09f/fff.png">
40
- <a
41
- href="https://github.com/CoCreate-app/CoCreate-file-server"
42
- target="_blank"
43
- class="margin-right:15px"
44
- ><i class="fab fa-github"></i
45
- ></a>
46
- <a
47
- class="margin-right:15px share"
48
- share-network="twitter"
49
- title="Share on twitter"
50
- ><i class="fab fa-twitter"></i
51
- ></a>
52
- <a
53
- class="margin-right:15px share"
54
- share-network="facebook"
55
- title="Share on Facebook"
56
- ><i class="fab fa-facebook"></i
57
- ></a>
58
- <a
59
- class="margin-right:15px share"
60
- share-network="instagram"
61
- title="Share on instagram"
62
- ><i class="fab fa-instagram"></i
63
- ></a>
64
- <a
65
- class="margin-right:15px share"
66
- share-network="share"
67
- title="Share on share"
68
- ><i class="fas fa-share-alt"></i
69
- ></a>
70
- </div>
71
- </div>
72
- <h1 class="max-width:500px margin:20px_10px">
73
- A simple HTML5, CSS and pure javascript component. Easy
74
- configuration using data-attributes and highly styleable.
75
- </h1>
76
- <div id="file-server-section" class="display:flex flex-wrap:wrap">
77
- <div class="flex-grow:1 width:300px padding:20px_10px">
78
- <h2
79
- class="border-bottom:1px_solid_lightgrey padding:5px_0px">
80
- Install
81
- </h2>
82
- <pre
83
- class="margin-top:15px"><code class="language-javascript">npm install cocreate-file-server</code></pre>
84
- <p class="padding:10px_0px">Or you can use cdn link:</p>
85
- <pre><code class="language-javascript">https://cdn.cocreate.app/js/CoCreate-file-server.min.js</code></pre>
86
-
87
- <h2
88
- class="border-bottom:1px_solid_lightgrey margin-top:20px padding:5px_0px">
89
- Usage
90
- </h2>
91
- <p class="padding:10px_0px">file-server usage content</p>
92
- <pre><code class="language-html">&lt;div&gt;&lt;/div&gt;</code></pre>
93
-
94
- <h2
95
- class="border-bottom:1px_solid_lightgrey margin-top:20px padding:5px_0px">
96
- Reference
97
- </h2>
98
- <p class="padding:10px_0px">
99
- This is file-server reference content
100
- </p>
101
- <pre><code class="language-javascript">&lt;div&gt;&lt;/div&gt;</code></pre>
102
- <p class="padding:10px_0px">
103
- This is file-server reference content
104
- </p>
105
-
106
- <h2
107
- class="border-bottom:1px_solid_lightgrey margin-top:20px padding:5px_0px">
108
- Attributes
109
- </h2>
110
- <ul class="list-style-type:none">
111
- <li
112
- class="padding:15px_0px border-bottom:1px_solid_lightgrey">
113
- <h4>
114
- <span>file-server</span>
115
- <span class="cocreate-badge success"
116
- >string</span
117
- >
118
- <span class="cocreate-badge warning"
119
- >optional</span
120
- >
121
- </h4>
122
- <p>file-server-attribute</p>
123
- </li>
124
- <li
125
- class="padding:15px_0px border-bottom:1px_solid_lightgrey">
126
- <h4>
127
- <span>file-server</span>
128
- <span class="cocreate-badge success"
129
- >string</span
130
- >
131
- <span class="cocreate-badge warning"
132
- >optional</span
133
- >
134
- </h4>
135
- <p>file-server-attribute</p>
136
- </li>
137
- </ul>
138
- </div>
139
-
140
- <div
141
- class="flex-grow:1 width:300px padding:0px_10px margin:20px_0px border-bottom:1px_solid_lightgrey">
142
- <!-- SandBox -->
143
- <h2
144
- class="border-bottom:1px_solid_lightgrey padding:5px_0px">
145
- Demo
146
- </h2>
147
- <div
148
- class="position:sticky top:0 padding:10px_0px height:100vh">
149
- <!-- SandBox -->
150
- <div
151
- class="display:flex flex-direction:column position:relative overflow:hidden card border-radius:2px width:auto height:600px margin-top:20px"
152
- id="playground">
153
- <div
154
- id="demo-code"
155
- resizable
156
- class="position:relative height:50%">
157
- <textarea
158
- type="code"
159
- lang="html"
160
- collection="demos"
161
- document_id=""
162
- name="demo"
163
- save="false"
164
- id="demo"
165
- class="height:100% width:100% outline:none border:none resize:none padding:5px"></textarea>
166
- <div
167
- resize="bottom"
168
- class="background:lightgrey"></div>
169
- </div>
170
-
171
- <div
172
- id="demo-preview"
173
- class="position:relative overflow:auto background-color:white">
174
- <div
175
- get-value="#demo"
176
- class="padding:20px"></div>
177
- </div>
178
-
179
- <div
180
- class="font-size:20px position:absolute top:10px right:10px opacity:0.6">
181
- <a
182
- class="margin-right:10px"
183
- id="eye"
184
- show="#eye-slash"
185
- hide="#eye, #demo-preview"
186
- toggle="code-height"
187
- toggle-target="#demo-code"
188
- ><i class="far fa-eye"></i
189
- ></a>
190
- <a
191
- class="margin-right:10px"
192
- hidden
193
- id="eye-slash"
194
- show="#eye, #demo-preview"
195
- hide="#eye-slash"
196
- toggle="code-height"
197
- toggle-target="#demo-code"
198
- ><i class="fas fa-eye-slash"></i
199
- ></a>
200
- <a
201
- class="margin-right:10px"
202
- id="code"
203
- show="#code-slash"
204
- hide="#code, #demo-code"
205
- ><i class="fa fa-code"></i
206
- ></a>
207
- <a
208
- class="margin-right:10px"
209
- hidden
210
- id="code-slash"
211
- show="#code, #demo-code"
212
- hide="#code-slash"
213
- ><i class="fas fa-code"></i
214
- ></a>
215
- <a
216
- class="margin-right:5px"
217
- fullscreen
218
- target="#playground"
219
- ><i class="fas fa-expand"></i
220
- ></a>
221
- </div>
222
- </div>
223
- <!-- End SandBox -->
224
- </div>
225
- </div>
226
- </div>
227
- </div>
228
- <script>
229
- var CoCreateConfig = {
230
- key: "2061acef-0451-4545-f754-60cf8160",
231
- organization_id: "5ff747727005da1c272740ab",
232
- };
233
- </script>
234
-
235
- <!--CoCreateJS-->
236
- <script src="https://cdn.cocreate.app/latest/CoCreate.min.js"></script>
237
- </body>
238
- </html>
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8" />
5
+ <meta http-equiv="X-UA-Compatible" content="IE=edge" />
6
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
7
+ <title>CoCreate-file-server Documentation | CoCreateJS</title>
8
+ <link
9
+ rel="icon"
10
+ type="image/png"
11
+ sizes="32x32"
12
+ href="https://cocreate.app/images/favicon.ico" />
13
+ <meta
14
+ name="description"
15
+ content="A simple HTML5 and pure javascript component. Easy configuration using data-attributes and highly styleable." />
16
+ <meta
17
+ name="keywords"
18
+ content="helper classes, utility classes, css framework, css library, inline style classes" />
19
+ <meta name="robots" content="index,follow" />
20
+
21
+ <!-- CoCreate CSS CDN -->
22
+ <link
23
+ rel="stylesheet"
24
+ href="https://cdn.cocreate.app/latest/CoCreate.min.css"
25
+ type="text/css" />
26
+ </head>
27
+
28
+ <body>
29
+ <div collection="" document_id="" name="" id="cocreate-file-server">
30
+ <div
31
+ class="display:flex flex-wrap:wrap justify-content:space-between margin:10px">
32
+ <div class="display:flex align-items:center">
33
+ <h2>CoCreate-file-server</h2>
34
+ </div>
35
+ <div
36
+ class="display:flex align-items:center font-size:20px"
37
+ share-height="600"
38
+ share-width="500"
39
+ share-media="https://via.placeholder.com/300/09f/fff.png">
40
+ <a
41
+ href="https://github.com/CoCreate-app/CoCreate-file-server"
42
+ target="_blank"
43
+ class="margin-right:15px"
44
+ ><i class="fab fa-github"></i
45
+ ></a>
46
+ <a
47
+ class="margin-right:15px share"
48
+ share-network="twitter"
49
+ title="Share on twitter"
50
+ ><i class="fab fa-twitter"></i
51
+ ></a>
52
+ <a
53
+ class="margin-right:15px share"
54
+ share-network="facebook"
55
+ title="Share on Facebook"
56
+ ><i class="fab fa-facebook"></i
57
+ ></a>
58
+ <a
59
+ class="margin-right:15px share"
60
+ share-network="instagram"
61
+ title="Share on instagram"
62
+ ><i class="fab fa-instagram"></i
63
+ ></a>
64
+ <a
65
+ class="margin-right:15px share"
66
+ share-network="share"
67
+ title="Share on share"
68
+ ><i class="fas fa-share-alt"></i
69
+ ></a>
70
+ </div>
71
+ </div>
72
+ <h1 class="max-width:500px margin:20px_10px">
73
+ A simple HTML5, CSS and pure javascript component. Easy
74
+ configuration using data-attributes and highly styleable.
75
+ </h1>
76
+ <div id="file-server-section" class="display:flex flex-wrap:wrap">
77
+ <div class="flex-grow:1 width:300px padding:20px_10px">
78
+ <h2
79
+ class="border-bottom:1px_solid_lightgrey padding:5px_0px">
80
+ Install
81
+ </h2>
82
+ <pre
83
+ class="margin-top:15px"><code class="language-javascript">npm install cocreate-file-server</code></pre>
84
+ <p class="padding:10px_0px">Or you can use cdn link:</p>
85
+ <pre><code class="language-javascript">https://cdn.cocreate.app/js/CoCreate-file-server.min.js</code></pre>
86
+
87
+ <h2
88
+ class="border-bottom:1px_solid_lightgrey margin-top:20px padding:5px_0px">
89
+ Usage
90
+ </h2>
91
+ <p class="padding:10px_0px">file-server usage content</p>
92
+ <pre><code class="language-html">&lt;div&gt;&lt;/div&gt;</code></pre>
93
+
94
+ <h2
95
+ class="border-bottom:1px_solid_lightgrey margin-top:20px padding:5px_0px">
96
+ Reference
97
+ </h2>
98
+ <p class="padding:10px_0px">
99
+ This is file-server reference content
100
+ </p>
101
+ <pre><code class="language-javascript">&lt;div&gt;&lt;/div&gt;</code></pre>
102
+ <p class="padding:10px_0px">
103
+ This is file-server reference content
104
+ </p>
105
+
106
+ <h2
107
+ class="border-bottom:1px_solid_lightgrey margin-top:20px padding:5px_0px">
108
+ Attributes
109
+ </h2>
110
+ <ul class="list-style-type:none">
111
+ <li
112
+ class="padding:15px_0px border-bottom:1px_solid_lightgrey">
113
+ <h4>
114
+ <span>file-server</span>
115
+ <span class="cocreate-badge success"
116
+ >string</span
117
+ >
118
+ <span class="cocreate-badge warning"
119
+ >optional</span
120
+ >
121
+ </h4>
122
+ <p>file-server-attribute</p>
123
+ </li>
124
+ <li
125
+ class="padding:15px_0px border-bottom:1px_solid_lightgrey">
126
+ <h4>
127
+ <span>file-server</span>
128
+ <span class="cocreate-badge success"
129
+ >string</span
130
+ >
131
+ <span class="cocreate-badge warning"
132
+ >optional</span
133
+ >
134
+ </h4>
135
+ <p>file-server-attribute</p>
136
+ </li>
137
+ </ul>
138
+ </div>
139
+
140
+ <div
141
+ class="flex-grow:1 width:300px padding:0px_10px margin:20px_0px border-bottom:1px_solid_lightgrey">
142
+ <!-- SandBox -->
143
+ <h2
144
+ class="border-bottom:1px_solid_lightgrey padding:5px_0px">
145
+ Demo
146
+ </h2>
147
+ <div
148
+ class="position:sticky top:0 padding:10px_0px height:100vh">
149
+ <!-- SandBox -->
150
+ <div
151
+ class="display:flex flex-direction:column position:relative overflow:hidden card border-radius:2px width:auto height:600px margin-top:20px"
152
+ id="playground">
153
+ <div
154
+ id="demo-code"
155
+ resizable
156
+ class="position:relative height:50%">
157
+ <textarea
158
+ type="code"
159
+ lang="html"
160
+ collection="demos"
161
+ document_id=""
162
+ name="demo"
163
+ save="false"
164
+ id="demo"
165
+ class="height:100% width:100% outline:none border:none resize:none padding:5px"></textarea>
166
+ <div
167
+ resize="bottom"
168
+ class="background:lightgrey"></div>
169
+ </div>
170
+
171
+ <div
172
+ id="demo-preview"
173
+ class="position:relative overflow:auto background-color:white">
174
+ <div
175
+ get-value="#demo"
176
+ class="padding:20px"></div>
177
+ </div>
178
+
179
+ <div
180
+ class="font-size:20px position:absolute top:10px right:10px opacity:0.6">
181
+ <a
182
+ class="margin-right:10px"
183
+ id="eye"
184
+ show="#eye-slash"
185
+ hide="#eye, #demo-preview"
186
+ toggle="code-height"
187
+ toggle-target="#demo-code"
188
+ ><i class="far fa-eye"></i
189
+ ></a>
190
+ <a
191
+ class="margin-right:10px"
192
+ hidden
193
+ id="eye-slash"
194
+ show="#eye, #demo-preview"
195
+ hide="#eye-slash"
196
+ toggle="code-height"
197
+ toggle-target="#demo-code"
198
+ ><i class="fas fa-eye-slash"></i
199
+ ></a>
200
+ <a
201
+ class="margin-right:10px"
202
+ id="code"
203
+ show="#code-slash"
204
+ hide="#code, #demo-code"
205
+ ><i class="fa fa-code"></i
206
+ ></a>
207
+ <a
208
+ class="margin-right:10px"
209
+ hidden
210
+ id="code-slash"
211
+ show="#code, #demo-code"
212
+ hide="#code-slash"
213
+ ><i class="fas fa-code"></i
214
+ ></a>
215
+ <a
216
+ class="margin-right:5px"
217
+ fullscreen
218
+ target="#playground"
219
+ ><i class="fas fa-expand"></i
220
+ ></a>
221
+ </div>
222
+ </div>
223
+ <!-- End SandBox -->
224
+ </div>
225
+ </div>
226
+ </div>
227
+ </div>
228
+ <script>
229
+ var CoCreateConfig = {
230
+ key: "2061acef-0451-4545-f754-60cf8160",
231
+ organization_id: "5ff747727005da1c272740ab",
232
+ };
233
+ </script>
234
+
235
+ <!--CoCreateJS-->
236
+ <script src="https://cdn.cocreate.app/latest/CoCreate.min.js"></script>
237
+ </body>
238
+ </html>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/file-server",
3
- "version": "1.4.5",
3
+ "version": "1.5.0",
4
4
  "description": "A simple file-server component in vanilla javascript. Easily configured using HTML5 data-attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "file-server",
@@ -28,7 +28,7 @@
28
28
  "build": "NODE_ENV=production npx webpack --config webpack.config.js",
29
29
  "dev": "npx webpack --config webpack.config.js --watch",
30
30
  "docs": "node ./node_modules/@cocreate/docs/src/index.js",
31
- "hosting": "node ./node_modules/@cocreate/hosting/src/index.js"
31
+ "postinstall": "node ./node_modules/@cocreate/cli/check-coc.js"
32
32
  },
33
33
  "repository": {
34
34
  "type": "git",
@@ -45,9 +45,11 @@
45
45
  "url": "https://github.com/sponsors/CoCreate-app"
46
46
  },
47
47
  "main": "./src/index.js",
48
+ "devDependencies": {
49
+ "@cocreate/cli": "^1.29.3"
50
+ },
48
51
  "dependencies": {
49
- "@cocreate/docs": "^1.7.15",
50
- "@cocreate/hosting": "^1.10.8",
52
+ "@cocreate/docs": "^1.8.2",
51
53
  "express": "^4.18.2"
52
54
  }
53
55
  }
package/src/index.js CHANGED
@@ -4,14 +4,12 @@
4
4
  *
5
5
  * SPDX-License-Identifier: MIT
6
6
  ********************************************************************************/
7
- const express = require('express');
8
- const router = express.Router();
9
- const mime = require('mime-types');
7
+ const { URL } = require('url');
8
+
10
9
  const organizations = new Map();
11
10
 
12
11
  class CoCreateFileSystem {
13
- constructor(crud, render) {
14
-
12
+ constructor(server, crud, render) {
15
13
  async function defaultFiles(fileName) {
16
14
  let file = await crud.readDocument({
17
15
  collection: 'files',
@@ -41,129 +39,141 @@ class CoCreateFileSystem {
41
39
  signup = file
42
40
  })
43
41
 
44
- this.router = router.get('/*', async (req, res) => {
45
- let hostname = req.hostname;
46
- let organization = organizations.get(hostname);
47
- if (!organization) {
48
- let org = await crud.readDocument({
49
- collection: 'organizations',
50
- filter: {
51
- query: [
52
- { name: "hosts", value: [hostname], operator: "$in" }
53
- ]
54
- },
55
- organization_id: process.env.organization_id
56
- })
42
+ server.on('request', async (req, res) => {
43
+ try {
44
+ const valideUrl = new URL(`http://${req.headers.host}${req.url}`);
45
+ const hostname = valideUrl.hostname;
46
+
47
+ let organization = organizations.get(hostname);
48
+ if (!organization) {
49
+ let org = await crud.readDocument({
50
+ collection: 'organizations',
51
+ filter: {
52
+ query: [
53
+ { name: "hosts", value: [hostname], operator: "$in" }
54
+ ]
55
+ },
56
+ organization_id: process.env.organization_id
57
+ })
58
+
59
+ if (!org || !org.document || !org.document[0]) {
60
+ hostNotFound = hostNotFound || 'An organization could not be found using the host: ' + hostname + ' in platformDB: ' + process.env.organization_id
61
+ res.writeHead(404, { 'Content-Type': 'text/plain' });
62
+ return res.end(hostNotFound);
63
+ } else {
64
+ organization = { _id: org.document[0]._id }
65
+ organizations.set(hostname, organization)
66
+ }
67
+ }
68
+
69
+ let organization_id = organization._id
70
+ res.setHeader('organization', organization_id)
57
71
 
58
- if (!org || !org.document || !org.document[0]) {
59
- hostNotFound = hostNotFound || 'An organization could not be found using the host: ' + hostname + ' in platformDB: ' + process.env.organization_id
60
- return res.end(hostNotFound);
72
+ let pathname = valideUrl.pathname;
73
+ let parameters = valideUrl.searchParams;
74
+ if (parameters.size) {
75
+ console.log('parameters', parameters)
76
+ }
77
+ if (pathname.endsWith('/')) {
78
+ pathname += "index.html";
61
79
  } else {
62
- organization = { _id: org.document[0]._id }
63
- organizations.set(hostname, organization)
80
+ let directory = pathname.split("/").slice(-1)[0];
81
+ if (!directory.includes('.')) {
82
+ pathname += "/index.html";
83
+ }
64
84
  }
65
- }
66
85
 
67
- let organization_id = organization._id
68
- res.set('organization', organization_id)
69
-
70
- let [url, parameters] = req.url.split("?");
71
- if (parameters) { }
72
- if (url.endsWith('/')) {
73
- url += "index.html";
74
- } else {
75
- let directory = url.split("/").slice(-1)[0];
76
- if (!directory.includes('.')) {
77
- url += "/index.html";
86
+ let data = {
87
+ collection: 'files',
88
+ filter: {
89
+ query: [
90
+ { name: "hosts", value: [hostname, '*'], operator: "$in" },
91
+ { name: "path", value: pathname, operator: "$eq" }
92
+ ]
93
+ },
94
+ organization_id
78
95
  }
79
- }
80
-
81
- let data = {
82
- collection: 'files',
83
- filter: {
84
- query: [
85
- { name: "hosts", value: [hostname, '*'], operator: "$in" },
86
- { name: "path", value: url, operator: "$eq" }
87
- ]
88
- },
89
- organization_id
90
- }
91
-
92
- if (url.startsWith('/superadmin'))
93
- data.organization_id = process.env.organization_id
94
96
 
95
- let file = await crud.readDocument(data);
97
+ if (pathname.startsWith('/superadmin'))
98
+ data.organization_id = process.env.organization_id
96
99
 
97
- if (!file || !file.document || !file.document[0]) {
98
- data.filter.query[1].value = '/404.html'
99
- if (data.organization_id !== organization_id)
100
- data.organization_id = organization_id
100
+ let file = await crud.readDocument(data);
101
101
 
102
- let pageNotFound = await crud.readDocument(data);
103
- if (!pageNotFound || !pageNotFound.document || !pageNotFound.document[0])
104
- pageNotFound = default404 || `${url} could not be found for ${organization_id}`
105
- else
106
- pageNotFound = pageNotFound.document[0].src
107
- return res.status(404).send(pageNotFound);
108
- }
109
-
110
- file = file.document[0]
111
- if (!file['public'] || file['public'] === "false") {
112
- data.filter.query[1].value = '/403.html'
113
- if (data.organization_id !== organization_id)
114
- data.organization_id = organization_id
102
+ if (!file || !file.document || !file.document[0]) {
103
+ data.filter.query[1].value = '/404.html'
104
+ if (data.organization_id !== organization_id)
105
+ data.organization_id = organization_id
115
106
 
116
- let pageForbidden = await crud.readDocument(data);
117
- if (!pageForbidden || !pageForbidden.document || !pageForbidden.document[0])
118
- pageForbidden = default403 || `${url} access not allowed for ${organization_id}`
119
- else
120
- pageForbidden = pageForbidden.document[0].src
107
+ let pageNotFound = await crud.readDocument(data);
108
+ if (!pageNotFound || !pageNotFound.document || !pageNotFound.document[0])
109
+ pageNotFound = default404 || `${pathname} could not be found for ${organization_id}`
110
+ else
111
+ pageNotFound = pageNotFound.document[0].src
112
+ res.writeHead(404, { 'Content-Type': 'text/plain' });
113
+ return res.end(pageNotFound);
114
+ }
121
115
 
122
- return res.status(403).send(pageForbidden);
123
- }
116
+ file = file.document[0]
117
+ if (!file['public'] || file['public'] === "false") {
118
+ data.filter.query[1].value = '/403.html'
119
+ if (data.organization_id !== organization_id)
120
+ data.organization_id = organization_id
121
+
122
+ let pageForbidden = await crud.readDocument(data);
123
+ if (!pageForbidden || !pageForbidden.document || !pageForbidden.document[0])
124
+ pageForbidden = default403 || `${pathname} access not allowed for ${organization_id}`
125
+ else
126
+ pageForbidden = pageForbidden.document[0].src
127
+ res.writeHead(403, { 'Content-Type': 'text/plain' });
128
+ return res.end(pageForbidden);
129
+ }
124
130
 
125
- let src;
126
- if (file['src'])
127
- src = file['src'];
128
- else {
129
- let fileSrc = await crud.readDocument({
130
- collection: file['collection'],
131
- document: {
132
- _id: file._id
133
- },
134
- organization_id
135
- });
136
- src = fileSrc[file['name']];
137
- }
131
+ let src;
132
+ if (file['src'])
133
+ src = file['src'];
134
+ else {
135
+ let fileSrc = await crud.readDocument({
136
+ collection: file['collection'],
137
+ document: {
138
+ _id: file._id
139
+ },
140
+ organization_id
141
+ });
142
+ src = fileSrc[file['name']];
143
+ }
138
144
 
139
- if (!src) {
140
- data.filter.query[1].value = '/404.html'
141
- if (data.organization_id !== organization_id)
142
- data.organization_id = organization_id
145
+ if (!src) {
146
+ data.filter.query[1].value = '/404.html'
147
+ if (data.organization_id !== organization_id)
148
+ data.organization_id = organization_id
143
149
 
144
- let pageNotFound = await crud.readDocument(data);
145
- if (!pageNotFound || !pageNotFound.document || !pageNotFound.document[0])
146
- pageNotFound = `${url} could not be found for ${organization_id}`
147
- return res.status(404).send(pageNotFound);
148
- }
150
+ let pageNotFound = await crud.readDocument(data);
151
+ if (!pageNotFound || !pageNotFound.document || !pageNotFound.document[0])
152
+ pageNotFound = `${pathname} could not be found for ${organization_id}`
149
153
 
150
- let contentType = file['content-type'] || mime.lookup(url) || 'text/html';
154
+ res.writeHead(404, { 'Content-Type': 'text/plain' });
155
+ return res.end(pageNotFound);
156
+ }
151
157
 
152
- if (contentType.startsWith('image/') || contentType.startsWith('audio/') || contentType.startsWith('video/')) {
153
- src = src.replace(/^data:image\/(png|jpeg|jpg);base64,/, '');
154
- src = Buffer.from(src, 'base64');
155
- } else if (contentType === 'text/html') {
156
- try {
157
- src = await render.HTML(src, organization_id);
158
- } catch (err) {
159
- console.warn('server-render: ' + err.message)
158
+ let contentType = file['content-type'] || 'text/html';
159
+
160
+ if (contentType.startsWith('image/') || contentType.startsWith('audio/') || contentType.startsWith('video/')) {
161
+ src = src.replace(/^data:image\/(png|jpeg|jpg);base64,/, '');
162
+ src = Buffer.from(src, 'base64');
163
+ } else if (contentType === 'text/html') {
164
+ try {
165
+ src = await render.HTML(src, organization_id);
166
+ } catch (err) {
167
+ console.warn('server-render: ' + err.message)
168
+ }
160
169
  }
170
+ res.writeHead(200, { 'Content-Type': contentType });
171
+ return res.end(src);
172
+ } catch (error) {
173
+ res.writeHead(400, { 'Content-Type': 'text/plain' });
174
+ return res.end('Invalid host format');
161
175
  }
162
-
163
- return res.type(contentType).send(src);
164
-
165
176
  })
166
-
167
177
  }
168
178
  }
169
179