Skip to content

Commit

Permalink
- Added Env variables transfer into tests.yml
Browse files Browse the repository at this point in the history
- Changed image files support
- Changed image_compress
  • Loading branch information
onstabb committed Nov 17, 2023
1 parent cd41452 commit b5d2c4b
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 17 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ jobs:
env:
AUTH_SECRET_KEY: ${{ secrets.AUTH_SECRET_KEY }}
DB_HOST: ${{ secrets.DB_HOST }}
S3_API_ENDPOINT: ${{ env.S3_API_ENDPOINT }}
S3_IPFS_URL: ${{ env.S3_IPFS_URL }}
S3_ACCESS_KEY_ID: ${{ secrets.S3_ACCESS_KEY_ID }}
S3_SECRET_ACCESS_KEY_ID: ${{ secrets.S3_SECRET_ACCESS_KEY_ID }}

steps:
- name: Checkout code
Expand Down
2 changes: 0 additions & 2 deletions src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

SERVER_URL: str = os.getenv("SERVER_URL", "http://127.0.0.1:8000")

DB_USER: str = os.getenv("DB_USER", "admin")
DB_PASSWORD: str = os.getenv("DB_PASSWORD", "secretmongo")
DB_NAME: str = os.getenv("DB_NAME", "iamnotalone")
DB_HOST: str = os.getenv("DB_HOST", "localhost")

Expand Down
2 changes: 1 addition & 1 deletion src/photos/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
IMAGE_FILES_LOCAL_PATH.mkdir()

SERVER_STATIC_IMAGES_URL = f'{global_config.SERVER_URL}/static/images'
SUPPORTED_IMAGE_FORMATS = ('jpeg', 'jpg', 'bmp', 'png')
SUPPORTED_IMAGE_FORMATS = ('jpeg', 'jpg')
SUPPORTED_IMAGE_MEDIA_TYPES = tuple(f'image/{img_format}' for img_format in SUPPORTED_IMAGE_FORMATS)
FILE_TOKEN_LENGTH = 24
FILE_IMAGE_COMPRESSION_PERCENT: int = 50
Expand Down
20 changes: 8 additions & 12 deletions src/photos/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@
from pydantic import HttpUrl, Json
from pydantic_core import Url


from photos import config


log = logging.getLogger(__file__)


def filename_token_encode(filename: str, **additional_data: Json) -> str:
additional_data.update(filename=filename)
def filename_token_encode(extension: str, **additional_data: Json) -> str:
data = json.dumps(additional_data, ensure_ascii=True)
extension = filename.split(".")[-1]
return f'{base64.b64encode(data.encode(encoding="ascii")).decode("ascii")}.{extension}'


Expand Down Expand Up @@ -55,15 +52,14 @@ def check_image_is_valid(file: typing.BinaryIO) -> bool:


def image_compress(
file: typing.BinaryIO, *,
percent: int = config.FILE_IMAGE_COMPRESSION_PERCENT,
max_pixel_size: int = config.FILE_IMAGE_MAX_PIXEL_SIZE,
) -> TemporaryFile:

""" Creates a temporary file with compressed image """
file: typing.BinaryIO, *,
percent: int = config.FILE_IMAGE_COMPRESSION_PERCENT,
max_pixel_size: int = config.FILE_IMAGE_MAX_PIXEL_SIZE,
) -> typing.BinaryIO:
""" Creates a temporary file with compressed JPEG image """

original_image: Image = Image.open(file)
temp_file = TemporaryFile(suffix=f'.{original_image.format.lower()}')
temp_file = TemporaryFile(suffix=f'.jpg')

if max_pixel_size:
width, height = original_image.size
Expand All @@ -84,7 +80,7 @@ def image_compress(
original_image = original_image.resize((int(width * reduce_factor), int(height * reduce_factor)))

quality: int = 100 - percent
original_image.save(temp_file, original_image.format, quality=quality, optimize=True)
original_image.save(temp_file, format="jpeg", quality=quality, optimize=True)
original_image.close()
temp_file.seek(0)
return temp_file
3 changes: 2 additions & 1 deletion src/photos/routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ def update_profile_photo(
)

compressed_image = helpers.image_compress(photo.file)
extension: str = photo.filename.split(".")[-1]
image_url = service.get_bucket().upload(
compressed_image,
filename=helpers.filename_token_encode(photo.filename, user_id=str(current_user.id), index=list_index),
filename=helpers.filename_token_encode(extension, user_id=str(current_user.id), index=list_index),
ContentType=photo.content_type
)

Expand Down
Binary file removed tests/data/images/Test2.png
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/test_photos/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
)
def test_token_encoding(filename):
data = {"filename": filename, "num": 322}
encoded_filename = helpers.filename_token_encode(**data)
encoded_filename = helpers.filename_token_encode("jpg", **data)
decoded_data = helpers.filename_token_decode(encoded_filename)
assert data == decoded_data

Expand Down

0 comments on commit b5d2c4b

Please sign in to comment.