From 354347b4e9c50c5380195ca9eec17f55170b59f3 Mon Sep 17 00:00:00 2001 From: sunbeam Date: Mon, 30 Dec 2024 17:04:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 71 +++++++++++++++++++++++++++++++++++++++++++++++--- web/index.html | 28 ++++++++++++++++++++ 2 files changed, 96 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index 5e929d7..5ccd8f7 100644 --- a/main.py +++ b/main.py @@ -1,10 +1,75 @@ -from fastapi import FastAPI +from fastapi import FastAPI, File, UploadFile from fastapi.staticfiles import StaticFiles - +import os +from shutil import copyfileobj +import shutil +import subprocess +from fastapi.responses import FileResponse app = FastAPI() -app.mount("/", StaticFiles(directory="./web", html=True)) +app.mount("/tool", StaticFiles(directory="./web", html=True)) + +@app.get("/api/test/") +async def root(): + return {"message": "Hello World"} + +@app.post("/api/uploadfile/") +async def create_upload_file(file: UploadFile = File(...)): + try: + # 设置文件保存的路径 + folder_path = './uploads/' + # 清空文件夹 + shutil.rmtree(folder_path) + # 新建文件夹 + os.makedirs(folder_path, exist_ok=True) + os.makedirs(folder_path+'gen/', exist_ok=True) + # 复制coderdbc程序 + shutil.copy2("../dbc2c/coderdbc", folder_path) + file_location = os.path.join(folder_path, file.filename) + with open(file_location, "wb") as buffer: + copyfileobj(file.file, buffer) + # 执行二进制程序并等待其完成 + command = [ + folder_path+"coderdbc", + "-dbc", folder_path+file.filename, + "-out", folder_path+"gen/", + "-drvname", "CANmatrix", + "-nodeutils", + "-rw", + "-driverdir", # 这个参数后面没有值,所以它后面不应该有逗号 + "-gendate" + ] + result = subprocess.run(command, capture_output=True, text=True) + # 获取输出和错误 + + if result.stderr == "": + command = ['zip', '-r', folder_path+'gen/CANmatrix.zip', folder_path+'gen/CANmatrix'] + # 使用subprocess.run执行命令 + result = subprocess.run(command, capture_output=True, text=True) + + + return {"info": f"File {file.filename} uploaded successfully!", + "coderdbc_stdout":result.stdout, + "coderdbc_stderr":result.stderr, + } + except Exception as e: + return {"error": str(e)} + +@app.get("/api/download/") +async def download_file(): + try: + # 设置文件保存的路径 + folder_path = './uploads/' + return FileResponse(path=folder_path+"gen/CANmatrix.zip", filename="CANmatrix.zip", media_type="application/zip") + except FileNotFoundError: + return JSONResponse(content={"message": "File not found"}, status_code=404) + +def process_file(file_path, output_path): + with open(file_path, 'r') as file: + content = file.read() + with open(output_path, 'w') as file: + file.write(content.upper()) if __name__ == "__main__": import uvicorn diff --git a/web/index.html b/web/index.html index 697fc76..a544fab 100644 --- a/web/index.html +++ b/web/index.html @@ -8,5 +8,33 @@

Hello, FastAPI!

This is an example of loading an HTML file using FastAPI.

+ uploadfile +
+ + +
+ \ No newline at end of file