diff --git a/components/ManageSerie.async.vue b/components/ManageSerie.async.vue index e486219..7b4eef7 100644 --- a/components/ManageSerie.async.vue +++ b/components/ManageSerie.async.vue @@ -45,12 +45,15 @@ async function sendFile() { // send file to the server // - TODO: show loading spinner + // - Put file in a FormData object to send all format file + // (ex: csv, xls, xlsx, ...) + const formData = new FormData(); + formData.append('file', daughterFile.value); + await $fetch('/api/extractFromFile', { method: 'POST', - body: daughterFile.value, + body: formData, }); - console.log("sendFile"); - console.log(daughterFile.value?.name); } diff --git a/server/api/extractFromFile.ts b/server/api/extractFromFile.ts index f0bb6c0..cb7dac7 100644 --- a/server/api/extractFromFile.ts +++ b/server/api/extractFromFile.ts @@ -10,19 +10,23 @@ import fs from "fs"; import path from "path"; +import { P2M2ToolsApi, P2M2ToolsApiFill } from "@/types/p2m2Api"; export default defineEventHandler(async(event) => { // Read the form data from the event - const formData = await readBody(event); + const formData = await readMultipartFormData(event); + // Get the file from the form data if(!formData){ return 2; } - + + const rFile = formData[0].data; + // wrtie the file on disk const id = crypto.randomUUID(); const filePath = path.join("/shareFile", id); - fs.writeFileSync(filePath, formData); + fs.writeFileSync(filePath, rFile); // Send the file to the parser @@ -35,14 +39,14 @@ export default defineEventHandler(async(event) => { body: fs.readFileSync(filePath), }) .then((resp) => { - return resp.json() as Promise<{}|{ header : string[], samples : string[][]}>; + return resp.json() as Promise; }) .then((jsonResp) => { // If the file is unknown, delete it if(JSON.stringify(jsonResp) === JSON.stringify({})){ return 1; } - return jsonResp; + return extractMetabolites(jsonResp as P2M2ToolsApiFill); }) .catch(()=>{ // If an error occurs, delete the file @@ -51,10 +55,36 @@ export default defineEventHandler(async(event) => { .finally(()=>{ // Delete the file fs.rmSync(filePath); - console.log("File deleted"); }); + +}) + - // console.log(resp); - -}) \ No newline at end of file +/** + * We extract name and area of metabolites from the file. + * @param jsonResp The response from the parser + * @returns The list of tupple metabolites and their area + */ +function extractMetabolites(jsonResp: P2M2ToolsApiFill): [string, number][] { + // Check if the response contains the header and the samples + if(jsonResp.header && jsonResp.samples){ + // Get the index of the name and the area + const idxMeta = jsonResp.header.indexOf("metabolite"); + const idxArea = jsonResp.header.indexOf("area"); + // If the name and the area are found, return the list of metabolites + // and their area + if(idxMeta != -1 && idxArea != -1){ + + const metaArea = jsonResp.samples.filter(x => + // keep the metabolite and the area are not empty + x[idxMeta] !== "" && x[idxArea] !== "" && + // keep if the area is a number + !isNaN(parseFloat(x[idxArea]))); + + return metaArea.map(x => [x[idxMeta], parseFloat(x[idxArea])] + ); + } + } + return []; +} diff --git a/types/p2m2Api.ts b/types/p2m2Api.ts new file mode 100644 index 0000000..31dbcee --- /dev/null +++ b/types/p2m2Api.ts @@ -0,0 +1,9 @@ +// SPDX-FileCopyrightText: 2024 Marcellino Palerme +// +// SPDX-License-Identifier: MIT + +// We declare type of response of the p2m2ToolsApi + +export type P2M2ToolsApi = {}|{ header : string[], samples : string[][]}; + +export type P2M2ToolsApiFill = { header : string[], samples : string[][]}; \ No newline at end of file