Skip to content

Commit

Permalink
Remove Base64 styling support
Browse files Browse the repository at this point in the history
  • Loading branch information
banesullivan committed Apr 18, 2022
1 parent d88d4b7 commit 3c74b7f
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 52 deletions.
26 changes: 7 additions & 19 deletions django_large_image/rest/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import base64
import binascii
import json
from typing import Union

Expand Down Expand Up @@ -28,25 +26,15 @@ def get_path(self, request: Request, pk: int = None) -> str:
raise NotImplementedError('You must implement `get_path` on your viewset.')

def get_style(self, request: Request) -> dict:
# Check for base64 encoded style dict
# Check for url encoded style JSON
if 'style' in request.query_params:
style = request.query_params.get('style')
# Un Base64 the string
if utilities.is_base64(style):
try:
message_bytes = base64.b64decode(style.encode('ascii'))
style = json.loads(message_bytes.decode('ascii'))
except (json.JSONDecodeError, binascii.Error) as e:
raise ValidationError(
f'`style` query parameter is malformed and likely not base64 encoded: {e}'
)
else:
try:
style = json.loads(style)
except json.JSONDecodeError as e:
raise ValidationError(
f'`style` query parameter is malformed and likely not properly URL encoded: {e}'
)
try:
style = json.loads(style)
except json.JSONDecodeError as e:
raise ValidationError(
f'`style` query parameter is malformed and likely not properly URL encoded: {e}'
)
# else, fallback to supported query parameters for viewing a sinlge band
else:
band = int(request.query_params.get('band', 0))
Expand Down
2 changes: 1 addition & 1 deletion django_large_image/rest/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
style = openapi.Parameter(
'style',
openapi.IN_QUERY,
description='Encoded string of JSON style following https://girder.github.io/large_image/tilesource_options.html#style . This can optionally be Base64 encoded if too large for URL.',
description='Encoded string of JSON style following https://girder.github.io/large_image/tilesource_options.html#style',
type=openapi.TYPE_STRING,
)

Expand Down
15 changes: 0 additions & 15 deletions django_large_image/utilities.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import base64
from contextlib import contextmanager
import logging
import os
Expand Down Expand Up @@ -148,17 +147,3 @@ def field_file_to_local_path(field_file: FieldFile) -> pathlib.Path:
logger.debug('Marking as safely downloaded...')
safe.touch()
return dest_path


def is_base64(sb):
try:
if isinstance(sb, str):
# If there's any unicode here, an exception will be thrown
sb_bytes = bytes(sb, 'ascii')
elif isinstance(sb, bytes):
sb_bytes = sb
else:
raise ValueError
return base64.b64encode(base64.b64decode(sb_bytes)) == sb_bytes
except Exception:
return False
17 changes: 0 additions & 17 deletions project/example/core/tests/test_style.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import base64
import json
from urllib.parse import quote

Expand All @@ -19,22 +18,6 @@ def test_style_parameters(authenticated_api_client, image_file_geotiff):
assert response['Content-Type'] == 'image/png'


@pytest.mark.django_db(transaction=True)
def test_style_base64(authenticated_api_client, image_file_geotiff):
style = {
'bands': [
{'band': 1, 'palette': ['#000', '#0f0']},
]
}
style_base64 = base64.urlsafe_b64encode(json.dumps(style).encode()).decode()
response = authenticated_api_client.get(
f'/api/image-file/{image_file_geotiff.pk}/thumbnail.png?style={style_base64}',
)
assert response.status_code == 200
assert response['Content-Type'] == 'image/png'
# TODO: might want to verify style was actually used


@pytest.mark.django_db(transaction=True)
def test_style_encoded(authenticated_api_client, image_file_geotiff):
style = {
Expand Down

0 comments on commit 3c74b7f

Please sign in to comment.