Spaces:
Sleeping
Sleeping
File size: 7,834 Bytes
c730f0b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | /* eslint-disable @typescript-eslint/no-require-imports */
import { IMAGE_EXTENSIONS, TEXT_EXTENSIONS, SUPPORTED_EXTENSIONS } from "./constants";
import type { FileContext } from "./types";
import { v4 as uuidv4 } from "uuid";
function getExtension(filename: string): string {
const parts = filename.split(".");
return parts.length > 1 ? parts.pop()!.toLowerCase() : "";
}
export async function processFile(
buffer: Buffer,
filename: string
): Promise<FileContext> {
const ext = getExtension(filename);
const result: FileContext = {
id: uuidv4(),
filename,
extension: ext,
text: "",
error: null,
size: buffer.length,
};
if (!SUPPORTED_EXTENSIONS.includes(ext)) {
result.error = `Format '.${ext}' belum didukung.`;
return result;
}
try {
if (ext === "pdf") {
result.text = await processPdf(buffer);
} else if (ext === "doc") {
result.text = await processDoc(buffer);
} else if (ext === "docx") {
result.text = await processDocx(buffer);
} else if (ext === "xlsx" || ext === "xls") {
result.text = processExcel(buffer);
} else if (ext === "csv") {
result.text = processCsv(buffer);
} else if (IMAGE_EXTENSIONS.includes(ext)) {
result.text = await processImage(buffer);
} else if (TEXT_EXTENSIONS.includes(ext)) {
result.text = processText(buffer);
} else {
result.text = processText(buffer);
}
} catch (e: unknown) {
result.error = `Error memproses '${filename}': ${e instanceof Error ? e.message : String(e)}`;
}
return result;
}
async function processPdf(buffer: Buffer): Promise<string> {
const { writeFile, readFile } = require("fs/promises");
const { mkdtemp, rm } = require("fs/promises");
const { tmpdir } = require("os");
const path = require("path");
const { execFile } = require("child_process");
// Step 1: Try fast text extraction with pdftotext CLI (poppler)
const tmpDir = await mkdtemp(path.join(tmpdir(), "pdf-txt-"));
const pdfPath = path.join(tmpDir, "input.pdf");
const txtPath = path.join(tmpDir, "output.txt");
try {
await writeFile(pdfPath, buffer);
await new Promise<void>((resolve, reject) => {
execFile(
"pdftotext",
["-layout", pdfPath, txtPath],
{ timeout: 15000 },
(error: Error | null) => {
if (error) reject(error);
else resolve();
}
);
});
const text = (await readFile(txtPath, "utf-8")).trim();
console.log(`[pdf] pdftotext extracted ${text.length} chars`);
if (text.length > 50) {
return text;
}
} catch (e) {
console.log(`[pdf] pdftotext failed: ${e instanceof Error ? e.message : String(e)}`);
} finally {
await rm(tmpDir, { recursive: true, force: true }).catch(() => {});
}
// Step 2: Fallback — convert PDF pages to images with pdftoppm, then OCR
console.log("[pdf] Text extraction empty, starting OCR fallback...");
return await ocrPdf(buffer);
}
async function ocrPdf(buffer: Buffer): Promise<string> {
const { writeFile, readFile, readdir } = require("fs/promises");
const { mkdtemp, rm } = require("fs/promises");
const { tmpdir } = require("os");
const path = require("path");
const { execFile } = require("child_process");
const tmpDir = await mkdtemp(path.join(tmpdir(), "pdf-ocr-"));
const pdfPath = path.join(tmpDir, "input.pdf");
try {
await writeFile(pdfPath, buffer);
// Convert PDF to PNG images using pdftoppm (poppler)
// Always limit to 20 pages max to avoid excessive processing
const args = ["-png", "-r", "300", "-l", "20", pdfPath, path.join(tmpDir, "page")];
await new Promise<void>((resolve, reject) => {
execFile(
"pdftoppm",
args,
{ timeout: 120000 },
(error: Error | null) => {
if (error) reject(error);
else resolve();
}
);
});
// Find all generated page images
const files = await readdir(tmpDir);
const pageFiles = files
.filter((f: string) => f.startsWith("page") && f.endsWith(".png"))
.sort();
if (pageFiles.length === 0) {
return "(PDF berisi gambar tapi tidak dapat di-OCR)";
}
// OCR each page
const results: string[] = [];
for (const pageFile of pageFiles) {
const imgPath = path.join(tmpDir, pageFile);
const ocrBase = path.join(tmpDir, `ocr-${pageFile}`);
const ocrPath = ocrBase + ".txt";
try {
await new Promise<void>((resolve, reject) => {
execFile(
"tesseract",
[imgPath, ocrBase, "-l", "eng+ind"],
{ timeout: 60000 },
(error: Error | null) => {
if (error) reject(error);
else resolve();
}
);
});
const pageText = await readFile(ocrPath, "utf-8");
if (pageText.trim()) {
results.push(`--- Halaman ${results.length + 1} ---\n${pageText.trim()}`);
}
} catch {
// Skip pages that fail OCR
}
}
return results.length > 0
? results.join("\n\n")
: "(PDF berisi gambar tapi tidak dapat di-OCR)";
} finally {
// Clean up temp directory
await rm(tmpDir, { recursive: true, force: true }).catch(() => {});
}
}
async function processDoc(buffer: Buffer): Promise<string> {
const { writeFile, unlink } = require("fs/promises");
const { tmpdir } = require("os");
const path = require("path");
const tmpPath = path.join(tmpdir(), `doc-${uuidv4()}.doc`);
try {
await writeFile(tmpPath, buffer);
const WordExtractor = require("word-extractor");
const extractor = new WordExtractor();
const doc = await extractor.extract(tmpPath);
return doc.getBody().trim();
} finally {
await unlink(tmpPath).catch(() => {});
}
}
async function processDocx(buffer: Buffer): Promise<string> {
const mammoth = await import("mammoth");
const result = await mammoth.extractRawText({ buffer });
return result.value.trim();
}
function processExcel(buffer: Buffer): string {
const XLSX = require("xlsx");
const workbook = XLSX.read(buffer, { type: "buffer" });
const texts: string[] = [];
for (const sheetName of workbook.SheetNames) {
const sheet = workbook.Sheets[sheetName];
const csv = XLSX.utils.sheet_to_csv(sheet);
texts.push(`--- Sheet: ${sheetName} ---\n${csv}`);
}
return texts.join("\n\n");
}
function processCsv(buffer: Buffer): string {
const XLSX = require("xlsx");
const workbook = XLSX.read(buffer, { type: "buffer" });
const sheet = workbook.Sheets[workbook.SheetNames[0]];
return XLSX.utils.sheet_to_csv(sheet);
}
async function processImage(buffer: Buffer): Promise<string> {
const { writeFile, readFile, unlink } = require("fs/promises");
const { tmpdir } = require("os");
const path = require("path");
const { execFile } = require("child_process");
const inputPath = path.join(tmpdir(), `ocr-${uuidv4()}`);
const outputBase = path.join(tmpdir(), `ocr-out-${uuidv4()}`);
const outputPath = outputBase + ".txt";
try {
await writeFile(inputPath, buffer);
// Use system tesseract CLI — avoids Turbopack module resolution issues
await new Promise<void>((resolve, reject) => {
execFile(
"tesseract",
[inputPath, outputBase, "-l", "eng+ind"],
{ timeout: 60000 },
(error: Error | null) => {
if (error) reject(error);
else resolve();
}
);
});
const text = await readFile(outputPath, "utf-8");
return text.trim();
} finally {
await unlink(inputPath).catch(() => {});
await unlink(outputPath).catch(() => {});
}
}
function processText(buffer: Buffer): string {
try {
return buffer.toString("utf-8");
} catch {
return buffer.toString("latin1");
}
}
|