|
| 1 | +package com.runanywhereaI |
| 2 | + |
| 3 | +import android.net.Uri |
| 4 | +import com.facebook.react.bridge.Promise |
| 5 | +import com.facebook.react.bridge.ReactApplicationContext |
| 6 | +import com.facebook.react.bridge.ReactContextBaseJavaModule |
| 7 | +import com.facebook.react.bridge.ReactMethod |
| 8 | +import com.tom_roush.pdfbox.android.PDFBoxResourceLoader |
| 9 | +import com.tom_roush.pdfbox.pdmodel.PDDocument |
| 10 | +import com.tom_roush.pdfbox.text.PDFTextStripper |
| 11 | +import org.json.JSONArray |
| 12 | +import org.json.JSONObject |
| 13 | +import java.io.File |
| 14 | + |
| 15 | +class DocumentServiceModule(private val reactContext: ReactApplicationContext) : |
| 16 | + ReactContextBaseJavaModule(reactContext) { |
| 17 | + |
| 18 | + override fun getName(): String = "DocumentService" |
| 19 | + |
| 20 | + override fun initialize() { |
| 21 | + super.initialize() |
| 22 | + PDFBoxResourceLoader.init(reactContext) |
| 23 | + } |
| 24 | + |
| 25 | + @ReactMethod |
| 26 | + fun extractText(filePath: String, promise: Promise) { |
| 27 | + Thread { |
| 28 | + try { |
| 29 | + val fileType = resolveFileType(filePath) |
| 30 | + val text = when (fileType) { |
| 31 | + FileType.PDF -> extractPDFText(filePath) |
| 32 | + FileType.JSON -> extractJSONText(filePath) |
| 33 | + FileType.PLAIN_TEXT -> readPlainText(filePath) |
| 34 | + } |
| 35 | + promise.resolve(text) |
| 36 | + } catch (e: Exception) { |
| 37 | + promise.reject("EXTRACT_ERROR", e.localizedMessage, e) |
| 38 | + } |
| 39 | + }.start() |
| 40 | + } |
| 41 | + |
| 42 | + private enum class FileType { PDF, JSON, PLAIN_TEXT } |
| 43 | + |
| 44 | + private fun resolveFileType(filePath: String): FileType { |
| 45 | + // For content:// URIs, use the content resolver to get the MIME type |
| 46 | + if (filePath.startsWith("content://")) { |
| 47 | + val mimeType = reactContext.contentResolver.getType(Uri.parse(filePath)) |
| 48 | + if (mimeType != null) { |
| 49 | + return when { |
| 50 | + mimeType == "application/pdf" -> FileType.PDF |
| 51 | + mimeType == "application/json" -> FileType.JSON |
| 52 | + else -> FileType.PLAIN_TEXT |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + // Fall back to extension-based detection for file:// and raw paths |
| 58 | + val ext = filePath.substringAfterLast('.', "").lowercase() |
| 59 | + return when (ext) { |
| 60 | + "pdf" -> FileType.PDF |
| 61 | + "json" -> FileType.JSON |
| 62 | + else -> FileType.PLAIN_TEXT |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private fun extractPDFText(filePath: String): String { |
| 67 | + val inputStream = openInputStream(filePath) |
| 68 | + ?: throw Exception("Failed to open PDF. File may be corrupted or image-only.") |
| 69 | + |
| 70 | + val document = inputStream.use { PDDocument.load(it) } |
| 71 | + document.use { doc -> |
| 72 | + if (doc.numberOfPages == 0) { |
| 73 | + throw Exception("PDF has no pages.") |
| 74 | + } |
| 75 | + val stripper = PDFTextStripper() |
| 76 | + val result = stripper.getText(doc).trim() |
| 77 | + if (result.isEmpty()) { |
| 78 | + throw Exception("PDF contains no extractable text (may be image-only).") |
| 79 | + } |
| 80 | + return result |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private fun extractJSONText(filePath: String): String { |
| 85 | + val raw = readPlainText(filePath) |
| 86 | + val parsed = when { |
| 87 | + raw.trimStart().startsWith("[") -> JSONArray(raw) as Any |
| 88 | + else -> JSONObject(raw) as Any |
| 89 | + } |
| 90 | + val strings = mutableListOf<String>() |
| 91 | + extractStrings(parsed, strings) |
| 92 | + return strings.joinToString("\n") |
| 93 | + } |
| 94 | + |
| 95 | + private fun extractStrings(value: Any, result: MutableList<String>) { |
| 96 | + when (value) { |
| 97 | + is String -> result.add(value) |
| 98 | + is JSONObject -> { |
| 99 | + for (key in value.keys()) { |
| 100 | + extractStrings(value.get(key), result) |
| 101 | + } |
| 102 | + } |
| 103 | + is JSONArray -> { |
| 104 | + for (i in 0 until value.length()) { |
| 105 | + extractStrings(value.get(i), result) |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private fun readPlainText(filePath: String): String { |
| 112 | + val inputStream = openInputStream(filePath) |
| 113 | + ?: throw Exception("Failed to open file at: $filePath") |
| 114 | + return inputStream.use { it.bufferedReader().readText() } |
| 115 | + } |
| 116 | + |
| 117 | + private fun openInputStream(filePath: String): java.io.InputStream? { |
| 118 | + return if (filePath.startsWith("content://")) { |
| 119 | + reactContext.contentResolver.openInputStream(Uri.parse(filePath)) |
| 120 | + } else { |
| 121 | + val path = if (filePath.startsWith("file://")) { |
| 122 | + Uri.parse(filePath).path ?: filePath.removePrefix("file://") |
| 123 | + } else { |
| 124 | + filePath |
| 125 | + } |
| 126 | + File(path).inputStream() |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments