randydev commited on
Commit
1e7f8cc
·
verified ·
1 Parent(s): d4eeb87

Create carbon.js

Browse files
Files changed (1) hide show
  1. plugins/carbon.js +47 -0
plugins/carbon.js ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { ParametersUrl } from '../lib/scrapper.js'
2
+ import fetch from "node-fetch";
3
+ const CarbonRoutes = express.Router();
4
+
5
+ async function MakerCarbon(args) {
6
+ const url = ParametersUrl("maker/carbon");
7
+ try {
8
+ const response = await fetch(url, {
9
+ method: "GET",
10
+ body: JSON.stringify({ code: args }),
11
+ });
12
+
13
+ if (!response.ok) {
14
+ console.error(`API Error: ${response.status}`);
15
+ return null;
16
+ }
17
+ return await response.arrayBuffer();
18
+ } catch (error) {
19
+ console.error("Error fetching data:", error.message);
20
+ return null;
21
+ }
22
+ }
23
+
24
+ CarbonRoutes.post("/api/v1/maker/carbon", async (req, res) => {
25
+ try {
26
+ const query = req.body.query;
27
+ if (!query) {
28
+ return res.status(400).send("Query parameter is missing");
29
+ }
30
+
31
+ const imageBytes = await MakerCarbon(query);
32
+ if (!imageBytes) {
33
+ return res.status(500).json({ error: "Failed to fetch image bytes" });
34
+ }
35
+
36
+ const buffer = Buffer.isBuffer(imageBytes) ? imageBytes : Buffer.from(imageBytes);
37
+
38
+ const processedImage = await sharp(buffer).jpeg().toBuffer();
39
+ res.set("Content-Type", "image/jpeg");
40
+ Readable.from(processedImage).pipe(res);
41
+ } catch (error) {
42
+ console.error("Error processing image:", error.message);
43
+ res.status(500).json({ error: "Internal server error" });
44
+ }
45
+ });
46
+
47
+ export { CarbonRoutes };