File size: 2,791 Bytes
a2cbcc4
1e7f8cc
da3dc33
c303531
 
a2cbcc4
1e7f8cc
 
a2cbcc4
 
 
 
 
3e7a4be
a2cbcc4
 
3e7a4be
a2cbcc4
 
 
 
 
 
1e7f8cc
 
 
3e7a4be
a2cbcc4
 
1e7f8cc
 
a2cbcc4
1e7f8cc
 
 
 
a2cbcc4
eec3e1c
1e7f8cc
 
 
 
 
 
e8dca3f
a2cbcc4
e8dca3f
e1b410d
340cc99
a2cbcc4
 
75ae529
a2cbcc4
75ae529
 
 
 
 
e8dca3f
 
 
 
 
 
 
 
 
 
 
 
 
655e0fb
1e7f8cc
cfc7a40
3e7a4be
a2cbcc4
1e7f8cc
 
3e7a4be
1e7f8cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
import { ParametersUrl } from '../lib/scrapper.js';
import fetch from "node-fetch";
import express from 'express';
import { Readable } from "stream";
import sharp from "sharp";

const CarbonRoutes = express.Router();

/**
 * Encode the query parameter.
 * @param {string} code - The code to be encoded.
 * @returns {string} Encoded query parameter string.
 */
function paramCode(code) {
    const params = new URLSearchParams({ code: code });
    return params.toString();
}

/**
 * Fetch the carbon image using the provided code.
 * @param {string} args - The code to be processed.
 * @returns {Promise<Buffer|null>} The image as a Buffer, or null if an error occurs.
 */
async function MakerCarbon(args) {
    const url = ParametersUrl("maker/carbon");
    try {
        const params = paramCode(args);
        const finalUrl = `${url}?${params}`;
        const response = await fetch(finalUrl, {
            method: "GET",
        });

        if (!response.ok) {
            console.error(`API Error: ${response.status}`);
            return null;
        }

        return await response.arrayBuffer();
    } catch (error) {
        console.error("Error fetching data:", error.message);
        return null;
    }
}

/**
 * API route for processing carbon images.
 * @swagger
 * /api/v1/maker/carbon:
 *   get:
 *     summary: Generate a carbon image
 *     description: Processes a code snippet into a styled image.
 *     parameters:
 *       - in: query
 *         name: code
 *         required: true
 *         description: The code to be processed.
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: A successfully processed image.
 *         content:
 *           image/jpeg:
 *             schema:
 *               type: string
 *               format: binary
 *       400:
 *         description: Missing query parameter.
 *       500:
 *         description: Internal server error.
 */
CarbonRoutes.get("/api/v1/maker/carbon", async (req, res) => {
    try {
        const code = req.query.code;
        if (!code) {
            return res.status(400).send("Query parameter 'code' is missing");
        }

        const imageBytes = await MakerCarbon(code);
        if (!imageBytes) {
            return res.status(500).json({ error: "Failed to fetch image bytes" });
        }

        const buffer = Buffer.isBuffer(imageBytes) ? imageBytes : Buffer.from(imageBytes);

        const processedImage = await sharp(buffer).jpeg().toBuffer();
        res.set("Content-Type", "image/jpeg");
        Readable.from(processedImage).pipe(res);
    } catch (error) {
        console.error("Error processing image:", error.message);
        res.status(500).json({ error: "Internal server error" });
    }
});

export { CarbonRoutes };