@cocreate/file-server 1.10.0 → 1.11.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,25 @@
1
+ ## [1.11.1](https://github.com/CoCreate-app/CoCreate-file-server/compare/v1.11.0...v1.11.1) (2023-09-18)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * Add path and pathname ([f2ff42d](https://github.com/CoCreate-app/CoCreate-file-server/commit/f2ff42d5ecfc46807f03753c586feec1ea848148))
7
+ * Update dCoCreate dpendencies to latest versions ([7945630](https://github.com/CoCreate-app/CoCreate-file-server/commit/794563023c42884ddfad4a9d4c434423b67ebf32))
8
+
9
+ # [1.11.0](https://github.com/CoCreate-app/CoCreate-file-server/compare/v1.10.0...v1.11.0) (2023-09-17)
10
+
11
+
12
+ ### Bug Fixes
13
+
14
+ * applied $filter in object ([1d05b5c](https://github.com/CoCreate-app/CoCreate-file-server/commit/1d05b5cafb190fe782772d8c8add14cf8ea089d3))
15
+ * moved $filter outside of object ([5d6d232](https://github.com/CoCreate-app/CoCreate-file-server/commit/5d6d232dcc51f4b0969a94817d32a96998b4026f))
16
+ * removed file-content code ([1f59131](https://github.com/CoCreate-app/CoCreate-file-server/commit/1f59131a8089e0e9f334bd4e81b88ec0a8b54cca))
17
+
18
+
19
+ ### Features
20
+
21
+ * handeling of default files and improved bandwidth tracking ([f49516a](https://github.com/CoCreate-app/CoCreate-file-server/commit/f49516adcb6292b1dd57fdda68de3a97897f787b))
22
+
1
23
  # [1.10.0](https://github.com/CoCreate-app/CoCreate-file-server/compare/v1.9.2...v1.10.0) (2023-09-07)
2
24
 
3
25
 
@@ -1,25 +1,24 @@
1
- module.exports = {
2
- "organization_id": "",
3
- "key": "",
4
- "host": "",
5
-
6
- "sources": [{
7
- "array": "files",
8
- "object": {
9
- "_id": "60145dc49f64ba1680b86693",
10
- "name": "index.html",
11
- "path": "/docs/file-server/index.html",
12
- "src": "{{./docs/index.html}}",
13
- "host": [
14
- "cocreate.app",
15
- "general.cocreate.app"
16
- ],
17
- "directory": "/docs/file-server",
18
- "content-type": "text/html",
19
- "public": "true",
20
- "website_id": "5ffbceb7f11d2d00103c4535"
21
- }
22
- }
23
- ]
24
- }
25
-
1
+ module.exports = {
2
+ "organization_id": "",
3
+ "key": "",
4
+ "host": "",
5
+ "sources": [
6
+ {
7
+ "array": "files",
8
+ "object": {
9
+ "_id": "60145dc49f64ba1680b86693",
10
+ "name": "index.html",
11
+ "path": "/docs/file-server",
12
+ "pathname": "/docs/file-server/index.html",
13
+ "src": "{{./docs/index.html}}",
14
+ "host": [
15
+ "cocreate.app",
16
+ "general.cocreate.app"
17
+ ],
18
+ "directory": "file-server",
19
+ "content-type": "text/html",
20
+ "public": "true"
21
+ }
22
+ }
23
+ ]
24
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/file-server",
3
- "version": "1.10.0",
3
+ "version": "1.11.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
@@ -26,38 +26,8 @@ const organizations = new Map();
26
26
 
27
27
  class CoCreateFileSystem {
28
28
  constructor(server, crud, render) {
29
- async function defaultFiles(fileName) {
30
- let file = await crud.send({
31
- method: 'read.object',
32
- array: 'files',
33
- filter: {
34
- query: [
35
- { key: "path", value: fileName, operator: "$eq" }
36
- ]
37
- },
38
- organization_id: process.env.organization_id
39
- })
40
- if (!file || !file.object || !file.object[0])
41
- return ''
42
- return file.object[0].src
43
- }
44
-
45
- let default403, default404, hostNotFound, signup, balanceFalse
46
- defaultFiles('/403.html').then((file) => {
47
- default403 = file
48
- })
49
- defaultFiles('/404.html').then((file) => {
50
- default404 = file
51
- })
52
- defaultFiles('/hostNotFound.html').then((file) => {
53
- hostNotFound = file
54
- })
55
- defaultFiles('/balanceFalse').then((file) => {
56
- balanceFalse = file
57
- })
58
- defaultFiles('/superadmin/signup.html').then((file) => {
59
- signup = file
60
- })
29
+
30
+ let hostNotFound
61
31
 
62
32
  server.on('request', async (req, res) => {
63
33
  try {
@@ -69,7 +39,7 @@ class CoCreateFileSystem {
69
39
  let org = await crud.send({
70
40
  method: 'read.object',
71
41
  array: 'organizations',
72
- filter: {
42
+ $filter: {
73
43
  query: [
74
44
  { key: "host", value: [hostname], operator: "$in" }
75
45
  ]
@@ -78,9 +48,9 @@ class CoCreateFileSystem {
78
48
  })
79
49
 
80
50
  if (!org || !org.object || !org.object[0]) {
81
- hostNotFound = hostNotFound || 'An organization could not be found using the host: ' + hostname + ' in platformDB: ' + process.env.organization_id
82
- res.writeHead(404, { 'Content-Type': 'text/html', 'storage': organization.storage });
83
- return res.end(hostNotFound);
51
+ if (!hostNotFound)
52
+ hostNotFound = await getDefaultFile('/hostNotFound.html')
53
+ return sendResponse(hostNotFound.object[0].src, 404, { 'Content-Type': 'text/html', 'storage': organization.storage })
84
54
  } else {
85
55
  organization = { _id: org.object[0]._id, storage: !!org.object[0].storage }
86
56
  organizations.set(hostname, organization)
@@ -89,109 +59,61 @@ class CoCreateFileSystem {
89
59
 
90
60
  let organization_id = organization._id
91
61
 
92
- let active = crud.wsManager.organizations.get(organization_id)
93
- if (active === false) {
94
- balanceFalse = balanceFalse || 'This organizations account balance has fallen bellow 0: '
95
- res.writeHead(404, { 'Content-Type': 'text/html', 'Account-Balance': 'false', 'storage': organization.storage });
96
- return res.end(balanceFalse);
97
- }
98
-
99
62
  res.setHeader('organization', organization_id)
100
63
  res.setHeader('storage', organization.storage);
101
64
  res.setHeader('Access-Control-Allow-Origin', '*');
102
65
  res.setHeader('Access-Control-Allow-Methods', '');
103
66
  res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
104
67
 
105
- let pathname = valideUrl.pathname;
106
68
  let parameters = valideUrl.searchParams;
107
69
  if (parameters.size) {
108
70
  console.log('parameters', parameters)
109
71
  }
72
+
73
+ let pathname = valideUrl.pathname;
110
74
  if (pathname.endsWith('/')) {
111
75
  pathname += "index.html";
112
76
  } else {
113
77
  let directory = pathname.split("/").slice(-1)[0];
114
- if (!directory.includes('.')) {
78
+ if (!directory.includes('.'))
115
79
  pathname += "/index.html";
116
- }
80
+ }
81
+
82
+ let active = crud.wsManager.organizations.get(organization_id)
83
+ if (active === false) {
84
+ let balanceFalse = await getDefaultFile('/balanceFalse.html')
85
+ return sendResponse(balanceFalse.object[0].src, 403, { 'Content-Type': 'text/html', 'Account-Balance': 'false', 'storage': organization.storage })
117
86
  }
118
87
 
119
88
  let data = {
120
89
  method: 'read.object',
121
90
  array: 'files',
122
- filter: {
91
+ $filter: {
123
92
  query: [
124
93
  { key: "host", value: [hostname, '*'], operator: "$in" },
125
- { key: "path", value: pathname, operator: "$eq" }
126
- ]
94
+ { key: "pathname", value: pathname, operator: "$eq" }
95
+ ],
96
+ limit: 1
127
97
  },
128
98
  organization_id
129
99
  }
130
100
 
131
- if (pathname.startsWith('/superadmin'))
132
- data.organization_id = process.env.organization_id
133
-
134
- let file = await crud.send(data);
135
-
136
- const fileContent = req.headers['File-Content']
137
- if (fileContent && !pathname.startsWith('/superadmin')) {
138
- crud.wsManager.emit("setBandwidth", {
139
- type: 'in',
140
- data: fileContent,
141
- organization_id
142
- });
143
-
144
- crud.wsManager.emit("setBandwidth", {
145
- type: 'out',
146
- data: fileContent,
147
- organization_id
148
- });
149
101
 
150
- res.writeHead(200, { 'Content-Type': req.headers['Content-Type'] });
151
- return res.end(fileContent);
152
- }
102
+ let file
103
+ if (pathname.startsWith('/dist') || pathname.startsWith('/admin') || ['/403.html', '/404.html', '/offline.html', '/manifest.webmanifest', '/service-worker.js'].includes(pathname))
104
+ file = await getDefaultFile(pathname)
105
+ else
106
+ file = await crud.send(data);
153
107
 
154
108
  if (!file || !file.object || !file.object[0]) {
155
- data.filter.query[1].value = '/404.html'
156
- if (data.organization_id !== organization_id)
157
- data.organization_id = organization_id
158
-
159
- let pageNotFound = await crud.send(data);
160
- if (!pageNotFound || !pageNotFound.object || !pageNotFound.object[0])
161
- pageNotFound = default404 || `${pathname} could not be found for ${organization_id}`
162
- else
163
- pageNotFound = pageNotFound.object[0].src
164
-
165
- crud.wsManager.emit("setBandwidth", {
166
- type: 'out',
167
- data: pageNotFound,
168
- organization_id
169
- });
170
-
171
- res.writeHead(404, { 'Content-Type': 'text/html' });
172
- return res.end(pageNotFound);
109
+ let pageNotFound = await getDefaultFile('/404.html')
110
+ return sendResponse(pageNotFound.object[0].src, 404, { 'Content-Type': 'text/html' })
173
111
  }
174
112
 
175
113
  file = file.object[0]
176
114
  if (!file['public'] || file['public'] === "false") {
177
- data.filter.query[1].value = '/403.html'
178
- if (data.organization_id !== organization_id)
179
- data.organization_id = organization_id
180
-
181
- let pageForbidden = await crud.send(data);
182
- if (!pageForbidden || !pageForbidden.object || !pageForbidden.object[0])
183
- pageForbidden = default403 || `${pathname} access not allowed for ${organization_id}`
184
- else
185
- pageForbidden = pageForbidden.object[0].src
186
-
187
- crud.wsManager.emit("setBandwidth", {
188
- type: 'out',
189
- data: pageForbidden,
190
- organization_id
191
- });
192
-
193
- res.writeHead(403, { 'Content-Type': 'text/html' });
194
- return res.end(pageForbidden);
115
+ let pageForbidden = await getDefaultFile('/403.html')
116
+ return sendResponse(pageForbidden.object[0].src, 403, { 'Content-Type': 'text/html' })
195
117
  }
196
118
 
197
119
  let src;
@@ -210,28 +132,11 @@ class CoCreateFileSystem {
210
132
  }
211
133
 
212
134
  if (!src) {
213
- data.filter.query[1].value = '/404.html'
214
- if (data.organization_id !== organization_id)
215
- data.organization_id = organization_id
216
-
217
- let pageNotFound = await crud.send(data);
218
- if (!pageNotFound || !pageNotFound.object || !pageNotFound.object[0])
219
- pageNotFound = `${pathname} could not be found for ${organization_id}`
220
- else
221
- pageNotFound = pageNotFound.object[0].src
222
-
223
- crud.wsManager.emit("setBandwidth", {
224
- type: 'out',
225
- data: pageNotFound,
226
- organization_id
227
- });
228
-
229
- res.writeHead(404, { 'Content-Type': 'text/html' });
230
- return res.end(pageNotFound);
135
+ let pageNotFound = await getDefaultFile('/404.html')
136
+ return sendResponse(pageNotFound.object[0].src, 404, { 'Content-Type': 'text/html' })
231
137
  }
232
138
 
233
139
  let contentType = file['content-type'] || 'text/html';
234
-
235
140
  if (contentType.startsWith('image/') || contentType.startsWith('audio/') || contentType.startsWith('video/')) {
236
141
  src = src.replace(/^data:image\/(png|jpeg|jpg);base64,/, '');
237
142
  src = Buffer.from(src, 'base64');
@@ -242,20 +147,84 @@ class CoCreateFileSystem {
242
147
  console.warn('server-render: ' + err.message)
243
148
  }
244
149
  }
150
+
245
151
  if (file.modified)
246
152
  res.setHeader('Last-Modified', file.modified.on);
247
153
 
248
- crud.wsManager.emit("setBandwidth", {
249
- type: 'out',
250
- data: src,
251
- organization_id
252
- });
154
+ sendResponse(src, 200, { 'Content-Type': contentType })
155
+
156
+ function sendResponse(src, statusCode, headers) {
157
+ crud.wsManager.emit("setBandwidth", {
158
+ type: 'out',
159
+ data: src,
160
+ organization_id
161
+ });
162
+
163
+ res.writeHead(statusCode, headers);
164
+ return res.end(src);
165
+ }
166
+
167
+ async function getDefaultFile(fileName) {
168
+ data.$filter.query[1].value = fileName
169
+ let defaultFile
170
+ if (fileName !== '/hostNotFound.html')
171
+ defaultFile = await crud.send(data);
172
+
173
+ if (defaultFile && defaultFile.object && defaultFile.object[0] && defaultFile.object[0].src) {
174
+ return defaultFile
175
+ } else {
176
+ data.$filter.query[0].value = ['*']
177
+ data.organization_id = process.env.organization_id
178
+
179
+ defaultFile = await crud.send(data)
180
+
181
+ if (fileName !== '/hostNotFound.html') {
182
+ crud.wsManager.emit("setBandwidth", {
183
+ type: 'out',
184
+ data,
185
+ organization_id
186
+ });
187
+
188
+ crud.wsManager.emit("setBandwidth", {
189
+ type: 'in',
190
+ data: defaultFile,
191
+ organization_id
192
+ });
193
+ }
194
+
195
+ if (defaultFile && defaultFile.object && defaultFile.object[0] && defaultFile.object[0].src) {
196
+ crud.send({
197
+ method: 'create.object',
198
+ array: 'files',
199
+ object: defaultFile.object[0],
200
+ organization_id
201
+ })
202
+
203
+ return defaultFile
204
+ } else {
205
+ switch (fileName) {
206
+ case '/403.html':
207
+ defaultFile.object = [{ src: `${pathname} access not allowed for ${organization_id}` }]
208
+ break;
209
+ case '/404.html':
210
+ defaultFile.object = [{ src: `${pathname} could not be found for ${organization_id}` }];
211
+ break;
212
+ case '/balanceFalse.html':
213
+ defaultFile.object = [{ src: 'This organizations account balance has fallen bellow 0: ' }];
214
+ break;
215
+ case '/hostNotFound.html':
216
+ defaultFile.object = [{ src: 'An organization could not be found using the host: ' + hostname + ' in platformDB: ' + process.env.organization_id }];
217
+ break;
218
+ }
219
+ return defaultFile
220
+ }
221
+ }
222
+ }
223
+
253
224
 
254
- res.writeHead(200, { 'Content-Type': contentType });
255
- return res.end(src);
256
225
  } catch (error) {
257
226
  res.writeHead(400, { 'Content-Type': 'text/plain' });
258
- return res.end('Invalid host format');
227
+ res.end('Invalid host format');
259
228
  }
260
229
  })
261
230
  }