2018-01-17 17:56:35 +01:00
|
|
|
# -*- 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.
|
2018-07-09 08:09:20 +02:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import tempfile
|
2020-01-22 13:30:49 +01:00
|
|
|
from binascii import unhexlify
|
2020-06-05 12:54:27 +02:00
|
|
|
from io import BytesIO
|
|
|
|
from typing import Optional
|
2020-06-16 14:51:47 +02:00
|
|
|
from urllib import parse
|
2018-07-09 08:09:20 +02:00
|
|
|
|
|
|
|
from mock import Mock
|
|
|
|
|
2020-06-05 12:54:27 +02:00
|
|
|
import attr
|
|
|
|
from parameterized import parameterized_class
|
2020-07-05 17:32:02 +02:00
|
|
|
from PIL import Image as Image
|
2020-06-05 12:54:27 +02:00
|
|
|
|
2020-07-24 15:39:02 +02:00
|
|
|
from twisted.internet import defer
|
2018-11-15 22:55:58 +01:00
|
|
|
from twisted.internet.defer import Deferred
|
2018-01-17 17:56:35 +01:00
|
|
|
|
2019-07-03 16:07:04 +02:00
|
|
|
from synapse.logging.context import make_deferred_yieldable
|
2018-01-17 17:56:35 +01:00
|
|
|
from synapse.rest.media.v1._base import FileInfo
|
|
|
|
from synapse.rest.media.v1.filepath import MediaFilePaths
|
2018-07-09 08:09:20 +02:00
|
|
|
from synapse.rest.media.v1.media_storage import MediaStorage
|
2018-01-17 17:56:35 +01:00
|
|
|
from synapse.rest.media.v1.storage_provider import FileStorageProviderBackend
|
|
|
|
|
|
|
|
from tests import unittest
|
2020-11-14 00:11:43 +01:00
|
|
|
from tests.server import FakeSite, make_request
|
2018-01-17 17:56:35 +01:00
|
|
|
|
|
|
|
|
2019-06-29 09:06:55 +02:00
|
|
|
class MediaStorageTests(unittest.HomeserverTestCase):
|
|
|
|
|
|
|
|
needs_threadpool = True
|
|
|
|
|
|
|
|
def prepare(self, reactor, clock, hs):
|
2018-01-17 17:56:35 +01:00
|
|
|
self.test_dir = tempfile.mkdtemp(prefix="synapse-tests-")
|
2019-06-29 09:06:55 +02:00
|
|
|
self.addCleanup(shutil.rmtree, self.test_dir)
|
2018-01-17 17:56:35 +01:00
|
|
|
|
|
|
|
self.primary_base_path = os.path.join(self.test_dir, "primary")
|
|
|
|
self.secondary_base_path = os.path.join(self.test_dir, "secondary")
|
|
|
|
|
2018-02-05 13:44:03 +01:00
|
|
|
hs.config.media_store_path = self.primary_base_path
|
|
|
|
|
2018-08-10 15:54:09 +02:00
|
|
|
storage_providers = [FileStorageProviderBackend(hs, self.secondary_base_path)]
|
2018-01-17 17:56:35 +01:00
|
|
|
|
|
|
|
self.filepaths = MediaFilePaths(self.primary_base_path)
|
|
|
|
self.media_storage = MediaStorage(
|
2018-08-10 15:54:09 +02:00
|
|
|
hs, self.primary_base_path, self.filepaths, storage_providers
|
2018-01-17 17:56:35 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
2019-06-29 09:06:55 +02:00
|
|
|
|
|
|
|
# This uses a real blocking threadpool so we have to wait for it to be
|
|
|
|
# actually done :/
|
2020-07-24 15:39:02 +02:00
|
|
|
x = defer.ensureDeferred(
|
|
|
|
self.media_storage.ensure_media_is_in_local_cache(file_info)
|
|
|
|
)
|
2019-06-29 09:06:55 +02:00
|
|
|
|
|
|
|
# Hotloop until the threadpool does its job...
|
|
|
|
self.wait_on_thread(x)
|
|
|
|
|
|
|
|
local_path = self.get_success(x)
|
2018-01-17 17:56:35 +01:00
|
|
|
|
|
|
|
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)
|
2018-11-15 22:55:58 +01:00
|
|
|
|
|
|
|
|
2020-06-05 12:54:27 +02:00
|
|
|
@attr.s
|
|
|
|
class _TestImage:
|
|
|
|
"""An image for testing thumbnailing with the expected results
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
data: The raw image to thumbnail
|
|
|
|
content_type: The type of the image as a content type, e.g. "image/png"
|
|
|
|
extension: The extension associated with the format, e.g. ".png"
|
|
|
|
expected_cropped: The expected bytes from cropped thumbnailing, or None if
|
|
|
|
test should just check for success.
|
|
|
|
expected_scaled: The expected bytes from scaled thumbnailing, or None if
|
|
|
|
test should just check for a valid image returned.
|
|
|
|
"""
|
|
|
|
|
|
|
|
data = attr.ib(type=bytes)
|
|
|
|
content_type = attr.ib(type=bytes)
|
|
|
|
extension = attr.ib(type=bytes)
|
|
|
|
expected_cropped = attr.ib(type=Optional[bytes])
|
|
|
|
expected_scaled = attr.ib(type=Optional[bytes])
|
2020-09-09 18:59:41 +02:00
|
|
|
expected_found = attr.ib(default=True, type=bool)
|
2020-06-05 12:54:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
@parameterized_class(
|
|
|
|
("test_image",),
|
|
|
|
[
|
2020-09-09 18:59:41 +02:00
|
|
|
# smoll png
|
2020-06-05 12:54:27 +02:00
|
|
|
(
|
|
|
|
_TestImage(
|
|
|
|
unhexlify(
|
|
|
|
b"89504e470d0a1a0a0000000d4948445200000001000000010806"
|
|
|
|
b"0000001f15c4890000000a49444154789c63000100000500010d"
|
|
|
|
b"0a2db40000000049454e44ae426082"
|
|
|
|
),
|
|
|
|
b"image/png",
|
|
|
|
b".png",
|
|
|
|
unhexlify(
|
|
|
|
b"89504e470d0a1a0a0000000d4948445200000020000000200806"
|
|
|
|
b"000000737a7af40000001a49444154789cedc101010000008220"
|
|
|
|
b"ffaf6e484001000000ef0610200001194334ee0000000049454e"
|
|
|
|
b"44ae426082"
|
|
|
|
),
|
|
|
|
unhexlify(
|
|
|
|
b"89504e470d0a1a0a0000000d4948445200000001000000010806"
|
|
|
|
b"0000001f15c4890000000d49444154789c636060606000000005"
|
|
|
|
b"0001a5f645400000000049454e44ae426082"
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
# small lossless webp
|
|
|
|
(
|
|
|
|
_TestImage(
|
|
|
|
unhexlify(
|
|
|
|
b"524946461a000000574542505650384c0d0000002f0000001007"
|
|
|
|
b"1011118888fe0700"
|
|
|
|
),
|
|
|
|
b"image/webp",
|
|
|
|
b".webp",
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
),
|
2020-09-09 18:59:41 +02:00
|
|
|
# an empty file
|
|
|
|
(_TestImage(b"", b"image/gif", b".gif", None, None, False,),),
|
2020-06-05 12:54:27 +02:00
|
|
|
],
|
|
|
|
)
|
2018-11-15 22:55:58 +01:00
|
|
|
class MediaRepoTests(unittest.HomeserverTestCase):
|
|
|
|
|
|
|
|
hijack_auth = True
|
|
|
|
user_id = "@test:user"
|
|
|
|
|
|
|
|
def make_homeserver(self, reactor, clock):
|
|
|
|
|
|
|
|
self.fetches = []
|
|
|
|
|
|
|
|
def get_file(destination, path, output_stream, args=None, max_size=None):
|
|
|
|
"""
|
|
|
|
Returns tuple[int,dict,str,int] of file length, response headers,
|
|
|
|
absolute URI, and response code.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def write_to(r):
|
|
|
|
data, response = r
|
|
|
|
output_stream.write(data)
|
|
|
|
return response
|
|
|
|
|
|
|
|
d = Deferred()
|
|
|
|
d.addCallback(write_to)
|
|
|
|
self.fetches.append((d, destination, path, args))
|
2018-11-27 03:01:04 +01:00
|
|
|
return make_deferred_yieldable(d)
|
2018-11-15 22:55:58 +01:00
|
|
|
|
|
|
|
client = Mock()
|
|
|
|
client.get_file = get_file
|
|
|
|
|
|
|
|
self.storage_path = self.mktemp()
|
2019-05-13 22:01:14 +02:00
|
|
|
self.media_store_path = self.mktemp()
|
2018-11-15 22:55:58 +01:00
|
|
|
os.mkdir(self.storage_path)
|
2019-05-13 22:01:14 +02:00
|
|
|
os.mkdir(self.media_store_path)
|
2018-11-15 22:55:58 +01:00
|
|
|
|
|
|
|
config = self.default_config()
|
2019-05-13 22:01:14 +02:00
|
|
|
config["media_store_path"] = self.media_store_path
|
|
|
|
config["thumbnail_requirements"] = {}
|
|
|
|
config["max_image_pixels"] = 2000000
|
2018-11-15 22:55:58 +01:00
|
|
|
|
|
|
|
provider_config = {
|
|
|
|
"module": "synapse.rest.media.v1.storage_provider.FileStorageProviderBackend",
|
|
|
|
"store_local": True,
|
|
|
|
"store_synchronous": False,
|
|
|
|
"store_remote": True,
|
|
|
|
"config": {"directory": self.storage_path},
|
|
|
|
}
|
2019-05-13 22:01:14 +02:00
|
|
|
config["media_storage_providers"] = [provider_config]
|
2018-11-15 22:55:58 +01:00
|
|
|
|
|
|
|
hs = self.setup_test_homeserver(config=config, http_client=client)
|
|
|
|
|
|
|
|
return hs
|
|
|
|
|
|
|
|
def prepare(self, reactor, clock, hs):
|
|
|
|
|
|
|
|
self.media_repo = hs.get_media_repository_resource()
|
2019-06-20 11:32:02 +02:00
|
|
|
self.download_resource = self.media_repo.children[b"download"]
|
2020-01-22 13:28:07 +01:00
|
|
|
self.thumbnail_resource = self.media_repo.children[b"thumbnail"]
|
2018-11-15 22:55:58 +01:00
|
|
|
|
2020-01-22 13:28:07 +01:00
|
|
|
self.media_id = "example.com/12345"
|
|
|
|
|
2018-11-15 22:55:58 +01:00
|
|
|
def _req(self, content_disposition):
|
|
|
|
|
2020-11-14 00:11:43 +01:00
|
|
|
request, channel = make_request(
|
|
|
|
self.reactor,
|
|
|
|
FakeSite(self.download_resource),
|
|
|
|
"GET",
|
|
|
|
self.media_id,
|
|
|
|
shorthand=False,
|
|
|
|
)
|
2018-11-15 22:55:58 +01:00
|
|
|
request.render(self.download_resource)
|
|
|
|
self.pump()
|
|
|
|
|
|
|
|
# We've made one fetch, to example.com, using the media URL, and asking
|
|
|
|
# the other server not to do a remote fetch
|
|
|
|
self.assertEqual(len(self.fetches), 1)
|
|
|
|
self.assertEqual(self.fetches[0][1], "example.com")
|
|
|
|
self.assertEqual(
|
2020-06-17 14:36:46 +02:00
|
|
|
self.fetches[0][2], "/_matrix/media/r0/download/" + self.media_id
|
2018-11-15 22:55:58 +01:00
|
|
|
)
|
|
|
|
self.assertEqual(self.fetches[0][3], {"allow_remote": "false"})
|
|
|
|
|
|
|
|
headers = {
|
2020-06-05 12:54:27 +02:00
|
|
|
b"Content-Length": [b"%d" % (len(self.test_image.data))],
|
|
|
|
b"Content-Type": [self.test_image.content_type],
|
2018-11-15 22:55:58 +01:00
|
|
|
}
|
|
|
|
if content_disposition:
|
|
|
|
headers[b"Content-Disposition"] = [content_disposition]
|
|
|
|
|
|
|
|
self.fetches[0][0].callback(
|
2020-06-05 12:54:27 +02:00
|
|
|
(self.test_image.data, (len(self.test_image.data), headers))
|
2018-11-15 22:55:58 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
self.pump()
|
|
|
|
self.assertEqual(channel.code, 200)
|
|
|
|
|
|
|
|
return channel
|
|
|
|
|
|
|
|
def test_disposition_filename_ascii(self):
|
|
|
|
"""
|
|
|
|
If the filename is filename=<ascii> then Synapse will decode it as an
|
|
|
|
ASCII string, and use filename= in the response.
|
|
|
|
"""
|
2020-06-05 12:54:27 +02:00
|
|
|
channel = self._req(b"inline; filename=out" + self.test_image.extension)
|
2018-11-15 22:55:58 +01:00
|
|
|
|
|
|
|
headers = channel.headers
|
|
|
|
self.assertEqual(
|
2020-06-05 12:54:27 +02:00
|
|
|
headers.getRawHeaders(b"Content-Type"), [self.test_image.content_type]
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
headers.getRawHeaders(b"Content-Disposition"),
|
|
|
|
[b"inline; filename=out" + self.test_image.extension],
|
2018-11-15 22:55:58 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_disposition_filenamestar_utf8escaped(self):
|
|
|
|
"""
|
|
|
|
If the filename is filename=*utf8''<utf8 escaped> then Synapse will
|
|
|
|
correctly decode it as the UTF-8 string, and use filename* in the
|
|
|
|
response.
|
|
|
|
"""
|
2019-06-20 11:32:02 +02:00
|
|
|
filename = parse.quote("\u2603".encode("utf8")).encode("ascii")
|
2020-06-05 12:54:27 +02:00
|
|
|
channel = self._req(
|
|
|
|
b"inline; filename*=utf-8''" + filename + self.test_image.extension
|
|
|
|
)
|
2018-11-15 22:55:58 +01:00
|
|
|
|
|
|
|
headers = channel.headers
|
2020-06-05 12:54:27 +02:00
|
|
|
self.assertEqual(
|
|
|
|
headers.getRawHeaders(b"Content-Type"), [self.test_image.content_type]
|
|
|
|
)
|
2018-11-15 22:55:58 +01:00
|
|
|
self.assertEqual(
|
|
|
|
headers.getRawHeaders(b"Content-Disposition"),
|
2020-06-05 12:54:27 +02:00
|
|
|
[b"inline; filename*=utf-8''" + filename + self.test_image.extension],
|
2018-11-15 22:55:58 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_disposition_none(self):
|
|
|
|
"""
|
|
|
|
If there is no filename, one isn't passed on in the Content-Disposition
|
|
|
|
of the request.
|
|
|
|
"""
|
|
|
|
channel = self._req(None)
|
|
|
|
|
|
|
|
headers = channel.headers
|
2020-06-05 12:54:27 +02:00
|
|
|
self.assertEqual(
|
|
|
|
headers.getRawHeaders(b"Content-Type"), [self.test_image.content_type]
|
|
|
|
)
|
2018-11-15 22:55:58 +01:00
|
|
|
self.assertEqual(headers.getRawHeaders(b"Content-Disposition"), None)
|
2020-01-22 13:28:07 +01:00
|
|
|
|
|
|
|
def test_thumbnail_crop(self):
|
2020-09-09 18:59:41 +02:00
|
|
|
self._test_thumbnail(
|
|
|
|
"crop", self.test_image.expected_cropped, self.test_image.expected_found
|
|
|
|
)
|
2020-01-22 13:28:07 +01:00
|
|
|
|
|
|
|
def test_thumbnail_scale(self):
|
2020-09-09 18:59:41 +02:00
|
|
|
self._test_thumbnail(
|
|
|
|
"scale", self.test_image.expected_scaled, self.test_image.expected_found
|
|
|
|
)
|
2020-01-22 13:28:07 +01:00
|
|
|
|
2020-09-09 18:59:41 +02:00
|
|
|
def _test_thumbnail(self, method, expected_body, expected_found):
|
2020-01-22 13:28:07 +01:00
|
|
|
params = "?width=32&height=32&method=" + method
|
2020-11-14 00:11:43 +01:00
|
|
|
request, channel = make_request(
|
|
|
|
self.reactor,
|
|
|
|
FakeSite(self.thumbnail_resource),
|
|
|
|
"GET",
|
|
|
|
self.media_id + params,
|
|
|
|
shorthand=False,
|
2020-01-22 13:28:07 +01:00
|
|
|
)
|
|
|
|
request.render(self.thumbnail_resource)
|
|
|
|
self.pump()
|
|
|
|
|
|
|
|
headers = {
|
2020-06-05 12:54:27 +02:00
|
|
|
b"Content-Length": [b"%d" % (len(self.test_image.data))],
|
|
|
|
b"Content-Type": [self.test_image.content_type],
|
2020-01-22 13:28:07 +01:00
|
|
|
}
|
|
|
|
self.fetches[0][0].callback(
|
2020-06-05 12:54:27 +02:00
|
|
|
(self.test_image.data, (len(self.test_image.data), headers))
|
2020-01-22 13:28:07 +01:00
|
|
|
)
|
|
|
|
self.pump()
|
|
|
|
|
2020-09-09 18:59:41 +02:00
|
|
|
if expected_found:
|
|
|
|
self.assertEqual(channel.code, 200)
|
|
|
|
if expected_body is not None:
|
|
|
|
self.assertEqual(
|
|
|
|
channel.result["body"], expected_body, channel.result["body"]
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
# ensure that the result is at least some valid image
|
|
|
|
Image.open(BytesIO(channel.result["body"]))
|
|
|
|
else:
|
|
|
|
# A 404 with a JSON body.
|
|
|
|
self.assertEqual(channel.code, 404)
|
2020-06-05 12:54:27 +02:00
|
|
|
self.assertEqual(
|
2020-09-09 18:59:41 +02:00
|
|
|
channel.json_body,
|
|
|
|
{
|
|
|
|
"errcode": "M_NOT_FOUND",
|
|
|
|
"error": "Not found [b'example.com', b'12345?width=32&height=32&method=%s']"
|
|
|
|
% method,
|
|
|
|
},
|
2020-06-05 12:54:27 +02:00
|
|
|
)
|