Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

response_backends.py 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. from oscar.apps.image.dynamic.exceptions import ResizerConfigurationException
  2. class BaseResponse(object):
  3. def __init__(self,config,mime_type,cache,start_response):
  4. self.config = config
  5. self.mime_type = mime_type
  6. self.cache = cache
  7. self.start_response = start_response
  8. def build_response(self):
  9. pass
  10. class DirectResponse(BaseResponse):
  11. """
  12. Serve the file directly, can use any caching mechanism
  13. """
  14. def build_response(self):
  15. """
  16. Serves the cached image directly.
  17. """
  18. status = '200 OK'
  19. data = self.cache.read()
  20. response_headers = [('Content-Type', self.mime_type),
  21. ('Content-Length', str(len(data)))]
  22. self.start_response(status, response_headers)
  23. return [data]
  24. class NginxSendfileResponse(BaseResponse):
  25. """
  26. This can only work with a disk-based caching system since it only returns
  27. headers about the file rather than the file data itself
  28. """
  29. def build_response(self):
  30. if not hasattr(self.cache, 'file_info'):
  31. msg = "Cache doesn't implements the method 'file_info'"
  32. raise ResizerConfigurationException(msg)
  33. if not self.config.get('nginx_sendfile_path',None):
  34. msg = 'Must provide nginx_sendfile_path in configuration'
  35. raise ResizerConfigurationException(msg)
  36. status = '200 OK'
  37. filename, content_length = self.cache.file_info()
  38. filename = self.config['nginx_sendfile_path'] + filename
  39. response_headers = [('Content-Type', self.mime_type),
  40. ('Content-Length', content_length),
  41. ('X-Accel-Redirect', filename)]
  42. self.start_response(status, response_headers)
  43. return ['']