Skip to content

Commit 5a889ba

Browse files
committed
feat: enhance file retrieval by using Pydantic models for serialization
1 parent 5fee1c7 commit 5a889ba

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

apps/admin/services.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from core.response import APIResponse
55
from core.storage import FileStorageInterface, storages
66
from core.settings import settings
7-
from apps.base.models import FileCodes, KeyValue
7+
from apps.base.models import FileCodes, KeyValue, file_codes_pydantic
88
from apps.base.utils import get_expire_info, get_file_path_name
99
from fastapi import HTTPException
1010
from core.settings import data_root
@@ -26,7 +26,10 @@ async def list_files(self, page: int, size: int, keyword: str = ""):
2626
await FileCodes.filter(prefix__icontains=keyword).limit(size).offset(offset)
2727
)
2828
total = await FileCodes.filter(prefix__icontains=keyword).count()
29-
return files, total
29+
files_pydantic = [
30+
await file_codes_pydantic.from_tortoise_orm(f) for f in files
31+
]
32+
return files_pydantic, total
3033

3134
async def download_file(self, file_id: int):
3235
file_code = await FileCodes.filter(id=file_id).first()
@@ -112,7 +115,12 @@ async def list_files(self):
112115
if not os.path.exists(data_root / "local"):
113116
os.makedirs(data_root / "local")
114117
for file in os.listdir(data_root / "local"):
115-
files.append(LocalFileClass(file))
118+
local_file = LocalFileClass(file)
119+
files.append({
120+
"file": local_file.file,
121+
"ctime": local_file.ctime,
122+
"size": local_file.size,
123+
})
116124
return files
117125

118126
async def delete_file(self, filename: str):

0 commit comments

Comments
 (0)