-
Notifications
You must be signed in to change notification settings - Fork 322
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[py-tx] Implement "File" Content Type for Image or Video subclassing (#…
- Loading branch information
1 parent
e37e1a5
commit 148d8cc
Showing
5 changed files
with
194 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#!/usr/bin/env python | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
|
||
""" | ||
Wrapper around the file content type. | ||
""" | ||
import logging | ||
from pathlib import Path | ||
from .photo import PhotoContent | ||
from .video import VideoContent | ||
from .content_base import ContentType | ||
from PIL import Image | ||
import typing as t | ||
|
||
# Initialize the logger for this module | ||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class FileContent(ContentType): | ||
""" | ||
Content type for general files, capable of routing to the appropriate | ||
specific content type (e.g., PhotoContent or VideoContent) based on file extension. | ||
""" | ||
|
||
@classmethod | ||
def map_to_content_type(cls, file_path: Path) -> t.Type[ContentType]: | ||
""" | ||
Map the file to a specific content type based on its extension by taking in file path. | ||
Returns the ContentType subclass or raises error if the file type is unsupported. | ||
""" | ||
extension = file_path.suffix.lower() | ||
logger.info(f"Processing file: {file_path}") | ||
logger.info(f"Detected file extension: {extension}") | ||
content_type: t.Type[ContentType] | ||
|
||
if extension in {".jpg", ".jpeg", ".png"}: | ||
content_type = PhotoContent | ||
elif extension in {".mp4", ".avi", ".mov"}: | ||
content_type = VideoContent | ||
elif extension == ".gif": | ||
try: | ||
with Image.open(file_path) as img: | ||
# Check if the GIF is animated | ||
is_animated = getattr(img, "is_animated", False) | ||
if is_animated: | ||
logger.info("File is an animated GIF.") | ||
content_type = VideoContent | ||
else: | ||
logger.info("File is a static GIF.") | ||
content_type = PhotoContent | ||
except Exception as e: | ||
raise ValueError(f"Error processing GIF: {e}") | ||
else: | ||
raise ValueError(f"Unsupported file type: {extension}") | ||
|
||
logger.info(f"Content type set to: {content_type.__name__}") | ||
return content_type |