thumbnail_resource.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from .base_resource import BaseMediaResource
  16. from twisted.web.server import NOT_DONE_YET
  17. from twisted.internet import defer
  18. import logging
  19. logger = logging.getLogger(__name__)
  20. class ThumbnailResource(BaseMediaResource):
  21. isLeaf = True
  22. def render_GET(self, request):
  23. self._async_render_GET(request)
  24. return NOT_DONE_YET
  25. @BaseMediaResource.catch_errors
  26. @defer.inlineCallbacks
  27. def _async_render_GET(self, request):
  28. server_name, media_id = self._parse_media_id(request)
  29. width = self._parse_integer(request, "width")
  30. height = self._parse_integer(request, "height")
  31. method = self._parse_string(request, "method", "scale")
  32. m_type = self._parse_string(request, "type", "image/png")
  33. if server_name == self.server_name:
  34. yield self._respond_local_thumbnail(
  35. request, media_id, width, height, method, m_type
  36. )
  37. else:
  38. yield self._respond_remote_thumbnail(
  39. request, server_name, media_id,
  40. width, height, method, m_type
  41. )
  42. @defer.inlineCallbacks
  43. def _respond_local_thumbnail(self, request, media_id, width, height,
  44. method, m_type):
  45. media_info = yield self.store.get_local_media(media_id)
  46. if not media_info:
  47. self._respond_404(request)
  48. return
  49. thumbnail_infos = yield self.store.get_local_media_thumbnails(media_id)
  50. if thumbnail_infos:
  51. thumbnail_info = self._select_thumbnail(
  52. width, height, method, m_type, thumbnail_infos
  53. )
  54. t_width = thumbnail_info["thumbnail_width"]
  55. t_height = thumbnail_info["thumbnail_height"]
  56. t_type = thumbnail_info["thumbnail_type"]
  57. t_method = thumbnail_info["thumbnail_method"]
  58. file_path = self.filepaths.local_media_thumbnail(
  59. media_id, t_width, t_height, t_type, t_method,
  60. )
  61. yield self._respond_with_file(request, t_type, file_path)
  62. else:
  63. yield self._respond_default_thumbnail(
  64. request, media_info, width, height, method, m_type,
  65. )
  66. @defer.inlineCallbacks
  67. def _respond_remote_thumbnail(self, request, server_name, media_id, width,
  68. height, method, m_type):
  69. # TODO: Don't download the whole remote file
  70. # We should proxy the thumbnail from the remote server instead.
  71. media_info = yield self._get_remote_media(server_name, media_id)
  72. thumbnail_infos = yield self.store.get_remote_media_thumbnails(
  73. server_name, media_id,
  74. )
  75. if thumbnail_infos:
  76. thumbnail_info = self._select_thumbnail(
  77. width, height, method, m_type, thumbnail_infos
  78. )
  79. t_width = thumbnail_info["thumbnail_width"]
  80. t_height = thumbnail_info["thumbnail_height"]
  81. t_type = thumbnail_info["thumbnail_type"]
  82. t_method = thumbnail_info["thumbnail_method"]
  83. file_id = thumbnail_info["filesystem_id"]
  84. file_path = self.filepaths.remote_media_thumbnail(
  85. server_name, file_id, t_width, t_height, t_type, t_method,
  86. )
  87. yield self._respond_with_file(request, t_type, file_path)
  88. else:
  89. yield self._respond_default_thumbnail(
  90. request, media_info, width, height, method, m_type,
  91. )
  92. @defer.inlineCallbacks
  93. def _respond_default_thumbnail(self, request, media_info, width, height,
  94. method, m_type):
  95. media_type = media_info["media_type"]
  96. top_level_type = media_type.split("/")[0]
  97. sub_type = media_type.split("/")[-1].split(";")[0]
  98. thumbnail_infos = yield self.store.get_default_thumbnails(
  99. top_level_type, sub_type,
  100. )
  101. if not thumbnail_infos:
  102. thumbnail_infos = yield self.store.get_default_thumbnails(
  103. top_level_type, "_default",
  104. )
  105. if not thumbnail_infos:
  106. thumbnail_infos = yield self.store.get_default_thumbnails(
  107. "_default", "_default",
  108. )
  109. if not thumbnail_infos:
  110. self._respond_404(request)
  111. return
  112. thumbnail_info = self._select_thumbnail(
  113. width, height, "crop", m_type, thumbnail_infos
  114. )
  115. t_width = thumbnail_info["thumbnail_width"]
  116. t_height = thumbnail_info["thumbnail_height"]
  117. t_type = thumbnail_info["thumbnail_type"]
  118. t_method = thumbnail_info["thumbnail_method"]
  119. file_path = self.filepaths.default_thumbnail(
  120. top_level_type, sub_type, t_width, t_height, t_type, t_method,
  121. )
  122. yield self.respond_with_file(request, t_type, file_path)
  123. def _select_thumbnail(self, desired_width, desired_height, desired_method,
  124. desired_type, thumbnail_infos):
  125. d_w = desired_width
  126. d_h = desired_height
  127. if desired_method.lower() == "crop":
  128. info_list = []
  129. for info in thumbnail_infos:
  130. t_w = info["thumbnail_width"]
  131. t_h = info["thumbnail_height"]
  132. t_method = info["thumbnail_method"]
  133. if t_method == "scale" or t_method == "crop":
  134. aspect_quality = abs(d_w * t_h - d_h * t_w)
  135. size_quality = abs((d_w - t_w) * (d_h - t_h))
  136. type_quality = desired_type != info["thumbnail_type"]
  137. length_quality = info["thumbnail_length"]
  138. info_list.append((
  139. aspect_quality, size_quality, type_quality,
  140. length_quality, info
  141. ))
  142. return min(info_list)[-1]
  143. else:
  144. info_list = []
  145. for info in thumbnail_infos:
  146. t_w = info["thumbnail_width"]
  147. t_h = info["thumbnail_height"]
  148. t_method = info["thumbnail_method"]
  149. if t_method == "scale" and (t_w >= d_w or t_h >= d_h):
  150. size_quality = abs((d_w - t_w) * (d_h - t_h))
  151. type_quality = desired_type != info["thumbnail_type"]
  152. length_quality = info["thumbnail_length"]
  153. info_list.append((
  154. size_quality, type_quality, length_quality, info
  155. ))
  156. return min(info_list)[-1]