mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2025-09-03 17:52:30 +02:00
Fix Split to be a single file interface (#4327)
# Description of Changes Split was previously incorrectly marked as a multi-file interface, which meant that if you fed 2 files into it, it'd just process the first and discard the second. This PR changes it to a single-file interface, and implements a custom response handler because Split returns Zip files instead of PDFs, so the response you get when running Split now is the union of all of the split input files in the workbench (or them all zipped if you download it).
This commit is contained in:
parent
2f2f966ee9
commit
6dbaff5342
@ -1,16 +1,16 @@
|
||||
import { useCallback } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { ToolType, useToolOperation } from '../shared/useToolOperation';
|
||||
import { ToolType, useToolOperation, ToolOperationConfig } from '../shared/useToolOperation';
|
||||
import { createStandardErrorHandler } from '../../../utils/toolErrorHandler';
|
||||
import { SplitParameters, defaultParameters } from './useSplitParameters';
|
||||
import { SPLIT_MODES } from '../../../constants/splitConstants';
|
||||
import { useToolResources } from '../shared/useToolResources';
|
||||
|
||||
// Static functions that can be used by both the hook and automation executor
|
||||
export const buildSplitFormData = (parameters: SplitParameters, selectedFiles: File[]): FormData => {
|
||||
export const buildSplitFormData = (parameters: SplitParameters, file: File): FormData => {
|
||||
const formData = new FormData();
|
||||
|
||||
selectedFiles.forEach(file => {
|
||||
formData.append("fileInput", file);
|
||||
});
|
||||
formData.append("fileInput", file);
|
||||
|
||||
switch (parameters.mode) {
|
||||
case SPLIT_MODES.BY_PAGES:
|
||||
@ -57,7 +57,7 @@ export const getSplitEndpoint = (parameters: SplitParameters): string => {
|
||||
|
||||
// Static configuration object
|
||||
export const splitOperationConfig = {
|
||||
toolType: ToolType.multiFile,
|
||||
toolType: ToolType.singleFile,
|
||||
buildFormData: buildSplitFormData,
|
||||
operationType: 'splitPdf',
|
||||
endpoint: getSplitEndpoint,
|
||||
@ -67,9 +67,20 @@ export const splitOperationConfig = {
|
||||
|
||||
export const useSplitOperation = () => {
|
||||
const { t } = useTranslation();
|
||||
const { extractZipFiles } = useToolResources();
|
||||
|
||||
return useToolOperation<SplitParameters>({
|
||||
// Custom response handler that extracts ZIP files
|
||||
// Can't add to exported config because it requires access to the zip code so must be part of the hook
|
||||
const responseHandler = useCallback(async (blob: Blob, originalFiles: File[]): Promise<File[]> => {
|
||||
// Split operations return ZIP files with multiple PDF pages
|
||||
return await extractZipFiles(blob);
|
||||
}, [extractZipFiles]);
|
||||
|
||||
const splitConfig: ToolOperationConfig<SplitParameters> = {
|
||||
...splitOperationConfig,
|
||||
responseHandler,
|
||||
getErrorMessage: createStandardErrorHandler(t('split.error.failed', 'An error occurred while splitting the PDF.'))
|
||||
});
|
||||
};
|
||||
|
||||
return useToolOperation(splitConfig);
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user