mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-09-18 01:19:24 +00:00
Merge branch 'V2' into feature/V2/AddStamp
This commit is contained in:
commit
4d68d3a7af
@ -1,22 +0,0 @@
|
|||||||
{
|
|
||||||
"permissions": {
|
|
||||||
"allow": [
|
|
||||||
"Bash(chmod:*)",
|
|
||||||
"Bash(mkdir:*)",
|
|
||||||
"Bash(./gradlew:*)",
|
|
||||||
"Bash(grep:*)",
|
|
||||||
"Bash(cat:*)",
|
|
||||||
"Bash(find:*)",
|
|
||||||
"Bash(npm test)",
|
|
||||||
"Bash(npm test:*)",
|
|
||||||
"Bash(ls:*)",
|
|
||||||
"Bash(npx tsc:*)",
|
|
||||||
"Bash(node:*)",
|
|
||||||
"Bash(npm run dev:*)",
|
|
||||||
"Bash(sed:*)",
|
|
||||||
"Bash(npm run typecheck:*)"
|
|
||||||
],
|
|
||||||
"deny": [],
|
|
||||||
"defaultMode": "acceptEdits"
|
|
||||||
}
|
|
||||||
}
|
|
@ -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_'
|
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -11,6 +11,8 @@ import FileEditorThumbnail from './FileEditorThumbnail';
|
|||||||
import FilePickerModal from '../shared/FilePickerModal';
|
import FilePickerModal from '../shared/FilePickerModal';
|
||||||
import SkeletonLoader from '../shared/SkeletonLoader';
|
import SkeletonLoader from '../shared/SkeletonLoader';
|
||||||
import { FileId, StirlingFile } from '../../types/fileContext';
|
import { FileId, StirlingFile } from '../../types/fileContext';
|
||||||
|
import { downloadBlob } from '../../utils/downloadUtils';
|
||||||
|
|
||||||
|
|
||||||
interface FileEditorProps {
|
interface FileEditorProps {
|
||||||
onOpenPageEditor?: () => void;
|
onOpenPageEditor?: () => void;
|
||||||
@ -278,7 +280,6 @@ const FileEditor = ({
|
|||||||
const handleDeleteFile = useCallback((fileId: FileId) => {
|
const handleDeleteFile = useCallback((fileId: FileId) => {
|
||||||
const record = activeStirlingFileStubs.find(r => r.id === fileId);
|
const record = activeStirlingFileStubs.find(r => r.id === fileId);
|
||||||
const file = record ? selectors.getFile(record.id) : null;
|
const file = record ? selectors.getFile(record.id) : null;
|
||||||
|
|
||||||
if (record && file) {
|
if (record && file) {
|
||||||
// Remove file from context but keep in storage (close, don't delete)
|
// Remove file from context but keep in storage (close, don't delete)
|
||||||
const contextFileId = record.id;
|
const contextFileId = record.id;
|
||||||
@ -290,6 +291,14 @@ const FileEditor = ({
|
|||||||
}
|
}
|
||||||
}, [activeStirlingFileStubs, selectors, removeFiles, setSelectedFiles, selectedFileIds]);
|
}, [activeStirlingFileStubs, selectors, removeFiles, setSelectedFiles, selectedFileIds]);
|
||||||
|
|
||||||
|
const handleDownloadFile = useCallback((fileId: FileId) => {
|
||||||
|
const record = activeStirlingFileStubs.find(r => r.id === fileId);
|
||||||
|
const file = record ? selectors.getFile(record.id) : null;
|
||||||
|
if (record && file) {
|
||||||
|
downloadBlob(file, file.name);
|
||||||
|
}
|
||||||
|
}, [activeStirlingFileStubs, selectors, setStatus]);
|
||||||
|
|
||||||
const handleViewFile = useCallback((fileId: FileId) => {
|
const handleViewFile = useCallback((fileId: FileId) => {
|
||||||
const record = activeStirlingFileStubs.find(r => r.id === fileId);
|
const record = activeStirlingFileStubs.find(r => r.id === fileId);
|
||||||
if (record) {
|
if (record) {
|
||||||
@ -401,6 +410,7 @@ const FileEditor = ({
|
|||||||
onViewFile={handleViewFile}
|
onViewFile={handleViewFile}
|
||||||
onSetStatus={setStatus}
|
onSetStatus={setStatus}
|
||||||
onReorderFiles={handleReorderFiles}
|
onReorderFiles={handleReorderFiles}
|
||||||
|
onDownloadFile={handleDownloadFile}
|
||||||
toolMode={toolMode}
|
toolMode={toolMode}
|
||||||
isSupported={isFileSupported(record.name)}
|
isSupported={isFileSupported(record.name)}
|
||||||
/>
|
/>
|
||||||
|
@ -13,6 +13,7 @@ import { StirlingFileStub } from '../../types/fileContext';
|
|||||||
import styles from './FileEditor.module.css';
|
import styles from './FileEditor.module.css';
|
||||||
import { useFileContext } from '../../contexts/FileContext';
|
import { useFileContext } from '../../contexts/FileContext';
|
||||||
import { FileId } from '../../types/file';
|
import { FileId } from '../../types/file';
|
||||||
|
import { formatFileSize } from '../../utils/fileUtils';
|
||||||
import ToolChain from '../shared/ToolChain';
|
import ToolChain from '../shared/ToolChain';
|
||||||
|
|
||||||
|
|
||||||
@ -28,7 +29,7 @@ interface FileEditorThumbnailProps {
|
|||||||
onViewFile: (fileId: FileId) => void;
|
onViewFile: (fileId: FileId) => void;
|
||||||
onSetStatus: (status: string) => void;
|
onSetStatus: (status: string) => void;
|
||||||
onReorderFiles?: (sourceFileId: FileId, targetFileId: FileId, selectedFileIds: FileId[]) => void;
|
onReorderFiles?: (sourceFileId: FileId, targetFileId: FileId, selectedFileIds: FileId[]) => void;
|
||||||
onDownloadFile?: (fileId: FileId) => void;
|
onDownloadFile: (fileId: FileId) => void;
|
||||||
toolMode?: boolean;
|
toolMode?: boolean;
|
||||||
isSupported?: boolean;
|
isSupported?: boolean;
|
||||||
}
|
}
|
||||||
@ -61,29 +62,6 @@ const FileEditorThumbnail = ({
|
|||||||
|
|
||||||
const pageCount = file.processedFile?.totalPages || 0;
|
const pageCount = file.processedFile?.totalPages || 0;
|
||||||
|
|
||||||
const downloadSelectedFile = useCallback(() => {
|
|
||||||
// Prefer parent-provided handler if available
|
|
||||||
if (typeof onDownloadFile === 'function') {
|
|
||||||
onDownloadFile(file.id);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fallback: attempt to download using the File object if provided
|
|
||||||
const maybeFile = (file as unknown as { file?: File }).file;
|
|
||||||
if (maybeFile instanceof File) {
|
|
||||||
const link = document.createElement('a');
|
|
||||||
link.href = URL.createObjectURL(maybeFile);
|
|
||||||
link.download = maybeFile.name || file.name || 'download';
|
|
||||||
document.body.appendChild(link);
|
|
||||||
link.click();
|
|
||||||
document.body.removeChild(link);
|
|
||||||
URL.revokeObjectURL(link.href);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we can't find a way to download, surface a status message
|
|
||||||
onSetStatus?.(typeof t === 'function' ? t('downloadUnavailable', 'Download unavailable for this item') : 'Download unavailable for this item');
|
|
||||||
}, [file, onDownloadFile, onSetStatus, t]);
|
|
||||||
const handleRef = useRef<HTMLSpanElement | null>(null);
|
const handleRef = useRef<HTMLSpanElement | null>(null);
|
||||||
|
|
||||||
// ---- Selection ----
|
// ---- Selection ----
|
||||||
@ -91,12 +69,7 @@ const FileEditorThumbnail = ({
|
|||||||
|
|
||||||
// ---- Meta formatting ----
|
// ---- Meta formatting ----
|
||||||
const prettySize = useMemo(() => {
|
const prettySize = useMemo(() => {
|
||||||
const bytes = file.size ?? 0;
|
return formatFileSize(file.size);
|
||||||
if (bytes === 0) return '0 B';
|
|
||||||
const k = 1024;
|
|
||||||
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
|
|
||||||
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
||||||
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(1))} ${sizes[i]}`;
|
|
||||||
}, [file.size]);
|
}, [file.size]);
|
||||||
|
|
||||||
const extUpper = useMemo(() => {
|
const extUpper = useMemo(() => {
|
||||||
@ -305,7 +278,7 @@ const FileEditorThumbnail = ({
|
|||||||
|
|
||||||
<button
|
<button
|
||||||
className={styles.actionRow}
|
className={styles.actionRow}
|
||||||
onClick={() => { downloadSelectedFile(); setShowActions(false); }}
|
onClick={() => { onDownloadFile(file.id); setShowActions(false); }}
|
||||||
>
|
>
|
||||||
<DownloadOutlinedIcon fontSize="small" />
|
<DownloadOutlinedIcon fontSize="small" />
|
||||||
<span>{t('download', 'Download')}</span>
|
<span>{t('download', 'Download')}</span>
|
||||||
|
@ -3,5 +3,5 @@ import { useAppConfig } from '../hooks/useAppConfig';
|
|||||||
// Get base URL from app config with fallback
|
// Get base URL from app config with fallback
|
||||||
export const getBaseUrl = (): string => {
|
export const getBaseUrl = (): string => {
|
||||||
const { config } = useAppConfig();
|
const { config } = useAppConfig();
|
||||||
return config?.baseUrl || 'https://demo.stirlingpdf.com';
|
return config?.baseUrl || 'https://stirling.com';
|
||||||
};
|
};
|
@ -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