mirror of
https://github.com/iib0011/omni-tools.git
synced 2026-04-23 05:36:24 +05:30
chore: service updated for batch processing support
This commit is contained in:
parent
9af9891b3e
commit
dc73240787
1 changed files with 33 additions and 9 deletions
|
|
@ -1,10 +1,11 @@
|
|||
import { InitialValuesType } from './types';
|
||||
import imageCompression from 'browser-image-compression';
|
||||
import JSZip from 'jszip';
|
||||
|
||||
export const compressImage = async (
|
||||
file: File,
|
||||
export const compressImages = async (
|
||||
files: File[],
|
||||
options: InitialValuesType
|
||||
): Promise<File | null> => {
|
||||
): Promise<{ results: File[]; zipFile: File } | null> => {
|
||||
try {
|
||||
const { maxFileSizeInMB, quality } = options;
|
||||
|
||||
|
|
@ -16,15 +17,38 @@ export const compressImage = async (
|
|||
initialQuality: quality / 100 // Convert percentage to decimal
|
||||
};
|
||||
|
||||
// Compress the image
|
||||
const compressedFile = await imageCompression(file, compressionOptions);
|
||||
// Compress the given images
|
||||
const compressed = await Promise.all(
|
||||
files.map(async (file) => {
|
||||
try {
|
||||
const compressedFile = await imageCompression(
|
||||
file,
|
||||
compressionOptions
|
||||
);
|
||||
return new File([compressedFile], file.name, {
|
||||
type: compressedFile.type
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(`Error compressing ${file.name}:`, error);
|
||||
return null;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
// Create a new file with the original name
|
||||
return new File([compressedFile], file.name, {
|
||||
type: compressedFile.type
|
||||
const results = compressed.filter((f): f is File => f !== null);
|
||||
|
||||
if (results.length === 0) return null;
|
||||
|
||||
const zip = new JSZip();
|
||||
results.forEach((file) => zip.file(file.name, file));
|
||||
const zipBlob = await zip.generateAsync({ type: 'blob' });
|
||||
const zipFile = new File([zipBlob], 'compressed-images.zip', {
|
||||
type: 'application/zip'
|
||||
});
|
||||
|
||||
return { results, zipFile };
|
||||
} catch (error) {
|
||||
console.error('Error compressing image:', error);
|
||||
console.error('Error compressing images:', error);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue