Allow guest document file token access
All checks were successful
build-and-deploy / build-deploy (push) Successful in 1m36s
All checks were successful
build-and-deploy / build-deploy (push) Successful in 1m36s
This commit is contained in:
@@ -27,7 +27,6 @@ import java.time.OffsetDateTime
|
|||||||
import java.nio.file.Files
|
import java.nio.file.Files
|
||||||
import java.nio.file.Paths
|
import java.nio.file.Paths
|
||||||
import java.util.UUID
|
import java.util.UUID
|
||||||
import java.util.Base64
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/properties/{propertyId}/guests/{guestId}/documents")
|
@RequestMapping("/properties/{propertyId}/guests/{guestId}/documents")
|
||||||
@@ -44,7 +43,9 @@ class GuestDocuments(
|
|||||||
private val llamaClient: LlamaClient,
|
private val llamaClient: LlamaClient,
|
||||||
private val objectMapper: ObjectMapper,
|
private val objectMapper: ObjectMapper,
|
||||||
@org.springframework.beans.factory.annotation.Value("\${storage.documents.publicBaseUrl}")
|
@org.springframework.beans.factory.annotation.Value("\${storage.documents.publicBaseUrl}")
|
||||||
private val publicBaseUrl: String
|
private val publicBaseUrl: String,
|
||||||
|
@org.springframework.beans.factory.annotation.Value("\${storage.documents.aiBaseUrl:\${storage.documents.publicBaseUrl}}")
|
||||||
|
private val aiBaseUrl: String
|
||||||
) {
|
) {
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
@@ -142,7 +143,9 @@ class GuestDocuments(
|
|||||||
extractionQueue.enqueue {
|
extractionQueue.enqueue {
|
||||||
val document = guestDocumentRepo.findById(documentId).orElse(null) ?: return@enqueue
|
val document = guestDocumentRepo.findById(documentId).orElse(null) ?: return@enqueue
|
||||||
try {
|
try {
|
||||||
val imageUrl = buildImageDataUrl(document) ?: return@enqueue
|
val token = tokenService.createToken(document.id.toString())
|
||||||
|
val imageUrl =
|
||||||
|
"${aiBaseUrl}/properties/$propertyId/guests/$guestId/documents/${document.id}/file?token=$token"
|
||||||
|
|
||||||
val results = linkedMapOf<String, String>()
|
val results = linkedMapOf<String, String>()
|
||||||
results["hasAadhar"] = llamaClient.ask(imageUrl, "CONTAINS AADHAAR? Answer YES or NO only.")
|
results["hasAadhar"] = llamaClient.ask(imageUrl, "CONTAINS AADHAAR? Answer YES or NO only.")
|
||||||
@@ -200,24 +203,6 @@ class GuestDocuments(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun buildImageDataUrl(document: GuestDocument): String? {
|
|
||||||
val contentType = document.contentType?.trim()?.lowercase()
|
|
||||||
if (contentType == null || !contentType.startsWith("image/")) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
val path = Paths.get(document.storagePath)
|
|
||||||
if (!Files.exists(path)) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
return try {
|
|
||||||
val bytes = Files.readAllBytes(path)
|
|
||||||
val encoded = Base64.getEncoder().encodeToString(bytes)
|
|
||||||
"data:$contentType;base64,$encoded"
|
|
||||||
} catch (_: Exception) {
|
|
||||||
null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
data class GuestDocumentResponse(
|
data class GuestDocumentResponse(
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ internal object PublicEndpoints {
|
|||||||
private val iconPngFile = Regex("^/icons/png/[^/]+$")
|
private val iconPngFile = Regex("^/icons/png/[^/]+$")
|
||||||
private val payuWebhook = Regex("^/properties/[^/]+/payu/webhook$")
|
private val payuWebhook = Regex("^/properties/[^/]+/payu/webhook$")
|
||||||
private val payuReturn = Regex("^/properties/[^/]+/payu/return/(success|failure)$")
|
private val payuReturn = Regex("^/properties/[^/]+/payu/return/(success|failure)$")
|
||||||
|
private val guestDocumentFile = Regex("^/properties/[^/]+/guests/[^/]+/documents/[^/]+/file$")
|
||||||
|
|
||||||
fun isPublic(request: HttpServletRequest): Boolean {
|
fun isPublic(request: HttpServletRequest): Boolean {
|
||||||
val path = request.requestURI
|
val path = request.requestURI
|
||||||
@@ -30,5 +31,6 @@ internal object PublicEndpoints {
|
|||||||
|| (path == "/image-tags" && method == "GET")
|
|| (path == "/image-tags" && method == "GET")
|
||||||
|| path == "/icons/png"
|
|| path == "/icons/png"
|
||||||
|| iconPngFile.matches(path)
|
|| iconPngFile.matches(path)
|
||||||
|
|| guestDocumentFile.matches(path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user