AdeshRathod commited on
Commit
bbd6a26
·
1 Parent(s): bad0ea1

Add update functionality for latest.json in OTA routes

Browse files
Files changed (3) hide show
  1. requirements.txt +1 -0
  2. src/ota/README.md +1 -0
  3. src/routes/ota.py +27 -0
requirements.txt CHANGED
@@ -6,3 +6,4 @@ requests>=2.28
6
  motor>=3.1
7
  python-dotenv>=1.0
8
  PyJWT>=2.0
 
 
6
  motor>=3.1
7
  python-dotenv>=1.0
8
  PyJWT>=2.0
9
+ python-multipart>=0.0.9
src/ota/README.md CHANGED
@@ -3,6 +3,7 @@ Place OTA assets (latest.json, \*.apk, etc.) in this directory.
3
  Endpoints (via /api/ota):
4
 
5
  - GET /api/ota/latest.json?abi=<abi> # returns ABI-specific apkUrl/sha256 if matching .apk exists; 404 when requested abi not found
 
6
  - GET /api/ota/download/{filename} # download explicit file
7
  - GET /api/ota/download?abi=<abi> # download first matching ABI .apk
8
  - GET /api/ota/{filename} # serve file directly
 
3
  Endpoints (via /api/ota):
4
 
5
  - GET /api/ota/latest.json?abi=<abi> # returns ABI-specific apkUrl/sha256 if matching .apk exists; 404 when requested abi not found
6
+ - PUT /api/ota/latest.json?abi=<abi> # merge/update latest.json (or latest-<abi>.json) with JSON body
7
  - GET /api/ota/download/{filename} # download explicit file
8
  - GET /api/ota/download?abi=<abi> # download first matching ABI .apk
9
  - GET /api/ota/{filename} # serve file directly
src/routes/ota.py CHANGED
@@ -5,6 +5,7 @@ from typing import Optional
5
 
6
  from fastapi import APIRouter, HTTPException, UploadFile, File
7
  from fastapi.responses import FileResponse, JSONResponse
 
8
 
9
  router = APIRouter(prefix="/ota", tags=["ota"])
10
 
@@ -50,6 +51,10 @@ async def latest(abi: Optional[str] = None):
50
  except Exception:
51
  raise HTTPException(status_code=500, detail="Invalid latest.json content")
52
 
 
 
 
 
53
  if abi:
54
  apk_path = _find_apk_for_abi(abi)
55
  if not apk_path:
@@ -61,6 +66,28 @@ async def latest(abi: Optional[str] = None):
61
  return JSONResponse(data)
62
 
63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  @router.get("/download")
65
  async def download_by_abi(abi: Optional[str] = None):
66
  if not abi:
 
5
 
6
  from fastapi import APIRouter, HTTPException, UploadFile, File
7
  from fastapi.responses import FileResponse, JSONResponse
8
+ from fastapi import Body
9
 
10
  router = APIRouter(prefix="/ota", tags=["ota"])
11
 
 
51
  except Exception:
52
  raise HTTPException(status_code=500, detail="Invalid latest.json content")
53
 
54
+ # Normalize versionCode if a snake_case key is used
55
+ if "versionCode" not in data and "version_code" in data:
56
+ data["versionCode"] = data.get("version_code")
57
+
58
  if abi:
59
  apk_path = _find_apk_for_abi(abi)
60
  if not apk_path:
 
66
  return JSONResponse(data)
67
 
68
 
69
+ @router.put("/latest.json")
70
+ async def update_latest(body: dict = Body(...), abi: Optional[str] = None):
71
+ """Update latest.json (or latest-{abi}.json). Merges existing with provided body."""
72
+ target = OTA_DIR / (f"latest-{abi}.json" if abi else "latest.json")
73
+
74
+ if target.exists():
75
+ try:
76
+ existing = json.loads(target.read_text())
77
+ except Exception:
78
+ raise HTTPException(status_code=500, detail="Invalid existing latest.json content")
79
+ else:
80
+ existing = {}
81
+
82
+ if not isinstance(body, dict):
83
+ raise HTTPException(status_code=400, detail="Payload must be a JSON object")
84
+
85
+ merged = {**existing, **body}
86
+ target.write_text(json.dumps(merged, indent=2))
87
+
88
+ return JSONResponse({"ok": True, "file": target.name, "data": merged})
89
+
90
+
91
  @router.get("/download")
92
  async def download_by_abi(abi: Optional[str] = None):
93
  if not abi: