mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-08-26 22:29:24 +00:00

- Introduced `useToolApiCalls` for handling API calls with file processing and cancellation support. - Created `useToolOperation` to manage tool operations, including state management, error handling, and file processing. - Added `useToolResources` for managing blob URLs and generating thumbnails. - Developed `useToolState` for centralized state management of tool operations. - Refactored `useSplitOperation` to utilize the new shared hooks, simplifying the execution of split operations. - Updated `useSplitParameters` to remove mode state and integrate with the new parameter structure. - Enhanced error handling with `toolErrorHandler` utilities for standardized error extraction and messaging. - Implemented `toolOperationTracker` for creating operation tracking data for file context integration. - Added `toolResponseProcessor` for processing API response blobs based on handler configuration.
51 lines
1.8 KiB
TypeScript
51 lines
1.8 KiB
TypeScript
import { useTranslation } from 'react-i18next';
|
|
import { useToolOperation, ToolOperationConfig } from '../shared/useToolOperation';
|
|
import { createStandardErrorHandler } from '../../../utils/toolErrorHandler';
|
|
|
|
export interface CompressParameters {
|
|
compressionLevel: number;
|
|
grayscale: boolean;
|
|
expectedSize: string;
|
|
compressionMethod: 'quality' | 'filesize';
|
|
fileSizeValue: string;
|
|
fileSizeUnit: 'KB' | 'MB';
|
|
}
|
|
|
|
const buildFormData = (parameters: CompressParameters, file: File): FormData => {
|
|
const formData = new FormData();
|
|
formData.append("fileInput", file);
|
|
|
|
if (parameters.compressionMethod === 'quality') {
|
|
formData.append("optimizeLevel", parameters.compressionLevel.toString());
|
|
} else {
|
|
// File size method
|
|
const fileSize = parameters.fileSizeValue ? `${parameters.fileSizeValue}${parameters.fileSizeUnit}` : '';
|
|
if (fileSize) {
|
|
formData.append("expectedOutputSize", fileSize);
|
|
}
|
|
}
|
|
|
|
formData.append("grayscale", parameters.grayscale.toString());
|
|
return formData;
|
|
};
|
|
|
|
export const useCompressOperation = () => {
|
|
const { t } = useTranslation();
|
|
|
|
return useToolOperation<CompressParameters>({
|
|
operationType: 'compress',
|
|
endpoint: '/api/v1/misc/compress-pdf',
|
|
buildFormData,
|
|
filePrefix: 'compressed_',
|
|
singleFileMode: false, // Process files individually
|
|
timeout: 60000, // 1 minute timeout per file
|
|
validateParams: (params) => {
|
|
if (params.compressionMethod === 'filesize' && !params.fileSizeValue) {
|
|
return { valid: false, errors: [t('compress.validation.fileSizeRequired', 'File size value is required when using filesize method')] };
|
|
}
|
|
return { valid: true };
|
|
},
|
|
getErrorMessage: createStandardErrorHandler(t('compress.error.failed', 'An error occurred while compressing the PDF.'))
|
|
});
|
|
};
|