mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
feat: refactor imageBlock
This commit is contained in:
parent
7ca481960c
commit
3715157722
10 changed files with 87 additions and 138 deletions
|
|
@ -1,18 +1,26 @@
|
|||
import os
|
||||
import uuid
|
||||
from fastapi import Request, UploadFile
|
||||
from fastapi import HTTPException, Request, UploadFile, status
|
||||
from src.services.blocks.schemas.files import BlockFile
|
||||
|
||||
from src.services.users.schemas.users import PublicUser
|
||||
|
||||
|
||||
async def upload_file_and_return_file_object(request: Request, file: UploadFile, current_user: PublicUser, lecture_id: str, block_id: str):
|
||||
async def upload_file_and_return_file_object(request: Request, file: UploadFile, lecture_id: str, block_id: str, list_of_allowed_file_formats: list, type_of_block: str):
|
||||
# get file id
|
||||
file_id = str(uuid.uuid4())
|
||||
|
||||
# get file format
|
||||
file_format = file.filename.split(".")[-1]
|
||||
|
||||
# validate file format
|
||||
if file_format not in list_of_allowed_file_formats:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT, detail="File format not supported")
|
||||
|
||||
# create file
|
||||
file_binary = await file.read()
|
||||
|
||||
# get file size
|
||||
file_size = len(await file.read())
|
||||
|
||||
|
|
@ -33,14 +41,15 @@ async def upload_file_and_return_file_object(request: Request, file: UploadFile,
|
|||
)
|
||||
|
||||
# create folder for lecture
|
||||
if not os.path.exists(f"content/uploads/files/lectures/{lecture_id}/blocks/{block_id}"):
|
||||
os.mkdir(f"content/uploads/files/lectures/{lecture_id}/blocks/{block_id}")
|
||||
if not os.path.exists(f"content/uploads/files/lectures/{lecture_id}/blocks/{type_of_block}/{block_id}"):
|
||||
# create folder for lecture
|
||||
os.makedirs(f"content/uploads/files/lectures/{lecture_id}/blocks/{type_of_block}/{block_id}")
|
||||
|
||||
# upload file to server
|
||||
with open(f"content/uploads/files/lectures/{lecture_id}/blocks/{block_id}/{file_id}.{file_format}", 'wb') as f:
|
||||
f.write(await file.read())
|
||||
with open(f"content/uploads/files/lectures/{lecture_id}/blocks/{type_of_block}/{block_id}/{file_id}.{file_format}", 'wb') as f:
|
||||
f.write(file_binary)
|
||||
f.close()
|
||||
|
||||
|
||||
# TODO: do some error handling here
|
||||
|
||||
return uploadable_file
|
||||
return uploadable_file
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue