Make ThumbnailResource use MediaStorage

This commit is contained in:
Erik Johnston 2018-01-09 11:08:46 +00:00
parent 9e20840e02
commit 9d30a7691c
2 changed files with 67 additions and 47 deletions

View file

@ -637,7 +637,9 @@ class MediaRepositoryResource(Resource):
self.putChild("upload", UploadResource(hs, media_repo)) self.putChild("upload", UploadResource(hs, media_repo))
self.putChild("download", DownloadResource(hs, media_repo)) self.putChild("download", DownloadResource(hs, media_repo))
self.putChild("thumbnail", ThumbnailResource(hs, media_repo)) self.putChild("thumbnail", ThumbnailResource(
hs, media_repo, media_repo.media_storage,
))
self.putChild("identicon", IdenticonResource()) self.putChild("identicon", IdenticonResource())
if hs.config.url_preview_enabled: if hs.config.url_preview_enabled:
self.putChild("preview_url", PreviewUrlResource(hs, media_repo)) self.putChild("preview_url", PreviewUrlResource(hs, media_repo))

View file

@ -14,7 +14,10 @@
# limitations under the License. # limitations under the License.
from ._base import parse_media_id, respond_404, respond_with_file from ._base import (
parse_media_id, respond_404, respond_with_file, FileInfo,
respond_with_responder,
)
from twisted.web.resource import Resource from twisted.web.resource import Resource
from synapse.http.servlet import parse_string, parse_integer from synapse.http.servlet import parse_string, parse_integer
from synapse.http.server import request_handler, set_cors_headers from synapse.http.server import request_handler, set_cors_headers
@ -30,12 +33,12 @@ logger = logging.getLogger(__name__)
class ThumbnailResource(Resource): class ThumbnailResource(Resource):
isLeaf = True isLeaf = True
def __init__(self, hs, media_repo): def __init__(self, hs, media_repo, media_storage):
Resource.__init__(self) Resource.__init__(self)
self.store = hs.get_datastore() self.store = hs.get_datastore()
self.filepaths = media_repo.filepaths
self.media_repo = media_repo self.media_repo = media_repo
self.media_storage = media_storage
self.dynamic_thumbnails = hs.config.dynamic_thumbnails self.dynamic_thumbnails = hs.config.dynamic_thumbnails
self.server_name = hs.hostname self.server_name = hs.hostname
self.version_string = hs.version_string self.version_string = hs.version_string
@ -91,23 +94,22 @@ class ThumbnailResource(Resource):
thumbnail_info = self._select_thumbnail( thumbnail_info = self._select_thumbnail(
width, height, method, m_type, thumbnail_infos width, height, method, m_type, thumbnail_infos
) )
t_width = thumbnail_info["thumbnail_width"]
t_height = thumbnail_info["thumbnail_height"]
t_type = thumbnail_info["thumbnail_type"]
t_method = thumbnail_info["thumbnail_method"]
if media_info["url_cache"]: file_info = FileInfo(
# TODO: Check the file still exists, if it doesn't we can redownload server_name=None, file_id=media_id,
# it from the url `media_info["url_cache"]` url_cache=media_info["url_cache"],
file_path = self.filepaths.url_cache_thumbnail( thumbnail=True,
media_id, t_width, t_height, t_type, t_method, thumbnail_width=thumbnail_info["thumbnail_width"],
) thumbnail_height=thumbnail_info["thumbnail_height"],
else: thumbnail_type=thumbnail_info["thumbnail_type"],
file_path = self.filepaths.local_media_thumbnail( thumbnail_method=thumbnail_info["thumbnail_method"],
media_id, t_width, t_height, t_type, t_method, )
)
yield respond_with_file(request, t_type, file_path)
t_type = file_info.thumbnail_type
t_length = thumbnail_info["thumbnail_length"]
responder = yield self.media_storage.fetch_media(file_info)
yield respond_with_responder(request, responder, t_type, t_length)
else: else:
respond_404(request) respond_404(request)
@ -129,20 +131,23 @@ class ThumbnailResource(Resource):
t_type = info["thumbnail_type"] == desired_type t_type = info["thumbnail_type"] == desired_type
if t_w and t_h and t_method and t_type: if t_w and t_h and t_method and t_type:
if media_info["url_cache"]: file_info = FileInfo(
# TODO: Check the file still exists, if it doesn't we can redownload server_name=None, file_id=media_id,
# it from the url `media_info["url_cache"]` url_cache=media_info["url_cache"],
file_path = self.filepaths.url_cache_thumbnail( thumbnail=True,
media_id, desired_width, desired_height, desired_type, thumbnail_width=info["thumbnail_width"],
desired_method, thumbnail_height=info["thumbnail_height"],
) thumbnail_type=info["thumbnail_type"],
else: thumbnail_method=info["thumbnail_method"],
file_path = self.filepaths.local_media_thumbnail( )
media_id, desired_width, desired_height, desired_type,
desired_method, t_type = file_info.thumbnail_type
) t_length = info["thumbnail_length"]
yield respond_with_file(request, desired_type, file_path)
return responder = yield self.media_storage.fetch_media(file_info)
if responder:
yield respond_with_responder(request, responder, t_type, t_length)
return
logger.debug("We don't have a local thumbnail of that size. Generating") logger.debug("We don't have a local thumbnail of that size. Generating")
@ -175,12 +180,22 @@ class ThumbnailResource(Resource):
t_type = info["thumbnail_type"] == desired_type t_type = info["thumbnail_type"] == desired_type
if t_w and t_h and t_method and t_type: if t_w and t_h and t_method and t_type:
file_path = self.filepaths.remote_media_thumbnail( file_info = FileInfo(
server_name, file_id, desired_width, desired_height, server_name=None, file_id=media_id,
desired_type, desired_method, thumbnail=True,
thumbnail_width=info["thumbnail_width"],
thumbnail_height=info["thumbnail_height"],
thumbnail_type=info["thumbnail_type"],
thumbnail_method=info["thumbnail_method"],
) )
yield respond_with_file(request, desired_type, file_path)
return t_type = file_info.thumbnail_type
t_length = info["thumbnail_length"]
responder = yield self.media_storage.fetch_media(file_info)
if responder:
yield respond_with_responder(request, responder, t_type, t_length)
return
logger.debug("We don't have a local thumbnail of that size. Generating") logger.debug("We don't have a local thumbnail of that size. Generating")
@ -206,17 +221,20 @@ class ThumbnailResource(Resource):
thumbnail_info = self._select_thumbnail( thumbnail_info = self._select_thumbnail(
width, height, method, m_type, thumbnail_infos width, height, method, m_type, thumbnail_infos
) )
t_width = thumbnail_info["thumbnail_width"] file_info = FileInfo(
t_height = thumbnail_info["thumbnail_height"] server_name=None, file_id=media_id,
t_type = thumbnail_info["thumbnail_type"] thumbnail=True,
t_method = thumbnail_info["thumbnail_method"] thumbnail_width=thumbnail_info["thumbnail_width"],
file_id = thumbnail_info["filesystem_id"] thumbnail_height=thumbnail_info["thumbnail_height"],
thumbnail_type=thumbnail_info["thumbnail_type"],
thumbnail_method=thumbnail_info["thumbnail_method"],
)
t_type = file_info.thumbnail_type
t_length = thumbnail_info["thumbnail_length"] t_length = thumbnail_info["thumbnail_length"]
file_path = self.filepaths.remote_media_thumbnail( responder = yield self.media_storage.fetch_media(file_info)
server_name, file_id, t_width, t_height, t_type, t_method, yield respond_with_responder(request, responder, t_type, t_length)
)
yield respond_with_file(request, t_type, file_path, t_length)
else: else:
respond_404(request) respond_404(request)