diff --git a/src/main/java/org/cftoolsuite/client/SanfordClient.java b/src/main/java/org/cftoolsuite/client/SanfordClient.java index bc98bc6..008b6e0 100644 --- a/src/main/java/org/cftoolsuite/client/SanfordClient.java +++ b/src/main/java/org/cftoolsuite/client/SanfordClient.java @@ -1,8 +1,5 @@ package org.cftoolsuite.client; -import java.io.IOException; -import java.util.List; - import org.cftoolsuite.domain.FileMetadata; import org.cftoolsuite.domain.chat.AudioResponse; import org.cftoolsuite.domain.chat.Inquiry; @@ -14,15 +11,11 @@ import org.springframework.core.io.Resource; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RequestPart; +import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; +import java.util.List; + @FeignClient(name = "sanford-client", url = "${document.service.url}") public interface SanfordClient { @@ -35,8 +28,8 @@ public interface SanfordClient { @PostMapping("/api/fetch") public ResponseEntity fetchUrls(@RequestBody FetchRequest request); - @PostMapping("/api/converse") - public ResponseEntity converse(@RequestParam("file") MultipartFile file) throws IOException; + @PostMapping(value = "/api/converse", consumes = MediaType.APPLICATION_OCTET_STREAM_VALUE) + public ResponseEntity converse(@RequestBody byte[] audioBytes); @PostMapping("/api/chat") public ResponseEntity chat(@RequestBody Inquiry inquiry); diff --git a/src/main/java/org/cftoolsuite/ui/view/ConverseView.java b/src/main/java/org/cftoolsuite/ui/view/ConverseView.java index a8b37d0..aa3ad29 100644 --- a/src/main/java/org/cftoolsuite/ui/view/ConverseView.java +++ b/src/main/java/org/cftoolsuite/ui/view/ConverseView.java @@ -12,7 +12,6 @@ import com.vaadin.flow.router.PageTitle; import com.vaadin.flow.router.Route; import elemental.json.JsonObject; -import org.apache.commons.io.FileUtils; import org.cftoolsuite.client.SanfordClient; import org.cftoolsuite.domain.AppProperties; import org.cftoolsuite.domain.CustomMultipartFile; @@ -21,7 +20,8 @@ import org.slf4j.LoggerFactory; import org.springframework.web.multipart.MultipartFile; -import java.io.File; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; import java.util.Base64; @PageTitle("sanford-ui ยป Converse") @@ -131,23 +131,12 @@ private void handleRecordingResult(JsonObject result) { String base64Audio = result.getString("audioData"); byte[] audioData = Base64.getDecoder().decode(base64Audio); - // Create temporary file - File tempFile = File.createTempFile("audio-", ".webm"); - FileUtils.writeByteArrayToFile(tempFile, audioData); - - // Convert to MultipartFile - MultipartFile multipartFile = new CustomMultipartFile( - "file", - "recording.webm", - "audio/webm", - FileUtils.readFileToByteArray(tempFile) - ); // Show loading indicator UI.getCurrent().access(() -> loadingDiv.getStyle().set("display", "flex")); // Make API call using Feign client - var response = sanfordClient.converse(multipartFile); + var response = sanfordClient.converse(audioData); if (response.getStatusCode().is2xxSuccessful() && response.getBody() != null) { // Update UI with response UI.getCurrent().access(() -> { @@ -170,9 +159,6 @@ private void handleRecordingResult(JsonObject result) { showNotification("Error conversing with chatbot", NotificationVariant.LUMO_ERROR); } - // Cleanup temp file - tempFile.delete(); - } catch (Exception e) { log.error("Error conversing with chatbot", e); UI.getCurrent().access(() -> { diff --git a/src/main/java/org/cftoolsuite/ui/view/UploadView.java b/src/main/java/org/cftoolsuite/ui/view/UploadView.java index ec000cf..f411305 100644 --- a/src/main/java/org/cftoolsuite/ui/view/UploadView.java +++ b/src/main/java/org/cftoolsuite/ui/view/UploadView.java @@ -3,6 +3,7 @@ import java.io.IOException; import java.io.InputStream; import java.util.List; +import java.util.stream.Stream; import org.apache.commons.io.FilenameUtils; import org.cftoolsuite.client.SanfordClient; @@ -44,7 +45,7 @@ protected void setupUI() { int numberOfSupportedContentTypes = supportedContentTypes.size() * 2; List acceptedFileExtensions = supportedContentTypes.keySet().stream().map(k -> String.format(".%s", k)).toList(); List acceptedContentTypes = supportedContentTypes.values().stream().toList(); - List combinedList = List.of(acceptedFileExtensions, acceptedContentTypes).stream().flatMap(List::stream).toList(); + List combinedList = Stream.of(acceptedFileExtensions, acceptedContentTypes).flatMap(List::stream).toList(); String[] acceptedFileTypes = combinedList.toArray(new String[numberOfSupportedContentTypes]); upload = new Upload(buffer); upload.setDropAllowed(true);