Skip to content

Commit

Permalink
feat(schemas): Media paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Alirezaja1384 committed Oct 22, 2024
1 parent 57ca3b0 commit 2616d4f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
17 changes: 16 additions & 1 deletion api/micro_media/media/context.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Literal, Self, Type
from typing import Literal, Self, Type, overload

import yaml

Expand Down Expand Up @@ -46,6 +46,21 @@ def from_yaml_file(cls, config_path: str) -> Self:
config=MediaConfig.model_validate(yaml.safe_load(config_file))
)

@overload
def get_manager(
self, media_type: Literal["image"]
) -> ImageMediaManager: ...

@overload
def get_manager(
self, media_type: Literal["video"]
) -> BaseMediaManager: ...

@overload
def get_manager(
self, media_type: Literal["document"]
) -> BaseMediaManager: ...

def get_manager(self, media_type: MediaType) -> BaseMediaManager:
"""
Returns the media_type's manager.
Expand Down
31 changes: 30 additions & 1 deletion api/micro_media/schemas/v1/media.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
from enum import Enum
from typing import Annotated
from typing import Annotated, Literal

from uuid import UUID
from pydantic import Field, computed_field, create_model
from fastapi import File, Form, UploadFile

from micro_media.media.manager import ImageMediaManager
from micro_media.utils import APIModel
from micro_media.media import MEDIA_CONTEXT as MC


class MediaType(str, Enum):
Expand Down Expand Up @@ -38,5 +43,29 @@ def as_form(
)


THUMBNAIL_SIZES = MC.get_manager("image").get_thumbnail_sizes()

ThumbnailSizesModel = create_model(
"ThumbnailSizesModel",
**{size: (str, Field(...)) for size in THUMBNAIL_SIZES},
)


class MediaRead(MediaBase):
id: UUID

@computed_field
def original_path(self) -> str:
return f"/v1/public/media/original/{self.id}"

@computed_field
def thumbnail_paths(self) -> ThumbnailSizesModel | None:
if self.media_type != MediaType.IMAGE:
return None

return ThumbnailSizesModel.model_validate(
{
size: f"/v1/public/media/thumbnail/{self.id}?size={size}"
for size in THUMBNAIL_SIZES
}
)

0 comments on commit 2616d4f

Please sign in to comment.