feat: subir PDF al servidor al seleccionarlo
This commit is contained in:
parent
ebed934246
commit
d1ea8376ed
27
app.py
27
app.py
|
|
@ -230,6 +230,33 @@ def api_datos():
|
||||||
datos = json.load(f)
|
datos = json.load(f)
|
||||||
return jsonify(datos)
|
return jsonify(datos)
|
||||||
|
|
||||||
|
# -----------------------------------------------------------------------
|
||||||
|
# API: subir ticket PDF al servidor
|
||||||
|
# -----------------------------------------------------------------------
|
||||||
|
@app.route("/api/subir-pdf", methods=["POST"])
|
||||||
|
@login_required
|
||||||
|
def api_subir_pdf():
|
||||||
|
if 'pdf' not in request.files:
|
||||||
|
return jsonify({"ok": False, "mensaje": "No se recibio ningun archivo"}), 400
|
||||||
|
file = request.files['pdf']
|
||||||
|
if not file or file.filename == '':
|
||||||
|
return jsonify({"ok": False, "mensaje": "Archivo vacio"}), 400
|
||||||
|
if not file.filename.lower().endswith('.pdf'):
|
||||||
|
return jsonify({"ok": False, "mensaje": "Solo se aceptan archivos PDF"}), 400
|
||||||
|
|
||||||
|
pdf_bytes = file.read()
|
||||||
|
if len(pdf_bytes) > 20 * 1024 * 1024:
|
||||||
|
return jsonify({"ok": False, "mensaje": "El PDF es demasiado grande (max 20 MB)"}), 400
|
||||||
|
|
||||||
|
tdir = get_tickets_dir(session["usuario"])
|
||||||
|
# Nombre seguro: eliminar caracteres problemáticos
|
||||||
|
nombre_seguro = re.sub(r'[^\w\-. ]', '_', file.filename)
|
||||||
|
dest = tdir / nombre_seguro
|
||||||
|
with open(dest, 'wb') as f:
|
||||||
|
f.write(pdf_bytes)
|
||||||
|
|
||||||
|
return jsonify({"ok": True, "mensaje": f"PDF guardado: {nombre_seguro}"})
|
||||||
|
|
||||||
# -----------------------------------------------------------------------
|
# -----------------------------------------------------------------------
|
||||||
# API: forzar regeneracion del pipeline
|
# API: forzar regeneracion del pipeline
|
||||||
# -----------------------------------------------------------------------
|
# -----------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
|
@ -278,7 +278,15 @@
|
||||||
async function leerPDF(file) {
|
async function leerPDF(file) {
|
||||||
if (!file) return;
|
if (!file) return;
|
||||||
const estado = document.getElementById('pdfEstado');
|
const estado = document.getElementById('pdfEstado');
|
||||||
|
estado.style.color = 'var(--text-muted)';
|
||||||
estado.textContent = 'Leyendo ' + file.name + '...';
|
estado.textContent = 'Leyendo ' + file.name + '...';
|
||||||
|
|
||||||
|
// Subir al servidor en paralelo
|
||||||
|
const form = new FormData();
|
||||||
|
form.append('pdf', file);
|
||||||
|
const subirPromise = fetch('/api/subir-pdf', { method: 'POST', body: form });
|
||||||
|
|
||||||
|
// Extraer texto localmente con pdf.js
|
||||||
const pdfData = await file.arrayBuffer();
|
const pdfData = await file.arrayBuffer();
|
||||||
const pdf = await pdfjsLib.getDocument({ data: pdfData }).promise;
|
const pdf = await pdfjsLib.getDocument({ data: pdfData }).promise;
|
||||||
let texto = '';
|
let texto = '';
|
||||||
|
|
@ -287,10 +295,24 @@
|
||||||
const content = await page.getTextContent();
|
const content = await page.getTextContent();
|
||||||
texto += content.items.map(item => item.str).join(' ') + '\n';
|
texto += content.items.map(item => item.str).join(' ') + '\n';
|
||||||
}
|
}
|
||||||
estado.textContent = file.name + ' — ' + pdf.numPages + ' pagina(s)';
|
|
||||||
const salida = document.getElementById('pdfTexto');
|
const salida = document.getElementById('pdfTexto');
|
||||||
salida.style.display = 'block';
|
salida.style.display = 'block';
|
||||||
salida.textContent = texto.trim();
|
salida.textContent = texto.trim();
|
||||||
|
|
||||||
|
// Resultado de la subida
|
||||||
|
try {
|
||||||
|
const res = await subirPromise;
|
||||||
|
const data = await res.json();
|
||||||
|
if (data.ok) {
|
||||||
|
estado.textContent = '✅ ' + file.name + ' guardado en el servidor (' + pdf.numPages + ' pag.) — Pulsa "Regenerar predicciones" para actualizar.';
|
||||||
|
} else {
|
||||||
|
estado.style.color = '#f97316';
|
||||||
|
estado.textContent = '⚠️ ' + file.name + ' leido localmente pero no se pudo guardar en el servidor: ' + data.mensaje;
|
||||||
|
}
|
||||||
|
} catch(e) {
|
||||||
|
estado.style.color = '#f97316';
|
||||||
|
estado.textContent = '⚠️ ' + file.name + ' leido localmente pero no se pudo subir al servidor.';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue