@ejfdelgado/ejflab-back 1.35.8 → 1.35.10
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/package.json +1 -1
- package/srv/EmailHandler.mjs +40 -13
- package/srv/MyFileService.mjs +22 -3
package/package.json
CHANGED
package/srv/EmailHandler.mjs
CHANGED
|
@@ -5,20 +5,35 @@ import { MyTemplate } from "@ejfdelgado/ejflab-common/src/MyTemplate.js";
|
|
|
5
5
|
import { General } from "./common/General.mjs";
|
|
6
6
|
import MyDatesBack from "@ejfdelgado/ejflab-common/src/MyDatesBack.mjs";
|
|
7
7
|
import * as sortifyModule from "@ejfdelgado/ejflab-common/src/sortify.js";
|
|
8
|
+
import { MyFileService } from "./MyFileService.mjs";
|
|
8
9
|
|
|
9
10
|
const sortify = sortifyModule.default;
|
|
10
11
|
|
|
11
12
|
export class EmailHandler {
|
|
12
13
|
static async send(req, res) {
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
let debug = false;
|
|
15
|
+
const body = General.readParam(req, "body");
|
|
16
|
+
debug = General.readParam(req, "debug", "0", false) != "0";
|
|
17
|
+
let templateSource = General.readParam(req, "source", "local", false);
|
|
18
|
+
if (debug) {
|
|
19
|
+
console.log(`Using: SEND_GRID_VARIABLE ${JSON.stringify(process.env.SEND_GRID_VARIABLE.substring(0, 7))}...`);
|
|
20
|
+
}
|
|
15
21
|
sgMail.setApiKey(
|
|
16
22
|
process.env.SEND_GRID_VARIABLE
|
|
17
23
|
);
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
24
|
+
let contenido = '<body style="font-family: sans-serif;">Misconfigured</body>';
|
|
25
|
+
if (templateSource == "local") {
|
|
26
|
+
contenido = await MainHandler.resolveLocalFile({
|
|
27
|
+
files: [body.template],
|
|
28
|
+
});
|
|
29
|
+
} else if (templateSource == "bucket-private") {
|
|
30
|
+
const normalizedTemplate = body.template.replace(/^\s*\//, "");
|
|
31
|
+
const { data } = await MyFileService.read(normalizedTemplate);
|
|
32
|
+
contenido = { data: data.toString() }
|
|
33
|
+
}
|
|
34
|
+
if (debug) {
|
|
35
|
+
console.log(contenido);
|
|
36
|
+
}
|
|
22
37
|
const renderer = new MyTemplate();
|
|
23
38
|
renderer.registerFunction("formatDate", (millis) => {
|
|
24
39
|
try {
|
|
@@ -30,15 +45,23 @@ export class EmailHandler {
|
|
|
30
45
|
renderer.registerFunction("porcentaje1", (por) => {
|
|
31
46
|
return (100 * por).toFixed(1) + " %";
|
|
32
47
|
});
|
|
33
|
-
if (
|
|
48
|
+
if (debug) {
|
|
34
49
|
console.log(JSON.stringify(body.params, null, 4));
|
|
35
50
|
}
|
|
36
51
|
const contenidoFinal = renderer.render(
|
|
37
52
|
contenido.data,//template
|
|
38
53
|
body.params//params
|
|
39
54
|
);
|
|
55
|
+
let to = body.to;
|
|
56
|
+
if (!to) {
|
|
57
|
+
to = [MyConstants.EMAIL_SENDER];
|
|
58
|
+
} else if (to instanceof Array && to.length == 0) {
|
|
59
|
+
to.push(MyConstants.EMAIL_SENDER);
|
|
60
|
+
} else if (typeof to == "string") {
|
|
61
|
+
to = [to];
|
|
62
|
+
}
|
|
40
63
|
const msg = {
|
|
41
|
-
to
|
|
64
|
+
to,
|
|
42
65
|
from: MyConstants.EMAIL_SENDER,
|
|
43
66
|
subject: body.subject,
|
|
44
67
|
html: contenidoFinal,
|
|
@@ -46,14 +69,18 @@ export class EmailHandler {
|
|
|
46
69
|
|
|
47
70
|
if (body.replyTo) {
|
|
48
71
|
msg.replyTo = body.replyTo;
|
|
49
|
-
|
|
72
|
+
if (debug) {
|
|
73
|
+
console.log(`replyTo: ${msg.replyTo}`);
|
|
74
|
+
}
|
|
50
75
|
}
|
|
51
76
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
77
|
+
if (debug) {
|
|
78
|
+
console.log(`Using EMAIL_SENDER ${JSON.stringify(MyConstants.EMAIL_SENDER)}`);
|
|
79
|
+
//console.log(JSON.stringify(body.params, null, 4));
|
|
80
|
+
//console.log(JSON.stringify(contenidoFinal, null, 4));
|
|
81
|
+
}
|
|
55
82
|
|
|
56
|
-
if (
|
|
83
|
+
if (debug) {
|
|
57
84
|
res.status(200).set({ 'content-type': 'text/html; charset=utf-8' }).send(contenidoFinal).end();
|
|
58
85
|
} else {
|
|
59
86
|
let answer = {};
|
package/srv/MyFileService.mjs
CHANGED
|
@@ -7,14 +7,20 @@ import { MyConstants } from "@ejfdelgado/ejflab-common/src/MyConstants.js";
|
|
|
7
7
|
import { General } from "./common/General.mjs";
|
|
8
8
|
import { PassThrough } from "stream";
|
|
9
9
|
import { Gif } from "@ejfdelgado/ejflab-common/src/gif/index.mjs";
|
|
10
|
+
import { NoAutorizadoException } from "./MyError.mjs";
|
|
10
11
|
|
|
11
12
|
const storage = new Storage();
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
let defaultBucket = storage.bucket(MyConstants.BUCKET.PUBLIC);
|
|
15
|
+
let privateBucket = storage.bucket(MyConstants.BUCKET.PRIVATE);
|
|
15
16
|
|
|
16
17
|
export class MyFileService {
|
|
17
18
|
|
|
19
|
+
static refreshBucketReference() {
|
|
20
|
+
defaultBucket = storage.bucket(MyConstants.BUCKET.PUBLIC);
|
|
21
|
+
privateBucket = storage.bucket(MyConstants.BUCKET.PRIVATE);
|
|
22
|
+
}
|
|
23
|
+
|
|
18
24
|
async setFilePublic(bucketRef, fileName) {
|
|
19
25
|
await bucketRef
|
|
20
26
|
.file(fileName)
|
|
@@ -217,7 +223,7 @@ export class MyFileService {
|
|
|
217
223
|
/**
|
|
218
224
|
* @param encoding ascii, utf8 or null
|
|
219
225
|
*/
|
|
220
|
-
static async read(originalUrl, encoding = null) {
|
|
226
|
+
static async read(originalUrl, encoding = null, user = null) {
|
|
221
227
|
const filePath = decodeURIComponent(originalUrl.replace(/^\//, "").replace(/\?.*$/, ""));
|
|
222
228
|
const fileName = /[^/]+$/.exec(filePath)[0];
|
|
223
229
|
const bucket = privateBucket;
|
|
@@ -236,6 +242,19 @@ export class MyFileService {
|
|
|
236
242
|
metadata.filename = fileName;
|
|
237
243
|
metadata.fullPath = originalUrl;
|
|
238
244
|
const content = respuesta[1];
|
|
245
|
+
|
|
246
|
+
const customMetadata = metadata.metadata;
|
|
247
|
+
if (customMetadata) {
|
|
248
|
+
const { roles } = customMetadata;
|
|
249
|
+
if (roles) {
|
|
250
|
+
// It roles, then at least needs authenticated user
|
|
251
|
+
if (!user) {
|
|
252
|
+
throw new NoAutorizadoException();
|
|
253
|
+
}
|
|
254
|
+
// Here improve security adding role based scheme
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
239
258
|
resolve({
|
|
240
259
|
metadata: metadata,
|
|
241
260
|
data: content,
|