Skip to content

Commit

Permalink
#3 download archive
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcellino-Palerme committed Oct 18, 2023
1 parent fb2e364 commit a24f41b
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 72 deletions.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ FROM node:latest
WORKDIR /EP2M2
COPY ./.output .
EXPOSE 3000
RUN mkdir /results
ENV EP2M2_DIR_SHARE=/shareFile
ENV EP2M2_DIR_RESULT=/results
CMD [ "node", "./server/index.mjs" ]
9 changes: 5 additions & 4 deletions components/ExtractInfo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -96,27 +96,28 @@ function deleteAll(){
}

async function extract() {
const response = await $fetch("/api/extract",{method:"post", body:{files:files, loc: window.location}});
const response = await $fetch("/api/extract",{method:"post", body:files});
console.log(response);

if (response === "" || !response){
return 0
}

const data = new Blob ([response], { type: 'text/plain' });
const data = new Blob ([response], { type: 'application/zip' });

// console.log(data.value);

const eleLink = document.createElement('a');
eleLink.download = "test.csv";
eleLink.download = `Extration_${new Date(Date.now()).toISOString()
.replaceAll(/\W/g, "")}.tar`;
eleLink.style.display = 'none';

eleLink.href = URL.createObjectURL(data);

document.body.appendChild(eleLink);
eleLink.click();

URL.revokeObjectURL(eleLink.href); // 释放URL 对象
URL.revokeObjectURL(eleLink.href);
document.body.removeChild(eleLink);

}
Expand Down
6 changes: 4 additions & 2 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// https://nuxt.com/docs/api/configuration/nuxt-config
import {} from 'nuxt'

export default defineNuxtConfig({
devtools: { enabled: true },
modules: [
"@nuxt/ui"
"@nuxt/ui",
],
colorMode: {
preference: "light"
},
}
});
98 changes: 43 additions & 55 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 44 additions & 11 deletions server/api/extract.post.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@

import { writeFileSync, readFileSync, createReadStream} from "fs";
import { writeFileSync, readFileSync, createReadStream, existsSync,
mkdirSync} from "fs";
import {open} from "fs/promises"
import {tFile} from "~/types/file";
import { sendStream } from 'h3'
import { sendStream } from 'h3';
import {join} from "path";
import {execSync} from "child_process"

function json2csv(jsonFile: JSON):string{
// thx : https://stackoverflow.com/a/31536517
Expand All @@ -21,32 +25,61 @@ function json2csv(jsonFile: JSON):string{
...items.map((row:{[key: string]:any}) => header.map(fieldName => String(row[fieldName]?row[fieldName]:"")).join(","))
].join("\r\n");

console.log(csv);
return csv;

}

export default defineEventHandler(async (event) => {
const listFiles = await readBody(event) as {files:tFile[], loc: string};

if (!listFiles){
const listFiles = await readBody(event) as tFile[];
const shareDir = process.env.EP2M2_DIR_SHARE;
const resultsDir = process.env.EP2M2_DIR_RESULT;

if (!listFiles || !shareDir || !resultsDir){
return "";
}

for (const infoFile of listFiles.files){
const myFile = readFileSync(`/shareFile/${infoFile.id}`)
// Create directory to save result
const resultFolder = join(resultsDir, crypto.randomUUID());

if (!existsSync(resultFolder)){
mkdirSync(resultFolder,{recursive:true});

}

for (const infoFile of listFiles){
// Ask extraction
const response = await fetch("http://p2m2ToolsApi:8080/p2m2tools/api/format/parse",{
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
body: myFile
body: readFileSync(join(shareDir, infoFile.id))
});

// Save extraction
const jsonContent = await response.json();

writeFileSync(`./public/${infoFile.id}.csv`, json2csv(jsonContent));
const resultFile = infoFile.name.substring(0,
infoFile.name.indexOf("."));


return sendStream(event, createReadStream(`./public/${infoFile.id}.csv`));

writeFileSync(join(resultFolder, `${resultFile}.csv`),
json2csv(jsonContent));
}

// create archive with all resutls
const resultFile = join(resultsDir, new Date(Date.now()).toISOString())
.replaceAll(/[^a-zA-Z0-9_\/\\]/g, "") + ".tar";
console.log(resultFile);

// TODO Add error manage
execSync(`find ${resultFolder} -maxdepth 1 -printf "%P\n" \
|tar cf ${resultFile} -C ${resultFolder} -T -`);

console.log("next");

const zipFile = await open(resultFile);
console.log("suite")
return sendStream(event, zipFile.createReadStream());
});

0 comments on commit a24f41b

Please sign in to comment.