Spaces:
Paused
Paused
Update server.js
Browse files
server.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
const express = require("express");
|
| 2 |
-
const
|
| 3 |
-
const { createCanvas, loadImage } = require("canvas");
|
| 4 |
const fs = require("fs");
|
| 5 |
const os = require("os");
|
| 6 |
const path = require("path");
|
|
@@ -17,26 +16,12 @@ function generateRandomHex(length) {
|
|
| 17 |
app.use("/tmp", express.static(tempDir));
|
| 18 |
|
| 19 |
app.all("/", async (req, res) => {
|
| 20 |
-
const captcha =
|
| 21 |
-
size: 6,
|
| 22 |
-
ignoreChars: "0oO1iIl",
|
| 23 |
-
noise: 2,
|
| 24 |
-
color: false,
|
| 25 |
-
background: "#FFFFFF"
|
| 26 |
-
});
|
| 27 |
|
| 28 |
const fileName = `temp_${generateRandomHex(8)}.png`;
|
| 29 |
const filePath = path.join(tempDir, fileName);
|
| 30 |
-
|
| 31 |
-
const
|
| 32 |
-
const ctx = canvas.getContext("2d");
|
| 33 |
-
const img = await loadImage(`data:image/svg+xml;base64,${Buffer.from(captcha.data).toString("base64")}`);
|
| 34 |
-
|
| 35 |
-
ctx.fillStyle = "#FFFFFF";
|
| 36 |
-
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
| 37 |
-
ctx.drawImage(img, 0, 0, 200, 80);
|
| 38 |
-
|
| 39 |
-
const buffer = canvas.toBuffer("image/png");
|
| 40 |
|
| 41 |
fs.writeFile(filePath, buffer, (err) => {
|
| 42 |
if (err) return res.status(500).json({ error: "Failed to save captcha" });
|
|
|
|
| 1 |
const express = require("express");
|
| 2 |
+
const { CaptchaGenerator } = require("captcha-canvas");
|
|
|
|
| 3 |
const fs = require("fs");
|
| 4 |
const os = require("os");
|
| 5 |
const path = require("path");
|
|
|
|
| 16 |
app.use("/tmp", express.static(tempDir));
|
| 17 |
|
| 18 |
app.all("/", async (req, res) => {
|
| 19 |
+
const captcha = new CaptchaGenerator({ height: 200, width: 600 })
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
const fileName = `temp_${generateRandomHex(8)}.png`;
|
| 22 |
const filePath = path.join(tempDir, fileName);
|
| 23 |
+
|
| 24 |
+
const buffer = await captcha.generate()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
fs.writeFile(filePath, buffer, (err) => {
|
| 27 |
if (err) return res.status(500).json({ error: "Failed to save captcha" });
|