z230
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
# app.py | v1.9 | 2026-06-08
|
||||
# app.py | v2.0 | 2026-06-08
|
||||
# FastAPI server pro příjem .msg a .db souborů, upload do Dropboxu a import do Graph API.
|
||||
# Endpointy: /upload (.msg → /msgs + Graph import), /upload-db (.db → /msgs/db),
|
||||
# /upload-dropbox (→ Dropbox /!!!Days/Downloads Z230),
|
||||
# /message-delete, /message-update (sync: smazání, přečtení, přesun složky),
|
||||
# /mirror-plan (diff manifestu z JNJ vůči schránce → smaže přebytky, vrátí to_add),
|
||||
# /pending-files (seznam souborů k odeslání na JNJ), /download-file/{filename}.
|
||||
# /status (seznam souborů k odeslání na JNJ — jména zašifrována Fernetem),
|
||||
# /item/{enc_filename} (stažení souboru — enc_filename je Fernet token).
|
||||
|
||||
from fastapi import FastAPI, UploadFile, File, Form, Header, HTTPException, Response
|
||||
from pydantic import BaseModel
|
||||
@@ -540,19 +541,26 @@ async def pending_files(authorization: str = Header(None)):
|
||||
except Exception:
|
||||
files = []
|
||||
log.info("pending-files: %d souboru", len(files))
|
||||
return {"files": files}
|
||||
# Jména souborů zašifrujeme — klient vidí v URL jen neprůhledný token (bypass Zscaler)
|
||||
encrypted_names = [_FERNET.encrypt(name.encode()).decode() for name in files]
|
||||
return {"files": encrypted_names}
|
||||
|
||||
|
||||
@app.get("/item/{filename:path}")
|
||||
async def download_file(filename: str, authorization: str = Header(None)):
|
||||
if authorization != f"Bearer {TOKEN}":
|
||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||
# filename je Fernet token (zašifrované původní jméno souboru)
|
||||
try:
|
||||
orig_filename = _FERNET.decrypt(filename.encode()).decode()
|
||||
except Exception:
|
||||
raise HTTPException(status_code=400, detail="Invalid filename token")
|
||||
dbx = dropbox.Dropbox(
|
||||
app_key=DROPBOX_APP_KEY,
|
||||
app_secret=DROPBOX_APP_SECRET,
|
||||
oauth2_refresh_token=DROPBOX_REFRESH_TOKEN,
|
||||
)
|
||||
dropbox_path = f"{DROPBOX_UPLOAD_TO_JNJ}/{filename}"
|
||||
dropbox_path = f"{DROPBOX_UPLOAD_TO_JNJ}/{orig_filename}"
|
||||
try:
|
||||
_, response = dbx.files_download(dropbox_path)
|
||||
raw = response.content
|
||||
@@ -563,15 +571,15 @@ async def download_file(filename: str, authorization: str = Header(None)):
|
||||
encrypted = _FERNET.encrypt(raw)
|
||||
|
||||
# Přesun do Sent
|
||||
sent_path = f"{DROPBOX_UPLOAD_TO_JNJ}/##Trash/{filename}"
|
||||
sent_path = f"{DROPBOX_UPLOAD_TO_JNJ}/##Trash/{orig_filename}"
|
||||
try:
|
||||
dbx.files_move_v2(dropbox_path, sent_path, autorename=True)
|
||||
log.info("download-file: %s přesunut do Sent", filename)
|
||||
log.info("download-file: %s přesunut do Sent", orig_filename)
|
||||
except Exception as e:
|
||||
log.warning("download-file: nelze přesunout %s do Sent: %s", filename, e)
|
||||
log.warning("download-file: nelze přesunout %s do Sent: %s", orig_filename, e)
|
||||
|
||||
return Response(
|
||||
content=encrypted,
|
||||
media_type="application/octet-stream",
|
||||
headers={"Content-Disposition": f'attachment; filename="{filename}.enc"'},
|
||||
headers={"Content-Disposition": f'attachment; filename="{orig_filename}.enc"'},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user