Store a discussion file attachment atomically within an existing DB transaction.
(conn, msg_id: int, file_storage)
| 12528 | meta_path = _upload_meta_path(upload_id) |
| 12529 | with UPLOAD_LOCK: |
| 12530 | try: |
| 12531 | meta = json.loads(open(meta_path, "r", encoding="utf-8").read()) |
| 12532 | except Exception: |
| 12533 | meta = None |
| 12534 | if meta and meta.get("u") != current_user(): |
| 12535 | return jsonify({"ok": False, "error": "forbidden"}), 403 |
| 12536 | for fp in (_upload_tmp_path(upload_id), meta_path, meta_path + ".tmp"): |
| 12537 | try: |
| 12538 | if os.path.exists(fp): |
| 12539 | os.remove(fp) |
| 12540 | except Exception: |
| 12541 | pass |
| 12542 | return jsonify({"ok": True}) |
| 12543 | |
| 12544 | |
| 12545 | @app.route("/api/upload/chunk", methods=["POST"]) |
| 12546 | @login_required |
| 12547 | def api_upload_chunk(): |
| 12548 | upload_id = (request.form.get("upload_id") or "").strip() |
| 12549 | try: idx = int(request.form.get("index") or 0); total = int(request.form.get("total") or 0) |
| 12550 | except Exception: return jsonify({"ok": False, "error": "bad_request"}), 400 |
| 12551 | chunk = request.files.get("chunk") |
| 12552 | if not re.fullmatch(r"[A-Za-z0-9_-]{20,128}", upload_id or "") or chunk is None: return jsonify({"ok": False, "error": "bad_request"}), 400 |
| 12553 | meta_path = _upload_meta_path(upload_id) |
| 12554 | with UPLOAD_LOCK: |
| 12555 | try: meta = json.loads(open(meta_path, "r", encoding="utf-8").read()) |
no test coverage detected