All files / src/utils fileUtils.ts

81.08% Statements 30/37
100% Branches 8/8
75% Functions 3/4
84.61% Lines 22/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 262x 2x 2x 2x 2x 2x 2x   34x 34x 29x 29x   2x 2x 2x 2x 2x 2x 2x 2x 34x 8x   5x  
// src/utils/fileUtils.ts
import fs from 'node:fs/promises';
import { logger } from '../services/logger.server';
 
/**
 * Safely deletes a file from the filesystem, ignoring errors if the file doesn't exist.
 * @param file The multer file object to delete.
 */
export const cleanupUploadedFile = async (file?: Express.Multer.File) => {
  if (!file) return;
  try {
    await fs.unlink(file.path);
  } catch (err) {
    logger.warn({ err, filePath: file.path }, 'Failed to clean up uploaded file.');
  }
};
 
/**
 * Safely deletes multiple files from the filesystem.
 * @param files An array of multer file objects to delete.
 */
export const cleanupUploadedFiles = async (files?: Express.Multer.File[]) => {
  if (!files || !Array.isArray(files)) return;
  // Use Promise.all to run cleanups in parallel for efficiency.
  await Promise.all(files.map((file) => cleanupUploadedFile(file)));
};