Merged PR 195: Update downloadFile and resetStore

Update downloadFile and resetStore
This commit is contained in:
2025-07-10 15:25:12 +00:00
4 changed files with 102 additions and 60 deletions

View File

@@ -0,0 +1,18 @@
import axios from 'axios';
export const ScenarioExecutionService = {
getExecScenariosByUser(page = 0, size = 10, filters = {}, sortField, sortOrder) {
const requestBody = { page, size, ...filters, sortField, sortOrder };
return axios.post('/executions', requestBody);
},
downloadFile(filePath, executionId) {
return axios.get('/downloadFile', {
params: {
filePath: filePath,
executionId: executionId
},
responseType: 'blob' // Importante per gestire il download di file
});
}
};

View File

@@ -1,6 +1,6 @@
import { defineStore } from 'pinia';
import { computed, ref } from 'vue';
import { ScenarioService } from '../service/ScenarioService';
import { ScenarioExecutionService } from '../service/ScenarioExecutionService';
import { LoadingStore } from './LoadingStore';
export const ScenarioExecutionStore = defineStore('scenario_execution_store', () => {
@@ -17,14 +17,44 @@ export const ScenarioExecutionStore = defineStore('scenario_execution_store', ()
async function fetchScenariosExecution(page = 0, size = 10, filters, sortField, sortOrder) {
loadingStore.scenario_loading = true;
try {
console.log("Fetching executions with filters:", page, size, filters, sortField, sortOrder);
const resp = await ScenarioService.getExecScenariosByUser(page, size, filters, sortField, sortOrder); // Passiamo anche i filtri
console.log('Fetching executions with filters:', page, size, filters, sortField, sortOrder);
const resp = await ScenarioExecutionService.getExecScenariosByUser(page, size, filters, sortField, sortOrder);
lstScenarioExecution.value = resp.data.content;
totalRecords.value = resp.data.totalElements;
currentPage.value = page;
pageSize.value = size;
} catch (error) {
console.error("Error fetching executions:", error);
console.error('Error fetching executions:', error);
} finally {
loadingStore.scenario_loading = false;
}
}
async function downloadFile(filePath, executionId) {
loadingStore.scenario_loading = true;
try {
const response = await ScenarioExecutionService.downloadFile(filePath, executionId);
// Crea un URL per il blob e avvia il download
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
// Estrae il nome del file dal filePath o usa un nome di default
const fileName = filePath.split('/').pop() || `file_${executionId}`;
link.setAttribute('download', fileName);
document.body.appendChild(link);
link.click();
// Cleanup
link.remove();
window.URL.revokeObjectURL(url);
return response;
} catch (error) {
console.error('Error downloading file:', error);
throw error;
} finally {
loadingStore.scenario_loading = false;
}
@@ -56,23 +86,35 @@ export const ScenarioExecutionStore = defineStore('scenario_execution_store', ()
// Funzione per aggiornare i filtri
const updateFilters = (newFilters) => {
console.log("New filters:", newFilters);
console.log('New filters:', newFilters);
filters.value = newFilters;
filters.value = encodeURIComponent(JSON.stringify(newFilters));
fetchScenariosExecution(currentPage.value, pageSize.value, filters.value); // Ricarica con i nuovi filtri
};
// Funzione per resettare lo store
const resetStore = () => {
lstScenarioExecution.value = [];
selectedExecScenario.value = null;
totalRecords.value = 0;
currentPage.value = 0;
pageSize.value = 10;
filters.value = {};
};
return {
getCurrentPage,
getPageSize,
getTotalRecords,
fetchScenariosExecution,
downloadFile,
selectedExecScenario,
lstScenarioExecution,
scenariosExecution,
getSelectedExecScenario,
setSelectedExecScenario,
updateFilters, // Aggiunto per aggiornare i filtri
filters // Rende disponibile i filtri come parte dello store
filters,
resetStore // Rende disponibile i filtri come parte dello store
};
});

View File

@@ -4,13 +4,6 @@
</div>
<div v-else>
<div class="flex items-center justify-between p-2">
<!-- <Button
@click="back()"
label="Load"
class="flex items-center text-sm">
<ChevronLeftIcon name="chevron-left" class="w-4 h-5 text-white"/>
<span>Back to Scenarios</span>
</Button> -->
</div>
</div>
@@ -39,24 +32,15 @@
<th v-if="index === 'MultiFileUpload'" class="border border-gray-300 px-4 py-2">Files Uploaded</th>
<th v-else-if="index === 'SingleFileUpload'" class="border border-gray-300 px-4 py-2 bg-gray-500 text-white">Parameter</th>
<th v-else-if="index === 'input_multiselect_name'">
{{ (scenario.inputs && Array.isArray(scenario.inputs) ? scenario.inputs.find((i) => i.name === 'input_multiselect')?.label : null)}}
{{ scenario.inputs && Array.isArray(scenario.inputs) ? scenario.inputs.find((i) => i.name === 'input_multiselect')?.label : null }}
</th>
<th v-else class="border border-gray-300 px-4 py-2">
{{ index.replace(/_/g, ' ').replace(/\b\w/g, (char) => char.toUpperCase()) }}
</th>
<td class="border border-gray-300 px-4 py-2">
<div v-if="index === 'MultiFileUpload'">
<div v-if="fileNames.length">
<ul>
<li v-for="(file, idx) in fileNames" :key="idx" class="file-item">
{{ file.substring(file.lastIndexOf('/') + 1) }}
<Button icon="pi pi-download" class="p-button-text p-button-sm" label="Download" @click="downloadFileInput(file)" />
</li>
</ul>
</div>
<div v-else>
<p>No files found in the zip.</p>
</div>
{{ filteredInputs.SingleFileUpload.replace(/\\/g, '/').split('/').pop() }}
<Button icon="pi pi-download" class="p-button-text p-button-sm" label="Download" @click="downloadFile(inputs['SingleFileUpload'])" />
</div>
<div v-else-if="index !== 'SingleFileUpload'">{{ input }}</div>
</td>
@@ -110,7 +94,7 @@
<ul class="file-list">
<li class="file-item">
sf_document-{{ execution_id }}
<Button icon="pi pi-download" class="p-button-text p-button-sm" label="Download" @click="downloadFile(scenario_output)" />
<Button icon="pi pi-download" class="p-button-text p-button-sm" label="Download" @click="downloadFile(inputs['SingleFileUpload'])" />
</li>
</ul>
</div>
@@ -198,6 +182,7 @@ const updateLoading = ref(false);
const fileContent = ref('');
const fileType = ref('');
const chat_enabled = ref(false);
const baseUploadDir = 'mnt/hermione_storage/documents/file_input_scenarios/';
onMounted(() => {
if (execution) {
@@ -210,7 +195,6 @@ onMounted(() => {
});
const retrieveScenarioExec = (id) => {
//const id = execution_id.value.id;
loading.value = true;
axios.get('/execution?id=' + id).then((response) => {
@@ -224,8 +208,8 @@ const retrieveScenarioExec = (id) => {
inputs.value = response.data.scenarioExecutionInput.inputs;
steps.value = response.data.scenario.steps;
if (inputs.value['MultiFileUpload']) {
extractFiles(inputs.value['MultiFileUpload'], 'input', zipInput);
if (steps.value[0].attributes['codegenie_output_type']) {
extractFiles(inputs.value['MultiFileUpload'], 'input', zipInput);
if (steps.value[0].attributes['codegenie_output_type'] == 'FILE') {
fileType.value = 'FILE';
} else if (steps.value[0].attributes['codegenie_output_type'] == 'MARKDOWN') {
@@ -380,34 +364,29 @@ const downloadFileOutput = async (fileName) => {
}
};
const downloadFile = (base64String) => {
// Decodifica la stringa Base64
const binaryString = atob(base64String);
const binaryLength = binaryString.length;
const bytes = new Uint8Array(binaryLength);
for (let i = 0; i < binaryLength; i++) {
bytes[i] = binaryString.charCodeAt(i);
const downloadFile = async (filePath) => {
try {
let relativePath = filePath;
if (filePath.startsWith(baseUploadDir)) {
relativePath = filePath.substring(baseUploadDir.length);
}
// Creazione di un Blob dal file binario
const blob = new Blob([bytes], { type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' });
console.log('Original path:', filePath);
console.log('Relative path:', relativePath);
// Creazione di un URL per il Blob
const url = URL.createObjectURL(blob);
// Chiamata all'API backend per ottenere il file
await scenario_execution_store.downloadFile(relativePath, execution_id.value);
} catch (error) {
console.error('Error downloading file:', error);
// Creazione di un elemento anchor per il download
const link = document.createElement('a');
link.href = url;
link.download = 'sf_document-' + execution_id.value + '.docx';
// Simulazione di un click per scaricare il file
document.body.appendChild(link);
link.click();
// Pulizia del DOM
document.body.removeChild(link);
URL.revokeObjectURL(url);
// Notifica di errore
toast.add({
severity: 'error',
summary: 'Error',
detail: 'Error downloading file. Please try again.',
life: 3000
});
}
};
const downloadFolderFromBase64 = async (base64String) => {

View File

@@ -80,6 +80,7 @@ import { useRouter } from 'vue-router';
import { LoadingStore } from '../../stores/LoadingStore.js';
import { UserPrefStore } from '../../stores/UserPrefStore.js';
import { ScenarioStore } from '../../stores/ScenarioStore.js';
import { ScenarioExecutionStore } from '../../stores/ScenarioExecutionStore.js';
const loadingStore = LoadingStore()
@@ -87,6 +88,7 @@ const userPrefStore = UserPrefStore();
const auth = useAuth();
const user = computed(() => auth.user());
const scenario_store = ScenarioStore();
const scenario_execution_store = ScenarioExecutionStore();
const router = useRouter()
@@ -125,6 +127,7 @@ const scenario_store = ScenarioStore();
try {
// Esegui l'update del progetto
scenario_store.resetStore();
scenario_execution_store.resetStore();
await userPrefStore.updateSelectedProject(project);
console.log('Progetto aggiornato e dati utente ricaricati');