Compare commits

...

6 Commits

Author SHA1 Message Date
James Brunton
668c47d5a0 Tidy 2025-09-12 12:17:58 +01:00
James Brunton
6a1caf0904 Properly type PDFs 2025-09-12 12:14:54 +01:00
James Brunton
e31e6461e4 Tidy 2025-09-12 11:34:45 +01:00
James Brunton
cef61d3733 Fix type errors 2025-09-12 09:49:20 +01:00
James Brunton
965ad354a8 Fix resetting 2025-09-12 09:41:12 +01:00
James Brunton
52885ec939 Move Custom Metadata into advanced 2025-09-12 09:10:15 +01:00
8 changed files with 211 additions and 204 deletions

View File

@ -5,7 +5,6 @@ import { useMetadataExtraction } from "../../../hooks/tools/changeMetadata/useMe
import DeleteAllStep from "./steps/DeleteAllStep"; import DeleteAllStep from "./steps/DeleteAllStep";
import StandardMetadataStep from "./steps/StandardMetadataStep"; import StandardMetadataStep from "./steps/StandardMetadataStep";
import DocumentDatesStep from "./steps/DocumentDatesStep"; import DocumentDatesStep from "./steps/DocumentDatesStep";
import CustomMetadataStep from "./steps/CustomMetadataStep";
import AdvancedOptionsStep from "./steps/AdvancedOptionsStep"; import AdvancedOptionsStep from "./steps/AdvancedOptionsStep";
interface ChangeMetadataSingleStepProps { interface ChangeMetadataSingleStepProps {
@ -27,17 +26,10 @@ const ChangeMetadataSingleStep = ({
}: ChangeMetadataSingleStepProps) => { }: ChangeMetadataSingleStepProps) => {
const { t } = useTranslation(); const { t } = useTranslation();
// Create a params object that matches the hook interface
const paramsHook = {
parameters,
updateParameter: onParameterChange,
addCustomMetadata,
removeCustomMetadata,
updateCustomMetadata,
};
// Extract metadata from uploaded files // Extract metadata from uploaded files
const { isExtractingMetadata } = useMetadataExtraction(paramsHook); const { isExtractingMetadata } = useMetadataExtraction({
updateParameter: onParameterChange,
});
const isDeleteAllEnabled = parameters.deleteAll; const isDeleteAllEnabled = parameters.deleteAll;
const fieldsDisabled = disabled || isDeleteAllEnabled || isExtractingMetadata; const fieldsDisabled = disabled || isDeleteAllEnabled || isExtractingMetadata;
@ -86,23 +78,6 @@ const ChangeMetadataSingleStep = ({
<Divider /> <Divider />
{/* Custom Metadata */}
<Stack gap="md">
<Text size="sm" fw={500}>
{t('changeMetadata.customFields.title', 'Custom Metadata')}
</Text>
<CustomMetadataStep
parameters={parameters}
onParameterChange={onParameterChange}
disabled={fieldsDisabled}
addCustomMetadata={addCustomMetadata}
removeCustomMetadata={removeCustomMetadata}
updateCustomMetadata={updateCustomMetadata}
/>
</Stack>
<Divider />
{/* Advanced Options */} {/* Advanced Options */}
<Stack gap="md"> <Stack gap="md">
<Text size="sm" fw={500}> <Text size="sm" fw={500}>
@ -112,6 +87,9 @@ const ChangeMetadataSingleStep = ({
parameters={parameters} parameters={parameters}
onParameterChange={onParameterChange} onParameterChange={onParameterChange}
disabled={fieldsDisabled} disabled={fieldsDisabled}
addCustomMetadata={addCustomMetadata}
removeCustomMetadata={removeCustomMetadata}
updateCustomMetadata={updateCustomMetadata}
/> />
</Stack> </Stack>
</Stack> </Stack>

View File

@ -1,22 +1,31 @@
import { Select } from "@mantine/core"; import { Stack, Select, Divider } from "@mantine/core";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { ChangeMetadataParameters } from "../../../../hooks/tools/changeMetadata/useChangeMetadataParameters"; import { ChangeMetadataParameters } from "../../../../hooks/tools/changeMetadata/useChangeMetadataParameters";
import { TrappedStatus } from "../../../../types/metadata"; import { TrappedStatus } from "../../../../types/metadata";
import CustomMetadataStep from "./CustomMetadataStep";
interface AdvancedOptionsStepProps { interface AdvancedOptionsStepProps {
parameters: ChangeMetadataParameters; parameters: ChangeMetadataParameters;
onParameterChange: <K extends keyof ChangeMetadataParameters>(key: K, value: ChangeMetadataParameters[K]) => void; onParameterChange: <K extends keyof ChangeMetadataParameters>(key: K, value: ChangeMetadataParameters[K]) => void;
disabled?: boolean; disabled?: boolean;
addCustomMetadata: (key?: string, value?: string) => void;
removeCustomMetadata: (id: string) => void;
updateCustomMetadata: (id: string, key: string, value: string) => void;
} }
const AdvancedOptionsStep = ({ const AdvancedOptionsStep = ({
parameters, parameters,
onParameterChange, onParameterChange,
disabled = false disabled = false,
addCustomMetadata,
removeCustomMetadata,
updateCustomMetadata
}: AdvancedOptionsStepProps) => { }: AdvancedOptionsStepProps) => {
const { t } = useTranslation(); const { t } = useTranslation();
return ( return (
<Stack gap="md">
{/* Trapped Status */}
<Select <Select
label={t('changeMetadata.trapped.label', 'Trapped Status')} label={t('changeMetadata.trapped.label', 'Trapped Status')}
description={t('changeMetadata.trapped.description', 'Indicates whether the document has been trapped for high-quality printing')} description={t('changeMetadata.trapped.description', 'Indicates whether the document has been trapped for high-quality printing')}
@ -33,6 +42,19 @@ const AdvancedOptionsStep = ({
{ value: TrappedStatus.FALSE, label: t('changeMetadata.trapped.false', 'False') } { value: TrappedStatus.FALSE, label: t('changeMetadata.trapped.false', 'False') }
]} ]}
/> />
<Divider />
{/* Custom Metadata */}
<CustomMetadataStep
parameters={parameters}
onParameterChange={onParameterChange}
disabled={disabled}
addCustomMetadata={addCustomMetadata}
removeCustomMetadata={removeCustomMetadata}
updateCustomMetadata={updateCustomMetadata}
/>
</Stack>
); );
}; };

View File

@ -98,6 +98,15 @@ export const useAdvancedOptionsTips = (): TooltipContent => {
t("changeMetadata.tooltip.advanced.trapped.bullet2", "False: Document has not been trapped"), t("changeMetadata.tooltip.advanced.trapped.bullet2", "False: Document has not been trapped"),
t("changeMetadata.tooltip.advanced.trapped.bullet3", "Unknown: Trapped status is not specified") t("changeMetadata.tooltip.advanced.trapped.bullet3", "Unknown: Trapped status is not specified")
] ]
},
{
title: t("changeMetadata.tooltip.customFields.title", "Custom Metadata"),
description: t("changeMetadata.tooltip.customFields.text", "Add your own custom key-value metadata pairs."),
bullets: [
t("changeMetadata.tooltip.customFields.bullet1", "Add any custom fields relevant to your document"),
t("changeMetadata.tooltip.customFields.bullet2", "Examples: Department, Project, Version, Status"),
t("changeMetadata.tooltip.customFields.bullet3", "Both key and value are required for each entry")
]
} }
] ]
}; };

