Stirling-PDF/frontend/src/hooks/tools/convert/useConvertParameters.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

203 lines
6.2 KiB
TypeScript
Raw Normal View History

2025-07-30 14:24:23 +01:00
import { useState, useEffect } from 'react';
2025-07-23 11:25:55 +01:00
import {
COLOR_TYPES,
OUTPUT_OPTIONS,
2025-07-30 14:24:23 +01:00
FIT_OPTIONS,
2025-07-23 11:25:55 +01:00
TO_FORMAT_OPTIONS,
CONVERSION_MATRIX,
type ColorType,
2025-07-30 14:24:23 +01:00
type OutputOption,
type FitOption
2025-07-23 11:25:55 +01:00
} from '../../../constants/convertConstants';
2025-07-30 14:24:23 +01:00
import { getEndpointName as getEndpointNameUtil, getEndpointUrl, isImageFormat } from '../../../utils/convertUtils';
2025-07-23 11:25:55 +01:00
export interface ConvertParameters {
fromExtension: string;
toExtension: string;
imageOptions: {
colorType: ColorType;
dpi: number;
singleOrMultiple: OutputOption;
2025-07-30 14:24:23 +01:00
fitOption: FitOption;
autoRotate: boolean;
combineImages: boolean;
2025-07-23 11:25:55 +01:00
};
2025-07-30 14:24:23 +01:00
isSmartDetection: boolean;
smartDetectionType: 'mixed' | 'images' | 'none';
2025-07-23 11:25:55 +01:00
}
export interface ConvertParametersHook {
parameters: ConvertParameters;
updateParameter: (parameter: keyof ConvertParameters, value: any) => void;
resetParameters: () => void;
validateParameters: () => boolean;
getEndpointName: () => string;
getEndpoint: () => string;
getAvailableToExtensions: (fromExtension: string) => Array<{value: string, label: string, group: string}>;
detectFileExtension: (filename: string) => string;
2025-07-30 14:24:23 +01:00
analyzeFileTypes: (files: Array<{name: string}>) => void;
2025-07-23 11:25:55 +01:00
}
const initialParameters: ConvertParameters = {
fromExtension: '',
toExtension: '',
imageOptions: {
colorType: COLOR_TYPES.COLOR,
dpi: 300,
singleOrMultiple: OUTPUT_OPTIONS.MULTIPLE,
2025-07-30 14:24:23 +01:00
fitOption: FIT_OPTIONS.MAINTAIN_ASPECT,
autoRotate: true,
combineImages: true,
2025-07-23 11:25:55 +01:00
},
2025-07-30 14:24:23 +01:00
isSmartDetection: false,
smartDetectionType: 'none',
2025-07-23 11:25:55 +01:00
};
export const useConvertParameters = (): ConvertParametersHook => {
const [parameters, setParameters] = useState<ConvertParameters>(initialParameters);
const updateParameter = (parameter: keyof ConvertParameters, value: any) => {
setParameters(prev => ({ ...prev, [parameter]: value }));
};
const resetParameters = () => {
setParameters(initialParameters);
};
const validateParameters = () => {
const { fromExtension, toExtension } = parameters;
if (!fromExtension || !toExtension) return false;
// Check if conversion is supported
const supportedToExtensions = CONVERSION_MATRIX[fromExtension];
if (!supportedToExtensions || !supportedToExtensions.includes(toExtension)) {
return false;
}
// Additional validation for image conversions
if (['png', 'jpg'].includes(toExtension)) {
return parameters.imageOptions.dpi >= 72 && parameters.imageOptions.dpi <= 600;
}
return true;
};
const getEndpointName = () => {
2025-07-30 14:24:23 +01:00
const { fromExtension, toExtension, isSmartDetection, smartDetectionType } = parameters;
if (isSmartDetection) {
if (smartDetectionType === 'mixed') {
// Mixed file types -> PDF using file-to-pdf endpoint
return 'file-to-pdf';
} else if (smartDetectionType === 'images') {
// All images -> PDF using img-to-pdf endpoint
return 'img-to-pdf';
}
}
2025-07-23 17:23:25 +01:00
return getEndpointNameUtil(fromExtension, toExtension);
2025-07-23 11:25:55 +01:00
};
const getEndpoint = () => {
2025-07-30 14:24:23 +01:00
const { fromExtension, toExtension, isSmartDetection, smartDetectionType } = parameters;
if (isSmartDetection) {
if (smartDetectionType === 'mixed') {
// Mixed file types -> PDF using file-to-pdf endpoint
return '/api/v1/convert/file/pdf';
} else if (smartDetectionType === 'images') {
// All images -> PDF using img-to-pdf endpoint
return '/api/v1/convert/img/pdf';
}
}
2025-07-23 17:23:25 +01:00
return getEndpointUrl(fromExtension, toExtension);
2025-07-23 11:25:55 +01:00
};
const getAvailableToExtensions = (fromExtension: string) => {
if (!fromExtension) return [];
const supportedExtensions = CONVERSION_MATRIX[fromExtension] || [];
return TO_FORMAT_OPTIONS.filter(option =>
supportedExtensions.includes(option.value)
);
};
const detectFileExtension = (filename: string): string => {
const extension = filename.split('.').pop()?.toLowerCase();
return extension || '';
};
2025-07-30 14:24:23 +01:00
const analyzeFileTypes = (files: Array<{name: string}>) => {
if (files.length <= 1) {
// Single file or no files - use regular detection with auto-target selection
const fromExt = files.length === 1 ? detectFileExtension(files[0].name) : '';
const availableTargets = fromExt ? CONVERSION_MATRIX[fromExt] || [] : [];
const autoTarget = availableTargets.length === 1 ? availableTargets[0] : '';
setParameters(prev => ({
...prev,
isSmartDetection: false,
smartDetectionType: 'none',
fromExtension: fromExt,
toExtension: autoTarget
}));
return;
}
// Multiple files - analyze file types
const extensions = files.map(file => detectFileExtension(file.name));
const uniqueExtensions = [...new Set(extensions)];
if (uniqueExtensions.length === 1) {
// All files are the same type - use regular detection with auto-target selection
const fromExt = uniqueExtensions[0];
const availableTargets = CONVERSION_MATRIX[fromExt] || [];
const autoTarget = availableTargets.length === 1 ? availableTargets[0] : '';
setParameters(prev => ({
...prev,
isSmartDetection: false,
smartDetectionType: 'none',
fromExtension: fromExt,
toExtension: autoTarget
}));
} else {
// Mixed file types
const allImages = uniqueExtensions.every(ext => isImageFormat(ext));
if (allImages) {
// All files are images - use image-to-pdf conversion
setParameters(prev => ({
...prev,
isSmartDetection: true,
smartDetectionType: 'images',
fromExtension: 'image',
toExtension: 'pdf'
}));
} else {
// Mixed non-image types - use file-to-pdf conversion
setParameters(prev => ({
...prev,
isSmartDetection: true,
smartDetectionType: 'mixed',
fromExtension: 'any',
toExtension: 'pdf'
}));
}
}
};
2025-07-23 11:25:55 +01:00
return {
parameters,
updateParameter,
resetParameters,
validateParameters,
getEndpointName,
getEndpoint,
getAvailableToExtensions,
detectFileExtension,
2025-07-30 14:24:23 +01:00
analyzeFileTypes,
2025-07-23 11:25:55 +01:00
};
};