Skip to content

Commit

Permalink
Merge pull request matrix-org#2791 from matrix-org/erikj/media_storag…
Browse files Browse the repository at this point in the history
…e_refactor

Ensure media is in local cache before thumbnailing
  • Loading branch information
erikjohnston authored Feb 5, 2018
2 parents 9c93565 + ce4f661 commit 1f881e0
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 12 deletions.
20 changes: 11 additions & 9 deletions synapse/rest/media/v1/media_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,10 @@ def _generate_thumbnail(self, thumbnailer, t_width, t_height,

@defer.inlineCallbacks
def generate_local_exact_thumbnail(self, media_id, t_width, t_height,
t_method, t_type):
input_path = self.filepaths.local_media_filepath(media_id)
t_method, t_type, url_cache):
input_path = yield self.media_storage.ensure_media_is_in_local_cache(FileInfo(
None, media_id, url_cache=url_cache,
))

thumbnailer = Thumbnailer(input_path)
t_byte_source = yield make_deferred_yieldable(threads.deferToThread(
Expand All @@ -486,6 +488,7 @@ def generate_local_exact_thumbnail(self, media_id, t_width, t_height,
file_info = FileInfo(
server_name=None,
file_id=media_id,
url_cache=url_cache,
thumbnail=True,
thumbnail_width=t_width,
thumbnail_height=t_height,
Expand All @@ -512,7 +515,9 @@ def generate_local_exact_thumbnail(self, media_id, t_width, t_height,
@defer.inlineCallbacks
def generate_remote_exact_thumbnail(self, server_name, file_id, media_id,
t_width, t_height, t_method, t_type):
input_path = self.filepaths.remote_media_filepath(server_name, file_id)
input_path = yield self.media_storage.ensure_media_is_in_local_cache(FileInfo(
server_name, file_id, url_cache=False,
))

thumbnailer = Thumbnailer(input_path)
t_byte_source = yield make_deferred_yieldable(threads.deferToThread(
Expand Down Expand Up @@ -570,12 +575,9 @@ def _generate_thumbnails(self, server_name, media_id, file_id, media_type,
if not requirements:
return

if server_name:
input_path = self.filepaths.remote_media_filepath(server_name, file_id)
elif url_cache:
input_path = self.filepaths.url_cache_filepath(media_id)
else:
input_path = self.filepaths.local_media_filepath(media_id)
input_path = yield self.media_storage.ensure_media_is_in_local_cache(FileInfo(
server_name, file_id, url_cache=url_cache,
))

thumbnailer = Thumbnailer(input_path)
m_width = thumbnailer.width
Expand Down
36 changes: 34 additions & 2 deletions synapse/rest/media/v1/media_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from ._base import Responder

from synapse.util.file_consumer import BackgroundFileConsumer
from synapse.util.logcontext import make_deferred_yieldable

import contextlib
Expand All @@ -26,6 +27,7 @@
import shutil
import sys


logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -151,6 +153,37 @@ def fetch_media(self, file_info):

defer.returnValue(None)

@defer.inlineCallbacks
def ensure_media_is_in_local_cache(self, file_info):
"""Ensures that the given file is in the local cache. Attempts to
download it from storage providers if it isn't.
Args:
file_info (FileInfo)
Returns:
Deferred[str]: Full path to local file
"""
path = self._file_info_to_path(file_info)
local_path = os.path.join(self.local_media_directory, path)
if os.path.exists(local_path):
defer.returnValue(local_path)

dirname = os.path.dirname(local_path)
if not os.path.exists(dirname):
os.makedirs(dirname)

for provider in self.storage_providers:
res = yield provider.fetch(path, file_info)
if res:
with res:
consumer = BackgroundFileConsumer(open(local_path, "w"))
yield res.write_to_consumer(consumer)
yield consumer.wait()
defer.returnValue(local_path)

raise Exception("file could not be found")

def _file_info_to_path(self, file_info):
"""Converts file_info into a relative path.
Expand Down Expand Up @@ -228,9 +261,8 @@ class FileResponder(Responder):
def __init__(self, open_file):
self.open_file = open_file

@defer.inlineCallbacks
def write_to_consumer(self, consumer):
yield FileSender().beginFileTransfer(self.open_file, consumer)
return FileSender().beginFileTransfer(self.open_file, consumer)

def __exit__(self, exc_type, exc_val, exc_tb):
self.open_file.close()
3 changes: 2 additions & 1 deletion synapse/rest/media/v1/thumbnail_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ def _select_or_generate_local_thumbnail(self, request, media_id, desired_width,

# Okay, so we generate one.
file_path = yield self.media_repo.generate_local_exact_thumbnail(
media_id, desired_width, desired_height, desired_method, desired_type
media_id, desired_width, desired_height, desired_method, desired_type,
url_cache=media_info["url_cache"],
)

if file_path:
Expand Down
14 changes: 14 additions & 0 deletions tests/rest/media/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# -*- coding: utf-8 -*-
# Copyright 2018 New Vector Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
14 changes: 14 additions & 0 deletions tests/rest/media/v1/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# -*- coding: utf-8 -*-
# Copyright 2018 New Vector Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
81 changes: 81 additions & 0 deletions tests/rest/media/v1/test_media_storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# -*- coding: utf-8 -*-
# Copyright 2018 New Vector Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from twisted.internet import defer

from synapse.rest.media.v1._base import FileInfo
from synapse.rest.media.v1.media_storage import MediaStorage
from synapse.rest.media.v1.filepath import MediaFilePaths
from synapse.rest.media.v1.storage_provider import FileStorageProviderBackend

from tests import unittest

import os
import shutil
import tempfile


class MediaStorageTests(unittest.TestCase):
def setUp(self):
self.test_dir = tempfile.mkdtemp(prefix="synapse-tests-")

self.primary_base_path = os.path.join(self.test_dir, "primary")
self.secondary_base_path = os.path.join(self.test_dir, "secondary")

storage_providers = [FileStorageProviderBackend(
self.primary_base_path, self.secondary_base_path
)]

self.filepaths = MediaFilePaths(self.primary_base_path)
self.media_storage = MediaStorage(
self.primary_base_path, self.filepaths, storage_providers,
)

def tearDown(self):
shutil.rmtree(self.test_dir)

@defer.inlineCallbacks
def test_ensure_media_is_in_local_cache(self):
media_id = "some_media_id"
test_body = "Test\n"

# First we create a file that is in a storage provider but not in the
# local primary media store
rel_path = self.filepaths.local_media_filepath_rel(media_id)
secondary_path = os.path.join(self.secondary_base_path, rel_path)

os.makedirs(os.path.dirname(secondary_path))

with open(secondary_path, "w") as f:
f.write(test_body)

# Now we run ensure_media_is_in_local_cache, which should copy the file
# to the local cache.
file_info = FileInfo(None, media_id)
local_path = yield self.media_storage.ensure_media_is_in_local_cache(file_info)

self.assertTrue(os.path.exists(local_path))

# Asserts the file is under the expected local cache directory
self.assertEquals(
os.path.commonprefix([self.primary_base_path, local_path]),
self.primary_base_path,
)

with open(local_path) as f:
body = f.read()

self.assertEqual(test_body, body)

0 comments on commit 1f881e0

Please sign in to comment.