Spaces:
Sleeping

File size: 1,193 Bytes
90b3952
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import fs from "fs";
import path from "path";
import util from "util";
import { fileURLToPath } from "url";

const unlinkAsync = util.promisify(fs.unlink);
const __dirname = path.dirname(fileURLToPath(import.meta.url));

const uploadDir = path.join("/tmp", "audios");

if (!fs.existsSync(uploadDir)) {
  fs.mkdirSync(uploadDir, { recursive: true });
}

/**
 * Saves an uploaded file to the temporary upload directory.
 * @param {Object} file - The file object containing the audio data.
 * @returns {Promise<string>} The file path of the saved file.
 */
export const saveFile = async (file) => {
  const tempFilePath = path.join(uploadDir, file.filename);

  await new Promise((resolve, reject) => {
    const writeStream = fs.createWriteStream(tempFilePath);
    file.file.pipe(writeStream);
    writeStream.on("finish", resolve);
    writeStream.on("error", reject);
  });

  return tempFilePath;
};

/**
 * Cleans up a temporary file by removing it from the filesystem.
 * @param {string} filePath - The path of the file to be deleted.
 * @returns {Promise<void>}
 */
export const cleanupFile = async (filePath) => {
  if (fs.existsSync(filePath)) {
    await unlinkAsync(filePath);
  }
};