From dff936fa2db7865f4d2463218936ef44cac5e4cd Mon Sep 17 00:00:00 2001 From: "ms@Nicro" Date: Sat, 13 Jan 2024 15:38:06 +0100 Subject: [PATCH] Little bugfixing --- .../service/exercise/SpeechRecognition.java | 14 +++++- src/main/webapp/JS/exercise.js | 50 ++++++++----------- 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/src/main/java/model/service/exercise/SpeechRecognition.java b/src/main/java/model/service/exercise/SpeechRecognition.java index f143d46..f3ec8cc 100644 --- a/src/main/java/model/service/exercise/SpeechRecognition.java +++ b/src/main/java/model/service/exercise/SpeechRecognition.java @@ -51,11 +51,12 @@ else if (speechRecognitionResult.getReason() == ResultReason.NoMatch){ if (cancellation.getReason() == CancellationReason.Error) { System.out.println("CANCELED: ErrorCode=" + cancellation.getErrorCode()); System.out.println("CANCELED: ErrorDetails=" + cancellation.getErrorDetails()); - System.out.println("CANCELED: Did you set the speech resource key and region values?"); } } + speechRecognizer.close(); return result; + } public String generateFile(InputStream inputAudio) throws IOException { @@ -75,7 +76,7 @@ public String generateFile(InputStream inputAudio) throws IOException { //Controlla che non esista giĆ  un file try { // Use the delete method from Files class to delete the file - Files.delete(Paths.get(path)); + Files.delete(outputPath); } catch (FileNotFoundException e){ System.err.println("File not found"); } catch (IOException e) { @@ -109,6 +110,15 @@ public String generateFile(InputStream inputAudio) throws IOException { e.printStackTrace(); } + try { + // Use the delete method from Files class to delete the file + Files.delete(Path.of(tempFile.toURI())); + } catch (FileNotFoundException e){ + System.err.println("File not found"); + } catch (IOException e) { + System.err.println("Error deleting the file: " + e.getMessage()); + } + return path; } diff --git a/src/main/webapp/JS/exercise.js b/src/main/webapp/JS/exercise.js index 3fb2000..bbb21e0 100644 --- a/src/main/webapp/JS/exercise.js +++ b/src/main/webapp/JS/exercise.js @@ -462,11 +462,6 @@ function loadRightText(initialState){ // Salvataggio dell'esecuzione //---------------------------------------------------------------------------------------------------------------------- -//Read Text e Read Images -function saveReadExercise(execution){ - saveAudioExecution(execution); -} - //Images to text function saveITT(n) { let execution = {}; @@ -518,43 +513,42 @@ function saveCT(n){ function saveExecution(execution){ $("#buttonDiv > button").prop("disabled", true).text("Esercizio Inviato!"); - - $.ajax({ - type: "POST", + $.post({ url: "../exerciseLogger", data: JSON.stringify(execution), - contentType: "application/json", - error: function (error) { - console.error("Errore durante l'invio dell'esecuzione alla servlet:", error); - redirect("500.html") - } - }); - setTimeout(()=> { + contentType: "application/json" + }) + .done(function(response) { redirect("home"); - }, 3000); + }) + .fail(function(error) { + console.error("Error during execution submission to the servlet:", error); + redirect("500.html"); + }); + } -function saveAudioExecution(execution){ +//Read Text e Read Images +function saveReadExercise(execution){ if(execution != null){ let formData = new FormData(); formData.append("audioFile", execution); - $.ajax({ - type: "POST", + $.post({ url: "../exerciseLogger", data: formData, processData: false, contentType: false, - cache: false, - success: function (){ - redirect("home"); - }, - error: function(xhr, status, error) { - console.error("Errore durante l'invio dell'audio alla servlet:", error); - console.log(xhr.responseText); - redirect("500.html"); - } + cache: false + }) + .done(function() { + redirect("home"); + }) + .fail(function(xhr, status, error) { + console.error("Errore durante l'invio dell'audio alla servlet:", error); + console.log(xhr.responseText); + redirect("500.html"); }); } } \ No newline at end of file