-
Notifications
You must be signed in to change notification settings - Fork 321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[pytx] Implement FileContent class #1680
Open
ZeyadTarekk
wants to merge
9
commits into
facebook:main
Choose a base branch
from
ZeyadTarekk:implement_file_content_type
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a661224
Implement filecontent class
ZeyadTarekkk b31647e
discard extract_additional_content changes
ZeyadTarekk 8aff837
Change the valid photo types
ZeyadTarekk d18a8e9
Change the unit tests
ZeyadTarekk 95282f0
use pytest in unit tests
ZeyadTarekkk 69d9abc
modify the tests
ZeyadTarekk bb816aa
Fix linting problems
ZeyadTarekk a5a1930
Fix error
ZeyadTarekk 2580a48
fix formatting
ZeyadTarekk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
python-threatexchange/threatexchange/content_type/file_content.py
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,31 @@ | ||
import typing as t | ||
from content_base import ContentType | ||
from photo import PhotoContent | ||
from video import VideoContent | ||
from PIL import Image | ||
|
||
|
||
class FileContent(ContentType): | ||
""" | ||
ContentType representing a generic file. | ||
|
||
Determines if a file is a photo or video based on file extension. | ||
""" | ||
|
||
VALID_PHOTO_EXTENSIONS = {ext.lower() for ext in Image.registered_extensions()} | ||
VALID_VIDEO_EXTENSIONS = {".mp4", ".avi", ".mov", ".mkv"} | ||
|
||
@classmethod | ||
def get_content_type_from_filename(cls, file_name: str) -> t.Type[ContentType]: | ||
""" | ||
Determines content type based on file extension. | ||
""" | ||
file_extension = file_name.lower().rsplit(".", 1)[-1] | ||
file_extension = f".{file_extension}" | ||
|
||
if file_extension in cls.VALID_PHOTO_EXTENSIONS: | ||
return PhotoContent | ||
elif file_extension in cls.VALID_VIDEO_EXTENSIONS: | ||
return VideoContent | ||
else: | ||
return None |
25 changes: 25 additions & 0 deletions
25
python-threatexchange/threatexchange/content_type/tests/test_file_content_type.py
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,25 @@ | ||
import pytest | ||
from threatexchange.content_type.photo import PhotoContent | ||
from threatexchange.content_type.video import VideoContent | ||
from threatexchange.content_type.file_content import FileContent | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"file_name,expected_content_type", | ||
[ | ||
("file.jpg", PhotoContent), | ||
("file.JPG", PhotoContent), | ||
("file.mp4", VideoContent), | ||
("file.MP4", VideoContent), | ||
("archive.photo.png", PhotoContent), | ||
("movie.backup.mp4", VideoContent), | ||
("file.txt", None), | ||
], | ||
) | ||
def test_file_content_detection(file_name, expected_content_type): | ||
""" | ||
Tests that FileContent correctly identifies the content type | ||
as either PhotoContent or VideoContent based on file extension. | ||
""" | ||
content_type = FileContent.get_content_type_from_filename(file_name) | ||
assert content_type == expected_content_type |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is doing work during module import time, which is generally considered an anti-pattern, but I don't think it's worth changing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you provide more details?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you tell me more details?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's the first result for me from google for "doing work during module import time"
https://www.benkuhn.net/importtime/
As mentioned, I don't think it's worth changing.