Cancel vault rereading without persisting a false document error
This commit is contained in:
@@ -21,9 +21,10 @@ export const DocumentVault = () => {
|
||||
|
||||
const DocumentVaultSession = ({signedIn}) => {
|
||||
const live = useRef(true);
|
||||
const readLifetime = useRef(new AbortController());
|
||||
const session = useRef(sessionGuard(getToken));
|
||||
const current = () => live.current && session.current();
|
||||
useEffect(() => { live.current = true; return () => { live.current = false; }; }, []);
|
||||
useEffect(() => { live.current = true; readLifetime.current = new AbortController(); return () => { live.current = false; readLifetime.current.abort(); }; }, []);
|
||||
const useOwnedState = (initial) => {
|
||||
const [value, setValue] = useState(initial);
|
||||
const setOwned = useCallback((next) => { if (current()) setValue(next); }, []);
|
||||
@@ -47,7 +48,7 @@ const DocumentVaultSession = ({signedIn}) => {
|
||||
if (!current()) return;
|
||||
setBusy(true); setHata(null); setIslemeBilgi(null);
|
||||
try {
|
||||
const result = await retryDocumentExtraction(document);
|
||||
const result = await retryDocumentExtraction(document,{signal:readLifetime.current.signal});
|
||||
setIslemeBilgi(`${document.ad}: ${result.sayi || 0} kalem çıkarıldı.`);
|
||||
} catch (error) { setHata(error.message); }
|
||||
finally {
|
||||
|
||||
@@ -1,35 +1,45 @@
|
||||
import {awaitWithAbort} from './abortable.js';
|
||||
import { getToken } from './auth.js';
|
||||
import { reportDocumentReadError } from './account.js';
|
||||
|
||||
export const readableDocument = (name) => /\.(pdf|txt|md|docx|xlsx|zip)$/i.test(String(name || ''));
|
||||
|
||||
export async function inspectStoredDocument(document) {
|
||||
export async function inspectStoredDocument(document,{signal}={}) {
|
||||
const token=getToken();
|
||||
const check=()=>{signal?.throwIfAborted();if(!token || token!==getToken())throw new Error('Oturum değişti; belgeyi yeniden açın.');};
|
||||
check();
|
||||
if (!readableDocument(document.ad)) throw new Error('Bu dosya biçiminden metin çıkarımı desteklenmiyor.');
|
||||
const office = /\.(docx|xlsx|zip)$/i.test(document.ad);
|
||||
const response = await fetch(`/api/me/documents/${office ? 'text' : 'download'}?id=${encodeURIComponent(document.id)}`, {
|
||||
headers: {Authorization:`Bearer ${getToken()}`},
|
||||
signal, headers: {Authorization:`Bearer ${token}`},
|
||||
});
|
||||
if (office) {
|
||||
const result = await response.json().catch(() => ({}));
|
||||
const result = await awaitWithAbort(response.json(),signal);
|
||||
check();
|
||||
if (!response.ok || !result.success) throw new Error(result.error || 'Belge metni alınamadı.');
|
||||
return {text:result.metin,format:document.ad.split('.').pop()};
|
||||
}
|
||||
if (!response.ok) throw new Error('Belge indirilemedi.');
|
||||
const blob = await response.blob();
|
||||
const blob = await awaitWithAbort(response.blob(),signal);
|
||||
check();
|
||||
const { extractText } = await import('./pdfText');
|
||||
return extractText(new File([blob],document.ad,{type:document.mime || 'application/octet-stream'}));
|
||||
check();
|
||||
const result=await extractText(new File([blob],document.ad,{type:document.mime || 'application/octet-stream'}),{signal});
|
||||
check();
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function readStoredDocument(document) { return (await inspectStoredDocument(document)).text; }
|
||||
export async function readStoredDocument(document,options) { return (await inspectStoredDocument(document,options)).text; }
|
||||
|
||||
export async function retryDocumentExtraction(document) {
|
||||
export async function retryDocumentExtraction(document,{signal}={}) {
|
||||
if (document.tur !== 'ihale' || !document.ikn) throw new Error('İhaleye bağlı ihale dokümanı gerekli.');
|
||||
const token = getToken();
|
||||
const checkSession = () => { if (!token || token !== getToken()) throw new Error('Oturum değişti; belgeyi yeniden açın.'); };
|
||||
const checkSession = () => { signal?.throwIfAborted(); if (!token || token !== getToken()) throw new Error('Oturum değişti; belgeyi yeniden açın.'); };
|
||||
checkSession();
|
||||
let text;
|
||||
try { text = await readStoredDocument(document); }
|
||||
try { text = await readStoredDocument(document,{signal}); }
|
||||
catch (error) {
|
||||
if(error.name === 'AbortError') throw error;
|
||||
checkSession();
|
||||
try { await reportDocumentReadError(document.id,document.isleme); }
|
||||
catch { throw new Error(`${error.message} (hata durumu kaydedilemedi; listeyi yenileyin)`); }
|
||||
|
||||
Reference in New Issue
Block a user