-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
503 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,72 @@ | ||
// src/utils/cleanUpFiles.ts | ||
|
||
/** | ||
* @file Utility for cleaning up temporary files generated during processing. | ||
* Handles removal of intermediate files with specific extensions. | ||
* @packageDocumentation | ||
*/ | ||
|
||
import { unlink } from 'node:fs/promises' | ||
import { log, step, success } from '../models.js' | ||
|
||
/** | ||
* Asynchronous function to clean up temporary files. | ||
* @param {string} id - The base filename (without extension) for the files to be cleaned up. | ||
* @returns {Promise<void>} | ||
* @throws {Error} - If an error occurs while deleting files. | ||
* Removes temporary files generated during content processing. | ||
* Attempts to delete files with specific extensions and logs the results. | ||
* Silently ignores attempts to delete non-existent files. | ||
* | ||
* Files cleaned up include: | ||
* - .wav: Audio files | ||
* - .txt: Transcription text | ||
* - .md: Markdown content | ||
* - .lrc: Lyrics/subtitles | ||
* | ||
* @param {string} id - Base filename (without extension) used to identify related files. | ||
* All files matching pattern `${id}${extension}` will be deleted. | ||
* | ||
* @returns {Promise<void>} Resolves when cleanup is complete. | ||
* | ||
* @throws {Error} If deletion fails for reasons other than file not existing: | ||
* - Permission denied | ||
* - File is locked/in use | ||
* - I/O errors | ||
* | ||
* @example | ||
* try { | ||
* await cleanUpFiles('content/my-video-2024-03-21') | ||
* // Will attempt to delete: | ||
* // - content/my-video-2024-03-21.wav | ||
* // - content/my-video-2024-03-21.txt | ||
* // - content/my-video-2024-03-21.md | ||
* // - content/my-video-2024-03-21.lrc | ||
* } catch (error) { | ||
* console.error('Cleanup failed:', error) | ||
* } | ||
*/ | ||
export async function cleanUpFiles(id: string): Promise<void> { | ||
log(step('\nStep 5 - Cleaning up temporary files...\n')) | ||
// Array of file extensions to delete | ||
const extensions = ['.wav', '.txt', '.md', '.lrc'] | ||
|
||
// Define extensions of temporary files to be cleaned up | ||
const extensions = [ | ||
'.wav', // Audio files | ||
'.txt', // Transcription text | ||
'.md', // Markdown content | ||
'.lrc' // Lyrics/subtitles | ||
] | ||
|
||
log(success(` Temporary files deleted:`)) | ||
|
||
// Attempt to delete each file type | ||
for (const ext of extensions) { | ||
try { | ||
// Delete file and log success | ||
await unlink(`${id}${ext}`) | ||
log(success(` - ${id}${ext}`)) | ||
} catch (error) { | ||
// Only log errors that aren't "file not found" (ENOENT) | ||
if (error instanceof Error && (error as Error).message !== 'ENOENT') { | ||
console.error(`Error deleting file ${id}${ext}: ${(error as Error).message}`) | ||
} | ||
// If the file does not exist, silently continue | ||
// Silently continue if file doesn't exist | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.