fix: thumbnails

This commit is contained in:
swve 2023-12-13 17:06:51 +01:00
parent c39d9d5340
commit 3413e6ca73
33 changed files with 161 additions and 740 deletions

View file

@ -49,7 +49,7 @@ async def get_course(
.where(ResourceAuthor.resource_uuid == course.course_uuid)
)
authors = db_session.exec(authors_statement).all()
# convert from User to UserRead
authors = [UserRead.from_orm(author) for author in authors]
@ -124,6 +124,11 @@ async def create_course(
# Complete course object
course.org_id = course.org_id
# Get org uuid
org_statement = select(Organization).where(Organization.id == org_id)
org = db_session.exec(org_statement).first()
course.course_uuid = str(f"course_{uuid4()}")
course.creation_date = str(datetime.now())
course.update_date = str(datetime.now())
@ -132,9 +137,9 @@ async def create_course(
if thumbnail_file and thumbnail_file.filename:
name_in_disk = f"{course.course_uuid}_thumbnail_{uuid4()}.{thumbnail_file.filename.split('.')[-1]}"
await upload_thumbnail(
thumbnail_file, name_in_disk, org_id, course.course_uuid
thumbnail_file, name_in_disk, org.org_uuid, course.course_uuid
)
course_object.thumbnail_image = name_in_disk
course.thumbnail_image = name_in_disk
# Insert course
db_session.add(course)
@ -192,11 +197,15 @@ async def update_course_thumbnail(
# RBAC check
await rbac_check(request, course.course_uuid, current_user, "update", db_session)
# Get org uuid
org_statement = select(Organization).where(Organization.id == course.org_id)
org = db_session.exec(org_statement).first()
# Upload thumbnail
if thumbnail_file and thumbnail_file.filename:
name_in_disk = f"{course_uuid}_thumbnail_{uuid4()}.{thumbnail_file.filename.split('.')[-1]}"
await upload_thumbnail(
thumbnail_file, name_in_disk, course.org_id, course.course_uuid
thumbnail_file, name_in_disk, org.org_uuid, course.course_uuid
)
# Update course
@ -223,8 +232,6 @@ async def update_course_thumbnail(
)
authors = db_session.exec(authors_statement).all()
# convert from User to UserRead
authors = [UserRead.from_orm(author) for author in authors]
@ -331,7 +338,7 @@ async def get_courses_orgslug(
courses = db_session.exec(statement)
courses = [CourseRead(**course.dict(),authors=[]) for course in courses]
courses = [CourseRead(**course.dict(), authors=[]) for course in courses]
# for every course, get the authors
for course in courses:

View file

@ -3,13 +3,13 @@ from uuid import uuid4
from src.services.utils.upload_content import upload_content
async def upload_org_logo(logo_file, org_id):
async def upload_org_logo(logo_file, org_uuid):
contents = logo_file.file.read()
name_in_disk = f"{uuid4()}.{logo_file.filename.split('.')[-1]}"
await upload_content(
"logos",
org_id,
org_uuid,
contents,
name_in_disk,
)

View file

@ -187,7 +187,7 @@ async def update_org_logo(
await rbac_check(request, org.org_uuid, current_user, "update", db_session)
# Upload logo
name_in_disk = await upload_org_logo(logo_file, org_id)
name_in_disk = await upload_org_logo(logo_file, org.org_uuid)
# Update org
org.logo_image = name_in_disk

View file

@ -6,7 +6,7 @@ from config.config import get_learnhouse_config
async def upload_content(
directory: str, org_id: str, file_binary: bytes, file_and_format: str
directory: str, org_uuid: str, file_binary: bytes, file_and_format: str
):
# Get Learnhouse Config
learnhouse_config = get_learnhouse_config()
@ -16,12 +16,12 @@ async def upload_content(
if content_delivery == "filesystem":
# create folder for activity
if not os.path.exists(f"content/{org_id}/{directory}"):
if not os.path.exists(f"content/{org_uuid}/{directory}"):
# create folder for activity
os.makedirs(f"content/{org_id}/{directory}")
os.makedirs(f"content/{org_uuid}/{directory}")
# upload file to server
with open(
f"content/{org_id}/{directory}/{file_and_format}",
f"content/{org_uuid}/{directory}/{file_and_format}",
"wb",
) as f:
f.write(file_binary)
@ -37,13 +37,13 @@ async def upload_content(
)
# Create folder for activity
if not os.path.exists(f"content/{org_id}/{directory}"):
if not os.path.exists(f"content/{org_uuid}/{directory}"):
# create folder for activity
os.makedirs(f"content/{org_id}/{directory}")
os.makedirs(f"content/{org_uuid}/{directory}")
# Upload file to server
with open(
f"content/{org_id}/{directory}/{file_and_format}",
f"content/{org_uuid}/{directory}/{file_and_format}",
"wb",
) as f:
f.write(file_binary)
@ -52,9 +52,9 @@ async def upload_content(
print("Uploading to s3 using boto3...")
try:
s3.upload_file(
f"content/{org_id}/{directory}/{file_and_format}",
f"content/{org_uuid}/{directory}/{file_and_format}",
"learnhouse-media",
f"content/{org_id}/{directory}/{file_and_format}",
f"content/{org_uuid}/{directory}/{file_and_format}",
)
except ClientError as e:
print(e)
@ -63,7 +63,7 @@ async def upload_content(
try:
s3.head_object(
Bucket="learnhouse-media",
Key=f"content/{org_id}/{directory}/{file_and_format}",
Key=f"content/{org_uuid}/{directory}/{file_and_format}",
)
print("File upload successful!")
except Exception as e: