Update plugins/carbon.js
Browse files- plugins/carbon.js +27 -11
plugins/carbon.js
CHANGED
@@ -1,28 +1,43 @@
|
|
1 |
-
import { ParametersUrl } from '../lib/scrapper.js'
|
2 |
import fetch from "node-fetch";
|
3 |
import express from 'express';
|
4 |
import { Readable } from "stream";
|
5 |
import sharp from "sharp";
|
|
|
6 |
const CarbonRoutes = express.Router();
|
7 |
|
|
|
|
|
|
|
|
|
|
|
8 |
function paramCode(code) {
|
9 |
-
|
10 |
-
|
11 |
}
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
13 |
async function MakerCarbon(args) {
|
14 |
const url = ParametersUrl("maker/carbon");
|
15 |
try {
|
16 |
const params = paramCode(args);
|
17 |
-
const
|
|
|
|
|
|
|
18 |
method: "GET",
|
19 |
});
|
20 |
-
|
21 |
if (!response.ok) {
|
22 |
console.error(`API Error: ${response.status}`);
|
23 |
return null;
|
24 |
}
|
25 |
-
|
|
|
26 |
} catch (error) {
|
27 |
console.error("Error fetching data:", error.message);
|
28 |
return null;
|
@@ -30,13 +45,14 @@ async function MakerCarbon(args) {
|
|
30 |
}
|
31 |
|
32 |
/**
|
|
|
33 |
* @swagger
|
34 |
* /api/v1/maker/carbon:
|
35 |
* get:
|
36 |
-
* summary:
|
37 |
-
* description:
|
38 |
* parameters:
|
39 |
-
* - in:
|
40 |
* name: code
|
41 |
* required: true
|
42 |
* description: The code to be processed.
|
@@ -59,7 +75,7 @@ CarbonRoutes.get("/api/v1/maker/carbon", async (req, res) => {
|
|
59 |
try {
|
60 |
const code = req.query.code;
|
61 |
if (!code) {
|
62 |
-
return res.status(400).send("Query parameter is missing");
|
63 |
}
|
64 |
|
65 |
const imageBytes = await MakerCarbon(code);
|
|
|
1 |
+
import { ParametersUrl } from '../lib/scrapper.js';
|
2 |
import fetch from "node-fetch";
|
3 |
import express from 'express';
|
4 |
import { Readable } from "stream";
|
5 |
import sharp from "sharp";
|
6 |
+
|
7 |
const CarbonRoutes = express.Router();
|
8 |
|
9 |
+
/**
|
10 |
+
* Encode the query parameter.
|
11 |
+
* @param {string} code - The code to be encoded.
|
12 |
+
* @returns {string} Encoded query parameter string.
|
13 |
+
*/
|
14 |
function paramCode(code) {
|
15 |
+
const params = new URLSearchParams({ code: code });
|
16 |
+
return params.toString();
|
17 |
}
|
18 |
+
|
19 |
+
/**
|
20 |
+
* Fetch the carbon image using the provided code.
|
21 |
+
* @param {string} args - The code to be processed.
|
22 |
+
* @returns {Promise<Buffer|null>} The image as a Buffer, or null if an error occurs.
|
23 |
+
*/
|
24 |
async function MakerCarbon(args) {
|
25 |
const url = ParametersUrl("maker/carbon");
|
26 |
try {
|
27 |
const params = paramCode(args);
|
28 |
+
const finalUrl = `${url}?${params}`;
|
29 |
+
console.log("Fetching URL:", finalUrl); // Debugging the URL
|
30 |
+
|
31 |
+
const response = await fetch(finalUrl, {
|
32 |
method: "GET",
|
33 |
});
|
34 |
+
|
35 |
if (!response.ok) {
|
36 |
console.error(`API Error: ${response.status}`);
|
37 |
return null;
|
38 |
}
|
39 |
+
|
40 |
+
return await response.arrayBuffer(); // Fetch as binary data
|
41 |
} catch (error) {
|
42 |
console.error("Error fetching data:", error.message);
|
43 |
return null;
|
|
|
45 |
}
|
46 |
|
47 |
/**
|
48 |
+
* API route for processing carbon images.
|
49 |
* @swagger
|
50 |
* /api/v1/maker/carbon:
|
51 |
* get:
|
52 |
+
* summary: Generate a carbon image
|
53 |
+
* description: Processes a code snippet into a styled image.
|
54 |
* parameters:
|
55 |
+
* - in: query
|
56 |
* name: code
|
57 |
* required: true
|
58 |
* description: The code to be processed.
|
|
|
75 |
try {
|
76 |
const code = req.query.code;
|
77 |
if (!code) {
|
78 |
+
return res.status(400).send("Query parameter 'code' is missing");
|
79 |
}
|
80 |
|
81 |
const imageBytes = await MakerCarbon(code);
|