Skip to content
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

fix: Ensure result storage is passed results in bytes #1578

Merged
merged 1 commit into from
Jul 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions tests/handlers/test_base_handler_with_result_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# http://www.opensource.org/licenses/mit-license
# Copyright (c) 2011 globo.com thumbor@googlegroups.com

import json
import os
from os.path import dirname
from shutil import which
Expand Down Expand Up @@ -77,6 +78,34 @@ async def test_saves_image_to_result_storage(self, instance_mock):
expect(expected_path).to_exist()
expect(response.body).to_be_similar_to(animated_image())

@patch("tornado.ioloop.IOLoop.instance")
@gen_test
async def test_saves_meta_to_result_storage(self, instance_mock):
instance_mock.return_value = self.io_loop
response = await self.async_fetch(
"/oGi9XL4nHJKGEKVHjHEp1Jg1KpM=/meta/animated.gif"
)
expect(response.code).to_equal(200)

self.context.request = Mock(
accepts_webp=False,
)
expected_path = self.result_storage.normalize_path(
"/oGi9XL4nHJKGEKVHjHEp1Jg1KpM=/meta/animated.gif"
)
expect(expected_path).to_exist()

expected_source = {
"frameCount": 2,
"height": 100,
"url": "animated.gif",
"width": 100,
}
metadata = json.loads(response.body)
expect(metadata).to_include("thumbor")
expect(metadata["thumbor"]).to_include("source")
expect(metadata["thumbor"]["source"]).to_be_like(expected_source)


class ImageOperationsResultStorageOnlyTestCase(BaseImagingTestCase):
def get_context(self):
Expand Down
6 changes: 6 additions & 0 deletions thumbor/handlers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,11 +660,17 @@ async def finish_request(self, result_from_storage=None):
await self._write_results_to_client(results, content_type)

if should_store:
results = self._ensure_bytes(results)
await self._store_results(result_storage, metrics, results)

# can't cleanup before storing results as the storage requires context
self._cleanup()

def _ensure_bytes(self, results):
if isinstance(results, str):
return results.encode()
return results

def _cleanup(self):
self.context.request_handler = None

Expand Down