Last active
September 12, 2022 21:31
-
-
Save prateekkathal/a3428b4b893d623cade4d63718e88e8c to your computer and use it in GitHub Desktop.
For Medium Article (Creating/Writing/Downloading Files in NestJS)
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 should exist in `src/common/helpers` | |
import fs from 'fs'; | |
import { promisify } from 'util'; | |
/** | |
* Check if a file exists at a given path. | |
* | |
* @param {string} path | |
* | |
* @returns {boolean} | |
*/ | |
export const checkIfFileOrDirectoryExists = (path: string): boolean => { | |
return fs.existsSync(path); | |
}; | |
/** | |
* Gets file data from a given path via a promise interface. | |
* | |
* @param {string} path | |
* @param {string} encoding | |
* | |
* @returns {Promise<Buffer>} | |
*/ | |
export const getFile = async ( | |
path: string, | |
encoding: string, | |
): Promise<string | Buffer> => { | |
const readFile = promisify(fs.readFile); | |
return encoding ? readFile(path, encoding) : readFile(path, {}); | |
}; | |
/** | |
* Writes a file at a given path via a promise interface. | |
* | |
* @param {string} path | |
* @param {string} fileName | |
* @param {string} data | |
* | |
* @return {Promise<void>} | |
*/ | |
export const createFile = async ( | |
path: string, | |
fileName: string, | |
data: string, | |
): Promise<void> => { | |
if (!checkIfFileOrDirectoryExists(path)) { | |
fs.mkdirSync(path); | |
} | |
const writeFile = promisify(fs.writeFile); | |
return await writeFile(`${path}/${fileName}`, data, 'utf8'); | |
}; | |
/** | |
* Delete file at the given path via a promise interface | |
* | |
* @param {string} path | |
* | |
* @returns {Promise<void>} | |
*/ | |
export const deleteFile = async (path: string): Promise<void> => { | |
const unlink = promisify(fs.unlink); | |
return await unlink(path); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment