From 4e24d93a5a2770dc9698d29afb8616ec112ee2fc Mon Sep 17 00:00:00 2001 From: pratik Date: Wed, 22 Oct 2025 14:39:46 -0500 Subject: [PATCH] using bff --- deploy-chunked-simple.sh | 11 +++-- .../controller/CfDeployController.java | 44 +++++++++++++++++++ 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/deploy-chunked-simple.sh b/deploy-chunked-simple.sh index 4f9fe4c..29e5c0e 100644 --- a/deploy-chunked-simple.sh +++ b/deploy-chunked-simple.sh @@ -211,11 +211,14 @@ upload_file_in_chunks() { echo "DEBUG: Uploading chunk to: $API_BASE/upload/chunk?uploadSessionId=$SESSION_ID&fileType=$file_type&chunkIndex=$chunk_index&totalChunks=$total_chunks&fileName=$file_name" fi - # Send metadata as query params and chunk file as raw binary body - # This works with Java proxy that forwards body as-is + # Base64 encode the chunk so Java proxy can handle it as text/String + # This prevents corruption when proxy reads binary data as String + CHUNK_BASE64=$(base64 < "$chunk_file") + RESPONSE=$(curl -s -X POST "$API_BASE/upload/chunk?uploadSessionId=$SESSION_ID&fileType=$file_type&chunkIndex=$chunk_index&totalChunks=$total_chunks&fileName=$file_name" \ - -H "Content-Type: application/octet-stream" \ - --data-binary "@$chunk_file") + -H "Content-Type: text/plain" \ + -H "X-Chunk-Encoding: base64" \ + -d "$CHUNK_BASE64") if [ "$DEBUG_MODE" = "true" ]; then echo "DEBUG: Chunk response: $RESPONSE" diff --git a/src/main/java/com/cfdeployer/controller/CfDeployController.java b/src/main/java/com/cfdeployer/controller/CfDeployController.java index 9a0a9ea..9f8da3d 100644 --- a/src/main/java/com/cfdeployer/controller/CfDeployController.java +++ b/src/main/java/com/cfdeployer/controller/CfDeployController.java @@ -215,6 +215,50 @@ public class CfDeployController { } } + @PostMapping(value = "/upload/chunk", consumes = "text/plain") + public ResponseEntity uploadChunkBase64( + @RequestParam("uploadSessionId") String uploadSessionId, + @RequestParam("fileType") String fileType, + @RequestParam("chunkIndex") Integer chunkIndex, + @RequestParam("totalChunks") Integer totalChunks, + @RequestParam(value = "fileName", required = false) String fileName, + @RequestHeader(value = "X-Chunk-Encoding", required = false) String encoding, + @RequestBody String chunkDataBase64) { + try { + log.debug("Receiving base64 chunk {}/{} for session: {}, fileType: {}", + chunkIndex + 1, totalChunks, uploadSessionId, fileType); + + // Validate file type + if (!fileType.equals("jarFile") && !fileType.equals("manifest")) { + throw new IllegalArgumentException("Invalid file type. Must be 'jarFile' or 'manifest'"); + } + + // Decode base64 to binary + byte[] chunkData; + if ("base64".equalsIgnoreCase(encoding)) { + chunkData = java.util.Base64.getDecoder().decode(chunkDataBase64); + log.debug("Decoded base64 chunk: {} chars -> {} bytes", chunkDataBase64.length(), chunkData.length); + } else { + // Fallback: treat as raw bytes from string + chunkData = chunkDataBase64.getBytes(java.nio.charset.StandardCharsets.ISO_8859_1); + } + + chunkedUploadService.uploadChunkRaw(uploadSessionId, fileType, fileName, + chunkIndex, totalChunks, chunkData); + + var session = chunkedUploadService.getSession(uploadSessionId); + var fileState = session.getFileStates().get(fileType); + + return ResponseEntity.ok(ChunkUploadResponse.success( + uploadSessionId, fileType, chunkIndex, totalChunks, + fileState.getReceivedChunkCount())); + } catch (Exception e) { + log.error("Error uploading base64 chunk", e); + return ResponseEntity.status(HttpStatus.BAD_REQUEST) + .body(ChunkUploadResponse.failure("Failed to upload chunk: " + e.getMessage())); + } + } + @PostMapping("/upload/finalize") public ResponseEntity finalizeUpload( @RequestParam("uploadSessionId") String uploadSessionId,