-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
96 lines (76 loc) · 2.96 KB
/
Copy pathmain.py
File metadata and controls
96 lines (76 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import os
from pathlib import Path
from fastapi import HTTPException
from utils import battery_status, monitor_status , files_status
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from fastapi import FastAPI
# 1. Initialize the app
app = FastAPI()
# Tell FastAPI where CSS and JS files are
app.mount("/static", StaticFiles(directory="static"), name="static")
@app.get("/")
def home():
return FileResponse("static/index.html")
@app.get("/api/battery/stats")
def battery_stats():
battery, is_plugged_in = battery_status.battery_charge()
battery_health = battery_status.battery_health()
data = {
"battery_percent": battery.percent if battery else None,
"charging": is_plugged_in,
"seconds_left": battery.secsleft if battery else None,
"battery_health": battery_health if battery_health else None,
}
return data
@app.get("/api/monitor/stats")
def monitor_stats():
cpu, ram = monitor_status.monitor_system()
sent, recieve = monitor_status.monitor_network()
data = {
"cpu_usage" : cpu ,
"ram_percent" : ram.percent,
"ram_used" : (ram.used / (1024**3)),
"ram_total" : (ram.total/ (1024**3)),
"net_sent" : sent,
"net_recieve" : recieve
}
return data
@app.get("/api/files/stats")
def file_stats():
total, used, free = files_status.get_storage_info()
data = {
"storage_total" : files_status.get_size( total ) if total else None,
"storage_used" : files_status.get_size(used) if used else None,
"storage_free" : files_status.get_size(free) if free else None,
"raw_used" : used if used else None,
"raw_free" : free if free else None,
}
return data
@app.get("/api/files/explore")
def explore_files(path: str = None):
SAFE_ROOT = Path.home()
try:
requested_path = SAFE_ROOT if path is None else Path(path).resolve()
except Exception:
raise HTTPException(status_code=400, detail="Invalid path format")
if not requested_path.is_relative_to(SAFE_ROOT):
raise HTTPException(status_code=403, detail="Access denied: Cannot leave safe root directory")
if not requested_path.exists() or not requested_path.is_dir():
raise HTTPException(status_code=404, detail="Directory not found")
try:
items = []
for item in requested_path.iterdir():
if not item.name.startswith("."):
items.append({
"name": item.name,
"is_dir": item.is_dir(),
"path": str(item.absolute())
})
return {
"current_path": str(requested_path),
"parent": str(requested_path.parent) if requested_path != SAFE_ROOT else None,
"items": sorted(items, key=lambda x: (not x["is_dir"], x["name"].lower()))
}
except PermissionError:
raise HTTPException(status_code=403, detail="Permission denied")