fix not logging when user accessed they files and only show the file access from the user files and not all
Build and Push Docker Container / build-and-push (push) Successful in 1m37s

This commit is contained in:
2025-10-25 14:54:00 +02:00
parent 6f92fdefa9
commit cd96e9135f
4 changed files with 47 additions and 7 deletions
+34
View File
@@ -276,6 +276,40 @@ class EdgeDB:
"accessed_at": file.at, "accessed_at": file.at,
} for file in data] } for file in data]
async def get_all_access_of_user(self, user_id:str):
data = await self.run_query_with_reconnection(
self.client.query,
"""
select files {
file_id,
file_name,
note,
accesses: {
at,
status,
ip: {
value
},
user_agent: {
value
}
}
order by .at desc
}
filter .user_id = <str>$user_id
""",
user_id=user_id
)
return sorted([{
"file_id": file.file_id,
"file_name": file.file_name,
"file_note": file.note,
"status": access.status,
"ip": access.ip.value,
"user_agent": access.user_agent.value,
"accessed_at": access.at,
} for file in data for access in file.accesses], key=lambda x: x["accessed_at"], reverse=True)
async def get_file_access(self, file_id: str): async def get_file_access(self, file_id: str):
data = await self.run_query_with_reconnection( data = await self.run_query_with_reconnection(
self.client.query_single, self.client.query_single,
+4 -4
View File
@@ -19,7 +19,7 @@ async def index():
@side_main_bp.route('/access') @side_main_bp.route('/access')
@login_required @login_required
async def access_list(user): async def access_list(user):
access_data = await current_app.edgedb.get_all_file_access() access_data = await current_app.edgedb.get_all_access_of_user(user_id=user['sub'])
return await render_template("views/webpage/access/list.htm", access_logs=access_data) return await render_template("views/webpage/access/list.htm", access_logs=access_data)
@side_main_bp.route('/files') @side_main_bp.route('/files')
@@ -54,7 +54,7 @@ async def serve_file(file_id: str):
disable_logging = True disable_logging = True
if is_expired(file_data.get("expires_at")): if is_expired(file_data.get("expires_at")):
if disable_logging: if not disable_logging:
await current_app.edgedb.add_file_access(file_id=file_id, ip_address=get_ip(), user_agent=request.user_agent, status="expired", accessed_at=datetime.now(timezone.utc)) await current_app.edgedb.add_file_access(file_id=file_id, ip_address=get_ip(), user_agent=request.user_agent, status="expired", accessed_at=datetime.now(timezone.utc))
return Response("This file has expired.", status=410, headers={ return Response("This file has expired.", status=410, headers={
"Cache-Control": "no-store", "Cache-Control": "no-store",
@@ -68,11 +68,11 @@ async def serve_file(file_id: str):
path = current_app.upload_folder / file_name path = current_app.upload_folder / file_name
if not path.exists() or not path.is_file(): if not path.exists() or not path.is_file():
if disable_logging: if not disable_logging:
await current_app.edgedb.add_file_access(file_id=file_id, ip_address=get_ip(), user_agent=request.user_agent, status="error", accessed_at=datetime.now(timezone.utc)) await current_app.edgedb.add_file_access(file_id=file_id, ip_address=get_ip(), user_agent=request.user_agent, status="error", accessed_at=datetime.now(timezone.utc))
abort(404) abort(404)
if disable_logging: if not disable_logging:
await current_app.edgedb.add_file_access(file_id=file_id, ip_address=get_ip(), user_agent=request.user_agent, status="ok", accessed_at=datetime.now(timezone.utc)) await current_app.edgedb.add_file_access(file_id=file_id, ip_address=get_ip(), user_agent=request.user_agent, status="ok", accessed_at=datetime.now(timezone.utc))
return await send_from_directory( return await send_from_directory(
directory=current_app.upload_folder, directory=current_app.upload_folder,
+8 -2
View File
@@ -12,13 +12,15 @@
<main class="file-list"> <main class="file-list">
<section class="card" style="padding: clamp(18px, 2.6vw, 28px);"> <section class="card" style="padding: clamp(18px, 2.6vw, 28px);">
<h2 class="page-title">Accesslog</h2> <h2 class="page-title">Accesslog</h2>
<p class="subtle">Your uploaded files at a glance. Click a filename to open, or use the actions on the right.</p> <p class="subtle">See when and how others accessed your shared files. Click a filename to open your files</p>
<div class="table-wrap" role="region" aria-label="Files table" tabindex="0"> <div class="table-wrap" role="region" aria-label="Files table" tabindex="0">
<table class="table"> <table class="table">
<thead> <thead>
<tr> <tr>
<th scope="col">Accessed At</th> <th scope="col">Accessed At</th>
<th scope="col">Filename</th>
<th scope="col">Note (Only Visible to You)</th>
<th scope="col">Status</th> <th scope="col">Status</th>
<th scope="col">IP Address</th> <th scope="col">IP Address</th>
<th scope="col">User Agent</th> <th scope="col">User Agent</th>
@@ -27,7 +29,11 @@
<tbody> <tbody>
{% for access in access_logs %} {% for access in access_logs %}
<tr> <tr>
<td><time datetime="{{ access.accessed_at }}" class="local-time"></time></td> <td><time datetime="{{ access.accessed_at }}" class="local-time"></td>
<td class="cell--name">
<a href="{{ url_for('side_main.serve_file', file_id=access.file_id) }}">{{ access.file_name }}</time></a>
</td>
<td>{{ access.file_note }}</td>
<td><span class="badge">{{ access.status }}</span></td> <td><span class="badge">{{ access.status }}</span></td>
<td>{{ access.ip }}</td> <td>{{ access.ip }}</td>
<td>{{ access.user_agent }}</td> <td>{{ access.user_agent }}</td>
+1 -1
View File
@@ -19,7 +19,7 @@
<thead> <thead>
<tr> <tr>
<th scope="col">Filename</th> <th scope="col">Filename</th>
<th scope="col">Note</th> <th scope="col">Note (Only Visible to You)</th>
<th scope="col">Size</th> <th scope="col">Size</th>
<th scope="col">Uploaded</th> <th scope="col">Uploaded</th>
<th scope="col">Expires</th> <th scope="col">Expires</th>