@cocreate/file-server 1.1.3 → 1.2.1

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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,18 @@
1
+ ## [1.2.1](https://github.com/CoCreate-app/CoCreate-file-server/compare/v1.2.0...v1.2.1) (2023-05-10)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * handeling of superadmin signup ([06a5b96](https://github.com/CoCreate-app/CoCreate-file-server/commit/06a5b963bd8f552d02baa3ac9f749071a8387653))
7
+
8
+ # [1.2.0](https://github.com/CoCreate-app/CoCreate-file-server/compare/v1.1.3...v1.2.0) (2023-05-05)
9
+
10
+
11
+ ### Features
12
+
13
+ * /superadmin to aacess a default admin console ([04a6f15](https://github.com/CoCreate-app/CoCreate-file-server/commit/04a6f15ce0fe441d1aedda20b74d66f5d63abef0))
14
+ * 404.html and 403.html fetched from orgs files, with default fallbacks handeled by platform ([c69faab](https://github.com/CoCreate-app/CoCreate-file-server/commit/c69faab0af96752ca6f6e4b0a2faa3c0584f6f92))
15
+
1
16
  ## [1.1.3](https://github.com/CoCreate-app/CoCreate-file-server/compare/v1.1.2...v1.1.3) (2023-04-24)
2
17
 
3
18
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/file-server",
3
- "version": "1.1.3",
3
+ "version": "1.2.1",
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",
package/src/index.js CHANGED
@@ -11,27 +11,41 @@ const organizations = new Map();
11
11
 
12
12
  class CoCreateFileSystem {
13
13
  constructor(crud, render) {
14
+
15
+ async function defaultFiles(fileName) {
16
+ let file = await crud.readDocument({
17
+ collection: 'files',
18
+ filter: {
19
+ query: [
20
+ {name: "path", value: fileName, operator: "$eq"}
21
+ ]
22
+ },
23
+ organization_id: process.env.organization_id
24
+ })
25
+ if (!file || !file.document || !file.document[0])
26
+ return ''
27
+ return file.document[0].src
28
+ }
29
+
30
+ let default403, default404, hostNotFound, signup
31
+ defaultFiles('/403.html').then((file) => {
32
+ default403 = file
33
+ })
34
+ defaultFiles('/404.html').then((file) => {
35
+ default404 = file
36
+ })
37
+ defaultFiles('/hostNotFound.html').then((file) => {
38
+ hostNotFound = file
39
+ })
40
+ defaultFiles('/superadmin/signup.html').then((file) => {
41
+ signup = file
42
+ })
43
+
14
44
  this.router = router.get('/*', async(req, res) => {
15
45
  let hostname = req.hostname;
16
-
17
- let organization_id = organizations.get(hostname);
18
- // console.log(req.protocol, req.secure)
19
- // let ddns = await certManager.checkDns(hostname)
20
- // let hasCert = await certManager.checkCert(hostname)
21
- // console.log(hostname, 'crt==', hasCert)
22
- // ToDo: check if secured domain by running command
23
- // sudo certbot certificates using spawn
24
- // console.dir(req.headers.host)
25
- // dns.resolve(hostname, 'TXT', (err, records) => {
26
- // if (records)
27
- // organization_id = records[0][0];
28
- // if (err)
29
- // console.log(hostname, err);
30
- // });
31
-
32
-
33
- if (!organization_id) {
34
- let organization = await crud.readDocument({
46
+ let organization = organizations.get(hostname);
47
+ if (!organization) {
48
+ let org = await crud.readDocument({
35
49
  collection: 'organizations',
36
50
  filter: {
37
51
  query: [
@@ -40,28 +54,35 @@ class CoCreateFileSystem {
40
54
  },
41
55
  organization_id: process.env.organization_id
42
56
  })
43
- if (!organization || !organization.document || !organization.document[0])
44
- return res.send('Organization cannot be found using the host---: ' + hostname + ' in platformDB: ' + process.env.organization_id);
45
57
 
46
- organization_id = organization.document[0]._id
47
- organizations.set(hostname, organization_id)
58
+ 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
+ // res.redirect(301, '/superadmin/signup.html');
62
+ // if (req.url.startsWith('/superadmin/signup.html'))
63
+ // return res.send(signup);
64
+ // else
65
+ // return res.send(hostNotFound);
66
+
67
+ } else {
68
+ organization = {_id: org.document[0]._id, key: org.document[0].apiKey}
69
+ organizations.set(hostname, organization)
70
+ }
48
71
  }
49
72
 
73
+ let organization_id = organization._id
50
74
  let [url, parameters] = req.url.split("?");
51
75
  if (parameters){}
52
76
  if (url.endsWith('/')) {
53
77
  url += "index.html";
54
- }
55
- else {
78
+ } else {
56
79
  let directory = url.split("/").slice(-1)[0];
57
80
  if (!directory.includes('.')){
58
81
  url += "/index.html";
59
82
  }
60
83
  }
61
-
62
- url = url.startsWith('/ws') ? url.substring(3) : url; // dev
63
-
64
- let file = await crud.readDocument({
84
+
85
+ let data = {
65
86
  collection: 'files',
66
87
  filter: {
67
88
  query: [
@@ -70,14 +91,40 @@ class CoCreateFileSystem {
70
91
  ]
71
92
  },
72
93
  organization_id
73
- });
94
+ }
95
+
96
+ if (url.startsWith('/superadmin'))
97
+ data.organization_id = process.env.organization_id
98
+
99
+ let file = await crud.readDocument(data);
74
100
 
75
- if (!file || !file.document || !file.document[0])
76
- return res.status(404).send(`${url} could not be found for ${organization_id}`);
77
-
101
+ if (!file || !file.document || !file.document[0]) {
102
+ data.filter.query[1].value = '/404.html'
103
+ if (data.organization_id !== organization_id)
104
+ data.organization_id = organization_id
105
+
106
+ let pageNotFound = await crud.readDocument(data);
107
+ if (!pageNotFound || !pageNotFound.document || !pageNotFound.document[0])
108
+ pageNotFound = default404 || `${url} could not be found for ${organization_id}`
109
+ else
110
+ pageNotFound = pageNotFound.document[0].src
111
+ return res.status(404).send(pageNotFound);
112
+ }
113
+
78
114
  file = file.document[0]
79
- if (!file['public'] || file['public'] === "false")
80
- return res.status(404).send(`access not allowed`);
115
+ if (!file['public'] || file['public'] === "false") {
116
+ data.filter.query[1].value = '/403.html'
117
+ if (data.organization_id !== organization_id)
118
+ data.organization_id = organization_id
119
+
120
+ let pageForbidden = await crud.readDocument(data);
121
+ if (!pageForbidden || !pageForbidden.document || !pageForbidden.document[0])
122
+ pageForbidden = default403 || `${url} access not allowed for ${organization_id}`
123
+ else
124
+ pageForbidden = pageForbidden.document[0].src
125
+
126
+ return res.status(403).send(pageForbidden);
127
+ }
81
128
 
82
129
  let src;
83
130
  if (file['src'])
@@ -93,8 +140,16 @@ class CoCreateFileSystem {
93
140
  src = fileSrc[file['name']];
94
141
  }
95
142
 
96
- if (!src)
97
- return res.status(404).send(`src could not be found`);
143
+ if (!src) {
144
+ data.filter.query[1].value = '/404.html'
145
+ if (data.organization_id !== organization_id)
146
+ data.organization_id = organization_id
147
+
148
+ let pageNotFound = await crud.readDocument(data);
149
+ if (!pageNotFound || !pageNotFound.document || !pageNotFound.document[0])
150
+ pageNotFound = `${url} could not be found for ${organization_id}`
151
+ return res.status(404).send(pageNotFound);
152
+ }
98
153
 
99
154
  let contentType = file['content-type'] || mime.lookup(url) || 'text/html';
100
155
 
@@ -108,6 +163,10 @@ class CoCreateFileSystem {
108
163
  console.warn('server-render: ' + err.message)
109
164
  }
110
165
  }
166
+ if (process.env.organization_id !== organization_id && url.startsWith('/superadmin') && contentType === 'text/html' && !url.includes('/signup.html')) {
167
+ src = src.replace(process.env.organization_id, organization_id)
168
+ src = src.replace(process.env.key, organization.key)
169
+ }
111
170
 
112
171
  return res.type(contentType).send(src);
113
172