@cocreate/file-server 1.3.0 → 1.4.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,17 @@
1
+ ## [1.4.1](https://github.com/CoCreate-app/CoCreate-file-server/compare/v1.4.0...v1.4.1) (2023-05-20)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * hostNotFound response ([c97ea45](https://github.com/CoCreate-app/CoCreate-file-server/commit/c97ea4511e1275aacbe71bb08d4f4efed2b290a4))
7
+
8
+ # [1.4.0](https://github.com/CoCreate-app/CoCreate-file-server/compare/v1.3.0...v1.4.0) (2023-05-19)
9
+
10
+
11
+ ### Features
12
+
13
+ * add organization_id to response headers ([2f731f5](https://github.com/CoCreate-app/CoCreate-file-server/commit/2f731f582f535d93b223bfaad98a52675953e11e))
14
+
1
15
  # [1.3.0](https://github.com/CoCreate-app/CoCreate-file-server/compare/v1.2.1...v1.3.0) (2023-05-11)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/file-server",
3
- "version": "1.3.0",
3
+ "version": "1.4.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,13 +11,13 @@ const organizations = new Map();
11
11
 
12
12
  class CoCreateFileSystem {
13
13
  constructor(crud, render) {
14
-
14
+
15
15
  async function defaultFiles(fileName) {
16
- let file = await crud.readDocument({
16
+ let file = await crud.readDocument({
17
17
  collection: 'files',
18
18
  filter: {
19
19
  query: [
20
- {name: "path", value: fileName, operator: "$eq"}
20
+ { name: "path", value: fileName, operator: "$eq" }
21
21
  ]
22
22
  },
23
23
  organization_id: process.env.organization_id
@@ -41,68 +41,68 @@ class CoCreateFileSystem {
41
41
  signup = file
42
42
  })
43
43
 
44
- this.router = router.get('/*', async(req, res) => {
44
+ this.router = router.get('/*', async (req, res) => {
45
45
  let hostname = req.hostname;
46
46
  let organization = organizations.get(hostname);
47
47
  if (!organization) {
48
- let org = await crud.readDocument({
48
+ let org = await crud.readDocument({
49
49
  collection: 'organizations',
50
50
  filter: {
51
51
  query: [
52
- {name: "hosts", value: [hostname], operator: "$in"}
52
+ { name: "hosts", value: [hostname], operator: "$in" }
53
53
  ]
54
54
  },
55
55
  organization_id: process.env.organization_id
56
56
  })
57
57
 
58
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
- if (!req.url.startsWith('/superadmin'))
62
- return res.redirect(301, 'https://' + hostname + '/superadmin/signup.html')
59
+ hostNotFound = hostNotFound || 'An organization could not be found using the host: ' + hostname + ' in platformDB: ' + process.env.organization_id
60
+ return res.end(hostNotFound);
63
61
  } else {
64
- organization = {_id: org.document[0]._id, key: org.document[0].key}
62
+ organization = { _id: org.document[0]._id }
65
63
  organizations.set(hostname, organization)
66
64
  }
67
65
  }
68
66
 
69
67
  let organization_id = organization._id
68
+ res.set('organization', organization_id)
69
+
70
70
  let [url, parameters] = req.url.split("?");
71
- if (parameters){}
71
+ if (parameters) { }
72
72
  if (url.endsWith('/')) {
73
73
  url += "index.html";
74
74
  } else {
75
75
  let directory = url.split("/").slice(-1)[0];
76
- if (!directory.includes('.')){
76
+ if (!directory.includes('.')) {
77
77
  url += "/index.html";
78
78
  }
79
79
  }
80
-
80
+
81
81
  let data = {
82
82
  collection: 'files',
83
83
  filter: {
84
84
  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" }
87
87
  ]
88
88
  },
89
89
  organization_id
90
90
  }
91
91
 
92
- if (url.startsWith('/superadmin'))
92
+ if (url.startsWith('/superadmin'))
93
93
  data.organization_id = process.env.organization_id
94
94
 
95
95
  let file = await crud.readDocument(data);
96
-
96
+
97
97
  if (!file || !file.document || !file.document[0]) {
98
98
  data.filter.query[1].value = '/404.html'
99
99
  if (data.organization_id !== organization_id)
100
100
  data.organization_id = organization_id
101
101
 
102
- let pageNotFound = await crud.readDocument(data);
102
+ let pageNotFound = await crud.readDocument(data);
103
103
  if (!pageNotFound || !pageNotFound.document || !pageNotFound.document[0])
104
104
  pageNotFound = default404 || `${url} could not be found for ${organization_id}`
105
- else
105
+ else
106
106
  pageNotFound = pageNotFound.document[0].src
107
107
  return res.status(404).send(pageNotFound);
108
108
  }
@@ -113,15 +113,15 @@ class CoCreateFileSystem {
113
113
  if (data.organization_id !== organization_id)
114
114
  data.organization_id = organization_id
115
115
 
116
- let pageForbidden = await crud.readDocument(data);
116
+ let pageForbidden = await crud.readDocument(data);
117
117
  if (!pageForbidden || !pageForbidden.document || !pageForbidden.document[0])
118
118
  pageForbidden = default403 || `${url} access not allowed for ${organization_id}`
119
- else
119
+ else
120
120
  pageForbidden = pageForbidden.document[0].src
121
121
 
122
122
  return res.status(403).send(pageForbidden);
123
123
  }
124
-
124
+
125
125
  let src;
126
126
  if (file['src'])
127
127
  src = file['src'];
@@ -135,18 +135,18 @@ class CoCreateFileSystem {
135
135
  });
136
136
  src = fileSrc[file['name']];
137
137
  }
138
-
138
+
139
139
  if (!src) {
140
140
  data.filter.query[1].value = '/404.html'
141
141
  if (data.organization_id !== organization_id)
142
142
  data.organization_id = organization_id
143
143
 
144
- let pageNotFound = await crud.readDocument(data);
144
+ let pageNotFound = await crud.readDocument(data);
145
145
  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}`
147
147
  return res.status(404).send(pageNotFound);
148
148
  }
149
-
149
+
150
150
  let contentType = file['content-type'] || mime.lookup(url) || 'text/html';
151
151
 
152
152
  if (contentType.startsWith('image/') || contentType.startsWith('audio/') || contentType.startsWith('video/')) {
@@ -158,18 +158,13 @@ class CoCreateFileSystem {
158
158
  } catch (err) {
159
159
  console.warn('server-render: ' + err.message)
160
160
  }
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)
165
161
  }
166
162
 
167
163
  return res.type(contentType).send(src);
168
-
164
+
169
165
  })
170
-
171
- }
166
+
167
+ }
172
168
  }
173
169
 
174
170
  module.exports = CoCreateFileSystem;
175
-