// utils/fileStorage.js
const path = require('path');
const fs = require('fs-extra');
const { pipeline } = require('stream/promises');

async function savePdf(pdfPart) {
  const filename = `presentation-${Date.now()}.pdf`;
  const filePath = path.join(__dirname, '..', 'uploads', 'pdf', filename);
  await fs.ensureDir(path.dirname(filePath));
  await pipeline(pdfPart.file, fs.createWriteStream(filePath));
  return filePath;
}

async function saveCsv(csvPart) {
  const filename = `metadata-${Date.now()}.csv`;
  const filePath = path.join(__dirname, '..', 'uploads', 'media', filename);
  await fs.ensureDir(path.dirname(filePath));
  await pipeline(csvPart.file, fs.createWriteStream(filePath));
  return filePath;
}

module.exports = {
  savePdf,
  saveCsv
};
