Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ from typing import List, Optional
|
|
5 |
import datetime
|
6 |
import json
|
7 |
import os
|
|
|
8 |
|
9 |
app = FastAPI()
|
10 |
|
@@ -25,8 +26,8 @@ class BackupRequest(BaseModel):
|
|
25 |
phone: str
|
26 |
messages: List[SMSMessage]
|
27 |
|
28 |
-
#
|
29 |
-
DATA_DIR = "sms_data"
|
30 |
os.makedirs(DATA_DIR, exist_ok=True)
|
31 |
|
32 |
@app.post("/backup")
|
@@ -60,9 +61,9 @@ async def retrieve_sms(
|
|
60 |
try:
|
61 |
# Find all files for this phone number
|
62 |
matching_files = []
|
63 |
-
for
|
64 |
-
if
|
65 |
-
matching_files.append(
|
66 |
|
67 |
if not matching_files:
|
68 |
raise HTTPException(status_code=404, detail="No messages found for this phone number")
|
@@ -73,7 +74,7 @@ async def retrieve_sms(
|
|
73 |
# Read all messages
|
74 |
all_messages = []
|
75 |
for filename in matching_files:
|
76 |
-
with open(
|
77 |
data = json.load(f)
|
78 |
all_messages.extend(data["messages"])
|
79 |
|
@@ -92,4 +93,9 @@ async def retrieve_sms(
|
|
92 |
|
93 |
@app.get("/")
|
94 |
async def health_check():
|
95 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
5 |
import datetime
|
6 |
import json
|
7 |
import os
|
8 |
+
from pathlib import Path
|
9 |
|
10 |
app = FastAPI()
|
11 |
|
|
|
26 |
phone: str
|
27 |
messages: List[SMSMessage]
|
28 |
|
29 |
+
# Use /tmp directory which has write permissions
|
30 |
+
DATA_DIR = "/tmp/sms_data"
|
31 |
os.makedirs(DATA_DIR, exist_ok=True)
|
32 |
|
33 |
@app.post("/backup")
|
|
|
61 |
try:
|
62 |
# Find all files for this phone number
|
63 |
matching_files = []
|
64 |
+
for entry in os.scandir(DATA_DIR):
|
65 |
+
if entry.name.startswith(f"{phone}_") and entry.is_file():
|
66 |
+
matching_files.append(entry.path)
|
67 |
|
68 |
if not matching_files:
|
69 |
raise HTTPException(status_code=404, detail="No messages found for this phone number")
|
|
|
74 |
# Read all messages
|
75 |
all_messages = []
|
76 |
for filename in matching_files:
|
77 |
+
with open(filename, "r") as f:
|
78 |
data = json.load(f)
|
79 |
all_messages.extend(data["messages"])
|
80 |
|
|
|
93 |
|
94 |
@app.get("/")
|
95 |
async def health_check():
|
96 |
+
return {
|
97 |
+
"status": "healthy",
|
98 |
+
"timestamp": datetime.datetime.now().isoformat(),
|
99 |
+
"storage_path": DATA_DIR,
|
100 |
+
"writable": os.access(DATA_DIR, os.W_OK)
|
101 |
+
}
|