Atualizar app/app.py
This commit is contained in:
+124
-3
@@ -1182,9 +1182,130 @@ def subscribe():
|
||||
# ===========
|
||||
# IO
|
||||
|
||||
|
||||
from io.document import convert_document
|
||||
|
||||
@app.route("/api/import/d/<cid>", methods=["POST"])
|
||||
@login_required
|
||||
def import_document(cid):
|
||||
file = request.files.get("file")
|
||||
if not file:
|
||||
return jsonify({"erro": "Nenhum arquivo enviado."}), 400
|
||||
|
||||
ext = file.filename.lower().split(".")[-1]
|
||||
if ext not in ["doc", "docx", "odt", "txt", "md", "html", "htm"]:
|
||||
return jsonify({"erro": "Formato não suportado."}), 400
|
||||
|
||||
email = session["db_user"]["email"] if app.config["AUTH_MODE"] == "db" else session["user"]["email"]
|
||||
|
||||
temp_path = f"/data/{uuid.uuid4()}.{ext}"
|
||||
file.save(temp_path)
|
||||
|
||||
text = convert_document(temp_path)
|
||||
|
||||
# Convert to TSV (single column)
|
||||
tsv = "\x09".join([normalize_newlines(text)])
|
||||
filename = f"{file.filename}.tsv"
|
||||
|
||||
file_id = str(uuid.uuid4())
|
||||
path = f"/data/{file_id}_{filename}"
|
||||
|
||||
with open(path, "w", encoding="utf-8") as f:
|
||||
f.write(tsv)
|
||||
|
||||
db = SessionLocal()
|
||||
db.add(File(
|
||||
id=file_id,
|
||||
user_email=email,
|
||||
path=path,
|
||||
original_name=filename
|
||||
))
|
||||
|
||||
msg = Message(
|
||||
id=str(uuid.uuid4()),
|
||||
conversation_id=cid,
|
||||
role="user",
|
||||
content=f"Arquivo importado: {filename}\n```\n{tsv}\n```"
|
||||
)
|
||||
db.add(msg)
|
||||
|
||||
db.commit()
|
||||
db.close()
|
||||
|
||||
return jsonify({"status": "ok", "filename": filename})
|
||||
|
||||
@app.route("/api/export/txt/<cid>/<filename>")
|
||||
@login_required
|
||||
def export_txt(cid, filename):
|
||||
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}"
|
||||
|
||||
with open(out, "w", encoding="utf-8") as f:
|
||||
f.write(text)
|
||||
|
||||
return send_file(out, as_attachment=True, download_name=filename)
|
||||
|
||||
@app.route("/api/export/md/<cid>/<filename>")
|
||||
@login_required
|
||||
def export_md(cid, filename):
|
||||
# similar to TXT, but wrap content in Markdown
|
||||
...
|
||||
|
||||
@app.route("/api/export/html/<cid>/<filename>")
|
||||
@login_required
|
||||
def export_html(cid, filename):
|
||||
# wrap messages in <p> tags
|
||||
...
|
||||
|
||||
from docx import Document
|
||||
|
||||
@app.route("/api/export/docx/<cid>/<filename>")
|
||||
@login_required
|
||||
def export_docx(cid, filename):
|
||||
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()
|
||||
|
||||
doc = Document()
|
||||
for m in msgs:
|
||||
doc.add_paragraph(m.content)
|
||||
|
||||
out = f"/data/{uuid.uuid4()}_{filename}"
|
||||
doc.save(out)
|
||||
|
||||
return send_file(out, as_attachment=True, download_name=filename)
|
||||
|
||||
@app.route("/api/export/odt/<cid>/<filename>")
|
||||
@login_required
|
||||
def export_odt(cid, filename):
|
||||
# similar approach using odfpy
|
||||
...
|
||||
|
||||
|
||||
|
||||
from io.spreadsheet import convert_excel, convert_excel_all_sheets
|
||||
|
||||
@app.route("/api/io/s/<cid>", methods=["POST"])
|
||||
@app.route("/api/import/s/<cid>", methods=["POST"])
|
||||
@login_required
|
||||
def import_excel(cid):
|
||||
file = request.files.get("file")
|
||||
@@ -1220,7 +1341,7 @@ def import_excel(cid):
|
||||
|
||||
from io.presentation import convert_presentation
|
||||
|
||||
@app.route("/api/io/p/<cid>", methods=["POST"])
|
||||
@app.route("/api/import/p/<cid>", methods=["POST"])
|
||||
@login_required
|
||||
def import_presentation(cid):
|
||||
file = request.files.get("file")
|
||||
@@ -1272,7 +1393,7 @@ def import_presentation(cid):
|
||||
|
||||
from pptx import Presentation
|
||||
|
||||
@app.route("/api/io/p/<cid>/<filename>")
|
||||
@app.route("/api/export/pptx/<cid>/<filename>")
|
||||
@login_required
|
||||
def export_pptx(cid, filename):
|
||||
email = session["user"]["email"] if app.config["AUTH_MODE"] == "oauth" else session["db_user"]["email"]
|
||||
|
||||
Reference in New Issue
Block a user