Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions app/core/associations/endpoints_associations.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
)
from app.types.content_type import ContentType
from app.types.module import CoreModule
from app.utils.tools import get_file_from_data, save_file_as_data
from app.utils.tools import (
compress_and_save_image_file,
get_file_from_data,
)

router = APIRouter(tags=["Associations"])

Expand Down Expand Up @@ -173,16 +176,19 @@ async def create_association_logo(
if not association:
raise HTTPException(status_code=404, detail="Association not found")

await save_file_as_data(
await compress_and_save_image_file(
upload_file=image,
directory="associations/logos",
filename=association_id,
max_file_size=4 * 1024 * 1024,
accepted_content_types=[
ContentType.jpg,
ContentType.png,
ContentType.webp,
],
max_file_size=1024 * 1024 * 5, # 5 MB
height=300,
width=300,
quality=85,
)


Expand Down
9 changes: 6 additions & 3 deletions app/core/groups/endpoints_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
from app.types.module import CoreModule
from app.utils.communication.notifications import NotificationManager
from app.utils.tools import (
compress_and_save_image_file,
get_file_from_data,
is_user_member_of_any_group,
save_file_as_data,
)

router = APIRouter(tags=["Groups"])
Expand Down Expand Up @@ -374,16 +374,19 @@ async def create_group_logo(
if not group:
raise HTTPException(status_code=404, detail="Group not found")

await save_file_as_data(
await compress_and_save_image_file(
upload_file=image,
directory="groups/logos",
filename=group_id,
max_file_size=4 * 1024 * 1024,
accepted_content_types=[
ContentType.jpg,
ContentType.png,
ContentType.webp,
],
max_file_size=1024 * 1024 * 5, # 5 MB
height=300,
width=300,
quality=85,
)


Expand Down
9 changes: 6 additions & 3 deletions app/core/users/endpoints_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@
from app.utils.communication.notifications import NotificationManager
from app.utils.mail.mailworker import send_email
from app.utils.tools import (
compress_and_save_image_file,
create_and_send_email_migration,
get_file_from_data,
save_file_as_data,
sort_user,
)

Expand Down Expand Up @@ -1114,16 +1114,19 @@ async def create_current_user_profile_picture(
**The user must be authenticated to use this endpoint**
"""

await save_file_as_data(
await compress_and_save_image_file(
upload_file=image,
directory="profile-pictures",
filename=user.id,
max_file_size=4 * 1024 * 1024,
accepted_content_types=[
ContentType.jpg,
ContentType.png,
ContentType.webp,
],
max_file_size=1024 * 1024 * 5, # 5 MB
height=300,
width=300,
quality=85,
)

return standard_responses.Result(success=True)
Expand Down
10 changes: 7 additions & 3 deletions app/modules/advert/endpoints_advert.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
from app.types.module import Module
from app.utils.communication.notifications import NotificationManager, NotificationTool
from app.utils.tools import (
compress_and_save_image_file,
get_file_from_data,
is_user_member_of_an_association,
is_user_member_of_an_association_id,
save_file_as_data,
)

root = "advert"
Expand Down Expand Up @@ -323,14 +323,18 @@ async def create_advert_image(
detail=f"Unauthorized to manage {advert.advertiser_id} adverts",
)

await save_file_as_data(
await compress_and_save_image_file(
upload_file=image,
directory="adverts",
filename=advert_id,
max_file_size=4 * 1024 * 1024,
accepted_content_types=[
ContentType.jpg,
ContentType.png,
ContentType.webp,
],
max_file_size=1024 * 1024 * 5, # 5 MB
height=315,
width=851,
quality=85,
fit=True,
)
10 changes: 7 additions & 3 deletions app/modules/calendar/endpoints_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@
from app.types.module import Module
from app.utils.communication.notifications import NotificationManager, NotificationTool
from app.utils.tools import (
compress_and_save_image_file,
delete_file_from_data,
get_file_from_data,
is_user_member_of_an_association,
is_user_member_of_any_group,
save_file_as_data,
)

module = Module(
Expand Down Expand Up @@ -229,16 +229,20 @@ async def create_event_image(
detail="You are not allowed to access this event",
)

await save_file_as_data(
await compress_and_save_image_file(
upload_file=image,
directory="event",
filename=event_id,
max_file_size=4 * 1024 * 1024,
accepted_content_types=[
ContentType.jpg,
ContentType.png,
ContentType.webp,
],
max_file_size=1024 * 1024 * 5, # 5 MB
height=315,
width=851,
quality=85,
fit=True,
)


Expand Down
9 changes: 6 additions & 3 deletions app/modules/campaign/endpoints_campaign.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
from app.types.content_type import ContentType
from app.types.module import Module
from app.utils.tools import (
compress_and_save_image_file,
get_file_from_data,
is_user_member_of_any_group,
save_file_as_data,
)

module = Module(
Expand Down Expand Up @@ -816,16 +816,19 @@ async def create_campaigns_logo(
detail="The list does not exist.",
)

await save_file_as_data(
await compress_and_save_image_file(
upload_file=image,
directory="campaigns",
filename=str(list_id),
max_file_size=4 * 1024 * 1024,
accepted_content_types=[
ContentType.jpg,
ContentType.png,
ContentType.webp,
],
max_file_size=1024 * 1024 * 5, # 5 MB
height=300,
width=300,
quality=85,
)

return standard_responses.Result(success=True)
Expand Down
13 changes: 10 additions & 3 deletions app/modules/cinema/endpoints_cinema.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@
get_previous_sunday,
)
from app.utils.communication.notifications import NotificationTool
from app.utils.tools import get_file_from_data, save_file_as_data
from app.utils.tools import (
compress_and_save_image_file,
get_file_from_data,
)

root = "cinema"
cinema_topic = Topic(
Expand Down Expand Up @@ -214,16 +217,20 @@ async def create_campaigns_logo(
detail="The session does not exist.",
)

await save_file_as_data(
await compress_and_save_image_file(
upload_file=image,
directory="cinemasessions",
filename=str(session_id),
max_file_size=4 * 1024 * 1024,
accepted_content_types=[
ContentType.jpg,
ContentType.png,
ContentType.webp,
],
max_file_size=1024 * 1024 * 5, # 5 MB
height=750,
width=500,
quality=85,
fit=True,
)

return standard_responses.Result(success=True)
Expand Down
7 changes: 4 additions & 3 deletions app/modules/phonebook/endpoints_phonebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
from app.types.content_type import ContentType
from app.types.module import Module
from app.utils.tools import (
compress_and_save_image_file,
get_file_from_data,
is_user_member_of_any_group,
save_file_as_data,
)

module = Module(
Expand Down Expand Up @@ -690,17 +690,18 @@ async def create_association_logo(
detail="The Association does not exist.",
)

await save_file_as_data(
await compress_and_save_image_file(
upload_file=image,
directory="associations",
filename=association_id,
max_file_size=4 * 1024 * 1024,
accepted_content_types=[
ContentType.jpg,
ContentType.png,
ContentType.webp,
],
max_file_size=1024 * 1024 * 5, # 5 MB
)

return standard_responses.Result(success=True)


Expand Down
6 changes: 4 additions & 2 deletions app/modules/raffle/endpoints_raffle.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from app.types.module import Module
from app.utils.redis import locker_get, locker_set
from app.utils.tools import (
compress_and_save_image_file,
get_file_from_data,
is_user_member_of_any_group,
save_file_as_data,
Expand Down Expand Up @@ -228,16 +229,17 @@ async def create_current_raffle_logo(
detail=f"Raffle {raffle_id} is not in Creation Mode",
)

await save_file_as_data(
await compress_and_save_image_file(
upload_file=image,
directory="raffle-pictures",
filename=str(raffle_id),
max_file_size=4 * 1024 * 1024,
accepted_content_types=[
ContentType.jpg,
ContentType.png,
ContentType.webp,
],
max_file_size=1024 * 1024 * 5, # 5 MB
fit=True,
)

return standard_responses.Result(success=True)
Expand Down
12 changes: 9 additions & 3 deletions app/modules/recommendation/endpoints_recommendation.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
from app.types import standard_responses
from app.types.content_type import ContentType
from app.types.module import Module
from app.utils.tools import get_file_from_data, save_file_as_data
from app.utils.tools import (
compress_and_save_image_file,
get_file_from_data,
)

router = APIRouter()

Expand Down Expand Up @@ -184,16 +187,19 @@ async def create_recommendation_image(
if not recommendation:
raise HTTPException(status_code=404, detail="The recommendation does not exist")

await save_file_as_data(
await compress_and_save_image_file(
upload_file=image,
directory="recommendations",
filename=str(recommendation_id),
max_file_size=4 * 1024 * 1024,
accepted_content_types=[
ContentType.jpg,
ContentType.png,
ContentType.webp,
],
max_file_size=1024 * 1024 * 5, # 5 MB
height=300,
width=300,
quality=85,
)

return standard_responses.Result(success=True)
30 changes: 30 additions & 0 deletions app/types/content_type.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from enum import Enum

from app.types.exceptions import UnknownContentTypeExtensionError


class ContentType(str, Enum):
"""
Expand All @@ -10,3 +12,31 @@ class ContentType(str, Enum):
png = "image/png"
webp = "image/webp"
pdf = "application/pdf"

@property
def extension(self) -> str:
"""
Get the file extension corresponding to the content type
"""
if self == ContentType.jpg:
return "jpg"
if self == ContentType.png:
return "png"
if self == ContentType.webp:
return "webp"
if self == ContentType.pdf:
return "pdf"
raise UnknownContentTypeExtensionError(content_type=self.value)

def __str__(self):
return self.extension


class PillowImageFormat(str, Enum):
"""
Accepted image formats for Pillow
"""

jpg = "JPEG"
png = "PNG"
webp = "WEBP"
7 changes: 7 additions & 0 deletions app/types/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,10 @@ def __init__(self, object_name: str):
super().__init__(
f"Newly added object {object_name} not found in the database",
)


class UnknownContentTypeExtensionError(Exception):
def __init__(self, content_type: str):
super().__init__(
f"Unknown content type extension for content type: {content_type}",
)
Loading
Loading