From 9203514388742a085696bb6a177f6f8a70e28380 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Thu, 23 Feb 2023 16:11:52 -0500 Subject: [PATCH 1/5] Remove useless directory in tests. --- tests/rest/media/{v1 => }/test_base.py | 0 tests/rest/media/{v1 => }/test_filepath.py | 0 tests/rest/media/{v1 => }/test_html_preview.py | 0 tests/rest/media/{v1 => }/test_media_storage.py | 0 tests/rest/media/{v1 => }/test_oembed.py | 0 tests/rest/media/{v1 => }/test_url_preview.py | 0 tests/rest/media/v1/__init__.py | 13 ------------- 7 files changed, 13 deletions(-) rename tests/rest/media/{v1 => }/test_base.py (100%) rename tests/rest/media/{v1 => }/test_filepath.py (100%) rename tests/rest/media/{v1 => }/test_html_preview.py (100%) rename tests/rest/media/{v1 => }/test_media_storage.py (100%) rename tests/rest/media/{v1 => }/test_oembed.py (100%) rename tests/rest/media/{v1 => }/test_url_preview.py (100%) delete mode 100644 tests/rest/media/v1/__init__.py diff --git a/tests/rest/media/v1/test_base.py b/tests/rest/media/test_base.py similarity index 100% rename from tests/rest/media/v1/test_base.py rename to tests/rest/media/test_base.py diff --git a/tests/rest/media/v1/test_filepath.py b/tests/rest/media/test_filepath.py similarity index 100% rename from tests/rest/media/v1/test_filepath.py rename to tests/rest/media/test_filepath.py diff --git a/tests/rest/media/v1/test_html_preview.py b/tests/rest/media/test_html_preview.py similarity index 100% rename from tests/rest/media/v1/test_html_preview.py rename to tests/rest/media/test_html_preview.py diff --git a/tests/rest/media/v1/test_media_storage.py b/tests/rest/media/test_media_storage.py similarity index 100% rename from tests/rest/media/v1/test_media_storage.py rename to tests/rest/media/test_media_storage.py diff --git a/tests/rest/media/v1/test_oembed.py b/tests/rest/media/test_oembed.py similarity index 100% rename from tests/rest/media/v1/test_oembed.py rename to tests/rest/media/test_oembed.py diff --git a/tests/rest/media/v1/test_url_preview.py b/tests/rest/media/test_url_preview.py similarity index 100% rename from tests/rest/media/v1/test_url_preview.py rename to tests/rest/media/test_url_preview.py diff --git a/tests/rest/media/v1/__init__.py b/tests/rest/media/v1/__init__.py deleted file mode 100644 index b1ee10cfcc44..000000000000 --- a/tests/rest/media/v1/__init__.py +++ /dev/null @@ -1,13 +0,0 @@ -# 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 71cc0c209b242e1e1f5119aae872f36f0c20dca7 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Thu, 23 Feb 2023 16:38:43 -0500 Subject: [PATCH 2/5] Move non-rest media files to a separate directory. --- .../move_remote_media_to_new_store.py | 2 +- synapse/config/repository.py | 12 ++- synapse/events/spamcheck.py | 4 +- synapse/{rest/media/v1 => media}/_base.py | 0 synapse/{rest/media/v1 => media}/filepath.py | 0 .../media/v1 => media}/media_repository.py | 94 ++----------------- .../{rest/media/v1 => media}/media_storage.py | 2 +- synapse/{rest/media/v1 => media}/oembed.py | 2 +- .../{rest/media/v1 => media}/preview_html.py | 0 .../media/v1 => media}/storage_provider.py | 0 .../{rest/media/v1 => media}/thumbnailer.py | 0 synapse/rest/media/v1/download_resource.py | 5 +- .../media/v1/media_repository_resource.py | 93 ++++++++++++++++++ synapse/rest/media/v1/preview_url_resource.py | 12 +-- synapse/rest/media/v1/thumbnail_resource.py | 7 +- synapse/rest/media/v1/upload_resource.py | 4 +- synapse/server.py | 6 +- tests/media/__init__.py | 13 +++ tests/{rest => }/media/test_base.py | 2 +- tests/{rest => }/media/test_filepath.py | 2 +- tests/{rest => }/media/test_html_preview.py | 2 +- tests/{rest => }/media/test_media_storage.py | 10 +- tests/{rest => }/media/test_oembed.py | 2 +- tests/rest/admin/test_media.py | 2 +- tests/rest/admin/test_user.py | 2 +- tests/rest/media/test_url_preview.py | 4 +- 26 files changed, 155 insertions(+), 127 deletions(-) rename synapse/{rest/media/v1 => media}/_base.py (100%) rename synapse/{rest/media/v1 => media}/filepath.py (100%) rename synapse/{rest/media/v1 => media}/media_repository.py (92%) rename synapse/{rest/media/v1 => media}/media_storage.py (99%) rename synapse/{rest/media/v1 => media}/oembed.py (99%) rename synapse/{rest/media/v1 => media}/preview_html.py (100%) rename synapse/{rest/media/v1 => media}/storage_provider.py (100%) rename synapse/{rest/media/v1 => media}/thumbnailer.py (100%) create mode 100644 synapse/rest/media/v1/media_repository_resource.py create mode 100644 tests/media/__init__.py rename tests/{rest => }/media/test_base.py (95%) rename tests/{rest => }/media/test_filepath.py (99%) rename tests/{rest => }/media/test_html_preview.py (99%) rename tests/{rest => }/media/test_media_storage.py (98%) rename tests/{rest => }/media/test_oembed.py (98%) diff --git a/synapse/_scripts/move_remote_media_to_new_store.py b/synapse/_scripts/move_remote_media_to_new_store.py index 819afaaca6db..0dd36bee20ad 100755 --- a/synapse/_scripts/move_remote_media_to_new_store.py +++ b/synapse/_scripts/move_remote_media_to_new_store.py @@ -37,7 +37,7 @@ import shutil import sys -from synapse.rest.media.v1.filepath import MediaFilePaths +from synapse.media.filepath import MediaFilePaths logger = logging.getLogger() diff --git a/synapse/config/repository.py b/synapse/config/repository.py index 2da40c09f0e1..ecb3edbe3ade 100644 --- a/synapse/config/repository.py +++ b/synapse/config/repository.py @@ -178,11 +178,13 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None: for i, provider_config in enumerate(storage_providers): # We special case the module "file_system" so as not to need to # expose FileStorageProviderBackend - if provider_config["module"] == "file_system": - provider_config["module"] = ( - "synapse.rest.media.v1.storage_provider" - ".FileStorageProviderBackend" - ) + if ( + provider_config["module"] == "file_system" + or provider_config["module"] == "synapse.rest.media.v1.storage_provider" + ): + provider_config[ + "module" + ] = "synapse.media.storage_provider.FileStorageProviderBackend" provider_class, parsed_config = load_module( provider_config, ("media_storage_providers", "" % i) diff --git a/synapse/events/spamcheck.py b/synapse/events/spamcheck.py index 623a2c71eadb..765c15bb5135 100644 --- a/synapse/events/spamcheck.py +++ b/synapse/events/spamcheck.py @@ -33,8 +33,8 @@ import synapse from synapse.api.errors import Codes from synapse.logging.opentracing import trace -from synapse.rest.media.v1._base import FileInfo -from synapse.rest.media.v1.media_storage import ReadableFileWrapper +from synapse.media._base import FileInfo +from synapse.media.media_storage import ReadableFileWrapper from synapse.spam_checker_api import RegistrationBehaviour from synapse.types import JsonDict, RoomAlias, UserProfile from synapse.util.async_helpers import delay_cancellation, maybe_awaitable diff --git a/synapse/rest/media/v1/_base.py b/synapse/media/_base.py similarity index 100% rename from synapse/rest/media/v1/_base.py rename to synapse/media/_base.py diff --git a/synapse/rest/media/v1/filepath.py b/synapse/media/filepath.py similarity index 100% rename from synapse/rest/media/v1/filepath.py rename to synapse/media/filepath.py diff --git a/synapse/rest/media/v1/media_repository.py b/synapse/media/media_repository.py similarity index 92% rename from synapse/rest/media/v1/media_repository.py rename to synapse/media/media_repository.py index c70e1837afd0..b81e3c2b0c60 100644 --- a/synapse/rest/media/v1/media_repository.py +++ b/synapse/media/media_repository.py @@ -32,18 +32,10 @@ RequestSendFailed, SynapseError, ) -from synapse.config._base import ConfigError from synapse.config.repository import ThumbnailRequirement -from synapse.http.server import UnrecognizedRequestResource from synapse.http.site import SynapseRequest from synapse.logging.context import defer_to_thread -from synapse.metrics.background_process_metrics import run_as_background_process -from synapse.types import UserID -from synapse.util.async_helpers import Linearizer -from synapse.util.retryutils import NotRetryingDestination -from synapse.util.stringutils import random_string - -from ._base import ( +from synapse.media._base import ( FileInfo, Responder, ThumbnailInfo, @@ -51,15 +43,15 @@ respond_404, respond_with_responder, ) -from .config_resource import MediaConfigResource -from .download_resource import DownloadResource -from .filepath import MediaFilePaths -from .media_storage import MediaStorage -from .preview_url_resource import PreviewUrlResource -from .storage_provider import StorageProviderWrapper -from .thumbnail_resource import ThumbnailResource -from .thumbnailer import Thumbnailer, ThumbnailError -from .upload_resource import UploadResource +from synapse.media.filepath import MediaFilePaths +from synapse.media.media_storage import MediaStorage +from synapse.media.storage_provider import StorageProviderWrapper +from synapse.media.thumbnailer import Thumbnailer, ThumbnailError +from synapse.metrics.background_process_metrics import run_as_background_process +from synapse.types import UserID +from synapse.util.async_helpers import Linearizer +from synapse.util.retryutils import NotRetryingDestination +from synapse.util.stringutils import random_string if TYPE_CHECKING: from synapse.server import HomeServer @@ -1044,69 +1036,3 @@ async def _remove_local_media_from_disk( removed_media.append(media_id) return removed_media, len(removed_media) - - -class MediaRepositoryResource(UnrecognizedRequestResource): - """File uploading and downloading. - - Uploads are POSTed to a resource which returns a token which is used to GET - the download:: - - => POST /_matrix/media/r0/upload HTTP/1.1 - Content-Type: - Content-Length: - - - - <= HTTP/1.1 200 OK - Content-Type: application/json - - { "content_uri": "mxc:///" } - - => GET /_matrix/media/r0/download// HTTP/1.1 - - <= HTTP/1.1 200 OK - Content-Type: - Content-Disposition: attachment;filename= - - - - Clients can get thumbnails by supplying a desired width and height and - thumbnailing method:: - - => GET /_matrix/media/r0/thumbnail/ - /?width=&height=&method= HTTP/1.1 - - <= HTTP/1.1 200 OK - Content-Type: image/jpeg or image/png - - - - The thumbnail methods are "crop" and "scale". "scale" tries to return an - image where either the width or the height is smaller than the requested - size. The client should then scale and letterbox the image if it needs to - fit within a given rectangle. "crop" tries to return an image where the - width and height are close to the requested size and the aspect matches - the requested size. The client should scale the image if it needs to fit - within a given rectangle. - """ - - def __init__(self, hs: "HomeServer"): - # If we're not configured to use it, raise if we somehow got here. - if not hs.config.media.can_load_media_repo: - raise ConfigError("Synapse is not configured to use a media repo.") - - super().__init__() - media_repo = hs.get_media_repository() - - self.putChild(b"upload", UploadResource(hs, media_repo)) - self.putChild(b"download", DownloadResource(hs, media_repo)) - self.putChild( - b"thumbnail", ThumbnailResource(hs, media_repo, media_repo.media_storage) - ) - if hs.config.media.url_preview_enabled: - self.putChild( - b"preview_url", - PreviewUrlResource(hs, media_repo, media_repo.media_storage), - ) - self.putChild(b"config", MediaConfigResource(hs)) diff --git a/synapse/rest/media/v1/media_storage.py b/synapse/media/media_storage.py similarity index 99% rename from synapse/rest/media/v1/media_storage.py rename to synapse/media/media_storage.py index db25848744de..a7e22a91e115 100644 --- a/synapse/rest/media/v1/media_storage.py +++ b/synapse/media/media_storage.py @@ -46,7 +46,7 @@ from .filepath import MediaFilePaths if TYPE_CHECKING: - from synapse.rest.media.v1.storage_provider import StorageProvider + from synapse.media.storage_provider import StorageProvider from synapse.server import HomeServer logger = logging.getLogger(__name__) diff --git a/synapse/rest/media/v1/oembed.py b/synapse/media/oembed.py similarity index 99% rename from synapse/rest/media/v1/oembed.py rename to synapse/media/oembed.py index 7592aa5d4767..c0eaf04be544 100644 --- a/synapse/rest/media/v1/oembed.py +++ b/synapse/media/oembed.py @@ -18,7 +18,7 @@ import attr -from synapse.rest.media.v1.preview_html import parse_html_description +from synapse.media.preview_html import parse_html_description from synapse.types import JsonDict from synapse.util import json_decoder diff --git a/synapse/rest/media/v1/preview_html.py b/synapse/media/preview_html.py similarity index 100% rename from synapse/rest/media/v1/preview_html.py rename to synapse/media/preview_html.py diff --git a/synapse/rest/media/v1/storage_provider.py b/synapse/media/storage_provider.py similarity index 100% rename from synapse/rest/media/v1/storage_provider.py rename to synapse/media/storage_provider.py diff --git a/synapse/rest/media/v1/thumbnailer.py b/synapse/media/thumbnailer.py similarity index 100% rename from synapse/rest/media/v1/thumbnailer.py rename to synapse/media/thumbnailer.py diff --git a/synapse/rest/media/v1/download_resource.py b/synapse/rest/media/v1/download_resource.py index 048a042692c3..8f270cf4ccb8 100644 --- a/synapse/rest/media/v1/download_resource.py +++ b/synapse/rest/media/v1/download_resource.py @@ -22,11 +22,10 @@ ) from synapse.http.servlet import parse_boolean from synapse.http.site import SynapseRequest - -from ._base import parse_media_id, respond_404 +from synapse.media._base import parse_media_id, respond_404 if TYPE_CHECKING: - from synapse.rest.media.v1.media_repository import MediaRepository + from synapse.media.media_repository import MediaRepository from synapse.server import HomeServer logger = logging.getLogger(__name__) diff --git a/synapse/rest/media/v1/media_repository_resource.py b/synapse/rest/media/v1/media_repository_resource.py new file mode 100644 index 000000000000..5ebaa3b032cd --- /dev/null +++ b/synapse/rest/media/v1/media_repository_resource.py @@ -0,0 +1,93 @@ +# Copyright 2014-2016 OpenMarket Ltd +# Copyright 2018-2021 The Matrix.org Foundation C.I.C. +# +# 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 typing import TYPE_CHECKING + +from synapse.config._base import ConfigError +from synapse.http.server import UnrecognizedRequestResource + +from .config_resource import MediaConfigResource +from .download_resource import DownloadResource +from .preview_url_resource import PreviewUrlResource +from .thumbnail_resource import ThumbnailResource +from .upload_resource import UploadResource + +if TYPE_CHECKING: + from synapse.server import HomeServer + + +class MediaRepositoryResource(UnrecognizedRequestResource): + """File uploading and downloading. + + Uploads are POSTed to a resource which returns a token which is used to GET + the download:: + + => POST /_matrix/media/r0/upload HTTP/1.1 + Content-Type: + Content-Length: + + + + <= HTTP/1.1 200 OK + Content-Type: application/json + + { "content_uri": "mxc:///" } + + => GET /_matrix/media/r0/download// HTTP/1.1 + + <= HTTP/1.1 200 OK + Content-Type: + Content-Disposition: attachment;filename= + + + + Clients can get thumbnails by supplying a desired width and height and + thumbnailing method:: + + => GET /_matrix/media/r0/thumbnail/ + /?width=&height=&method= HTTP/1.1 + + <= HTTP/1.1 200 OK + Content-Type: image/jpeg or image/png + + + + The thumbnail methods are "crop" and "scale". "scale" tries to return an + image where either the width or the height is smaller than the requested + size. The client should then scale and letterbox the image if it needs to + fit within a given rectangle. "crop" tries to return an image where the + width and height are close to the requested size and the aspect matches + the requested size. The client should scale the image if it needs to fit + within a given rectangle. + """ + + def __init__(self, hs: "HomeServer"): + # If we're not configured to use it, raise if we somehow got here. + if not hs.config.media.can_load_media_repo: + raise ConfigError("Synapse is not configured to use a media repo.") + + super().__init__() + media_repo = hs.get_media_repository() + + self.putChild(b"upload", UploadResource(hs, media_repo)) + self.putChild(b"download", DownloadResource(hs, media_repo)) + self.putChild( + b"thumbnail", ThumbnailResource(hs, media_repo, media_repo.media_storage) + ) + if hs.config.media.url_preview_enabled: + self.putChild( + b"preview_url", + PreviewUrlResource(hs, media_repo, media_repo.media_storage), + ) + self.putChild(b"config", MediaConfigResource(hs)) diff --git a/synapse/rest/media/v1/preview_url_resource.py b/synapse/rest/media/v1/preview_url_resource.py index 4a594ab9d83c..7ada72875755 100644 --- a/synapse/rest/media/v1/preview_url_resource.py +++ b/synapse/rest/media/v1/preview_url_resource.py @@ -40,21 +40,19 @@ from synapse.http.servlet import parse_integer, parse_string from synapse.http.site import SynapseRequest from synapse.logging.context import make_deferred_yieldable, run_in_background +from synapse.media._base import FileInfo, get_filename_from_headers +from synapse.media.media_storage import MediaStorage +from synapse.media.oembed import OEmbedProvider +from synapse.media.preview_html import decode_body, parse_html_to_open_graph from synapse.metrics.background_process_metrics import run_as_background_process -from synapse.rest.media.v1._base import get_filename_from_headers -from synapse.rest.media.v1.media_storage import MediaStorage -from synapse.rest.media.v1.oembed import OEmbedProvider -from synapse.rest.media.v1.preview_html import decode_body, parse_html_to_open_graph from synapse.types import JsonDict, UserID from synapse.util import json_encoder from synapse.util.async_helpers import ObservableDeferred from synapse.util.caches.expiringcache import ExpiringCache from synapse.util.stringutils import random_string -from ._base import FileInfo - if TYPE_CHECKING: - from synapse.rest.media.v1.media_repository import MediaRepository + from synapse.media.media_repository import MediaRepository from synapse.server import HomeServer logger = logging.getLogger(__name__) diff --git a/synapse/rest/media/v1/thumbnail_resource.py b/synapse/rest/media/v1/thumbnail_resource.py index 3e720018b31c..4ee2a0dbda79 100644 --- a/synapse/rest/media/v1/thumbnail_resource.py +++ b/synapse/rest/media/v1/thumbnail_resource.py @@ -27,9 +27,7 @@ ) from synapse.http.servlet import parse_integer, parse_string from synapse.http.site import SynapseRequest -from synapse.rest.media.v1.media_storage import MediaStorage - -from ._base import ( +from synapse.media._base import ( FileInfo, ThumbnailInfo, parse_media_id, @@ -37,9 +35,10 @@ respond_with_file, respond_with_responder, ) +from synapse.media.media_storage import MediaStorage if TYPE_CHECKING: - from synapse.rest.media.v1.media_repository import MediaRepository + from synapse.media.media_repository import MediaRepository from synapse.server import HomeServer logger = logging.getLogger(__name__) diff --git a/synapse/rest/media/v1/upload_resource.py b/synapse/rest/media/v1/upload_resource.py index 97548b54e504..697348613b52 100644 --- a/synapse/rest/media/v1/upload_resource.py +++ b/synapse/rest/media/v1/upload_resource.py @@ -20,10 +20,10 @@ from synapse.http.server import DirectServeJsonResource, respond_with_json from synapse.http.servlet import parse_bytes_from_args from synapse.http.site import SynapseRequest -from synapse.rest.media.v1.media_storage import SpamMediaException +from synapse.media.media_storage import SpamMediaException if TYPE_CHECKING: - from synapse.rest.media.v1.media_repository import MediaRepository + from synapse.media.media_repository import MediaRepository from synapse.server import HomeServer logger = logging.getLogger(__name__) diff --git a/synapse/server.py b/synapse/server.py index e5a347524763..6e78a8662c2f 100644 --- a/synapse/server.py +++ b/synapse/server.py @@ -105,6 +105,7 @@ from synapse.handlers.user_directory import UserDirectoryHandler from synapse.http.client import InsecureInterceptableContextFactory, SimpleHttpClient from synapse.http.matrixfederationclient import MatrixFederationHttpClient +from synapse.media.media_repository import MediaRepository from synapse.metrics.common_usage_metrics import CommonUsageMetricsManager from synapse.module_api import ModuleApi from synapse.notifier import Notifier, ReplicationNotifier @@ -115,10 +116,7 @@ from synapse.replication.tcp.handler import ReplicationCommandHandler from synapse.replication.tcp.resource import ReplicationStreamer from synapse.replication.tcp.streams import STREAMS_MAP, Stream -from synapse.rest.media.v1.media_repository import ( - MediaRepository, - MediaRepositoryResource, -) +from synapse.rest.media.v1.media_repository_resource import MediaRepositoryResource from synapse.server_notices.server_notices_manager import ServerNoticesManager from synapse.server_notices.server_notices_sender import ServerNoticesSender from synapse.server_notices.worker_server_notices_sender import ( diff --git a/tests/media/__init__.py b/tests/media/__init__.py new file mode 100644 index 000000000000..68910cbf5b96 --- /dev/null +++ b/tests/media/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2023 The Matrix.org Foundation C.I.C. +# +# 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. diff --git a/tests/rest/media/test_base.py b/tests/media/test_base.py similarity index 95% rename from tests/rest/media/test_base.py rename to tests/media/test_base.py index c73179151adb..66498c744d6d 100644 --- a/tests/rest/media/test_base.py +++ b/tests/media/test_base.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from synapse.rest.media.v1._base import get_filename_from_headers +from synapse.media._base import get_filename_from_headers from tests import unittest diff --git a/tests/rest/media/test_filepath.py b/tests/media/test_filepath.py similarity index 99% rename from tests/rest/media/test_filepath.py rename to tests/media/test_filepath.py index 43e6f0f70aad..95e3b83d5acb 100644 --- a/tests/rest/media/test_filepath.py +++ b/tests/media/test_filepath.py @@ -15,7 +15,7 @@ import os from typing import Iterable -from synapse.rest.media.v1.filepath import MediaFilePaths, _wrap_with_jail_check +from synapse.media.filepath import MediaFilePaths, _wrap_with_jail_check from tests import unittest diff --git a/tests/rest/media/test_html_preview.py b/tests/media/test_html_preview.py similarity index 99% rename from tests/rest/media/test_html_preview.py rename to tests/media/test_html_preview.py index 1062081a060b..e7da75db3ee0 100644 --- a/tests/rest/media/test_html_preview.py +++ b/tests/media/test_html_preview.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from synapse.rest.media.v1.preview_html import ( +from synapse.media.preview_html import ( _get_html_media_encodings, decode_body, parse_html_to_open_graph, diff --git a/tests/rest/media/test_media_storage.py b/tests/media/test_media_storage.py similarity index 98% rename from tests/rest/media/test_media_storage.py rename to tests/media/test_media_storage.py index 8ed27179c46a..870047d0f250 100644 --- a/tests/rest/media/test_media_storage.py +++ b/tests/media/test_media_storage.py @@ -34,13 +34,13 @@ from synapse.events.spamcheck import load_legacy_spam_checkers from synapse.http.types import QueryParams from synapse.logging.context import make_deferred_yieldable +from synapse.media._base import FileInfo +from synapse.media.filepath import MediaFilePaths +from synapse.media.media_storage import MediaStorage, ReadableFileWrapper +from synapse.media.storage_provider import FileStorageProviderBackend from synapse.module_api import ModuleApi from synapse.rest import admin from synapse.rest.client import login -from synapse.rest.media.v1._base import FileInfo -from synapse.rest.media.v1.filepath import MediaFilePaths -from synapse.rest.media.v1.media_storage import MediaStorage, ReadableFileWrapper -from synapse.rest.media.v1.storage_provider import FileStorageProviderBackend from synapse.server import HomeServer from synapse.types import JsonDict, RoomAlias from synapse.util import Clock @@ -253,7 +253,7 @@ def write_to( config["max_image_pixels"] = 2000000 provider_config = { - "module": "synapse.rest.media.v1.storage_provider.FileStorageProviderBackend", + "module": "synapse.media.storage_provider.FileStorageProviderBackend", "store_local": True, "store_synchronous": False, "store_remote": True, diff --git a/tests/rest/media/test_oembed.py b/tests/media/test_oembed.py similarity index 98% rename from tests/rest/media/test_oembed.py rename to tests/media/test_oembed.py index 3f7f1dbab9b7..c8bf8421daf1 100644 --- a/tests/rest/media/test_oembed.py +++ b/tests/media/test_oembed.py @@ -18,7 +18,7 @@ from twisted.test.proto_helpers import MemoryReactor -from synapse.rest.media.v1.oembed import OEmbedProvider, OEmbedResult +from synapse.media.oembed import OEmbedProvider, OEmbedResult from synapse.server import HomeServer from synapse.types import JsonDict from synapse.util import Clock diff --git a/tests/rest/admin/test_media.py b/tests/rest/admin/test_media.py index f41319a5b66f..6d04911d6740 100644 --- a/tests/rest/admin/test_media.py +++ b/tests/rest/admin/test_media.py @@ -20,8 +20,8 @@ import synapse.rest.admin from synapse.api.errors import Codes +from synapse.media.filepath import MediaFilePaths from synapse.rest.client import login, profile, room -from synapse.rest.media.v1.filepath import MediaFilePaths from synapse.server import HomeServer from synapse.util import Clock diff --git a/tests/rest/admin/test_user.py b/tests/rest/admin/test_user.py index f5b213219fa8..4b8f889a71ba 100644 --- a/tests/rest/admin/test_user.py +++ b/tests/rest/admin/test_user.py @@ -28,8 +28,8 @@ from synapse.api.constants import ApprovalNoticeMedium, LoginType, UserTypes from synapse.api.errors import Codes, HttpResponseException, ResourceLimitError from synapse.api.room_versions import RoomVersions +from synapse.media.filepath import MediaFilePaths from synapse.rest.client import devices, login, logout, profile, register, room, sync -from synapse.rest.media.v1.filepath import MediaFilePaths from synapse.server import HomeServer from synapse.types import JsonDict, UserID, create_requester from synapse.util import Clock diff --git a/tests/rest/media/test_url_preview.py b/tests/rest/media/test_url_preview.py index 2acfccec6170..db555d9fcaaf 100644 --- a/tests/rest/media/test_url_preview.py +++ b/tests/rest/media/test_url_preview.py @@ -26,7 +26,7 @@ from twisted.test.proto_helpers import AccumulatingProtocol, MemoryReactor from synapse.config.oembed import OEmbedEndpointConfig -from synapse.rest.media.v1.media_repository import MediaRepositoryResource +from synapse.rest.media.v1.media_repository_resource import MediaRepositoryResource from synapse.rest.media.v1.preview_url_resource import IMAGE_CACHE_EXPIRY_MS from synapse.server import HomeServer from synapse.types import JsonDict @@ -82,7 +82,7 @@ def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer: config["media_store_path"] = self.media_store_path provider_config = { - "module": "synapse.rest.media.v1.storage_provider.FileStorageProviderBackend", + "module": "synapse.media.storage_provider.FileStorageProviderBackend", "store_local": True, "store_synchronous": False, "store_remote": True, From 60a4317c3839bdfc9b6834849259bdec3c76be29 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Thu, 23 Feb 2023 16:41:42 -0500 Subject: [PATCH 3/5] Remove the v1 directory for synapse.rest.media. --- synapse/rest/media/{v1 => }/config_resource.py | 0 synapse/rest/media/{v1 => }/download_resource.py | 0 synapse/rest/media/{v1 => }/media_repository_resource.py | 0 synapse/rest/media/{v1 => }/preview_url_resource.py | 0 synapse/rest/media/{v1 => }/thumbnail_resource.py | 0 synapse/rest/media/{v1 => }/upload_resource.py | 0 synapse/server.py | 2 +- tests/rest/media/test_url_preview.py | 4 ++-- 8 files changed, 3 insertions(+), 3 deletions(-) rename synapse/rest/media/{v1 => }/config_resource.py (100%) rename synapse/rest/media/{v1 => }/download_resource.py (100%) rename synapse/rest/media/{v1 => }/media_repository_resource.py (100%) rename synapse/rest/media/{v1 => }/preview_url_resource.py (100%) rename synapse/rest/media/{v1 => }/thumbnail_resource.py (100%) rename synapse/rest/media/{v1 => }/upload_resource.py (100%) diff --git a/synapse/rest/media/v1/config_resource.py b/synapse/rest/media/config_resource.py similarity index 100% rename from synapse/rest/media/v1/config_resource.py rename to synapse/rest/media/config_resource.py diff --git a/synapse/rest/media/v1/download_resource.py b/synapse/rest/media/download_resource.py similarity index 100% rename from synapse/rest/media/v1/download_resource.py rename to synapse/rest/media/download_resource.py diff --git a/synapse/rest/media/v1/media_repository_resource.py b/synapse/rest/media/media_repository_resource.py similarity index 100% rename from synapse/rest/media/v1/media_repository_resource.py rename to synapse/rest/media/media_repository_resource.py diff --git a/synapse/rest/media/v1/preview_url_resource.py b/synapse/rest/media/preview_url_resource.py similarity index 100% rename from synapse/rest/media/v1/preview_url_resource.py rename to synapse/rest/media/preview_url_resource.py diff --git a/synapse/rest/media/v1/thumbnail_resource.py b/synapse/rest/media/thumbnail_resource.py similarity index 100% rename from synapse/rest/media/v1/thumbnail_resource.py rename to synapse/rest/media/thumbnail_resource.py diff --git a/synapse/rest/media/v1/upload_resource.py b/synapse/rest/media/upload_resource.py similarity index 100% rename from synapse/rest/media/v1/upload_resource.py rename to synapse/rest/media/upload_resource.py diff --git a/synapse/server.py b/synapse/server.py index 6e78a8662c2f..a7c32e9a6092 100644 --- a/synapse/server.py +++ b/synapse/server.py @@ -116,7 +116,7 @@ from synapse.replication.tcp.handler import ReplicationCommandHandler from synapse.replication.tcp.resource import ReplicationStreamer from synapse.replication.tcp.streams import STREAMS_MAP, Stream -from synapse.rest.media.v1.media_repository_resource import MediaRepositoryResource +from synapse.rest.media.media_repository_resource import MediaRepositoryResource from synapse.server_notices.server_notices_manager import ServerNoticesManager from synapse.server_notices.server_notices_sender import ServerNoticesSender from synapse.server_notices.worker_server_notices_sender import ( diff --git a/tests/rest/media/test_url_preview.py b/tests/rest/media/test_url_preview.py index db555d9fcaaf..e91dc581c204 100644 --- a/tests/rest/media/test_url_preview.py +++ b/tests/rest/media/test_url_preview.py @@ -26,8 +26,8 @@ from twisted.test.proto_helpers import AccumulatingProtocol, MemoryReactor from synapse.config.oembed import OEmbedEndpointConfig -from synapse.rest.media.v1.media_repository_resource import MediaRepositoryResource -from synapse.rest.media.v1.preview_url_resource import IMAGE_CACHE_EXPIRY_MS +from synapse.rest.media.media_repository_resource import MediaRepositoryResource +from synapse.rest.media.preview_url_resource import IMAGE_CACHE_EXPIRY_MS from synapse.server import HomeServer from synapse.types import JsonDict from synapse.util import Clock From d632afc07e51910302279b21f0893935dbeeefed Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Thu, 23 Feb 2023 16:47:14 -0500 Subject: [PATCH 4/5] Add back compatibility layer for media providers & spam checkers. --- synapse/rest/media/v1/_base.py | 17 +++++++++++++++++ synapse/rest/media/v1/media_storage.py | 17 +++++++++++++++++ synapse/rest/media/v1/storage_provider.py | 17 +++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 synapse/rest/media/v1/_base.py create mode 100644 synapse/rest/media/v1/media_storage.py create mode 100644 synapse/rest/media/v1/storage_provider.py diff --git a/synapse/rest/media/v1/_base.py b/synapse/rest/media/v1/_base.py new file mode 100644 index 000000000000..88427a573708 --- /dev/null +++ b/synapse/rest/media/v1/_base.py @@ -0,0 +1,17 @@ +# Copyright 2023 The Matrix.org Foundation C.I.C. +# +# 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. +# + +# This exists purely for backwards compatibility with media providers and spam checkers. +from synapse.media._base import FileInfo, Responder # noqa: F401 diff --git a/synapse/rest/media/v1/media_storage.py b/synapse/rest/media/v1/media_storage.py new file mode 100644 index 000000000000..11b0e8e23185 --- /dev/null +++ b/synapse/rest/media/v1/media_storage.py @@ -0,0 +1,17 @@ +# Copyright 2023 The Matrix.org Foundation C.I.C. +# +# 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. +# + +# This exists purely for backwards compatibility with spam checkers. +from synapse.media.media_storage import ReadableFileWrapper # noqa: F401 diff --git a/synapse/rest/media/v1/storage_provider.py b/synapse/rest/media/v1/storage_provider.py new file mode 100644 index 000000000000..d7653f30aead --- /dev/null +++ b/synapse/rest/media/v1/storage_provider.py @@ -0,0 +1,17 @@ +# Copyright 2023 The Matrix.org Foundation C.I.C. +# +# 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. +# + +# This exists purely for backwards compatibility with media providers. +from synapse.media.storage_provider import StorageProvider # noqa: F401 From 9ada6eb01cc7d11f5439bd99fcca016786687ccf Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Thu, 23 Feb 2023 16:52:24 -0500 Subject: [PATCH 5/5] Newsfragment --- changelog.d/15146.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/15146.misc diff --git a/changelog.d/15146.misc b/changelog.d/15146.misc new file mode 100644 index 000000000000..8de5f952398f --- /dev/null +++ b/changelog.d/15146.misc @@ -0,0 +1 @@ +Refactor the media modules.