uplix
v2.0.7
Published
image, files & media handler (images, video, audio, docs) with auto-processing
Maintainers
Readme
uplix
MediaManager.js
A Node.js media management utility & media handling.
Supports single & multiple file uploads, any file type, image resizing & conversion, and controller-level usage (no middleware needed).
get usage in example folder
📦 Installation
npm install uplix
# Usage direct in controller
const { MediaManager } = require('uplix');
const media = new MediaManager(
'uploads',
'/uploads',
{ maxFileSizeMB: 10 },
{ convertToWebP: true } # false for original extension
);
# Create fields and files
# Parse fields and files automatically
const payload = await media.auto(req);
# Handle image upload
let imageFileName = null;
if (payload.image) {
const fileData = Array.isArray(payload.image) ? payload.image[0] : payload.image;
imageFileName = fileData.fileName;
}
# Create new model
const model = await Model.create({
name: payload.name,
image: imageFileName,
is_active: payload.is_active ?? false, # boolean parsed by auto()
});
# Update fields and files
const model = await Model.find(req.params.id);
# Auto-parse fields and files
const data = await media.auto(req);
# Fill other fields
model.fill({
name: data.name,
is_active: model.is_active ?? false, # already boolean from auto()
});
# Handle image upload
if (data.image) {
# Normalize: payload.image may be an array if multiple files uploaded
const fileData = Array.isArray(data.image) ? data.image[0] : data.image;
# Remove old file if it exists
if (model.image) {
await media.remove(model.image, 'image');
}
# Update with new file
model.image = fileData.fileName;
}
await model.save();
# Delet files
if (model.image) {
await media.remove(model.image, 'image');
}
# SECOND METHOD
# Create file
let image;
image = await media.file(req, 'image', {
resize: true,
width: 1920,
height: 1080,
format: 'webp',
quality: 80
});
await Model.create({
image: image.fileName
});
# Update file
const { files } = await media.parseRequest(req);
const model = await Model.find(req.params.id);
let imageName = model.image;
# ✅ DYNAMIC: works for single or multiple
if (media.hasFile(files, 'image')) {
const imageFile = media.getFirstFile(files, 'image');
const image = await media.processFile(imageFile, 'image', {
resize: true,
width: 1920,
height: 1080,
format: 'webp',
quality: 80
});
# Remove old image safely
if (model.image) {
await media.remove(model.image, 'image');
}
imageName = image.fileName;
}
await model.update({
image: imageName
});
# Controller Example Usage With Different File Types
const media = new MediaManager('./uploads/files', '/uploads/files');
# Upload a PDF
const pdfFile = await media.file(req, 'document');
# Upload multiple videos
const videos = await media.files(req, 'videos');
# Upload an MP3
const song = await media.file(req, 'song');
#Uploads one required file.
const avatar = await media.file(req, 'avatar');
<input type="file" name="avatar">
#BONUS: MULTIPLE FILES (Gallery Example)
const gallery = await media.files(req, 'gallery');
<input type="file" name="gallery" multiple>
# Automatically decides single vs multiple
const data = await media.auto(req);
# Example form
<input type="text" name="title">
<input type="file" name="thumbnail">
<input type="file" name="gallery" multiple>
const uploaded = await Promise.all(
galleryFiles
.filter(f => f.size > 0)
.map(f => media.processFile(f, 'gallery'))
);
# Supported image formats
jpg, jpeg, png, webp, tiff, gif
# Enable WebP conversion
new MediaManager('uploads', '/uploads', {}, {
convertToWebP: true
});
# Returned metadata
{
id: "e8c7a...",
field: "avatar",
fileName: "1699-abc.webp",
mime: "image/webp",
sizeMB: 0.43,
path: "/uploads/avatar/1699-abc.webp",
url: "/uploads/avatar/1699-abc.webp",
createdAt: Date
}