God, I hope I'm ready

This commit is contained in:
2024-08-21 05:22:00 +03:00
parent 04a27b592e
commit 5f0130d0d1
10 changed files with 193 additions and 98 deletions

View File

@@ -6,6 +6,8 @@ import hashlib
import os
import uuid
import dateutil
import dateutil.tz
import file_widgets
import pydantic
from PyQt6.QtGui import QIcon
@@ -32,12 +34,14 @@ class File(pydantic.BaseModel):
def details(self, list: file_widgets.FileListWidget) -> QWidget:
del list
file_size = self._format_bytes(self.file_size)
file_size = self._format_size(self.file_size)
file_size_text = f"{file_size[0]:.2f} {file_size[1]}"
created_at = self._format_date(self.created_at)
updated_at = self._format_date(self.updated_at)
details = (
f"file id: {self.file_id}\nfile_name: {self.file_name}\n"
+ f"file_size: {file_size_text}\n"
+ f"created at: {self.created_at}\nupdated at: {self.updated_at}"
+ f"created at: {created_at}\nupdated at: {updated_at}"
)
label = QLabel()
label.setWindowTitle("File info")
@@ -45,7 +49,7 @@ class File(pydantic.BaseModel):
return label
@staticmethod
def _format_bytes(size: int):
def _format_size(size: int):
power = 2**10
n = 0
power_labels = {0: "", 1: "kibi", 2: "mebi", 3: "gibi", 4: "tebi"}
@@ -54,6 +58,12 @@ class File(pydantic.BaseModel):
n += 1
return size, power_labels[n] + "bytes"
@staticmethod
def _format_date(date: datetime.datetime) -> str:
date = date.replace(tzinfo=dateutil.tz.tzutc())
date = date.astimezone(dateutil.tz.tzlocal())
return date.strftime("%Y-%m-%d %H:%M:%S")
def icon(self) -> QIcon:
return QIcon(resource_path("assets/file.png"))
@@ -76,6 +86,7 @@ class File(pydantic.BaseModel):
def create(path: str, parent_id: uuid.UUID):
"""Upload the file"""
print(path)
file_name = os.path.basename(path)
try:
with open(path, "rb") as f:
@@ -94,14 +105,21 @@ class File(pydantic.BaseModel):
QMessageBox.critical(None, "HTTP Error", str(e))
def download(self, path: str):
with open(path, "wb") as f, RequestClient().client.stream(
"GET", "/files", params={"file_id": self.file_id}
) as stream:
if not stream.is_success:
QMessageBox.warning(None, "Error downloading the file")
return
for data in stream.iter_bytes():
f.write(data)
try:
with open(path, "wb") as f, RequestClient().client.stream(
"GET", "/files", params={"file_id": self.file_id}
) as stream:
if not stream.is_success:
QMessageBox.warning(
None,
"Error downloading the file",
"Error downloading the file",
)
return
for data in stream.iter_bytes():
f.write(data)
except Exception as e:
QMessageBox.warning(None, "Error downloading the file", str(e))
def modify(self, path: str):
"""Upload the file"""