Spaces:
Sleeping
Sleeping
Update server.js
Browse files
server.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
-
const express = require(
|
| 2 |
-
const Tesseract = require(
|
| 3 |
-
const fs = require(
|
| 4 |
-
const path = require(
|
| 5 |
-
const {
|
| 6 |
|
| 7 |
const app = express();
|
| 8 |
app.use(express.json({ limit: '50mb' }));
|
|
@@ -11,45 +11,42 @@ app.use(express.json({ limit: '50mb' }));
|
|
| 11 |
const uploadDir = path.join('/tmp', 'uploads');
|
| 12 |
if (!fs.existsSync(uploadDir)) fs.mkdirSync(uploadDir, { recursive: true });
|
| 13 |
|
| 14 |
-
app.post(
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
res.status(500).json({ success: false, error: error.message });
|
| 48 |
-
}
|
| 49 |
});
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
console.log(`π OCR server running on http://0.0.0.0:${PORT}`);
|
| 55 |
});
|
|
|
|
| 1 |
+
const express = require("express");
|
| 2 |
+
const Tesseract = require("tesseract.js");
|
| 3 |
+
const fs = require("fs");
|
| 4 |
+
const path = require("path");
|
| 5 |
+
const { fromPath } = require("pdf2pic");
|
| 6 |
|
| 7 |
const app = express();
|
| 8 |
app.use(express.json({ limit: '50mb' }));
|
|
|
|
| 11 |
const uploadDir = path.join('/tmp', 'uploads');
|
| 12 |
if (!fs.existsSync(uploadDir)) fs.mkdirSync(uploadDir, { recursive: true });
|
| 13 |
|
| 14 |
+
app.post("/ocr", async (req, res) => {
|
| 15 |
+
try {
|
| 16 |
+
const { file } = req.body;
|
| 17 |
+
if (!file) return res.status(400).json({ success: false, error: "Base64 file string not provided" });
|
| 18 |
+
|
| 19 |
+
// Simpan base64 β PDF
|
| 20 |
+
const pdfPath = path.join(uploadDir, `file_${Date.now()}.pdf`);
|
| 21 |
+
fs.writeFileSync(pdfPath, Buffer.from(file, "base64"));
|
| 22 |
+
|
| 23 |
+
// Convert PDF β PNG (halaman pertama)
|
| 24 |
+
const options = {
|
| 25 |
+
density: 150,
|
| 26 |
+
saveFilename: "page",
|
| 27 |
+
savePath: uploadDir,
|
| 28 |
+
format: "png",
|
| 29 |
+
width: 1024,
|
| 30 |
+
height: 1024
|
| 31 |
+
};
|
| 32 |
+
const storeAsImage = fromPath(pdfPath, options);
|
| 33 |
+
const pageToConvertAsImage = 1;
|
| 34 |
+
const result = await storeAsImage(pageToConvertAsImage);
|
| 35 |
+
|
| 36 |
+
// OCR dengan Tesseract
|
| 37 |
+
const { data: { text } } = await Tesseract.recognize(result.path, "ind+eng");
|
| 38 |
+
|
| 39 |
+
// Hapus file sementara
|
| 40 |
+
fs.unlinkSync(pdfPath);
|
| 41 |
+
fs.unlinkSync(result.path);
|
| 42 |
+
|
| 43 |
+
res.json({ success: true, text });
|
| 44 |
+
} catch (error) {
|
| 45 |
+
res.status(500).json({ success: false, error: error.message });
|
| 46 |
+
}
|
|
|
|
|
|
|
| 47 |
});
|
| 48 |
|
| 49 |
+
const PORT = process.env.PORT || 5000;
|
| 50 |
+
app.listen(PORT, "0.0.0.0", () => {
|
| 51 |
+
console.log(`π OCR server running on http://0.0.0.0:${PORT}`);
|
|
|
|
| 52 |
});
|