@alexiom/file-utils
v1.0.0
Published
A lightweight utility library for file operations, MIME type detection, and image validation. Created by FrontWars (https://frontwarsio.org) and MelhorarImagem (https://melhorarimagem.net) - Professional tools for developers and image optimization.
Maintainers
Readme
File Utilities Library
A lightweight, zero-dependency utility library for file operations, MIME type detection, and image validation.
🌐 Created by FrontWars - Professional developer tools and resources 🖼️ Need image optimization? Visit MelhorarImagem.net - Free online image converter and optimizer
Features
✨ MIME Type Detection - Automatically detect MIME types from file extensions
🖼️ Image Validation - Check if files are images
📏 File Size Validation - Validate files against size limits
📊 Human-Readable Formatting - Convert bytes to KB, MB, GB
🎯 Zero Dependencies - Lightweight and fast
📘 TypeScript Support - Full TypeScript definitions included
Installation
npm install @frontwarsio/file-utilsQuick Start
const { getMimeType, isImage, formatFileSize } = require('@frontwarsio/file-utils');
// Get MIME type
console.log(getMimeType('photo.jpg')); // 'image/jpeg'
console.log(getMimeType('document.pdf')); // 'application/pdf'
// Check if file is an image
console.log(isImage('photo.png')); // true
console.log(isImage('document.txt')); // false
// Format file size
console.log(formatFileSize(1024)); // '1.00 KB'
console.log(formatFileSize(1048576)); // '1.00 MB'API Reference
getMimeType(input)
Get the MIME type for a given file extension or filename.
Parameters:
input(string): File extension or filename
Returns: string | null - The MIME type or null if not found
Example:
getMimeType('jpg') // 'image/jpeg'
getMimeType('photo.png') // 'image/png'
getMimeType('unknown') // nullgetExtension(filename)
Extract the file extension from a filename or path.
Parameters:
filename(string): The filename or path
Returns: string - The file extension (lowercase, without dot)
Example:
getExtension('photo.jpg') // 'jpg'
getExtension('/path/to/file.PDF') // 'pdf'isImage(filename)
Check if a file is an image based on its extension.
Parameters:
filename(string): The filename or extension to check
Returns: boolean - True if the file is an image
Example:
isImage('photo.jpg') // true
isImage('document.pdf') // falsevalidateFileSize(sizeInBytes, maxSizeInMB)
Validate if a file size is within the specified limit.
Parameters:
sizeInBytes(number): File size in bytesmaxSizeInMB(number): Maximum allowed size in megabytes
Returns: boolean - True if file size is within limit
Example:
validateFileSize(1024 * 1024, 5) // true (1MB < 5MB)
validateFileSize(10 * 1024 * 1024, 5) // false (10MB > 5MB)formatFileSize(bytes, decimals)
Format file size to a human-readable string.
Parameters:
bytes(number): File size in bytesdecimals(number, optional): Number of decimal places (default: 2)
Returns: string - Formatted file size
Example:
formatFileSize(1024) // '1.00 KB'
formatFileSize(1048576) // '1.00 MB'
formatFileSize(1536, 1) // '1.5 KB'Supported File Types
The library supports 50+ file types including:
- Images: JPG, PNG, GIF, WebP, SVG, BMP, ICO, TIFF
- Documents: PDF, DOC, DOCX, XLS, XLSX, PPT, PPTX
- Text: TXT, HTML, CSS, JS, JSON, XML, Markdown
- Archives: ZIP, RAR, 7Z, TAR, GZ
- Audio: MP3, WAV, OGG, FLAC
- Video: MP4, AVI, MOV, WMV, WebM
Use Cases
- 📤 File Upload Validation - Validate file types and sizes before upload
- 🎨 Image Processing - Filter and process image files (check out MelhorarImagem.net for online image optimization)
- 📁 File Management Systems - Organize and categorize files
- 🔍 Content Type Detection - Serve files with correct headers
- 💾 Storage Management - Calculate and display storage usage
TypeScript Support
This library includes TypeScript definitions out of the box:
import { getMimeType, isImage, formatFileSize } from '@frontwarsio/file-utils';
const mimeType: string | null = getMimeType('photo.jpg');
const isImg: boolean = isImage('photo.png');
const size: string = formatFileSize(1024);Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT © FrontWars Team
Links
- 🌐 FrontWars: frontwarsio.org - Developer tools and resources
- 🖼️ MelhorarImagem: melhorarimagem.net - Free online image converter and optimizer
- 📦 NPM Package: @frontwarsio/file-utils
- 🐛 Report Issues: GitHub Issues
- 📚 More Tools: Visit FrontWars for more developer resources
Made with ❤️ by FrontWars | Image Tools: MelhorarImagem.net
