mirror of
https://github.com/arsvendg/Stirling-PDF.git
synced 2026-07-14 10:34:06 +02:00
## default
<img width="1012" height="627"
alt="{BF57458D-50A6-4057-94F1-D6AB4628EFD8}"
src="https://github.com/user-attachments/assets/85e550ab-0aed-4341-be95-d5d3bc7146db"
/>
## disabled
<img width="1141" height="620"
alt="{140DB87B-05CF-4E0E-A14A-ED15075BD2EE}"
src="https://github.com/user-attachments/assets/e0f56e84-fb9d-4787-b5cb-ba7c5a54b1e1"
/>
## unzip options
<img width="530" height="255"
alt="{482CE185-73D5-4D90-91BB-B9305C711391}"
src="https://github.com/user-attachments/assets/609b18ee-4eae-4cee-afc1-5db01f9d1088"
/>
<img width="579" height="473"
alt="{4DFCA96D-792D-4370-8C62-4BA42C9F1A5F}"
src="https://github.com/user-attachments/assets/c67fa4af-04ef-41df-9420-65ce4247e25b"
/>
## pop up and maintains version metadata
<img width="1071" height="1220"
alt="{7F2A785C-5717-4A79-9D45-74BDA46DF273}"
src="https://github.com/user-attachments/assets/9374cd2a-b7e5-46c4-a722-e141ab42f0de"
/>
---------
Co-authored-by: Connor Yoh <[email protected]>
61 lines
2.6 KiB
TypeScript
61 lines
2.6 KiB
TypeScript
import { useCallback } from 'react';
|
|
import { useTranslation } from 'react-i18next';
|
|
import { ToolType, useToolOperation, ToolOperationConfig } from '../shared/useToolOperation';
|
|
import { createStandardErrorHandler } from '../../../utils/toolErrorHandler';
|
|
import { ScannerImageSplitParameters, defaultParameters } from './useScannerImageSplitParameters';
|
|
import { useToolResources } from '../shared/useToolResources';
|
|
|
|
export const buildScannerImageSplitFormData = (parameters: ScannerImageSplitParameters, file: File): FormData => {
|
|
const formData = new FormData();
|
|
formData.append('fileInput', file);
|
|
formData.append('angle_threshold', parameters.angle_threshold.toString());
|
|
formData.append('tolerance', parameters.tolerance.toString());
|
|
formData.append('min_area', parameters.min_area.toString());
|
|
formData.append('min_contour_area', parameters.min_contour_area.toString());
|
|
formData.append('border_size', parameters.border_size.toString());
|
|
return formData;
|
|
};
|
|
|
|
// Static configuration object
|
|
export const scannerImageSplitOperationConfig = {
|
|
toolType: ToolType.singleFile,
|
|
buildFormData: buildScannerImageSplitFormData,
|
|
operationType: 'scannerImageSplit',
|
|
endpoint: '/api/v1/misc/extract-image-scans',
|
|
defaultParameters,
|
|
} as const;
|
|
|
|
export const useScannerImageSplitOperation = () => {
|
|
const { t } = useTranslation();
|
|
const { extractAllZipFiles } = useToolResources();
|
|
|
|
// Custom response handler that extracts ZIP files containing images
|
|
// Can't add to exported config because it requires access to the hook so must be part of the hook
|
|
const responseHandler = useCallback(async (blob: Blob, originalFiles: File[]): Promise<File[]> => {
|
|
try {
|
|
// Scanner image split returns ZIP files with multiple images
|
|
const extractedFiles = await extractAllZipFiles(blob);
|
|
|
|
// If extraction succeeded and returned files, use them
|
|
if (extractedFiles.length > 0) {
|
|
return extractedFiles;
|
|
}
|
|
} catch (error) {
|
|
console.warn('Failed to extract as ZIP, treating as single file:', error);
|
|
}
|
|
|
|
// Fallback: treat as single file (PNG image)
|
|
const inputFileName = originalFiles[0]?.name || 'document';
|
|
const baseFileName = inputFileName.replace(/\.[^.]+$/, '');
|
|
const singleFile = new File([blob], `${baseFileName}.png`, { type: 'image/png' });
|
|
return [singleFile];
|
|
}, [extractAllZipFiles]);
|
|
|
|
const config: ToolOperationConfig<ScannerImageSplitParameters> = {
|
|
...scannerImageSplitOperationConfig,
|
|
responseHandler,
|
|
getErrorMessage: createStandardErrorHandler(t('scannerImageSplit.error.failed', 'An error occurred while extracting image scans.'))
|
|
};
|
|
|
|
return useToolOperation(config);
|
|
}; |