mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-09-18 01:19:24 +00:00
Remove custom response handler from Merge (#4457)
# Description of Changes Remove custom response handler from Merge. Also make `filePrefix` mandatory for `multiFile` tools to make the output more visually different since you get a new 'V1' file rather than 'V2' of the current file.
This commit is contained in:
parent
b51c2e42a6
commit
7ff1c66d09
@ -89,7 +89,6 @@ return useToolOperation({
|
|||||||
endpoint: '/api/v1/misc/compress-pdf',
|
endpoint: '/api/v1/misc/compress-pdf',
|
||||||
buildFormData: (params, file: File) => { /* single file */ },
|
buildFormData: (params, file: File) => { /* single file */ },
|
||||||
multiFileEndpoint: false,
|
multiFileEndpoint: false,
|
||||||
filePrefix: 'compressed_'
|
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -103,7 +102,7 @@ return useToolOperation({
|
|||||||
endpoint: '/api/v1/general/split-pages',
|
endpoint: '/api/v1/general/split-pages',
|
||||||
buildFormData: (params, files: File[]) => { /* all files */ },
|
buildFormData: (params, files: File[]) => { /* all files */ },
|
||||||
multiFileEndpoint: true,
|
multiFileEndpoint: true,
|
||||||
filePrefix: 'split_'
|
filePrefix: 'split_',
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -115,7 +114,6 @@ return useToolOperation({
|
|||||||
return useToolOperation({
|
return useToolOperation({
|
||||||
operationType: 'convert',
|
operationType: 'convert',
|
||||||
customProcessor: async (params, files) => { /* custom logic */ },
|
customProcessor: async (params, files) => { /* custom logic */ },
|
||||||
filePrefix: 'converted_'
|
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -81,24 +81,6 @@ describe('useMergeOperation', () => {
|
|||||||
expect(formData.get('generateToc')).toBe('false');
|
expect(formData.get('generateToc')).toBe('false');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should handle response correctly', () => {
|
|
||||||
renderHook(() => useMergeOperation());
|
|
||||||
|
|
||||||
const config = getToolConfig();
|
|
||||||
const mockBlob = new Blob(['merged content'], { type: 'application/pdf' });
|
|
||||||
const mockFiles = [
|
|
||||||
new File(['content1'], 'file1.pdf', { type: 'application/pdf' }),
|
|
||||||
new File(['content2'], 'file2.pdf', { type: 'application/pdf' })
|
|
||||||
];
|
|
||||||
|
|
||||||
const result = config.responseHandler!(mockBlob, mockFiles) as File[];
|
|
||||||
|
|
||||||
expect(result).toHaveLength(1);
|
|
||||||
expect(result[0].name).toBe('merged_file1.pdf');
|
|
||||||
expect(result[0].type).toBe('application/pdf');
|
|
||||||
expect(result[0].size).toBe(mockBlob.size);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('should return the hook result from useToolOperation', () => {
|
test('should return the hook result from useToolOperation', () => {
|
||||||
const { result } = renderHook(() => useMergeOperation());
|
const { result } = renderHook(() => useMergeOperation());
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { useToolOperation, ResponseHandler, ToolOperationConfig, ToolType } from '../shared/useToolOperation';
|
import { useToolOperation, ToolOperationConfig, ToolType } from '../shared/useToolOperation';
|
||||||
import { createStandardErrorHandler } from '../../../utils/toolErrorHandler';
|
import { createStandardErrorHandler } from '../../../utils/toolErrorHandler';
|
||||||
import { MergeParameters } from './useMergeParameters';
|
import { MergeParameters } from './useMergeParameters';
|
||||||
|
|
||||||
@ -16,11 +16,6 @@ const buildFormData = (parameters: MergeParameters, files: File[]): FormData =>
|
|||||||
return formData;
|
return formData;
|
||||||
};
|
};
|
||||||
|
|
||||||
const mergeResponseHandler: ResponseHandler = (blob: Blob, originalFiles: File[]): File[] => {
|
|
||||||
const filename = `merged_${originalFiles[0].name}`
|
|
||||||
return [new File([blob], filename, { type: 'application/pdf' })];
|
|
||||||
};
|
|
||||||
|
|
||||||
// Operation configuration for automation
|
// Operation configuration for automation
|
||||||
export const mergeOperationConfig: ToolOperationConfig<MergeParameters> = {
|
export const mergeOperationConfig: ToolOperationConfig<MergeParameters> = {
|
||||||
toolType: ToolType.multiFile,
|
toolType: ToolType.multiFile,
|
||||||
@ -28,7 +23,6 @@ export const mergeOperationConfig: ToolOperationConfig<MergeParameters> = {
|
|||||||
operationType: 'merge',
|
operationType: 'merge',
|
||||||
endpoint: '/api/v1/general/merge-pdfs',
|
endpoint: '/api/v1/general/merge-pdfs',
|
||||||
filePrefix: 'merged_',
|
filePrefix: 'merged_',
|
||||||
responseHandler: mergeResponseHandler,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useMergeOperation = () => {
|
export const useMergeOperation = () => {
|
||||||
|
@ -31,7 +31,10 @@ interface BaseToolOperationConfig<TParams> {
|
|||||||
/** Operation identifier for tracking and logging */
|
/** Operation identifier for tracking and logging */
|
||||||
operationType: string;
|
operationType: string;
|
||||||
|
|
||||||
/** Prefix added to processed filenames (e.g., 'compressed_', 'split_') */
|
/**
|
||||||
|
* Prefix added to processed filenames (e.g., 'compressed_', 'split_').
|
||||||
|
* Only generally useful for multiFile interfaces.
|
||||||
|
*/
|
||||||
filePrefix?: string;
|
filePrefix?: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -68,6 +71,9 @@ export interface MultiFileToolOperationConfig<TParams> extends BaseToolOperation
|
|||||||
/** This tool processes multiple files at once. */
|
/** This tool processes multiple files at once. */
|
||||||
toolType: ToolType.multiFile;
|
toolType: ToolType.multiFile;
|
||||||
|
|
||||||
|
/** Prefix added to processed filename (e.g., 'merged_', 'split_') */
|
||||||
|
filePrefix: string;
|
||||||
|
|
||||||
/** Builds FormData for API request. */
|
/** Builds FormData for API request. */
|
||||||
buildFormData: ((params: TParams, files: File[]) => FormData);
|
buildFormData: ((params: TParams, files: File[]) => FormData);
|
||||||
|
|
||||||
@ -214,9 +220,9 @@ export const useToolOperation = <TParams>(
|
|||||||
processedFiles = await config.responseHandler(response.data, filesForAPI);
|
processedFiles = await config.responseHandler(response.data, filesForAPI);
|
||||||
} else if (response.data.type === 'application/pdf' ||
|
} else if (response.data.type === 'application/pdf' ||
|
||||||
(response.headers && response.headers['content-type'] === 'application/pdf')) {
|
(response.headers && response.headers['content-type'] === 'application/pdf')) {
|
||||||
// Single PDF response (e.g. split with merge option) - use original filename
|
// Single PDF response (e.g. split with merge option) - add prefix to first original filename
|
||||||
const originalFileName = filesForAPI[0]?.name || 'document.pdf';
|
const filename = `${config.filePrefix}${filesForAPI[0]?.name || 'document.pdf'}`;
|
||||||
const singleFile = new File([response.data], originalFileName, { type: 'application/pdf' });
|
const singleFile = new File([response.data], filename, { type: 'application/pdf' });
|
||||||
processedFiles = [singleFile];
|
processedFiles = [singleFile];
|
||||||
} else {
|
} else {
|
||||||
// Default: assume ZIP response for multi-file endpoints
|
// Default: assume ZIP response for multi-file endpoints
|
||||||
|
Loading…
x
Reference in New Issue
Block a user