View File

@ -1,28 +1,41 @@
import { useState, useEffect } from "react"; import { useState, useEffect, useRef } from "react";
import { PDFMetadataService } from "../../../services/pdfMetadataService"; import { extractPDFMetadata } from "../../../services/pdfMetadataService";
import { useSelectedFiles } from "../../../contexts/file/fileHooks"; import { useSelectedFiles } from "../../../contexts/file/fileHooks";
import { ChangeMetadataParametersHook } from "./useChangeMetadataParameters"; import { ChangeMetadataParameters } from "./useChangeMetadataParameters";
export const useMetadataExtraction = (params: ChangeMetadataParametersHook) => { interface MetadataExtractionParams {
updateParameter: <K extends keyof ChangeMetadataParameters>(key: K, value: ChangeMetadataParameters[K]) => void;
}
export const useMetadataExtraction = (params: MetadataExtractionParams) => {
const { selectedFiles } = useSelectedFiles(); const { selectedFiles } = useSelectedFiles();
const [isExtractingMetadata, setIsExtractingMetadata] = useState(false); const [isExtractingMetadata, setIsExtractingMetadata] = useState(false);
const [hasExtractedMetadata, setHasExtractedMetadata] = useState(false); const [hasExtractedMetadata, setHasExtractedMetadata] = useState(false);
const previousFileCountRef = useRef(0);
// Reset extraction state only when files are cleared (length goes to 0)
useEffect(() => {
if (previousFileCountRef.current > 0 && selectedFiles.length === 0) {
setHasExtractedMetadata(false);
}
previousFileCountRef.current = selectedFiles.length;
}, [selectedFiles]);
// Extract metadata from first file when files change // Extract metadata from first file when files change
useEffect(() => { useEffect(() => {
const extractMetadata = async () => { const extractMetadata = async () => {
if (selectedFiles.length === 0 || hasExtractedMetadata) { if (selectedFiles.length === 0) {
return; return;
} }
const firstFile = selectedFiles[0]; const firstFile = selectedFiles[0];
if (!firstFile) {
if (hasExtractedMetadata) {
return; return;
} }
setIsExtractingMetadata(true); setIsExtractingMetadata(true);
try {
const result = await PDFMetadataService.extractMetadata(firstFile); const result = await extractPDFMetadata(firstFile);
if (result.success) { if (result.success) {
const metadata = result.metadata; const metadata = result.metadata;
@ -37,17 +50,14 @@ export const useMetadataExtraction = (params: ChangeMetadataParametersHook) => {
params.updateParameter('creationDate', metadata.creationDate); params.updateParameter('creationDate', metadata.creationDate);
params.updateParameter('modificationDate', metadata.modificationDate); params.updateParameter('modificationDate', metadata.modificationDate);
params.updateParameter('trapped', metadata.trapped); params.updateParameter('trapped', metadata.trapped);
// Set custom metadata entries directly to avoid state update timing issues
params.updateParameter('customMetadata', metadata.customMetadata); params.updateParameter('customMetadata', metadata.customMetadata);
setHasExtractedMetadata(true); setHasExtractedMetadata(true);
} else {
console.warn('Failed to extract metadata:', result.error);
} }
} catch (error) {
console.warn('Failed to extract metadata:', error);
} finally {
setIsExtractingMetadata(false); setIsExtractingMetadata(false);
}
}; };
extractMetadata(); extractMetadata();

View File

@ -1,6 +1,7 @@
import { pdfWorkerManager } from './pdfWorkerManager'; import { pdfWorkerManager } from './pdfWorkerManager';
import { FileAnalyzer } from './fileAnalyzer'; import { FileAnalyzer } from './fileAnalyzer';
import { TrappedStatus, CustomMetadataEntry, ExtractedPDFMetadata } from '../types/metadata'; import { TrappedStatus, CustomMetadataEntry, ExtractedPDFMetadata } from '../types/metadata';
import { PDFDocumentProxy } from 'pdfjs-dist/types/src/display/api';
export interface MetadataExtractionResult { export interface MetadataExtractionResult {
success: true; success: true;
@ -18,12 +19,11 @@ export type MetadataExtractionResponse = MetadataExtractionResult | MetadataExtr
* Utility to format PDF date strings to required format (yyyy/MM/dd HH:mm:ss) * Utility to format PDF date strings to required format (yyyy/MM/dd HH:mm:ss)
* Handles PDF date format: "D:YYYYMMDDHHmmSSOHH'mm'" or standard date strings * Handles PDF date format: "D:YYYYMMDDHHmmSSOHH'mm'" or standard date strings
*/ */
function formatPDFDate(dateString: unknown): string { function formatPDFDate(dateString: string): string {
if (!dateString || typeof dateString !== 'string') { if (!dateString) {
return ''; return '';
} }
try {
let date: Date; let date: Date;
// Check if it's a PDF date format (starts with "D:") // Check if it's a PDF date format (starts with "D:")
@ -58,9 +58,6 @@ function formatPDFDate(dateString: unknown): string {
const seconds = String(date.getSeconds()).padStart(2, '0'); const seconds = String(date.getSeconds()).padStart(2, '0');
return `${year}/${month}/${day} ${hours}:${minutes}:${seconds}`; return `${year}/${month}/${day} ${hours}:${minutes}:${seconds}`;
} catch {
return '';
}
} }
/** /**
@ -80,14 +77,14 @@ function convertTrappedStatus(trapped: unknown): TrappedStatus {
* Extract custom metadata fields from PDF.js info object * Extract custom metadata fields from PDF.js info object
* Custom metadata is nested under the "Custom" key * Custom metadata is nested under the "Custom" key
*/ */
function extractCustomMetadata(info: Record<string, unknown>): CustomMetadataEntry[] { function extractCustomMetadata(custom: unknown): CustomMetadataEntry[] {
const customMetadata: CustomMetadataEntry[] = []; const customMetadata: CustomMetadataEntry[] = [];
let customIdCounter = 1; let customIdCounter = 1;
// Check if there's a Custom object containing the custom metadata // Check if there's a Custom object containing the custom metadata
if (info.Custom && typeof info.Custom === 'object' && info.Custom !== null) { if (typeof custom === 'object' && custom !== null) {
const customObj = info.Custom as Record<string, unknown>; const customObj = custom as Record<string, unknown>;
Object.entries(customObj).forEach(([key, value]) => { Object.entries(customObj).forEach(([key, value]) => {
if (value != null && value !== '') { if (value != null && value !== '') {
@ -105,14 +102,31 @@ function extractCustomMetadata(info: Record<string, unknown>): CustomMetadataEnt
} }
/** /**
* Service to extract metadata from PDF files using PDF.js * Safely cleanup PDF document with error handling
*/ */
export class PDFMetadataService { function cleanupPdfDocument(pdfDoc: PDFDocumentProxy | null): void {
if (pdfDoc) {
try {
pdfWorkerManager.destroyDocument(pdfDoc);
} catch (cleanupError) {
console.warn('Failed to cleanup PDF document:', cleanupError);
}
}
}
function getStringMetadata(info: Record<string, unknown>, key: string): string {
if (typeof info[key] === 'string') {
return info[key];
} else {
return '';
}
}
/** /**
* Extract all metadata from a PDF file * Extract all metadata from a PDF file
* Returns a result object with success/error state * Returns a result object with success/error state
*/ */
static async extractMetadata(file: File): Promise<MetadataExtractionResponse> { export async function extractPDFMetadata(file: File): Promise<MetadataExtractionResponse> {
// Use existing PDF validation // Use existing PDF validation
const isValidPDF = await FileAnalyzer.isValidPDF(file); const isValidPDF = await FileAnalyzer.isValidPDF(file);
if (!isValidPDF) { if (!isValidPDF) {
@ -122,54 +136,46 @@ export class PDFMetadataService {
}; };
} }
let pdfDoc: any = null; let pdfDoc: PDFDocumentProxy | null = null;
let arrayBuffer: ArrayBuffer;
let metadata;
try { try {
const arrayBuffer = await file.arrayBuffer(); arrayBuffer = await file.arrayBuffer();
pdfDoc = await pdfWorkerManager.createDocument(arrayBuffer, { pdfDoc = await pdfWorkerManager.createDocument(arrayBuffer, {
disableAutoFetch: true, disableAutoFetch: true,
disableStream: true disableStream: true
}); });
metadata = await pdfDoc.getMetadata();
} catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
cleanupPdfDocument(pdfDoc);
return {
success: false,
error: `Failed to read PDF: ${errorMessage}`
};
}
const metadata = await pdfDoc.getMetadata(); const info = metadata.info as Record<string, unknown>;
const info = metadata.info || {};
// Safely extract metadata with proper type checking // Safely extract metadata with proper type checking
const extractedMetadata: ExtractedPDFMetadata = { const extractedMetadata: ExtractedPDFMetadata = {
title: typeof info.Title === 'string' ? info.Title : '', title: getStringMetadata(info, 'Title'),
author: typeof info.Author === 'string' ? info.Author : '', author: getStringMetadata(info, 'Author'),
subject: typeof info.Subject === 'string' ? info.Subject : '', subject: getStringMetadata(info, 'Subject'),
keywords: typeof info.Keywords === 'string' ? info.Keywords : '', keywords: getStringMetadata(info, 'Keywords'),
creator: typeof info.Creator === 'string' ? info.Creator : '', creator: getStringMetadata(info, 'Creator'),
producer: typeof info.Producer === 'string' ? info.Producer : '', producer: getStringMetadata(info, 'Producer'),
creationDate: formatPDFDate(info.CreationDate), creationDate: formatPDFDate(getStringMetadata(info, 'CreationDate')),
modificationDate: formatPDFDate(info.ModDate), modificationDate: formatPDFDate(getStringMetadata(info, 'ModDate')),
trapped: convertTrappedStatus(info.Trapped), trapped: convertTrappedStatus(info.Trapped),
customMetadata: extractCustomMetadata(info) customMetadata: extractCustomMetadata(info.Custom),
}; };
cleanupPdfDocument(pdfDoc);
return { return {
success: true, success: true,
metadata: extractedMetadata metadata: extractedMetadata
}; };
} catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
return {
success: false,
error: `Failed to extract PDF metadata: ${errorMessage}`
};
} finally {
// Ensure cleanup even if extraction fails
if (pdfDoc) {
try {
pdfWorkerManager.destroyDocument(pdfDoc);
} catch (cleanupError) {
console.warn('Failed to cleanup PDF document:', cleanupError);
}
}
}
}
} }

View File

@ -6,11 +6,12 @@
*/ */
import * as pdfjsLib from 'pdfjs-dist'; import * as pdfjsLib from 'pdfjs-dist';
import { PDFDocumentProxy } from 'pdfjs-dist/types/src/display/api';
const { getDocument, GlobalWorkerOptions } = pdfjsLib; const { getDocument, GlobalWorkerOptions } = pdfjsLib;
class PDFWorkerManager { class PDFWorkerManager {
private static instance: PDFWorkerManager; private static instance: PDFWorkerManager;
private activeDocuments = new Set<any>(); private activeDocuments = new Set<PDFDocumentProxy>();
private workerCount = 0; private workerCount = 0;
private maxWorkers = 10; // Limit concurrent workers private maxWorkers = 10; // Limit concurrent workers
private isInitialized = false; private isInitialized = false;
@ -48,7 +49,7 @@ class PDFWorkerManager {
stopAtErrors?: boolean; stopAtErrors?: boolean;
verbosity?: number; verbosity?: number;
} = {} } = {}
): Promise<any> { ): Promise<PDFDocumentProxy> {
// Wait if we've hit the worker limit // Wait if we've hit the worker limit
if (this.activeDocuments.size >= this.maxWorkers) { if (this.activeDocuments.size >= this.maxWorkers) {
await this.waitForAvailableWorker(); await this.waitForAvailableWorker();
@ -104,7 +105,7 @@ class PDFWorkerManager {
/** /**
* Properly destroy a PDF document and clean up resources * Properly destroy a PDF document and clean up resources
*/ */
destroyDocument(pdf: any): void { destroyDocument(pdf: PDFDocumentProxy): void {
if (this.activeDocuments.has(pdf)) { if (this.activeDocuments.has(pdf)) {
try { try {
pdf.destroy(); pdf.destroy();

View File

@ -1,10 +1,9 @@
import { useState, useEffect } from "react"; import { useState } from "react";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { createToolFlow } from "../components/tools/shared/createToolFlow"; import { createToolFlow } from "../components/tools/shared/createToolFlow";
import DeleteAllStep from "../components/tools/changeMetadata/steps/DeleteAllStep"; import DeleteAllStep from "../components/tools/changeMetadata/steps/DeleteAllStep";
import StandardMetadataStep from "../components/tools/changeMetadata/steps/StandardMetadataStep"; import StandardMetadataStep from "../components/tools/changeMetadata/steps/StandardMetadataStep";
import DocumentDatesStep from "../components/tools/changeMetadata/steps/DocumentDatesStep"; import DocumentDatesStep from "../components/tools/changeMetadata/steps/DocumentDatesStep";
import CustomMetadataStep from "../components/tools/changeMetadata/steps/CustomMetadataStep";
import AdvancedOptionsStep from "../components/tools/changeMetadata/steps/AdvancedOptionsStep"; import AdvancedOptionsStep from "../components/tools/changeMetadata/steps/AdvancedOptionsStep";
import { useChangeMetadataParameters } from "../hooks/tools/changeMetadata/useChangeMetadataParameters"; import { useChangeMetadataParameters } from "../hooks/tools/changeMetadata/useChangeMetadataParameters";
import { useChangeMetadataOperation } from "../hooks/tools/changeMetadata/useChangeMetadataOperation"; import { useChangeMetadataOperation } from "../hooks/tools/changeMetadata/useChangeMetadataOperation";
@ -15,7 +14,6 @@ import {
useDeleteAllTips, useDeleteAllTips,
useStandardMetadataTips, useStandardMetadataTips,
useDocumentDatesTips, useDocumentDatesTips,
useCustomMetadataTips,
useAdvancedOptionsTips useAdvancedOptionsTips
} from "../components/tooltips/useChangeMetadataTips"; } from "../components/tooltips/useChangeMetadataTips";
@ -26,14 +24,12 @@ const ChangeMetadata = (props: BaseToolProps) => {
const deleteAllTips = useDeleteAllTips(); const deleteAllTips = useDeleteAllTips();
const standardMetadataTips = useStandardMetadataTips(); const standardMetadataTips = useStandardMetadataTips();
const documentDatesTips = useDocumentDatesTips(); const documentDatesTips = useDocumentDatesTips();
const customMetadataTips = useCustomMetadataTips();
const advancedOptionsTips = useAdvancedOptionsTips(); const advancedOptionsTips = useAdvancedOptionsTips();
// Individual step collapse states // Individual step collapse states
const [deleteAllCollapsed, setDeleteAllCollapsed] = useState(false); const [deleteAllCollapsed, setDeleteAllCollapsed] = useState(false);
const [standardMetadataCollapsed, setStandardMetadataCollapsed] = useState(false); const [standardMetadataCollapsed, setStandardMetadataCollapsed] = useState(false);
const [documentDatesCollapsed, setDocumentDatesCollapsed] = useState(true); const [documentDatesCollapsed, setDocumentDatesCollapsed] = useState(true);
const [customMetadataCollapsed, setCustomMetadataCollapsed] = useState(true);
const [advancedOptionsCollapsed, setAdvancedOptionsCollapsed] = useState(true); const [advancedOptionsCollapsed, setAdvancedOptionsCollapsed] = useState(true);
const base = useBaseTool( const base = useBaseTool(
@ -102,24 +98,6 @@ const ChangeMetadata = (props: BaseToolProps) => {
/> />
), ),
}, },
{
title: t("changeMetadata.customFields.title", "Custom Metadata"),
isCollapsed: getActualCollapsedState(customMetadataCollapsed),
onCollapsedClick: base.hasResults
? (base.settingsCollapsed ? base.handleSettingsReset : undefined)
: () => setCustomMetadataCollapsed(!customMetadataCollapsed),
tooltip: customMetadataTips,
content: (
<CustomMetadataStep
parameters={base.params.parameters}
onParameterChange={base.params.updateParameter}
disabled={base.endpointLoading || base.params.parameters.deleteAll || isExtractingMetadata}
addCustomMetadata={base.params.addCustomMetadata}
removeCustomMetadata={base.params.removeCustomMetadata}
updateCustomMetadata={base.params.updateCustomMetadata}
/>
),
},
{ {
title: t("changeMetadata.advanced.title", "Advanced Options"), title: t("changeMetadata.advanced.title", "Advanced Options"),
isCollapsed: getActualCollapsedState(advancedOptionsCollapsed), isCollapsed: getActualCollapsedState(advancedOptionsCollapsed),
@ -132,6 +110,9 @@ const ChangeMetadata = (props: BaseToolProps) => {
parameters={base.params.parameters} parameters={base.params.parameters}
onParameterChange={base.params.updateParameter} onParameterChange={base.params.updateParameter}
disabled={base.endpointLoading || isExtractingMetadata} disabled={base.endpointLoading || isExtractingMetadata}
addCustomMetadata={base.params.addCustomMetadata}
removeCustomMetadata={base.params.removeCustomMetadata}
updateCustomMetadata={base.params.updateCustomMetadata}
/> />
), ),
}, },