feat: backend create & upload video

This commit is contained in:
swve 2022-11-29 20:39:13 +01:00
parent cc9397ca5f
commit 42d74aebde
8 changed files with 103 additions and 15 deletions

View file

@ -4,7 +4,7 @@ from typing import List
from uuid import uuid4
from pydantic import BaseModel
from src.services.courses.courses import Course, CourseInDB
from src.services.courses.elements import Element, ElementInDB
from src.services.courses.elements.elements import Element, ElementInDB
from src.services.database import create_config_collection, check_database, create_database, learnhouseDB, learnhouseDB
from src.services.security import verify_user_rights_with_roles
from src.services.users import PublicUser

View file

@ -3,7 +3,7 @@ import os
from typing import List
from uuid import uuid4
from pydantic import BaseModel
from src.services.courses.elements import ElementInDB
from src.services.courses.elements.elements import ElementInDB
from src.services.uploads import upload_thumbnail
from src.services.users import PublicUser, User
from src.services.database import create_config_collection, check_database, create_database, learnhouseDB

View file

@ -0,0 +1,55 @@
from pydantic import BaseModel
from src.services.database import create_config_collection, check_database, create_database, learnhouseDB
from src.services.security import verify_user_rights_with_roles
from src.services.uploads import upload_video
from src.services.users import PublicUser, User
from src.services.courses.elements.elements import ElementInDB, Element
from fastapi import FastAPI, HTTPException, status, Request, Response, BackgroundTasks, UploadFile, File
from uuid import uuid4
from datetime import datetime
async def create_video_element(name: str, coursechapter_id: str, current_user: PublicUser, video_file: UploadFile | None = None):
await check_database()
elements = learnhouseDB["elements"]
coursechapters = learnhouseDB["coursechapters"]
# generate element_id
element_id = str(f"element_{uuid4()}")
element_object = ElementInDB(
element_id=element_id,
coursechapter_id=coursechapter_id,
name=name,
type="video",
content={
"video": {
"filename": video_file.filename,
"element_id": element_id,
}
},
creationDate=str(datetime.now()),
updateDate=str(datetime.now()),
)
hasRoleRights = await verify_user_rights_with_roles("create", current_user.user_id, element_id)
if not hasRoleRights:
raise HTTPException(
status_code=status.HTTP_409_CONFLICT, detail="Roles : Insufficient rights to perform this action")
# create element
element = ElementInDB(**element_object.dict())
elements.insert_one(element.dict())
# upload video
if video_file:
print("uploading video")
await upload_video(video_file, video_file.filename, element_id)
# todo : choose whether to update the chapter or not
# update chapter
coursechapters.update_one({"coursechapter_id": coursechapter_id}, {
"$addToSet": {"elements": element_id}})
return element

View file

@ -1,12 +1,33 @@
import os
async def upload_thumbnail(thumbnail_file, name_in_disk):
contents = thumbnail_file.file.read()
try:
with open(f"content/uploads/img/{name_in_disk}", 'wb') as f:
f.write(contents)
f.close()
except Exception as e:
print(e)
return {"message": "There was an error uploading the file"}
finally:
thumbnail_file.file.close()
contents = thumbnail_file.file.read()
try:
with open(f"content/uploads/img/{name_in_disk}", 'wb') as f:
f.write(contents)
f.close()
except Exception as e:
print(e)
return {"message": "There was an error uploading the file"}
finally:
thumbnail_file.file.close()
async def upload_video(video_file, name_in_disk, element_id):
contents = video_file.file.read()
# create folder
os.mkdir(f"content/uploads/video/{element_id}")
try:
with open(f"content/uploads/video/{element_id}/{name_in_disk}", 'wb') as f:
f.write(contents)
f.close()
except Exception as e:
print(e)
return {"message": "There was an error uploading the file"}
finally:
video_file.file.close()