Atualizar app/app.py
This commit is contained in:
+40
-11
@@ -807,6 +807,7 @@ import uuid
|
|||||||
import pypandoc
|
import pypandoc
|
||||||
from pdfminer.high_level import extract_text
|
from pdfminer.high_level import extract_text
|
||||||
from docx import Document
|
from docx import Document
|
||||||
|
from io.pandoc import convert_document
|
||||||
|
|
||||||
@app.route("/api/import/<cid>", methods=["POST"])
|
@app.route("/api/import/<cid>", methods=["POST"])
|
||||||
@login_required
|
@login_required
|
||||||
@@ -814,41 +815,69 @@ from docx import Document
|
|||||||
def import_file(cid):
|
def import_file(cid):
|
||||||
file = request.files.get("file")
|
file = request.files.get("file")
|
||||||
if not file:
|
if not file:
|
||||||
return jsonify({"error": "No file"}), 400
|
return jsonify({"erro": "Nenhum arquivo enviado"}), 400
|
||||||
|
|
||||||
ext = file.filename.lower().split(".")[-1]
|
email = session["db_user"]["email"] if app.config["AUTH_MODE"] == "db" else session["user"]["email"]
|
||||||
file_id = str(uuid.uuid4())
|
|
||||||
path = f"/data/{file_id}_{file.filename}"
|
|
||||||
|
|
||||||
|
file_id = str(uuid.uuid4())
|
||||||
|
filename = file.filename
|
||||||
|
path = f"/data/{file_id}_{filename}"
|
||||||
file.save(path)
|
file.save(path)
|
||||||
|
|
||||||
db.add(File(id=file_id, user_email=email, path=path, original_name=file.filename))
|
ext = file.filename.lower().split(".")[-1]
|
||||||
db.commit()
|
if ext not in ["doc", "docx", "odt", "txt", "md", "html", "htm"]:
|
||||||
|
return jsonify({"erro": "Formato não suportado."}), 400
|
||||||
|
|
||||||
if ext == "pdf":
|
if ext == "pdf":
|
||||||
text = extract_text(temp_path)
|
text = extract_text(path)
|
||||||
elif ext == "docx":
|
elif ext == "docx":
|
||||||
doc = Document(temp_path)
|
doc = Document(path)
|
||||||
text = "\n".join([p.text for p in doc.paragraphs])
|
text = "\n".join([p.text for p in doc.paragraphs])
|
||||||
elif ext == "doc":
|
elif ext == "doc":
|
||||||
text = pypandoc.convert_file(temp_path, "md")
|
text = pypandoc.convert_file(path, "md")
|
||||||
else:
|
else:
|
||||||
return jsonify({"error": "Unsupported format"}), 400
|
return jsonify({"error": "Unsupported format"}), 400
|
||||||
|
|
||||||
md = pypandoc.convert_text(text, "md", format="plain")
|
md = pypandoc.convert_text(text, "md", format="plain")
|
||||||
|
|
||||||
db = SessionLocal()
|
db = SessionLocal()
|
||||||
|
db.add(File(id=file_id, user_email=email, path=path, original_name=filename))
|
||||||
|
db.commit()
|
||||||
msg = Message(
|
msg = Message(
|
||||||
id=str(uuid.uuid4()),
|
id=str(uuid.uuid4()),
|
||||||
conversation_id=cid,
|
conversation_id=cid,
|
||||||
role="user",
|
role="user",
|
||||||
content=md
|
content=f"Arquivo importado: {filename}\n```\n{md}\n```"
|
||||||
)
|
)
|
||||||
db.add(msg)
|
db.add(msg)
|
||||||
db.commit()
|
db.commit()
|
||||||
db.close()
|
db.close()
|
||||||
|
|
||||||
return jsonify({"status": "ok", "imported_as_markdown": True})
|
return jsonify({"status": "ok", "filename": filename,"imported_as_markdown": True})
|
||||||
|
|
||||||
|
@app.route("/api/export/pandoc/<writer>/<cid>/<filename>")
|
||||||
|
@login_required
|
||||||
|
def export_pandoc(writer, cid, filename):
|
||||||
|
if not writer in ["docx","xlsx","pptx","markdown","txt","odt","html"]:
|
||||||
|
return jsonify({"erro": "Formato de exportação não suportado."}), 400
|
||||||
|
email = session["db_user"]["email"] if app.config["AUTH_MODE"] == "db" else session["user"]["email"]
|
||||||
|
db = SessionLocal()
|
||||||
|
msgs = (
|
||||||
|
db.query(Message)
|
||||||
|
.join(Conversation)
|
||||||
|
.filter(Conversation.id == cid, Conversation.user_email == email)
|
||||||
|
.order_by(Message.created_at)
|
||||||
|
.all()
|
||||||
|
)
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
text = "\n".join([m.content for m in msgs])
|
||||||
|
|
||||||
|
out = f"/data/{uuid.uuid4()}_{filename}"
|
||||||
|
pypandoc.convert_text(text, writer, format="plain", outputfile=out)
|
||||||
|
|
||||||
|
return send_file(out, as_attachment=True, download_name=filename)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/api/export/pdf/<cid>/<filename>")
|
@app.route("/api/export/pdf/<cid>/<filename>")
|
||||||
@login_required
|
@login_required
|
||||||
|
|||||||
Reference in New Issue
Block a user