Browse Source

Refactor File.js to include generating a separate filepath for the generated file

node16
Onja 12 months ago
parent
commit
7be61d264d
  1. 11
      src/models/File.js

11
src/models/File.js

@ -7,6 +7,8 @@ const https = require('https');
const exec = util.promisify(require('child_process').exec);
const EventEmitter = require('events');
const csvParser = require('csv-parser');
const { PassThrough } = require('stream');
const slugify = require('slugify');
const dest = path.join(appRoot, 'public/csv');
@ -14,7 +16,10 @@ const dest = path.join(appRoot, 'public/csv');
// Create a generateFilePath function witch returns a path with a filename and datetime
function generateFilePath(filename) {
return path.join(dest, `${filename}-${Date.now()}.csv`);
return {
filepath: path.join(dest, `${filename}-${Date.now()}.csv`),
generatedpath: path.join('csv', `${filename}-generated-${Date.now()}.csv`),
};
}
@ -39,7 +44,9 @@ class File extends EventEmitter {
async download() {
const url = URL.parse(this.url);
this.filename = slugify(url.hostname, { lower: true });
this.filepath = generateFilePath(this.filename);
const { filepath, generatedpath } = generateFilePath(this.filename);
this.filepath = filepath;
this.generatedpath = generatedpath;
const file = fs.createWriteStream(this.filepath);
const request = url.protocol === 'https:' ? https : http;

Loading…
Cancel
Save