mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
feat: init utils & schemas for block
This commit is contained in:
parent
e1d4c4c594
commit
7ca481960c
5 changed files with 57 additions and 1 deletions
0
src/services/blocks/__init__.py
Normal file
0
src/services/blocks/__init__.py
Normal file
|
|
@ -2,7 +2,7 @@ from typing import List, Literal
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
from fastapi import Request
|
from fastapi import Request
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from src.services.blocks.blocks import Block
|
from src.services.blocks.schemas.blocks import Block
|
||||||
from src.services.users.users import PublicUser
|
from src.services.users.users import PublicUser
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
10
src/services/blocks/schemas/files.py
Normal file
10
src/services/blocks/schemas/files.py
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
|
class BlockFile(BaseModel):
|
||||||
|
file_id: str
|
||||||
|
file_format: str
|
||||||
|
file_name: str
|
||||||
|
file_size: int
|
||||||
|
file_type: str
|
||||||
|
lecture_id: str
|
||||||
46
src/services/blocks/utils/upload_files.py
Normal file
46
src/services/blocks/utils/upload_files.py
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
import os
|
||||||
|
import uuid
|
||||||
|
from fastapi import Request, UploadFile
|
||||||
|
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):
|
||||||
|
# get file id
|
||||||
|
file_id = str(uuid.uuid4())
|
||||||
|
|
||||||
|
# get file format
|
||||||
|
file_format = file.filename.split(".")[-1]
|
||||||
|
|
||||||
|
# get file size
|
||||||
|
file_size = len(await file.read())
|
||||||
|
|
||||||
|
# get file type
|
||||||
|
file_type = file.content_type
|
||||||
|
|
||||||
|
# get file name
|
||||||
|
file_name = file.filename
|
||||||
|
|
||||||
|
# create file object
|
||||||
|
uploadable_file = BlockFile(
|
||||||
|
file_id=file_id,
|
||||||
|
file_format=file_format,
|
||||||
|
file_name=file_name,
|
||||||
|
file_size=file_size,
|
||||||
|
file_type=file_type,
|
||||||
|
lecture_id=lecture_id
|
||||||
|
)
|
||||||
|
|
||||||
|
# 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}")
|
||||||
|
|
||||||
|
# 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())
|
||||||
|
f.close()
|
||||||
|
|
||||||
|
# TODO: do some error handling here
|
||||||
|
|
||||||
|
return uploadable_file
|
||||||
Loading…
Add table
Add a link
Reference in a new issue