Revert "PDF Testing"
This reverts commit 30298ac46eb71830a59cba5ae494fb12e3c2121d.
This commit is contained in:
parent
2ac0393847
commit
524f1c1885
@ -68,7 +68,7 @@ dependencies {
|
|||||||
debugImplementation(libs.ui.tooling)
|
debugImplementation(libs.ui.tooling)
|
||||||
debugImplementation(libs.ui.test.manifest)
|
debugImplementation(libs.ui.test.manifest)
|
||||||
|
|
||||||
// Room Database
|
// Room Database - TAMBAHKAN INI SEMUA
|
||||||
val roomVersion = "2.6.1"
|
val roomVersion = "2.6.1"
|
||||||
implementation("androidx.room:room-runtime:$roomVersion")
|
implementation("androidx.room:room-runtime:$roomVersion")
|
||||||
implementation("androidx.room:room-ktx:$roomVersion")
|
implementation("androidx.room:room-ktx:$roomVersion")
|
||||||
@ -91,7 +91,4 @@ dependencies {
|
|||||||
|
|
||||||
// Gson
|
// Gson
|
||||||
implementation("com.google.code.gson:gson:2.10.1")
|
implementation("com.google.code.gson:gson:2.10.1")
|
||||||
|
|
||||||
// PDF Support
|
|
||||||
implementation("com.tom-roush:pdfbox-android:2.0.27.0")
|
|
||||||
}
|
}
|
||||||
@ -15,7 +15,7 @@ import java.util.concurrent.TimeUnit
|
|||||||
*/
|
*/
|
||||||
interface GeminiApiService {
|
interface GeminiApiService {
|
||||||
|
|
||||||
@POST("v1beta/models/gemini-2.0-flash:generateContent")
|
@POST("v1beta/models/gemini-1.5-flash:generateContent")
|
||||||
suspend fun generateContent(
|
suspend fun generateContent(
|
||||||
@Query("key") apiKey: String,
|
@Query("key") apiKey: String,
|
||||||
@Body request: GeminiRequest
|
@Body request: GeminiRequest
|
||||||
|
|||||||
@ -76,27 +76,11 @@ object FileHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Baca text dari file (support Text, Markdown, dan PDF)
|
* Baca text dari file
|
||||||
*/
|
*/
|
||||||
fun readTextFromFile(filePath: String): String? {
|
fun readTextFromFile(filePath: String): String? {
|
||||||
return try {
|
return try {
|
||||||
val file = File(filePath)
|
File(filePath).readText()
|
||||||
val extension = file.extension.lowercase()
|
|
||||||
|
|
||||||
when (extension) {
|
|
||||||
"pdf" -> {
|
|
||||||
// Extract text dari PDF
|
|
||||||
PdfHelper.extractTextFromPdf(filePath)
|
|
||||||
}
|
|
||||||
"txt", "md", "markdown" -> {
|
|
||||||
// Baca text biasa
|
|
||||||
file.readText()
|
|
||||||
}
|
|
||||||
else -> {
|
|
||||||
println("⚠️ Format file tidak didukung untuk ekstraksi teks: $extension")
|
|
||||||
null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
null
|
null
|
||||||
|
|||||||
@ -1,93 +0,0 @@
|
|||||||
package com.example.notebook.utils
|
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import android.net.Uri
|
|
||||||
import com.tom_roush.pdfbox.android.PDFBoxResourceLoader
|
|
||||||
import com.tom_roush.pdfbox.pdmodel.PDDocument
|
|
||||||
import com.tom_roush.pdfbox.text.PDFTextStripper
|
|
||||||
import java.io.File
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Helper untuk extract text dari PDF
|
|
||||||
*/
|
|
||||||
object PdfHelper {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initialize PDFBox (panggil sekali saat app start)
|
|
||||||
*/
|
|
||||||
fun initialize(context: Context) {
|
|
||||||
PDFBoxResourceLoader.init(context)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Extract text dari PDF file
|
|
||||||
* Return: Text content atau null jika error
|
|
||||||
*/
|
|
||||||
fun extractTextFromPdf(filePath: String): String? {
|
|
||||||
return try {
|
|
||||||
val file = File(filePath)
|
|
||||||
val document = PDDocument.load(file)
|
|
||||||
|
|
||||||
val stripper = PDFTextStripper()
|
|
||||||
val text = stripper.getText(document)
|
|
||||||
|
|
||||||
document.close()
|
|
||||||
|
|
||||||
// Cleanup whitespace
|
|
||||||
text.trim()
|
|
||||||
} catch (e: Exception) {
|
|
||||||
println("❌ Error extract PDF: ${e.message}")
|
|
||||||
e.printStackTrace()
|
|
||||||
null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Extract text dari PDF URI (sebelum disimpan)
|
|
||||||
*/
|
|
||||||
fun extractTextFromPdfUri(context: Context, uri: Uri): String? {
|
|
||||||
return try {
|
|
||||||
val inputStream = context.contentResolver.openInputStream(uri)
|
|
||||||
val document = PDDocument.load(inputStream)
|
|
||||||
|
|
||||||
val stripper = PDFTextStripper()
|
|
||||||
val text = stripper.getText(document)
|
|
||||||
|
|
||||||
document.close()
|
|
||||||
inputStream?.close()
|
|
||||||
|
|
||||||
text.trim()
|
|
||||||
} catch (e: Exception) {
|
|
||||||
println("❌ Error extract PDF from URI: ${e.message}")
|
|
||||||
e.printStackTrace()
|
|
||||||
null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get info PDF (jumlah halaman, dll)
|
|
||||||
*/
|
|
||||||
fun getPdfInfo(filePath: String): PdfInfo? {
|
|
||||||
return try {
|
|
||||||
val file = File(filePath)
|
|
||||||
val document = PDDocument.load(file)
|
|
||||||
|
|
||||||
val info = PdfInfo(
|
|
||||||
pageCount = document.numberOfPages,
|
|
||||||
title = document.documentInformation.title ?: "Unknown",
|
|
||||||
author = document.documentInformation.author ?: "Unknown"
|
|
||||||
)
|
|
||||||
|
|
||||||
document.close()
|
|
||||||
info
|
|
||||||
} catch (e: Exception) {
|
|
||||||
null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
data class PdfInfo(
|
|
||||||
val pageCount: Int,
|
|
||||||
val title: String,
|
|
||||||
val author: String
|
|
||||||
)
|
|
||||||
Loading…
x
Reference in New Issue
Block a user