53 lines
2.5 KiB
JavaScript
53 lines
2.5 KiB
JavaScript
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,{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)}`, {
|
||
signal, headers: {Authorization:`Bearer ${token}`},
|
||
});
|
||
if (office) {
|
||
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 awaitWithAbort(response.blob(),signal);
|
||
check();
|
||
const { extractText } = await import('./pdfText');
|
||
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,options) { return (await inspectStoredDocument(document,options)).text; }
|
||
|
||
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 = () => { 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,{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)`); }
|
||
throw error;
|
||
}
|
||
checkSession();
|
||
const { kalemCikar } = await import('./kalem.js');
|
||
checkSession();
|
||
return kalemCikar({ikn:document.ikn,metin:text,dosya:document.ad,documentId:document.id});
|
||
}
|