@cocreate/file-server 1.9.2 → 1.11.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,25 @@
1
+ # [1.11.0](https://github.com/CoCreate-app/CoCreate-file-server/compare/v1.10.0...v1.11.0) (2023-09-17)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * applied $filter in object ([1d05b5c](https://github.com/CoCreate-app/CoCreate-file-server/commit/1d05b5cafb190fe782772d8c8add14cf8ea089d3))
7
+ * moved $filter outside of object ([5d6d232](https://github.com/CoCreate-app/CoCreate-file-server/commit/5d6d232dcc51f4b0969a94817d32a96998b4026f))
8
+ * removed file-content code ([1f59131](https://github.com/CoCreate-app/CoCreate-file-server/commit/1f59131a8089e0e9f334bd4e81b88ec0a8b54cca))
9
+
10
+
11
+ ### Features
12
+
13
+ * handeling of default files and improved bandwidth tracking ([f49516a](https://github.com/CoCreate-app/CoCreate-file-server/commit/f49516adcb6292b1dd57fdda68de3a97897f787b))
14
+
15
+ # [1.10.0](https://github.com/CoCreate-app/CoCreate-file-server/compare/v1.9.2...v1.10.0) (2023-09-07)
16
+
17
+
18
+ ### Features
19
+
20
+ * defaultFile balanceFalse returned as response if organization balance false below 0 ([ecd86b1](https://github.com/CoCreate-app/CoCreate-file-server/commit/ecd86b1ee76007bbfffa88841072970804cc8e98))
21
+ * Log inBound and outBound bandwidth ([2f79c40](https://github.com/CoCreate-app/CoCreate-file-server/commit/2f79c40e8429c6cb189964a51f4a275497bc352b))
22
+
1
23
  ## [1.9.2](https://github.com/CoCreate-app/CoCreate-file-server/compare/v1.9.1...v1.9.2) (2023-08-21)
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/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
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/file-server",
3
- "version": "1.9.2",
3
+ "version": "1.11.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
@@ -26,35 +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
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('/superadmin/signup.html').then((file) => {
56
- signup = file
57
- })
29
+
30
+ let hostNotFound
58
31
 
59
32
  server.on('request', async (req, res) => {
60
33
  try {
@@ -66,7 +39,7 @@ class CoCreateFileSystem {
66
39
  let org = await crud.send({
67
40
  method: 'read.object',
68
41
  array: 'organizations',
69
- filter: {
42
+ $filter: {
70
43
  query: [
71
44
  { key: "host", value: [hostname], operator: "$in" }
72
45
  ]
@@ -75,95 +48,72 @@ class CoCreateFileSystem {
75
48
  })
76
49
 
77
50
  if (!org || !org.object || !org.object[0]) {
78
- hostNotFound = hostNotFound || 'An organization could not be found using the host: ' + hostname + ' in platformDB: ' + process.env.organization_id
79
- res.writeHead(404, { 'Content-Type': 'text/html' });
80
- if (org.storage === false && org.error)
81
- res.setHeader('storage', 'false')
82
- else
83
- res.setHeader('storage', 'true')
84
-
85
- 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 })
86
54
  } else {
87
55
  organization = { _id: org.object[0]._id, storage: !!org.object[0].storage }
88
56
  organizations.set(hostname, organization)
89
57
  }
90
-
91
58
  }
92
59
 
93
60
  let organization_id = organization._id
61
+
94
62
  res.setHeader('organization', organization_id)
63
+ res.setHeader('storage', organization.storage);
95
64
  res.setHeader('Access-Control-Allow-Origin', '*');
96
65
  res.setHeader('Access-Control-Allow-Methods', '');
97
66
  res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
98
67
 
99
- let pathname = valideUrl.pathname;
100
68
  let parameters = valideUrl.searchParams;
101
69
  if (parameters.size) {
102
70
  console.log('parameters', parameters)
103
71
  }
72
+
73
+ let pathname = valideUrl.pathname;
104
74
  if (pathname.endsWith('/')) {
105
75
  pathname += "index.html";
106
76
  } else {
107
77
  let directory = pathname.split("/").slice(-1)[0];
108
- if (!directory.includes('.')) {
78
+ if (!directory.includes('.'))
109
79
  pathname += "/index.html";
110
- }
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 })
111
86
  }
112
87
 
113
88
  let data = {
114
89
  method: 'read.object',
115
90
  array: 'files',
116
- filter: {
91
+ $filter: {
117
92
  query: [
118
93
  { key: "host", value: [hostname, '*'], operator: "$in" },
119
94
  { key: "path", value: pathname, operator: "$eq" }
120
- ]
95
+ ],
96
+ limit: 1
121
97
  },
122
98
  organization_id
123
99
  }
124
100
 
125
- if (pathname.startsWith('/superadmin'))
126
- data.organization_id = process.env.organization_id
127
101
 
128
- let file = await crud.send(data);
129
- if (file.storage === false && file.error)
130
- res.setHeader('storage', 'false')
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)
131
105
  else
132
- res.setHeader('storage', 'true')
133
-
134
- const fileContent = req.headers['File-Content']
135
- if (fileContent && !pathname.startsWith('/superadmin')) {
136
- res.writeHead(200, { 'Content-Type': req.headers['Content-Type'] });
137
- return res.end(fileContent);
138
- }
106
+ file = await crud.send(data);
139
107
 
140
108
  if (!file || !file.object || !file.object[0]) {
141
- data.filter.query[1].value = '/404.html'
142
- if (data.organization_id !== organization_id)
143
- data.organization_id = organization_id
144
-
145
- let pageNotFound = await crud.send(data);
146
- if (!pageNotFound || !pageNotFound.object || !pageNotFound.object[0])
147
- pageNotFound = default404 || `${pathname} could not be found for ${organization_id}`
148
- else
149
- pageNotFound = pageNotFound.object[0].src
150
- res.writeHead(404, { 'Content-Type': 'text/html' });
151
- return res.end(pageNotFound);
109
+ let pageNotFound = await getDefaultFile('/404.html')
110
+ return sendResponse(pageNotFound.object[0].src, 404, { 'Content-Type': 'text/html' })
152
111
  }
153
112
 
154
113
  file = file.object[0]
155
114
  if (!file['public'] || file['public'] === "false") {
156
- data.filter.query[1].value = '/403.html'
157
- if (data.organization_id !== organization_id)
158
- data.organization_id = organization_id
159
-
160
- let pageForbidden = await crud.send(data);
161
- if (!pageForbidden || !pageForbidden.object || !pageForbidden.object[0])
162
- pageForbidden = default403 || `${pathname} access not allowed for ${organization_id}`
163
- else
164
- pageForbidden = pageForbidden.object[0].src
165
- res.writeHead(403, { 'Content-Type': 'text/html' });
166
- return res.end(pageForbidden);
115
+ let pageForbidden = await getDefaultFile('/403.html')
116
+ return sendResponse(pageForbidden.object[0].src, 403, { 'Content-Type': 'text/html' })
167
117
  }
168
118
 
169
119
  let src;
@@ -182,23 +132,11 @@ class CoCreateFileSystem {
182
132
  }
183
133
 
184
134
  if (!src) {
185
- data.filter.query[1].value = '/404.html'
186
- if (data.organization_id !== organization_id)
187
- data.organization_id = organization_id
188
-
189
- let pageNotFound = await crud.send(data);
190
- if (!pageNotFound || !pageNotFound.object || !pageNotFound.object[0])
191
- pageNotFound = `${pathname} could not be found for ${organization_id}`
192
- else
193
- pageNotFound = pageNotFound.object[0].src
194
-
195
-
196
- res.writeHead(404, { 'Content-Type': 'text/html' });
197
- return res.end(pageNotFound);
135
+ let pageNotFound = await getDefaultFile('/404.html')
136
+ return sendResponse(pageNotFound.object[0].src, 404, { 'Content-Type': 'text/html' })
198
137
  }
199
138
 
200
139
  let contentType = file['content-type'] || 'text/html';
201
-
202
140
  if (contentType.startsWith('image/') || contentType.startsWith('audio/') || contentType.startsWith('video/')) {
203
141
  src = src.replace(/^data:image\/(png|jpeg|jpg);base64,/, '');
204
142
  src = Buffer.from(src, 'base64');
@@ -209,14 +147,84 @@ class CoCreateFileSystem {
209
147
  console.warn('server-render: ' + err.message)
210
148
  }
211
149
  }
150
+
212
151
  if (file.modified)
213
152
  res.setHeader('Last-Modified', file.modified.on);
214
153
 
215
- res.writeHead(200, { 'Content-Type': contentType });
216
- return res.end(src);
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
+
224
+
217
225
  } catch (error) {
218
226
  res.writeHead(400, { 'Content-Type': 'text/plain' });
219
- return res.end('Invalid host format');
227
+ res.end('Invalid host format');
220
228
  }
221
229
  })
222
230
  }