@cocreate/file-server 1.18.0 → 1.18.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 +7 -0
- package/package.json +1 -1
- package/src/index.js +306 -244
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
## [1.18.1](https://github.com/CoCreate-app/CoCreate-file-server/compare/v1.18.0...v1.18.1) (2024-12-22)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* pass url to render.HTML ([c5827c3](https://github.com/CoCreate-app/CoCreate-file-server/commit/c5827c3247998014d2717b0ff426e98c9dffc145))
|
|
7
|
+
|
|
1
8
|
# [1.18.0](https://github.com/CoCreate-app/CoCreate-file-server/compare/v1.17.1...v1.18.0) (2024-11-04)
|
|
2
9
|
|
|
3
10
|
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -21,250 +21,312 @@
|
|
|
21
21
|
// For details, visit <https://cocreate.app/licenses/> or contact us at sales@cocreate.app.
|
|
22
22
|
|
|
23
23
|
class CoCreateFileSystem {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
24
|
+
constructor(render, sitemap) {
|
|
25
|
+
this.render = render;
|
|
26
|
+
this.sitemap = sitemap;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async send(req, res, crud, organization, valideUrl) {
|
|
30
|
+
try {
|
|
31
|
+
const hostname = valideUrl.hostname;
|
|
32
|
+
|
|
33
|
+
let data = {
|
|
34
|
+
method: "object.read",
|
|
35
|
+
host: hostname,
|
|
36
|
+
array: "files",
|
|
37
|
+
$filter: {
|
|
38
|
+
query: {
|
|
39
|
+
host: { $in: [hostname, "*"] }
|
|
40
|
+
},
|
|
41
|
+
limit: 1
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
let organization_id;
|
|
46
|
+
if (!organization || organization.error) {
|
|
47
|
+
let hostNotFound = await getDefaultFile("/hostNotFound.html");
|
|
48
|
+
return sendResponse(hostNotFound.object[0].src, 404, {
|
|
49
|
+
"Content-Type": "text/html"
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
organization_id = organization._id;
|
|
54
|
+
data.organization_id = organization_id;
|
|
55
|
+
|
|
56
|
+
res.setHeader("organization", organization_id);
|
|
57
|
+
res.setHeader("storage", !!organization.storage);
|
|
58
|
+
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
59
|
+
res.setHeader("Access-Control-Allow-Methods", "");
|
|
60
|
+
res.setHeader(
|
|
61
|
+
"Access-Control-Allow-Headers",
|
|
62
|
+
"Content-Type, Authorization"
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
let active = crud.wsManager.organizations.get(organization_id);
|
|
66
|
+
if (active === false) {
|
|
67
|
+
let balanceFalse = await getDefaultFile("/balanceFalse.html");
|
|
68
|
+
return sendResponse(balanceFalse.object[0].src, 403, {
|
|
69
|
+
"Content-Type": "text/html",
|
|
70
|
+
"Account-Balance": "false",
|
|
71
|
+
storage: organization.storage
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
let parameters = valideUrl.searchParams;
|
|
76
|
+
if (parameters.size) {
|
|
77
|
+
console.log("parameters", parameters);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
let pathname = valideUrl.pathname;
|
|
81
|
+
|
|
82
|
+
if (pathname.endsWith("/")) {
|
|
83
|
+
pathname += "index.html";
|
|
84
|
+
} else if (!pathname.startsWith("/.well-known/acme-challenge")) {
|
|
85
|
+
let directory = pathname.split("/").slice(-1)[0];
|
|
86
|
+
if (!directory.includes(".")) pathname += "/index.html";
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
data.$filter.query.pathname = pathname;
|
|
90
|
+
|
|
91
|
+
let file;
|
|
92
|
+
if (
|
|
93
|
+
pathname.startsWith("/dist") ||
|
|
94
|
+
pathname.startsWith("/admin") ||
|
|
95
|
+
[
|
|
96
|
+
"/403.html",
|
|
97
|
+
"/404.html",
|
|
98
|
+
"/offline.html",
|
|
99
|
+
"/manifest.webmanifest",
|
|
100
|
+
"/service-worker.js"
|
|
101
|
+
].includes(pathname)
|
|
102
|
+
)
|
|
103
|
+
file = await getDefaultFile(pathname);
|
|
104
|
+
else file = await crud.send(data);
|
|
105
|
+
|
|
106
|
+
if (!file || !file.object || !file.object[0]) {
|
|
107
|
+
pathname = valideUrl.pathname;
|
|
108
|
+
let lastIndex = pathname.lastIndexOf("/");
|
|
109
|
+
let wildcardPath = pathname.substring(0, lastIndex + 1);
|
|
110
|
+
let wildcard = pathname.substring(lastIndex + 1);
|
|
111
|
+
|
|
112
|
+
if (wildcard.includes(".")) {
|
|
113
|
+
let fileLastIndex = wildcard.lastIndexOf(".");
|
|
114
|
+
let fileExtension = wildcard.substring(fileLastIndex);
|
|
115
|
+
wildcard = wildcardPath + "*" + fileExtension; // Create wildcard for file name
|
|
116
|
+
} else {
|
|
117
|
+
wildcard = wildcardPath + "*/index.html"; // Append '*' if it's just a path or folder
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
data.$filter.query.pathname = wildcard;
|
|
121
|
+
file = await crud.send(data);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (!file || !file.object || !file.object[0]) {
|
|
125
|
+
let pageNotFound = await getDefaultFile("/404.html");
|
|
126
|
+
return sendResponse(pageNotFound.object[0].src, 404, {
|
|
127
|
+
"Content-Type": "text/html"
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
file = file.object[0];
|
|
132
|
+
if (!file["public"] || file["public"] === "false") {
|
|
133
|
+
let pageForbidden = await getDefaultFile("/403.html");
|
|
134
|
+
return sendResponse(pageForbidden.object[0].src, 403, {
|
|
135
|
+
"Content-Type": "text/html"
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
let src;
|
|
140
|
+
if (file["src"]) src = file["src"];
|
|
141
|
+
else {
|
|
142
|
+
let fileSrc = await crud.send({
|
|
143
|
+
method: "object.read",
|
|
144
|
+
host: hostname,
|
|
145
|
+
array: file["array"],
|
|
146
|
+
object: {
|
|
147
|
+
_id: file._id
|
|
148
|
+
},
|
|
149
|
+
organization_id
|
|
150
|
+
});
|
|
151
|
+
src = fileSrc[file["name"]];
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (!src) {
|
|
155
|
+
let pageNotFound = await getDefaultFile("/404.html");
|
|
156
|
+
return sendResponse(pageNotFound.object[0].src, 404, {
|
|
157
|
+
"Content-Type": "text/html"
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
let modifiedOn = file.modified || file.created;
|
|
162
|
+
if (modifiedOn) {
|
|
163
|
+
modifiedOn = modifiedOn.on;
|
|
164
|
+
if (modifiedOn instanceof Date)
|
|
165
|
+
modifiedOn = modifiedOn.toISOString();
|
|
166
|
+
res.setHeader("Last-Modified", modifiedOn);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
let contentType = file["content-type"] || "text/html";
|
|
170
|
+
|
|
171
|
+
if (
|
|
172
|
+
/^data:image\/[a-zA-Z0-9+.-]+;base64,([A-Za-z0-9+/]+={0,2})$/.test(
|
|
173
|
+
src
|
|
174
|
+
)
|
|
175
|
+
) {
|
|
176
|
+
src = src.replace(/^data:image\/(png|jpeg|jpg);base64,/, "");
|
|
177
|
+
src = Buffer.from(src, "base64");
|
|
178
|
+
} else if (/^([A-Za-z0-9+/]+={0,2})$/.test(src)) {
|
|
179
|
+
src = Buffer.from(src, "base64");
|
|
180
|
+
} else if (contentType === "text/html") {
|
|
181
|
+
try {
|
|
182
|
+
src = await this.render.HTML(
|
|
183
|
+
src,
|
|
184
|
+
organization_id,
|
|
185
|
+
valideUrl
|
|
186
|
+
);
|
|
187
|
+
} catch (err) {
|
|
188
|
+
console.warn("server-side-render: " + err.message);
|
|
189
|
+
}
|
|
190
|
+
} else if (
|
|
191
|
+
contentType === "text/xml" ||
|
|
192
|
+
contentType === "application/xml"
|
|
193
|
+
) {
|
|
194
|
+
const protocol = "https://"; // || req.headers['x-forwarded-proto'] || req.protocol;
|
|
195
|
+
src = src.replaceAll("{{$host}}", `${protocol}${hostname}`);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
sendResponse(src, 200, { "Content-Type": contentType });
|
|
199
|
+
this.sitemap.check(file, hostname);
|
|
200
|
+
|
|
201
|
+
function sendResponse(src, statusCode, headers) {
|
|
202
|
+
try {
|
|
203
|
+
if (src instanceof Uint8Array) {
|
|
204
|
+
src = Buffer.from(src);
|
|
205
|
+
} else if (Buffer.isBuffer(src)) {
|
|
206
|
+
console.log("buffer");
|
|
207
|
+
return;
|
|
208
|
+
} else if (typeof src === "object") {
|
|
209
|
+
src = JSON.stringify(src);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if (organization_id)
|
|
213
|
+
crud.wsManager.emit("setBandwidth", {
|
|
214
|
+
type: "out",
|
|
215
|
+
data: src,
|
|
216
|
+
organization_id
|
|
217
|
+
});
|
|
218
|
+
res.writeHead(statusCode, headers);
|
|
219
|
+
return res.end(src);
|
|
220
|
+
} catch (error) {
|
|
221
|
+
console.log(error);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
async function getDefaultFile(fileName) {
|
|
226
|
+
data.$filter.query.pathname = fileName;
|
|
227
|
+
// data.$filter.query.$or[0] = { pathname: fileName }
|
|
228
|
+
let defaultFile;
|
|
229
|
+
if (fileName !== "/hostNotFound.html")
|
|
230
|
+
defaultFile = await crud.send(data);
|
|
231
|
+
|
|
232
|
+
if (
|
|
233
|
+
defaultFile &&
|
|
234
|
+
defaultFile.object &&
|
|
235
|
+
defaultFile.object[0] &&
|
|
236
|
+
defaultFile.object[0].src
|
|
237
|
+
) {
|
|
238
|
+
return defaultFile;
|
|
239
|
+
} else {
|
|
240
|
+
data.$filter.query.host.$in = ["*"];
|
|
241
|
+
data.organization_id = process.env.organization_id;
|
|
242
|
+
|
|
243
|
+
if (fileName.startsWith("/admin"))
|
|
244
|
+
data.$filter.query.pathname =
|
|
245
|
+
"/superadmin" + fileName.replace("/admin", "");
|
|
246
|
+
|
|
247
|
+
defaultFile = await crud.send(data);
|
|
248
|
+
|
|
249
|
+
if (fileName !== "/hostNotFound.html") {
|
|
250
|
+
crud.wsManager.emit("setBandwidth", {
|
|
251
|
+
type: "out",
|
|
252
|
+
data,
|
|
253
|
+
organization_id
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
crud.wsManager.emit("setBandwidth", {
|
|
257
|
+
type: "in",
|
|
258
|
+
data: defaultFile,
|
|
259
|
+
organization_id
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
if (
|
|
264
|
+
defaultFile &&
|
|
265
|
+
defaultFile.object &&
|
|
266
|
+
defaultFile.object[0] &&
|
|
267
|
+
defaultFile.object[0].src
|
|
268
|
+
) {
|
|
269
|
+
if (fileName.startsWith("/admin")) {
|
|
270
|
+
data.object[0].directory = "admin";
|
|
271
|
+
data.object[0].path =
|
|
272
|
+
"/admin" +
|
|
273
|
+
data.object[0].path.replace("/superadmin", "");
|
|
274
|
+
data.object[0].pathname = fileName;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
crud.send({
|
|
278
|
+
method: "object.create",
|
|
279
|
+
host: hostname,
|
|
280
|
+
array: "files",
|
|
281
|
+
object: defaultFile.object[0],
|
|
282
|
+
organization_id
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
return defaultFile;
|
|
286
|
+
} else {
|
|
287
|
+
switch (fileName) {
|
|
288
|
+
case "/403.html":
|
|
289
|
+
defaultFile.object = [
|
|
290
|
+
{
|
|
291
|
+
src: `${pathname} access not allowed for ${organization_id}`
|
|
292
|
+
}
|
|
293
|
+
];
|
|
294
|
+
break;
|
|
295
|
+
case "/404.html":
|
|
296
|
+
defaultFile.object = [
|
|
297
|
+
{
|
|
298
|
+
src: `${pathname} could not be found for ${organization_id}`
|
|
299
|
+
}
|
|
300
|
+
];
|
|
301
|
+
break;
|
|
302
|
+
case "/balanceFalse.html":
|
|
303
|
+
defaultFile.object = [
|
|
304
|
+
{
|
|
305
|
+
src: "This organizations account balance has fallen bellow 0: "
|
|
306
|
+
}
|
|
307
|
+
];
|
|
308
|
+
break;
|
|
309
|
+
case "/hostNotFound.html":
|
|
310
|
+
defaultFile.object = [
|
|
311
|
+
{
|
|
312
|
+
src:
|
|
313
|
+
"An organization could not be found using the host: " +
|
|
314
|
+
hostname +
|
|
315
|
+
" in platformDB: " +
|
|
316
|
+
process.env.organization_id
|
|
317
|
+
}
|
|
318
|
+
];
|
|
319
|
+
break;
|
|
320
|
+
}
|
|
321
|
+
return defaultFile;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
} catch (error) {
|
|
326
|
+
res.writeHead(400, { "Content-Type": "text/plain" });
|
|
327
|
+
res.end("Invalid host format");
|
|
328
|
+
}
|
|
329
|
+
}
|
|
268
330
|
}
|
|
269
331
|
|
|
270
332
|
module.exports = CoCreateFileSystem;
|