Fourstore commited on
Commit
2d8987f
·
verified ·
1 Parent(s): 6ab573c

Update endpoints/antibot.js

Browse files
Files changed (1) hide show
  1. endpoints/antibot.js +19 -3
endpoints/antibot.js CHANGED
@@ -1,6 +1,18 @@
1
  const fs = require('fs');
2
  const path = require('path');
3
- const { extractTextFromImage } = require('./imageProcessor');
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  function mapAnswer(soalArray, jawaban, botIndex) {
6
  return jawaban;
@@ -131,8 +143,12 @@ async function antibot(data) {
131
  const { main, bots } = data;
132
 
133
  const mainBuffer = Buffer.from(main, 'base64');
134
- const mainText = await extractTextFromImage(mainBuffer);
135
 
 
 
 
 
136
  const soalArray = parseSoalText(mainText.response);
137
 
138
  if (soalArray.length === 0) {
@@ -145,7 +161,7 @@ async function antibot(data) {
145
  const bot = bots[i];
146
  try {
147
  const botBuffer = Buffer.from(bot.img, 'base64');
148
- const botText = await extractTextFromImage(botBuffer);
149
  const mappedValue = mapAnswer(soalArray, botText.response, i);
150
 
151
  botResults.push({
 
1
  const fs = require('fs');
2
  const path = require('path');
3
+ const { extractTextFromImage, uploadImageToHosting } = require('./imageProcessor');
4
+
5
+ async function extractTextFromBuffer(imageBuffer) {
6
+ try {
7
+ const tempPath = path.join(__dirname, `temp_${Date.now()}.jpg`);
8
+ fs.writeFileSync(tempPath, imageBuffer);
9
+ const result = await extractTextFromImage(tempPath);
10
+ fs.unlinkSync(tempPath);
11
+ return result;
12
+ } catch (error) {
13
+ return { status: false, response: 'Gagal memproses gambar' };
14
+ }
15
+ }
16
 
17
  function mapAnswer(soalArray, jawaban, botIndex) {
18
  return jawaban;
 
143
  const { main, bots } = data;
144
 
145
  const mainBuffer = Buffer.from(main, 'base64');
146
+ const mainText = await extractTextFromBuffer(mainBuffer);
147
 
148
+ if (!mainText.status) {
149
+ throw new Error('Gagal mengekstrak teks dari gambar utama: ' + mainText.response);
150
+ }
151
+
152
  const soalArray = parseSoalText(mainText.response);
153
 
154
  if (soalArray.length === 0) {
 
161
  const bot = bots[i];
162
  try {
163
  const botBuffer = Buffer.from(bot.img, 'base64');
164
+ const botText = await extractTextFromBuffer(botBuffer);
165
  const mappedValue = mapAnswer(soalArray, botText.response, i);
166
 
167
  botResults.push({