@learnpack/learnpack 5.0.265 → 5.0.267

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.
@@ -42,7 +42,7 @@ const handleAssetCreation = async (sessionPayload, learnJson, selectedLang, lear
42
42
  lang: selectedLang,
43
43
  description: learnJson.description[selectedLang],
44
44
  learnpack_deploy_url: learnpackDeployUrl,
45
- technologies: learnJson.technologies,
45
+ technologies: learnJson.technologies.map((tech) => tech.toLowerCase().replace(/\s+/g, "-")),
46
46
  url: learnpackDeployUrl,
47
47
  category: category,
48
48
  owner: user.id,
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.processImage = exports.createLearnJson = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const command_1 = require("@oclif/command");
6
+ const buffer_1 = require("buffer");
6
7
  const youtube_transcript_1 = require("youtube-transcript");
7
8
  const express = require("express");
8
9
  const cors = require("cors");
@@ -64,7 +65,7 @@ const createLearnJson = (courseInfo) => {
64
65
  exports.createLearnJson = createLearnJson;
65
66
  const uploadFileToBucket = async (bucket, file, path) => {
66
67
  const fileRef = bucket.file(path);
67
- await fileRef.save(Buffer.from(file, "utf8"));
68
+ await fileRef.save(buffer_1.Buffer.from(file, "utf8"));
68
69
  };
69
70
  const PARAMS = {
70
71
  expected_grade_level: "8",
@@ -123,7 +124,7 @@ const createMultiLangAsset = async (bucket, rigoToken, bcToken, courseSlug, cour
123
124
  // eslint-disable-next-line no-await-in-loop
124
125
  const [indexReadmeContent] = await indexReadme.download();
125
126
  const indexReadmeString = indexReadmeContent.toString();
126
- const b64IndexReadme = Buffer.from(indexReadmeString).toString("base64");
127
+ const b64IndexReadme = buffer_1.Buffer.from(indexReadmeString).toString("base64");
127
128
  // eslint-disable-next-line no-await-in-loop
128
129
  const asset = await (0, publish_1.handleAssetCreation)({ token: bcToken, rigobotToken: rigoToken.trim() }, courseJson, lang, deployUrl, b64IndexReadme, all_translations);
129
130
  if (!asset) {
@@ -236,7 +237,7 @@ class ServeCommand extends SessionCommand_1.default {
236
237
  if (!bucket) {
237
238
  return res.status(500).send("Upload failed");
238
239
  }
239
- const buffer = Buffer.from(content, "utf-8");
240
+ const buffer = buffer_1.Buffer.from(content, "utf-8");
240
241
  const file = bucket.file(destination);
241
242
  const stream = file.createWriteStream({
242
243
  resumable: false,
@@ -347,31 +348,60 @@ class ServeCommand extends SessionCommand_1.default {
347
348
  app.post("/webhooks/:courseSlug/images/:imageId", async (req, res) => {
348
349
  const { courseSlug, imageId } = req.params;
349
350
  const body = req.body;
350
- console.log("RECEIVING IMAGE WEBHOOK", body);
351
- const imageUrl = body.image_url;
352
- const imagePath = `courses/${courseSlug}/.learn/assets/${imageId}`;
353
- const imageFile = bucket.file(imagePath);
354
- const [exists] = await imageFile.exists();
355
- if (!exists) {
356
- // Descargar la imagen
357
- const response = await fetch(imageUrl);
358
- if (!response.ok) {
359
- return res.status(400).json({
351
+ try {
352
+ if (body.error) {
353
+ (0, creatorSocket_1.emitToNotification)(imageId, {
360
354
  status: "ERROR",
361
- message: "No se pudo descargar la imagen",
355
+ message: "Error generating image",
362
356
  });
357
+ return res.json({ status: "ERROR" });
358
+ }
359
+ fs.writeFileSync(`image-${imageId}.json`, JSON.stringify(body, null, 2));
360
+ const imageUrl = body.image_url;
361
+ const format = body.format;
362
+ const imagePath = `courses/${courseSlug}/.learn/assets/${imageId}`;
363
+ const imageFile = bucket.file(imagePath);
364
+ const [exists] = await imageFile.exists();
365
+ if (!exists) {
366
+ let buffer;
367
+ if (format === "b64" || imageUrl.startsWith("data:image/")) {
368
+ const base64Data = imageUrl.includes(",") ?
369
+ imageUrl.split(",")[1] :
370
+ imageUrl;
371
+ buffer = buffer_1.Buffer.from(base64Data, "base64");
372
+ }
373
+ else if (imageUrl.startsWith("http")) {
374
+ const response = await fetch(imageUrl);
375
+ if (!response.ok) {
376
+ return res.status(400).json({
377
+ status: "ERROR",
378
+ message: "Could not download the image",
379
+ });
380
+ }
381
+ buffer = new Uint8Array(await response.arrayBuffer());
382
+ }
383
+ else {
384
+ return res.status(400).json({
385
+ status: "ERROR",
386
+ message: "Image_url format not supported",
387
+ });
388
+ }
389
+ await imageFile.save(buffer, { contentType: "image/png" });
363
390
  }
364
- const buffer = await response.arrayBuffer();
365
- // Guardar la imagen en el bucket
366
- await imageFile.save(new Uint8Array(buffer), {
367
- contentType: "image/png",
391
+ (0, creatorSocket_1.emitToNotification)(imageId, {
392
+ status: "SUCCESS",
393
+ message: "Image generated successfully",
368
394
  });
395
+ res.json({ status: "SUCCESS" });
396
+ }
397
+ catch (error) {
398
+ (0, creatorSocket_1.emitToNotification)(imageId, {
399
+ status: "ERROR",
400
+ message: "Error receiving image webhook",
401
+ });
402
+ console.error("❌ Error receiving image webhook:", error);
403
+ res.status(500).json({ error: error.message });
369
404
  }
370
- (0, creatorSocket_1.emitToNotification)(imageId, {
371
- status: "SUCCESS",
372
- message: "Image generated successfully",
373
- });
374
- res.json({ status: "SUCCESS" });
375
405
  });
376
406
  app.post("/webhooks/:courseSlug/exercise-processor/:lessonID/:rigoToken", async (req, res) => {
377
407
  // console.log("Receiving a webhook to exercise processor")
@@ -1051,7 +1081,7 @@ class ServeCommand extends SessionCommand_1.default {
1051
1081
  const { link } = req.params;
1052
1082
  try {
1053
1083
  // 1) Decode the URL
1054
- const decoded = Buffer.from(link, "base64url").toString("utf-8");
1084
+ const decoded = buffer_1.Buffer.from(link, "base64url").toString("utf-8");
1055
1085
  const ytMatch = decoded.match(YT_REGEX);
1056
1086
  if (ytMatch) {
1057
1087
  const videoId = ytMatch[1];
@@ -1119,7 +1149,7 @@ class ServeCommand extends SessionCommand_1.default {
1119
1149
  return res.status(400).json({ error: "URL is required" });
1120
1150
  }
1121
1151
  try {
1122
- const decodedUrl = Buffer.from(url, "base64url").toString("utf-8");
1152
+ const decodedUrl = buffer_1.Buffer.from(url, "base64url").toString("utf-8");
1123
1153
  const response = await axios_1.default.get(decodedUrl, {
1124
1154
  responseType: "arraybuffer",
1125
1155
  });