Skip to content

Commit

Permalink
verificacao se arquivo para exportacao ja existe para evitar criar ar…
Browse files Browse the repository at this point in the history
…quivos duplicado
  • Loading branch information
jesielviana committed Dec 19, 2023
1 parent 88b37ca commit 7355cc3
Showing 1 changed file with 31 additions and 17 deletions.
48 changes: 31 additions & 17 deletions src/pages/api/bulk-download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const client = new Client({
const proxy = async (req: NextApiRequest, res: NextApiResponse) => {
const { query, index } = req.body;

let writeStream;
try {
const params: Search = {
index: index,
Expand All @@ -35,38 +34,53 @@ const proxy = async (req: NextApiRequest, res: NextApiResponse) => {
};

const fileName = getFileName(index, JSON.stringify(query));
const path = `${process.env.DOWNLOAD_FOLDER_PATH}/${fileName}.csv`;
const createStream = fs.createWriteStream(path);
createStream.end();

writeStream = fs.createWriteStream(path);
const filePath = getFilePath(fileName);
if (fs.existsSync(filePath)) {
console.log('arquivo já existe em: ', filePath);
} else {
createFile(filePath);
await writeToFile(filePath, params);
}
res.json({ file: filePath });
} catch (err) {
console.error('ERROR::', err);
res.status(400).json({ error: err.message });
}
};

async function writeToFile(filePath: string, params: Search) {
const writeStream = fs.createWriteStream(filePath);
try {
const options: Json2CsvOptions = {
prependHeader: true,
delimiter: { field: ';' },
};

let isFirstItem = true;
let c = 1;

console.log('iniciando escrita no arquivo');
for await (const hit of scrollSearch(params)) {
options.prependHeader = isFirstItem;
writeStream.write(json2csv(hit._source, options));
writeStream.write('\r\n');
isFirstItem = false;
console.log(c, JSON.stringify(params));
c++;
}
console.log('params', JSON.stringify(params));
res.json({ file: path });
} catch (err) {
console.error('ERROR::', err);
res.status(400).json({ error: err.message });
} finally {
console.log('close writeStream');
writeStream?.end();
console.log('encerrando escrita no arquivo e fechando-o');
}
};
}

function getFilePath(fileName: string) {
const filePath = `${process.env.DOWNLOAD_FOLDER_PATH}/${fileName}.csv`;
return filePath;
}

function createFile(filePath: string) {
const createStream = fs.createWriteStream(filePath);
createStream.end();
console.log('arquivo criado em: ', filePath);
return filePath;
}

// Scroll utility
async function* scrollSearch(params: Search) {
Expand Down

0 comments on commit 7355cc3

Please sign in to comment.