add code to convert datetime into localtime into file list and add access quarys

This commit is contained in:
2025-10-24 10:50:19 +02:00
parent d04cd5f831
commit 2eda108577
6 changed files with 204 additions and 67 deletions
+115 -28
View File
@@ -1,9 +1,6 @@
from my_modules.app.logger import logger
from datetime import datetime, timedelta, timezone
from zoneinfo import ZoneInfo
from collections import Counter
import asyncio, gel, json
import asyncio, gel
class EdgeDB:
def __init__(self, database:str=None, tls_security:str='insecure', timeout:int=1, max_retrys:int=10):
@@ -69,7 +66,7 @@ class EdgeDB:
""",
file_id=file_id
)
if data:
return {
"file_name": data.file_name,
@@ -78,7 +75,7 @@ class EdgeDB:
}
return None
async def get_files(self, current_datetime, user_id: str):
async def get_files(self, current_datetime, user_id:str):
data = await self.run_query_with_reconnection(
self.client.query,
"""
@@ -100,16 +97,14 @@ class EdgeDB:
now=current_datetime,
user_id=user_id,
)
return [
{
"file_id": i.file_id,
"file_name": i.file_name,
"file_size": i.file_size,
"note": i.note,
"uploaded_at": i.uploaded_at,
"expires_at": i.expires_at,
} for i in data
]
return [{
"file_id": i.file_id,
"file_name": i.file_name,
"file_size": i.file_size,
"note": i.note,
"uploaded_at": i.uploaded_at,
"expires_at": i.expires_at if i.expires_at else '',
} for i in data]
async def add_file(self, file_id, file_name, file_size, note, content_type, uploaded_at, expires_at, user_id:str):
return await self.run_query_with_reconnection(
@@ -180,13 +175,11 @@ class EdgeDB:
""",
now=current_datetime
)
return [
{
"file_id": item.file_id,
"file_name": item.file_name,
"expires_at": item.expires_at
} for item in data
]
return [{
"file_id": item.file_id,
"file_name": item.file_name,
"expires_at": item.expires_at
} for item in data]
async def delete_files_by_ids(self, remove_file_ids:list[str]):
if not remove_file_ids:
@@ -205,11 +198,105 @@ class EdgeDB:
pass
# File Access Quary Functions
async def add_file_access(self):
pass
async def add_file_access(self, file_id: str, ip_address: str, status: str, user_agent: str, accessed_at):
return await self.run_query_with_reconnection(
self.client.query,
"""
WITH
used_file := (
SELECT files
FILTER .file_id = <str>$file_id
LIMIT 1
),
ip_obj := (
INSERT IPAddr { value := <str>$ip_address }
UNLESS CONFLICT ON .value
ELSE (
SELECT IPAddr
FILTER .value = <str>$ip_address
)
),
ua_obj := (
INSERT UserAgent { value := <str>$user_agent }
UNLESS CONFLICT ON .value
ELSE (
SELECT UserAgent
FILTER .value = <str>$user_agent
)
),
new_file_access := (
INSERT file_access {
at := <datetime>$accessed_at,
status := <access_status>$status,
ip := ip_obj,
user_agent := ua_obj
}
),
_updated_file := (
UPDATE used_file
SET { accesses += (SELECT new_file_access) }
)
SELECT new_file_access {
at,
status,
ip: { value },
user_agent: { value },
};
""",
file_id=file_id,
accessed_at=accessed_at,
ip_address=ip_address,
status=status,
user_agent=str(user_agent),
)
async def get_all_file_access(self):
pass
data = await self.run_query_with_reconnection(
self.client.query,
"""
select file_access {
status,
ip: {
value
},
user_agent: {
value
},
at
}
"""
)
return [{
"status": str(file.status),
"ip": file.ip.value,
"user_agent": file.user_agent.value,
"accessed_at": file.at,
} for file in data]
async def get_file_access(self, file_id:str):
pass
async def get_file_access(self, file_id: str):
data = await self.run_query_with_reconnection(
self.client.query_single,
"""
SELECT files {
accesses: {
status,
ip: { value },
user_agent: { value },
at
}
}
FILTER .file_id = <str>$file_id
LIMIT 1
""",
file_id=file_id,
)
if data:
return [{
"status": str(access.status),
"ip": access.ip.value if access.ip else None,
"user_agent": access.user_agent.value if access.user_agent else None,
"accessed_at": access.at,
} for access in data.accesses]
return None
+10 -1
View File
@@ -1,7 +1,7 @@
from my_modules.app.constens import SECRET_KEY
import hmac, hashlib, base64, secrets, time
from urllib.parse import quote, unquote
from datetime import datetime, timezone
def base64url_encode(data: bytes) -> str:
return base64.urlsafe_b64encode(data).decode().rstrip("=")
@@ -27,6 +27,15 @@ def verify_signed_url(file_id: str, token: str, file_expiration: int) -> bool:
not_expired = file_expiration >= time.time()
return valid_sig and not_expired
def is_expired(expires_at):
if not expires_at:
return False
if expires_at.tzinfo is None:
expires_at = expires_at.replace(tzinfo=timezone.utc)
else:
expires_at = expires_at.astimezone(timezone.utc)
return expires_at <= datetime.now(timezone.utc)
if __name__ == "__main__":
file_id = generate_short_id()
url = generate_signed_url(file_id)