Spaces:
Sleeping
Sleeping
Create index.js
Browse files
index.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import express from "express";
|
| 2 |
+
import fs from "fs/promises";
|
| 3 |
+
import os from "os";
|
| 4 |
+
import path from "path";
|
| 5 |
+
import crypto from "crypto";
|
| 6 |
+
|
| 7 |
+
import { bratGen } from "brat-canvas";
|
| 8 |
+
import { bratVid } from "brat-canvas/video";
|
| 9 |
+
|
| 10 |
+
const app = express();
|
| 11 |
+
|
| 12 |
+
app.use(express.json({
|
| 13 |
+
limit: "10mb"
|
| 14 |
+
}));
|
| 15 |
+
|
| 16 |
+
app.get("/", (req, res) => {
|
| 17 |
+
res.json({
|
| 18 |
+
status: true,
|
| 19 |
+
creator: "Farel",
|
| 20 |
+
endpoints: {
|
| 21 |
+
image: "/image?text=halo",
|
| 22 |
+
video: "/video?text=halo",
|
| 23 |
+
gif: "/gif?text=halo"
|
| 24 |
+
}
|
| 25 |
+
});
|
| 26 |
+
});
|
| 27 |
+
|
| 28 |
+
app.get("/image", async (req, res) => {
|
| 29 |
+
try {
|
| 30 |
+
const text = req.query.text;
|
| 31 |
+
|
| 32 |
+
if (!text) {
|
| 33 |
+
return res.status(400).json({
|
| 34 |
+
status: false,
|
| 35 |
+
message: "text required"
|
| 36 |
+
});
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
const buffer = await bratGen(text);
|
| 40 |
+
|
| 41 |
+
res.setHeader("Content-Type", "image/png");
|
| 42 |
+
res.setHeader("Content-Length", buffer.length);
|
| 43 |
+
|
| 44 |
+
res.end(buffer);
|
| 45 |
+
|
| 46 |
+
} catch (e) {
|
| 47 |
+
res.status(500).json({
|
| 48 |
+
status: false,
|
| 49 |
+
error: e.message
|
| 50 |
+
});
|
| 51 |
+
}
|
| 52 |
+
});
|
| 53 |
+
|
| 54 |
+
app.get("/video", async (req, res) => {
|
| 55 |
+
try {
|
| 56 |
+
const text = req.query.text;
|
| 57 |
+
|
| 58 |
+
if (!text) {
|
| 59 |
+
return res.status(400).json({
|
| 60 |
+
status: false,
|
| 61 |
+
message: "text required"
|
| 62 |
+
});
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
const buffer = await bratVid(text, {
|
| 66 |
+
outputFormat: "mp4"
|
| 67 |
+
});
|
| 68 |
+
|
| 69 |
+
res.setHeader("Content-Type", "video/mp4");
|
| 70 |
+
res.setHeader("Content-Length", buffer.length);
|
| 71 |
+
|
| 72 |
+
res.end(buffer);
|
| 73 |
+
|
| 74 |
+
} catch (e) {
|
| 75 |
+
res.status(500).json({
|
| 76 |
+
status: false,
|
| 77 |
+
error: e.message
|
| 78 |
+
});
|
| 79 |
+
}
|
| 80 |
+
});
|
| 81 |
+
|
| 82 |
+
app.get("/gif", async (req, res) => {
|
| 83 |
+
try {
|
| 84 |
+
const text = req.query.text;
|
| 85 |
+
|
| 86 |
+
if (!text) {
|
| 87 |
+
return res.status(400).json({
|
| 88 |
+
status: false,
|
| 89 |
+
message: "text required"
|
| 90 |
+
});
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
const buffer = await bratVid(text, {
|
| 94 |
+
outputFormat: "gif"
|
| 95 |
+
});
|
| 96 |
+
|
| 97 |
+
res.setHeader("Content-Type", "image/gif");
|
| 98 |
+
res.setHeader("Content-Length", buffer.length);
|
| 99 |
+
|
| 100 |
+
res.end(buffer);
|
| 101 |
+
|
| 102 |
+
} catch (e) {
|
| 103 |
+
res.status(500).json({
|
| 104 |
+
status: false,
|
| 105 |
+
error: e.message
|
| 106 |
+
});
|
| 107 |
+
}
|
| 108 |
+
});
|
| 109 |
+
|
| 110 |
+
const PORT = process.env.PORT || 7860;
|
| 111 |
+
|
| 112 |
+
app.listen(PORT, () => {
|
| 113 |
+
console.log(`Brat Farel API running on ${PORT}`);
|
| 114 |
+
});
|