# -*- coding: utf-8 -*-
"""
筑医台意见反馈附件上传脚本（任意文件类型）

接口: POST https://www.zhuyitai.com/feedback/uploadAttachment
字段: files (multipart/form-data)
已知限制:
  - 前端 JS 的 2MB 限制仅浏览器端校验, 直接 POST 即可绕过
  - 后端不校验文件类型与大小 (txt/html/svg/bin/伪图片均实测通过)
  - nginx 整请求体上限 10MB, 受 multipart 头部开销影响, 文件本体建议 < 9.9MB
返回: JSON, 其中 fileUrl 为公开直链 (HTML/SVG 等会按原 MIME 渲染执行)

用法: python zhuyitai_upload.py <文件路径>
依赖: pip install requests
"""
import os
import sys

import requests

UPLOAD_URL = "https://www.zhuyitai.com/feedback/uploadAttachment"
MAX_BODY = 10 * 1024 * 1024  # nginx client_max_body_size = 10M


def upload(path: str) -> str:
    size = os.path.getsize(path)
    if size >= MAX_BODY:
        print(f"[警告] 文件 {size} 字节, 整请求体将超过 nginx 10MB 上限, 预计被 413 拒绝")
    with open(path, "rb") as f:
        resp = requests.post(
            UPLOAD_URL,
            files={"files": (os.path.basename(path), f)},
            timeout=60,
        )
    print(f"HTTP {resp.status_code}")
    print(resp.text)
    return resp.text


if __name__ == "__main__":
    if len(sys.argv) < 2:
        print(f"用法: python {os.path.basename(__file__)} <文件路径>")
        sys.exit(1)
    upload(sys.argv[1])