mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
feat: remake content upload and diffusion
This commit is contained in:
parent
aea9da388f
commit
fd0b5494cf
29 changed files with 345 additions and 162 deletions
|
|
@ -2,10 +2,19 @@ import os
|
|||
import uuid
|
||||
from fastapi import HTTPException, Request, UploadFile, status
|
||||
from src.services.blocks.schemas.files import BlockFile
|
||||
from src.services.utils.upload_content import upload_content
|
||||
|
||||
|
||||
|
||||
async def upload_file_and_return_file_object(request: Request, file: UploadFile, activity_id: str, block_id: str, list_of_allowed_file_formats: list, type_of_block: str):
|
||||
async def upload_file_and_return_file_object(
|
||||
request: Request,
|
||||
file: UploadFile,
|
||||
activity_id: str,
|
||||
block_id: str,
|
||||
list_of_allowed_file_formats: list,
|
||||
type_of_block: str,
|
||||
org_id: str,
|
||||
course_id: str,
|
||||
):
|
||||
# get file id
|
||||
file_id = str(uuid.uuid4())
|
||||
|
||||
|
|
@ -15,8 +24,9 @@ async def upload_file_and_return_file_object(request: Request, file: UploadFile,
|
|||
# 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")
|
||||
|
||||
status_code=status.HTTP_409_CONFLICT, detail="File format not supported"
|
||||
)
|
||||
|
||||
# create file
|
||||
file_binary = await file.read()
|
||||
|
||||
|
|
@ -36,18 +46,14 @@ async def upload_file_and_return_file_object(request: Request, file: UploadFile,
|
|||
file_name=file_name,
|
||||
file_size=file_size,
|
||||
file_type=file_type,
|
||||
activity_id=activity_id
|
||||
activity_id=activity_id,
|
||||
)
|
||||
|
||||
# create folder for activity
|
||||
if not os.path.exists(f"content/uploads/files/activities/{activity_id}/blocks/{type_of_block}/{block_id}"):
|
||||
# create folder for activity
|
||||
os.makedirs(f"content/uploads/files/activities/{activity_id}/blocks/{type_of_block}/{block_id}")
|
||||
|
||||
# upload file to server
|
||||
with open(f"content/uploads/files/activities/{activity_id}/blocks/{type_of_block}/{block_id}/{file_id}.{file_format}", 'wb') as f:
|
||||
f.write(file_binary)
|
||||
f.close()
|
||||
|
||||
await upload_content(
|
||||
f"courses/{course_id}/activities/{activity_id}/dynamic/blocks/{type_of_block}/{block_id}",
|
||||
org_id=org_id,
|
||||
file_binary=file_binary,
|
||||
file_and_format=f"{file_id}.{file_format}",
|
||||
)
|
||||
|
||||
return uploadable_file
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue