You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

response_backends.py 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. from oscar.apps.image.dynamic.exceptions import ResizerConfigurationException
  2. class BaseResponse(object):
  3. def __init__(self,mime_type,cache,start_response):
  4. self.mime_type = mime_type
  5. self.cache = cache
  6. self.start_response = start_response
  7. def build_response(self):
  8. pass
  9. class DirectResponse(BaseResponse):
  10. """
  11. Serve the file directly, can use any caching mechanism
  12. """
  13. def build_response(self):
  14. """
  15. Serves the (now) cached image off the disc. It is assumed that the file
  16. actually exists as it's non-existence should have been picked up while
  17. checking to see if the cached version is valid.
  18. """
  19. status = '200 OK'
  20. data = self.cache.read()
  21. response_headers = [('Content-Type', self.mime_type),
  22. ('Content-Length', str(len(data)))]
  23. self.start_response(status, response_headers)
  24. return [data]
  25. class NginxSendfileResponse(BaseResponse):
  26. """
  27. This can only work with a disk-based caching system since it only returns
  28. headers about the file rather than the file data itself
  29. """
  30. def build_response(self):
  31. if not hasattr(self.cache, 'file_info'):
  32. msg = "Cache doesn't implements the method 'file_info'"
  33. raise ResizerConfigurationException(msg)
  34. print 'moooo'
  35. status = '200 OK'
  36. filename, content_length = self.cache.file_info()
  37. response_headers = [('Content-Type', self.mime_type),
  38. ('Content-Length', content_length),
  39. ('X-Accel-Redirect', filename)]
  40. self.start_response(status, response_headers)
  41. return ['']
  42. class ApacheSendfileResponse(BaseResponse):
  43. def build_response(self):
  44. if not hasattr(self.cache, 'file_info'):
  45. msg = "ApacheSendfileResponse requires a cache that implements the method 'file_data'"
  46. raise ResizerConfigurationException(msg)
  47. status = '200 OK'
  48. filename, content_length = self.cache.file_info()
  49. response_headers = [('Content-Type', self.mime_type),
  50. ('Content-Length', content_length),
  51. ('X-Sendfile', filename)]
  52. self.start_response(status, response_headers)
  53. return ['']