MineContext icon indicating copy to clipboard operation
MineContext copied to clipboard

[BUG]: Screenshot images fail to load in backend (front-end shows correctly)

Open MumuTW opened this issue 1 month ago • 1 comments

🐛 Bug description [Please make everyone to understand it]

Summary

前端截圖顯示正常,但後端所有 screenshot 處理流程(包含 _encode_image_to_base64()screenshot_processor)全部失敗,log 顯示圖片路徑不存在:

图片加载失败

Root Cause

前端與後端使用完全不同的截圖路徑策略,並未對齊

組件 實際路徑行為
前端(Electron) 截圖存到:userData/Data/screenshot/activity/YYYY-MM-DD/HH-mm-ss/*.png(prod) 或 process.cwd()/backend/screenshot/activity/...(dev)
前端顯示圖片 直接用 window.screenMonitorAPI.readImageAsBase64() → 透過 Electron fs.readFile() 讀本機檔案(不經後端)
後端(FastAPI) 靜態檔案路徑硬寫為 ./screenshots,且僅在該資料夾存在時才掛載 /screenshots/*
後端實際期望的圖片位置 ./screenshots/<filename>.png(但該資料夾完全不存在)

所以後端會永遠找不到圖片。


Technical Breakdown

1. cli.py 的路徑邏輯不會被執行

screenshots_path = Path("./screenshots").resolve()
if screenshots_path.exists():
    app.mount("/screenshots", StaticFiles(directory=screenshots_path))

問題點:

  1. ./screenshots 目錄不存在
  2. screenshots_path.exists() 回傳 False
  3. FastAPI 根本沒有 mount 任何 /screenshots
  4. 後端任何基於此路徑的圖片讀取都會失敗

2. 後端 screenshot_processor.py 讀錯路徑

後端收到的 image_path 被當作檔案系統絕對路徑使用:

if not image_path or not os.path.exists(image_path):
    logger.error("Screenshot path is invalid or does not exist")

由於前端不會把「實際寫入的硬碟路徑」傳給後端且後端只會在 ./screenshots 裡找檔案,造成後端路徑檢查都 FAIL。


建議修復方案:

  1. 統一路徑策略:
    • 由 Electron 啟動後端時,透過環境變數 SCREENSHOTS_DIR 注入一個絕對路徑作為截圖的根目錄。
    • 後端 FastAPI 使用此環境變數來掛載靜態檔案目錄,並確保該目錄存在。
    • 前端將截圖儲存至此統一的根目錄下。
  2. 統一API協定:
    • 前端在與後端溝通時,一律傳遞相對於 SCREENSHOTS_DIR相對路徑
    • 後端根據接收到的相對路徑和自身的根目錄環境變數,即可拼湊出正確的完整檔案路徑。

具體要怎麼處理,可能得看開發團隊的偏好。

🧑‍💻 Step to reproduce

localhost中查看圖片

👾 Expected result

預期截圖能正常顯示

🚑 Any additional information

No response

🛠️ MineContext Version

0.1.5

💻 Platform Details

MacOS

MumuTW avatar Nov 15 '25 11:11 MumuTW

现在应用使用的是前端截图功能,会通过api把截图路径传到后端。所以 后端的 ./screenshot 并没有用到

qin-ctx avatar Nov 17 '25 03:11 qin-ctx

Image

目前localhost顯示如上,後端並沒有辦法正常顯示(所有圖片皆是如此),應該是錯誤需要修正 簡單的話應該直接把這邊去匹配前端API的路徑,修改cli.py路徑邏輯

MumuTW avatar Nov 18 '25 06:11 MumuTW