Spaces:
Build error
Build error
change content param type
Browse files- src/app/storage.py +8 -18
src/app/storage.py
CHANGED
|
@@ -41,37 +41,27 @@ def download_file(game: str, token: str, gamecode: str):
|
|
| 41 |
|
| 42 |
|
| 43 |
@router.post("/upload")
|
| 44 |
-
def upload_file(game: str, token: str, content:
|
| 45 |
if md5(token) != admin_token_md5:
|
| 46 |
print(f"[{beijing()}][upload file] failed")
|
| 47 |
return {"status": "Failure", "detail": "Invalid token"}
|
| 48 |
game = game.strip().lower()
|
| 49 |
-
try:
|
| 50 |
-
content_dict = json.loads(content)
|
| 51 |
-
except json.JSONDecodeError as e:
|
| 52 |
-
print(f"[{beijing()}][upload file] failed, {type(e)}: {e}")
|
| 53 |
-
return {"status": "Failure", "detail": "Invalid JSON"}
|
| 54 |
-
|
| 55 |
-
if not isinstance(content_dict, dict):
|
| 56 |
-
print(f"[{beijing()}][upload file] failed, not a dict")
|
| 57 |
-
return {"status": "Failure", "detail": "not a dict"}
|
| 58 |
-
|
| 59 |
needed_keys = ["game-file", "device-id"]
|
| 60 |
for key in needed_keys:
|
| 61 |
-
if key not in
|
| 62 |
-
print(f
|
| 63 |
-
return {"status": "Failure", "detail": f
|
| 64 |
|
| 65 |
-
if not isinstance(
|
| 66 |
print(f'[{beijing()}][upload file] failed, "device-id" is not a str')
|
| 67 |
return {"status": "Failure", "detail": '"device-id" is not a str'}
|
| 68 |
-
if not isinstance(
|
| 69 |
print(f'[{beijing()}][upload file] failed, "game-file" is not a dict')
|
| 70 |
return {"status": "Failure", "detail": '"game-file" is not a dict'}
|
| 71 |
|
| 72 |
obj = {
|
| 73 |
"upload-time": beijing().__str__(),
|
| 74 |
-
"game-file":
|
| 75 |
}
|
| 76 |
maxtry = 5
|
| 77 |
for retry in range(maxtry):
|
|
@@ -99,7 +89,7 @@ def upload_file(game: str, token: str, content: str):
|
|
| 99 |
if not os.path.exists(filedir):
|
| 100 |
os.makedirs(filedir)
|
| 101 |
filepath = os.path.join(filedir, md5code + ".json")
|
| 102 |
-
content_indented = json.dumps(
|
| 103 |
with open(filepath, "w") as f:
|
| 104 |
f.write(content_indented)
|
| 105 |
res = pgfile.upload(
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
@router.post("/upload")
|
| 44 |
+
def upload_file(game: str, token: str, content: dict):
|
| 45 |
if md5(token) != admin_token_md5:
|
| 46 |
print(f"[{beijing()}][upload file] failed")
|
| 47 |
return {"status": "Failure", "detail": "Invalid token"}
|
| 48 |
game = game.strip().lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
needed_keys = ["game-file", "device-id"]
|
| 50 |
for key in needed_keys:
|
| 51 |
+
if key not in content:
|
| 52 |
+
print(f"[{beijing()}][upload file] failed, missed key {key}")
|
| 53 |
+
return {"status": "Failure", "detail": f"missed key {key}"}
|
| 54 |
|
| 55 |
+
if not isinstance(content["device-id"], str):
|
| 56 |
print(f'[{beijing()}][upload file] failed, "device-id" is not a str')
|
| 57 |
return {"status": "Failure", "detail": '"device-id" is not a str'}
|
| 58 |
+
if not isinstance(content["game-file"], dict):
|
| 59 |
print(f'[{beijing()}][upload file] failed, "game-file" is not a dict')
|
| 60 |
return {"status": "Failure", "detail": '"game-file" is not a dict'}
|
| 61 |
|
| 62 |
obj = {
|
| 63 |
"upload-time": beijing().__str__(),
|
| 64 |
+
"game-file": content["game-file"],
|
| 65 |
}
|
| 66 |
maxtry = 5
|
| 67 |
for retry in range(maxtry):
|
|
|
|
| 89 |
if not os.path.exists(filedir):
|
| 90 |
os.makedirs(filedir)
|
| 91 |
filepath = os.path.join(filedir, md5code + ".json")
|
| 92 |
+
content_indented = json.dumps(content, indent=4)
|
| 93 |
with open(filepath, "w") as f:
|
| 94 |
f.write(content_indented)
|
| 95 |
res = pgfile.upload(
|