-
Notifications
You must be signed in to change notification settings - Fork 0
[4133] Тип файлов для аудио #106
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
Open
Open
Changes from all commits
Commits
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
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Empty file.
This file contains hidden or 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,28 @@ | ||
| import mimetypes | ||
|
|
||
| from app.common.filetype_ext import SUPPORTED_AUDIO_FORMATS | ||
|
|
||
| SUPPORTED_AUDIO_MIME_TYPES: dict[str, str] = { | ||
| # taken from https://developer.mozilla.org/en-US/docs/Web/Media/Guides/Formats/Containers | ||
| "audio/aac": ".aac", | ||
| "audio/mpeg": ".mp3", | ||
| "audio/mp3": ".mp3", | ||
| "audio/mp4": ".m4a", | ||
| "audio/ogg": ".ogg", | ||
| "audio/flac": ".flac", | ||
| "audio/x-flac": ".flac", | ||
| "audio/wave": ".wav", | ||
| "audio/wav": ".wav", | ||
| "audio/x-wav": ".wav", | ||
| "audio/x-pn-wav": ".wav", | ||
| } | ||
|
|
||
|
|
||
| def add_missing_mime_to_mimetypes() -> None: | ||
| for audio_format in SUPPORTED_AUDIO_FORMATS: | ||
| if mimetypes.guess_extension(audio_format.mime) is None: | ||
| mimetypes.add_type(audio_format.mime, f".{audio_format.extension}") | ||
|
|
||
| for mime, extension in SUPPORTED_AUDIO_MIME_TYPES.items(): | ||
| if mimetypes.guess_extension(mime) is None: | ||
| mimetypes.add_type(mime, extension) |
This file contains hidden or 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,57 @@ | ||
| """audio_files | ||
|
|
||
| Revision ID: 064 | ||
| Revises: 063 | ||
| Create Date: 2026-04-17 04:07:30.284265 | ||
|
|
||
| """ | ||
|
|
||
| from typing import Sequence, Union | ||
|
|
||
| import sqlalchemy as sa | ||
| from alembic import op | ||
|
|
||
| schema_name = "xi_back_2" | ||
| table_name = "files" | ||
| column_name = "kind" | ||
| enum_name = "file_kind" | ||
| tmp_enum_name = f"_{enum_name}" | ||
|
|
||
| old_enum = sa.Enum("UNCATEGORIZED", "IMAGE", "DOCUMENT", name=enum_name) | ||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision: str = "064" | ||
| down_revision: Union[str, None] = "063" | ||
| branch_labels: Union[str, Sequence[str], None] = None | ||
| depends_on: Union[str, Sequence[str], None] = None | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| op.execute(f"ALTER TYPE {enum_name} ADD VALUE 'AUDIO'") | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| conn = op.get_bind() | ||
|
|
||
| # rename new enum | ||
| op.execute(f"ALTER TYPE {enum_name} RENAME TO {tmp_enum_name}") | ||
|
|
||
| # update old rows | ||
| metadata = sa.MetaData(schema=schema_name) | ||
| Files = sa.Table(table_name, metadata, autoload_with=conn) | ||
|
|
||
| conn.execute( | ||
| sa.update(Files).where(Files.c.kind == "AUDIO").values(kind="UNCATEGORIZED") | ||
| ) | ||
|
|
||
| # remove old members by updating to the new enum | ||
| old_enum.create(bind=conn) | ||
| op.execute( | ||
| f"ALTER TABLE {schema_name}.{table_name}" | ||
| f" ALTER COLUMN {column_name}" | ||
| f" TYPE {old_enum.name}" | ||
| f" USING {column_name}::text::{old_enum.name}" | ||
| ) | ||
|
|
||
| # remove new enum | ||
| op.execute(f"DROP TYPE {tmp_enum_name}") |
This file contains hidden or 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 hidden or 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
Empty file.
35 changes: 35 additions & 0 deletions
35
tests/storage_v2/unit/test_adding_missing_mime_to_mimetypes.py
This file contains hidden or 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,35 @@ | ||
| import mimetypes | ||
|
|
||
| import filetype # type: ignore[import-untyped] | ||
| import pytest | ||
|
|
||
| from app.common.filetype_ext import SUPPORTED_AUDIO_FORMATS | ||
| from app.storage_v2.utils.mimetypes import SUPPORTED_AUDIO_MIME_TYPES | ||
|
|
||
| pytestmark = pytest.mark.anyio | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "audio_format", | ||
| [pytest.param(audio, id=audio.extension) for audio in SUPPORTED_AUDIO_FORMATS], | ||
| ) | ||
| async def test_matching_extension_by_filetype_mime_types( | ||
| audio_format: filetype.Type, | ||
| ) -> None: | ||
| assert f".{audio_format.extension}" in mimetypes.guess_all_extensions( | ||
| audio_format.mime | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ( | ||
| "mime", | ||
| "extension", | ||
| ), | ||
| [ | ||
| pytest.param(mime, extension, id=extension) | ||
| for mime, extension in SUPPORTED_AUDIO_MIME_TYPES.items() | ||
| ], | ||
| ) | ||
| async def test_presence_extension_by_mime_types(mime: str, extension: str) -> None: | ||
| assert extension in mimetypes.guess_all_extensions(mime) |
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.
Uh oh!
There was an error while loading. Please reload this page.