38 lines
1.2 KiB
JavaScript
38 lines
1.2 KiB
JavaScript
import CaptchaService from "../services/captcha.js";
|
|
import config from '../config.js';
|
|
import fs from 'fs/promises';
|
|
|
|
class ApiController {
|
|
async new(req, res) {
|
|
const id = await CaptchaService.new();
|
|
|
|
return res.status(200).send({"id": id});
|
|
}
|
|
|
|
async get(req, res) {
|
|
const id = req.params.id;
|
|
|
|
const hash = await CaptchaService.get(id);
|
|
if (hash == undefined) {
|
|
return res.status(404).send({"message": "captcha not found"});
|
|
}
|
|
const image = await fs.readFile(`${config.data_dir}/${hash}.jpeg`);
|
|
|
|
return res.status(200).send(image)
|
|
}
|
|
|
|
async solve (req, res) {
|
|
const id = req.params.id;
|
|
const solution = req.body["solution"];
|
|
if (solution == undefined || solution.length != 6) {
|
|
return res.status(400).send({"message": 'please, send a valid solution. Example: {"solution":"123456"}'});
|
|
}
|
|
|
|
if (!await CaptchaService.check_and_save_solution(id, solution))
|
|
return res.status(409).send({"message": "Solution is not correct"});
|
|
|
|
return res.status(200).send({"message": "Successful"});
|
|
}
|
|
}
|
|
|
|
export default new ApiController(); |