mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-23 20:37:42 +00:00
feat: image light ocr (#7224)
* feat: image light ocr * feat: optimize code * feat: update code * feat: update imagelight version --------- Co-authored-by: 杨国璇 <ygx@Hello-word.local>
This commit is contained in:
54
frontend/src/hooks/metadata-operation.js
Normal file
54
frontend/src/hooks/metadata-operation.js
Normal file
@@ -0,0 +1,54 @@
|
||||
import React, { useContext, useCallback, useMemo } from 'react';
|
||||
import metadataAPI from '../metadata/api';
|
||||
import { Utils } from '../utils/utils';
|
||||
import toaster from '../components/toast';
|
||||
import { PRIVATE_COLUMN_KEY, EVENT_BUS_TYPE } from '../metadata/constants';
|
||||
import { gettext } from '../utils/constants';
|
||||
|
||||
// This hook provides content related to metadata record operation
|
||||
const MetadataOperationsContext = React.createContext(null);
|
||||
|
||||
export const MetadataOperationsProvider = ({ repoID, enableMetadata, enableOCR, repoInfo, children }) => {
|
||||
const permission = useMemo(() => repoInfo.permission !== 'admin' && repoInfo.permission !== 'rw' ? 'r' : 'rw', [repoInfo]);
|
||||
const canModify = useMemo(() => permission === 'rw', [permission]);
|
||||
|
||||
const onOCR = useCallback((parentDir, fileName) => {
|
||||
const filePath = Utils.joinPath(parentDir, fileName);
|
||||
metadataAPI.ocr(repoID, filePath).then(res => {
|
||||
const ocrResult = res.data.ocr_result;
|
||||
const validResult = Array.isArray(ocrResult) && ocrResult.length > 0 ? JSON.stringify(ocrResult) : null;
|
||||
toaster.success(gettext('Successfully OCR'));
|
||||
if (validResult) {
|
||||
const update = { [PRIVATE_COLUMN_KEY.OCR]: validResult };
|
||||
metadataAPI.modifyRecord(repoID, { parentDir, fileName }, update).then(res => {
|
||||
const eventBus = window?.sfMetadataContext?.eventBus;
|
||||
if (eventBus) {
|
||||
eventBus.dispatch(EVENT_BUS_TYPE.LOCAL_RECORD_CHANGED, { parentDir, fileName }, update);
|
||||
}
|
||||
});
|
||||
}
|
||||
}).catch(error => {
|
||||
const errorMessage = Utils.getErrorMsg(error);
|
||||
toaster.danger(errorMessage);
|
||||
});
|
||||
}, [repoID]);
|
||||
|
||||
let value = {};
|
||||
if (canModify && enableMetadata && enableOCR) {
|
||||
value['onOCR'] = onOCR;
|
||||
}
|
||||
|
||||
return (
|
||||
<MetadataOperationsContext.Provider value={value}>
|
||||
{children}
|
||||
</MetadataOperationsContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export const useMetadataOperations = () => {
|
||||
const context = useContext(MetadataOperationsContext);
|
||||
if (!context) {
|
||||
throw new Error('\'MetadataOperationsContext\' is null');
|
||||
}
|
||||
return context;
|
||||
};
|
@@ -2,9 +2,10 @@ import React, { useContext, useEffect, useCallback, useState, useMemo } from 're
|
||||
import metadataAPI from '../metadata/api';
|
||||
import { Utils } from '../utils/utils';
|
||||
import toaster from '../components/toast';
|
||||
import { MetadataOperationsProvider } from './metadata-operation';
|
||||
|
||||
// This hook provides content related to seahub interaction, such as whether to enable extended attributes
|
||||
const EnableMetadataContext = React.createContext(null);
|
||||
const MetadataStatusContext = React.createContext(null);
|
||||
|
||||
export const MetadataStatusProvider = ({ repoID, currentRepoInfo, hideMetadataView, children }) => {
|
||||
const enableMetadataManagement = useMemo(() => {
|
||||
@@ -101,7 +102,7 @@ export const MetadataStatusProvider = ({ repoID, currentRepoInfo, hideMetadataVi
|
||||
}, [repoID, detailsSettings]);
|
||||
|
||||
return (
|
||||
<EnableMetadataContext.Provider
|
||||
<MetadataStatusContext.Provider
|
||||
value={{
|
||||
enableMetadataManagement,
|
||||
enableMetadata,
|
||||
@@ -117,15 +118,24 @@ export const MetadataStatusProvider = ({ repoID, currentRepoInfo, hideMetadataVi
|
||||
updateEnableOCR,
|
||||
}}
|
||||
>
|
||||
{!isLoading && children}
|
||||
</EnableMetadataContext.Provider>
|
||||
{!isLoading && (
|
||||
<MetadataOperationsProvider
|
||||
repoID={repoID}
|
||||
enableMetadata={enableMetadata}
|
||||
enableOCR={enableOCR}
|
||||
repoInfo={currentRepoInfo}
|
||||
>
|
||||
{children}
|
||||
</MetadataOperationsProvider>
|
||||
)}
|
||||
</MetadataStatusContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export const useMetadataStatus = () => {
|
||||
const context = useContext(EnableMetadataContext);
|
||||
const context = useContext(MetadataStatusContext);
|
||||
if (!context) {
|
||||
throw new Error('\'EnableMetadataContext\' is null');
|
||||
throw new Error('\'MetadataStatusContext\' is null');
|
||||
}
|
||||
return context;
|
||||
};
|
||||
|
Reference in New Issue
Block a user