82 lines
2.5 KiB
Python
82 lines
2.5 KiB
Python
import reflex as rx
|
|
import requests
|
|
|
|
API_URL = "http://127.0.0.1:8000"
|
|
|
|
class State(rx.State):
|
|
storage_status: dict = {}
|
|
upload_files: list[str]
|
|
download_link: str = ""
|
|
|
|
@rx.var
|
|
def total_space_gb(self) -> str:
|
|
total = self.storage_status.get("total_space", 0)
|
|
return f"{total / (1024**3):.2f} GB"
|
|
|
|
@rx.var
|
|
def used_space_gb(self) -> str:
|
|
used = self.storage_status.get("used_space", 0)
|
|
return f"{used / (1024**3):.2f} GB"
|
|
|
|
@rx.var
|
|
def free_space_gb(self) -> str:
|
|
free = self.storage_status.get("free_space", 0)
|
|
return f"{free / (1024**3):.2f} GB"
|
|
|
|
def get_storage_status(self):
|
|
try:
|
|
response = requests.get(f"{API_URL}/api/storage-status")
|
|
response.raise_for_status()
|
|
self.storage_status = response.json()
|
|
except requests.exceptions.RequestException as e:
|
|
print(f"Error fetching storage status: {e}")
|
|
|
|
async def handle_upload(self, files: list[rx.UploadFile]):
|
|
for file in files:
|
|
upload_data = await file.read()
|
|
files = {'file': (file.filename, upload_data, file.content_type)}
|
|
|
|
try:
|
|
response = requests.post(f"{API_URL}/api/upload-file", files=files)
|
|
response.raise_for_status()
|
|
self.download_link = response.json().get("download_link", "")
|
|
except requests.exceptions.RequestException as e:
|
|
print(f"Error uploading file: {e}")
|
|
|
|
def index():
|
|
return rx.container(
|
|
rx.heading("MultiDrive Box", size="9"),
|
|
rx.hstack(
|
|
rx.button("Refresh Status", on_click=State.get_storage_status),
|
|
rx.link(
|
|
rx.button("Add Account"),
|
|
href=f"{API_URL}/api/add-account",
|
|
is_external=True,
|
|
),
|
|
),
|
|
rx.box(
|
|
rx.text(f"Total Space: {State.total_space_gb}"),
|
|
rx.text(f"Used Space: {State.used_space_gb}"),
|
|
rx.text(f"Free Space: {State.free_space_gb}"),
|
|
),
|
|
rx.upload(
|
|
rx.text("Drag and drop files here or click to select files."),
|
|
id="upload",
|
|
),
|
|
rx.button(
|
|
"Upload", on_click=State.handle_upload(rx.upload_files(upload_id="upload"))
|
|
),
|
|
rx.cond(
|
|
State.download_link,
|
|
rx.link(
|
|
"Download File",
|
|
href=API_URL + State.download_link,
|
|
is_external=True
|
|
)
|
|
),
|
|
padding="2em",
|
|
)
|
|
|
|
app = rx.App()
|
|
app.add_page(index)
|