Skip to content

Commit

Permalink
#48 read excel file. List Metabo and thier area not empty
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcellino-Palerme committed Jun 19, 2024
1 parent 518599d commit b6184c9
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 12 deletions.
9 changes: 6 additions & 3 deletions components/ManageSerie.async.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
</script>
Expand Down
48 changes: 39 additions & 9 deletions server/api/extractFromFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<P2M2ToolsApi>;
})
.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
Expand All @@ -51,10 +55,36 @@ export default defineEventHandler(async(event) => {
.finally(()=>{
// Delete the file
fs.rmSync(filePath);
console.log("File deleted");

});

})


// console.log(resp);

})
/**
* 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 [];
}
9 changes: 9 additions & 0 deletions types/p2m2Api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-FileCopyrightText: 2024 Marcellino Palerme <[email protected]>
//
// 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[][]};

0 comments on commit b6184c9

Please sign in to comment.