@cocreate/file-server 1.1.3 → 1.2.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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ # [1.2.0](https://github.com/CoCreate-app/CoCreate-file-server/compare/v1.1.3...v1.2.0) (2023-05-05)
2
+
3
+
4
+ ### Features
5
+
6
+ * /superadmin to aacess a default admin console ([04a6f15](https://github.com/CoCreate-app/CoCreate-file-server/commit/04a6f15ce0fe441d1aedda20b74d66f5d63abef0))
7
+ * 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))
8
+
1
9
  ## [1.1.3](https://github.com/CoCreate-app/CoCreate-file-server/compare/v1.1.2...v1.1.3) (2023-04-24)
2
10
 
3
11
 
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.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",
package/src/index.js CHANGED
@@ -11,25 +11,36 @@ 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
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
+
14
41
  this.router = router.get('/*', async(req, res) => {
15
42
  let hostname = req.hostname;
16
-
17
43
  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
44
  if (!organization_id) {
34
45
  let organization = await crud.readDocument({
35
46
  collection: 'organizations',
@@ -40,8 +51,11 @@ class CoCreateFileSystem {
40
51
  },
41
52
  organization_id: process.env.organization_id
42
53
  })
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);
54
+
55
+ if (!organization || !organization.document || !organization.document[0]) {
56
+ hostNotFound = hostNotFound || 'Organization cannot be found using the host: ' + hostname + ' in platformDB: ' + process.env.organization_id
57
+ return res.send(hostNotFound);
58
+ }
45
59
 
46
60
  organization_id = organization.document[0]._id
47
61
  organizations.set(hostname, organization_id)
@@ -51,17 +65,14 @@ class CoCreateFileSystem {
51
65
  if (parameters){}
52
66
  if (url.endsWith('/')) {
53
67
  url += "index.html";
54
- }
55
- else {
68
+ } else {
56
69
  let directory = url.split("/").slice(-1)[0];
57
70
  if (!directory.includes('.')){
58
71
  url += "/index.html";
59
72
  }
60
73
  }
61
-
62
- url = url.startsWith('/ws') ? url.substring(3) : url; // dev
63
-
64
- let file = await crud.readDocument({
74
+
75
+ let data = {
65
76
  collection: 'files',
66
77
  filter: {
67
78
  query: [
@@ -70,14 +81,40 @@ class CoCreateFileSystem {
70
81
  ]
71
82
  },
72
83
  organization_id
73
- });
84
+ }
85
+
86
+ if (url.startsWith('/superadmin'))
87
+ data.organization_id = process.env.organization_id
88
+
89
+ let file = await crud.readDocument(data);
74
90
 
75
- if (!file || !file.document || !file.document[0])
76
- return res.status(404).send(`${url} could not be found for ${organization_id}`);
77
-
91
+ if (!file || !file.document || !file.document[0]) {
92
+ data.filter.query[1].value = '/404.html'
93
+ if (data.organization_id !== organization_id)
94
+ data.organization_id = organization_id
95
+
96
+ let pageNotFound = await crud.readDocument(data);
97
+ if (!pageNotFound || !pageNotFound.document || !pageNotFound.document[0])
98
+ pageNotFound = default404 || `${url} could not be found for ${organization_id}`
99
+ else
100
+ pageNotFound = pageNotFound.document[0].src
101
+ return res.status(404).send(pageNotFound);
102
+ }
103
+
78
104
  file = file.document[0]
79
- if (!file['public'] || file['public'] === "false")
80
- return res.status(404).send(`access not allowed`);
105
+ if (!file['public'] || file['public'] === "false") {
106
+ data.filter.query[1].value = '/403.html'
107
+ if (data.organization_id !== organization_id)
108
+ data.organization_id = organization_id
109
+
110
+ let pageForbidden = await crud.readDocument(data);
111
+ if (!pageForbidden || !pageForbidden.document || !pageForbidden.document[0])
112
+ pageForbidden = default403 || `${url} access not allowed for ${organization_id}`
113
+ else
114
+ pageForbidden = pageForbidden.document[0].src
115
+
116
+ return res.status(403).send(pageForbidden);
117
+ }
81
118
 
82
119
  let src;
83
120
  if (file['src'])
@@ -93,8 +130,16 @@ class CoCreateFileSystem {
93
130
  src = fileSrc[file['name']];
94
131
  }
95
132
 
96
- if (!src)
97
- return res.status(404).send(`src could not be found`);
133
+ if (!src) {
134
+ data.filter.query[1].value = '/404.html'
135
+ if (data.organization_id !== organization_id)
136
+ data.organization_id = organization_id
137
+
138
+ let pageNotFound = await crud.readDocument(data);
139
+ if (!pageNotFound || !pageNotFound.document || !pageNotFound.document[0])
140
+ pageNotFound = `${url} could not be found for ${organization_id}`
141
+ return res.status(404).send(pageNotFound);
142
+ }
98
143
 
99
144
  let contentType = file['content-type'] || mime.lookup(url) || 'text/html';
100
145
 
@@ -108,6 +153,11 @@ class CoCreateFileSystem {
108
153
  console.warn('server-render: ' + err.message)
109
154
  }
110
155
  }
156
+ if (url.startsWith('/superadmin')) {
157
+ let apikey = "e968b3a6-435e-4d79-a251-b41d7d08"
158
+ src = src.replace('5ff747727005da1c272740ab', organization_id).replace('2061acef-0451-4545-f754-60cf8160', apikey)
159
+ console.log('getapikey superadmin')
160
+ }
111
161
 
112
162
  return res.type(contentType).send(src);
113
163