mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-09-18 09:29:24 +00:00

# Description of Changes Redesigns `ToolOperationConfig` so that the types of the functions are always known depending on whether the tool runs on single files, multiple files, or uses custom behaviour
43 lines
1.5 KiB
TypeScript
43 lines
1.5 KiB
TypeScript
import { useTranslation } from 'react-i18next';
|
|
import { useToolOperation, ToolOperationConfig, ToolType } from '../shared/useToolOperation';
|
|
import { createStandardErrorHandler } from '../../../utils/toolErrorHandler';
|
|
import { CompressParameters, defaultParameters } from './useCompressParameters';
|
|
|
|
// Static configuration that can be used by both the hook and automation executor
|
|
export const buildCompressFormData = (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;
|
|
};
|
|
|
|
// Static configuration object
|
|
export const compressOperationConfig = {
|
|
toolType: ToolType.singleFile,
|
|
buildFormData: buildCompressFormData,
|
|
operationType: 'compress',
|
|
endpoint: '/api/v1/misc/compress-pdf',
|
|
filePrefix: 'compressed_',
|
|
defaultParameters,
|
|
} as const;
|
|
|
|
export const useCompressOperation = () => {
|
|
const { t } = useTranslation();
|
|
|
|
return useToolOperation<CompressParameters>({
|
|
...compressOperationConfig,
|
|
getErrorMessage: createStandardErrorHandler(t('compress.error.failed', 'An error occurred while compressing the PDF.'))
|
|
});
|
|
};
|