Skip to content

Commit

Permalink
media: move muxing of versioned endpoints to MediaRepositoryResource
Browse files Browse the repository at this point in the history
This allows for better handling of all of the versioned media endpoints

Signed-off-by: Sumner Evans <[email protected]>
  • Loading branch information
sumnerevans committed May 30, 2022
1 parent 6ed5b8f commit 759162d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
4 changes: 1 addition & 3 deletions synapse/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@
FEDERATION_UNSTABLE_PREFIX = FEDERATION_PREFIX + "/unstable"
STATIC_PREFIX = "/_matrix/static"
SERVER_KEY_V2_PREFIX = "/_matrix/key/v2"
MEDIA_R0_PREFIX = "/_matrix/media/r0"
MEDIA_V3_PREFIX = "/_matrix/media/v3"
LEGACY_MEDIA_PREFIX = "/_matrix/media/v1"
MEDIA_PREFIX = "/_matrix/media"


class ConsentURIBuilder:
Expand Down
8 changes: 2 additions & 6 deletions synapse/app/generic_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
from synapse.api.urls import (
CLIENT_API_PREFIX,
FEDERATION_PREFIX,
LEGACY_MEDIA_PREFIX,
MEDIA_R0_PREFIX,
MEDIA_V3_PREFIX,
MEDIA_PREFIX,
SERVER_KEY_V2_PREFIX,
)
from synapse.app import _base
Expand Down Expand Up @@ -340,9 +338,7 @@ def _listen_http(self, listener_config: ListenerConfig) -> None:

resources.update(
{
MEDIA_R0_PREFIX: media_repo,
MEDIA_V3_PREFIX: media_repo,
LEGACY_MEDIA_PREFIX: media_repo,
MEDIA_PREFIX: media_repo,
"/_synapse/admin": admin_resource,
}
)
Expand Down
12 changes: 2 additions & 10 deletions synapse/app/homeserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@
from synapse.api.urls import (
CLIENT_API_PREFIX,
FEDERATION_PREFIX,
LEGACY_MEDIA_PREFIX,
MEDIA_R0_PREFIX,
MEDIA_V3_PREFIX,
MEDIA_PREFIX,
SERVER_KEY_V2_PREFIX,
STATIC_PREFIX,
)
Expand Down Expand Up @@ -245,13 +243,7 @@ def _configure_named_resource(
if name in ["media", "federation", "client"]:
if self.config.server.enable_media_repo:
media_repo = self.get_media_repository_resource()
resources.update(
{
MEDIA_R0_PREFIX: media_repo,
MEDIA_V3_PREFIX: media_repo,
LEGACY_MEDIA_PREFIX: media_repo,
}
)
resources[MEDIA_PREFIX] = media_repo
elif name == "media":
raise ConfigError(
"'media' resource conflicts with enable_media_repo=False"
Expand Down
29 changes: 27 additions & 2 deletions synapse/rest/media/v1/media_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import logging
import os
import shutil
from enum import Enum
from io import BytesIO
from typing import IO, TYPE_CHECKING, Dict, List, Optional, Set, Tuple

Expand Down Expand Up @@ -949,7 +950,14 @@ async def _remove_local_media_from_disk(
return removed_media, len(removed_media)


class MediaRepositoryResource(Resource):
class MediaVersion(Enum):
R0 = b"r0"
V3 = b"v3"
LEGACY = b"v1"
UNSTABLE = b"unstable"


class VersionedMediaRepositoryResource(Resource):
"""File uploading and downloading.
Uploads are POSTed to a resource which returns a token which is used to GET
Expand Down Expand Up @@ -994,7 +1002,7 @@ class MediaRepositoryResource(Resource):
within a given rectangle.
"""

def __init__(self, hs: "HomeServer"):
def __init__(self, hs: "HomeServer", version: MediaVersion):
# 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.")
Expand All @@ -1013,3 +1021,20 @@ def __init__(self, hs: "HomeServer"):
PreviewUrlResource(hs, media_repo, media_repo.media_storage),
)
self.putChild(b"config", MediaConfigResource(hs))


class MediaRepositoryResource(Resource):
"""
Base media repository resource. This handles all /_matrix/media requests
and muxes them between the versioned media repository endpoints.
"""

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__()

for version in MediaVersion:
self.putChild(version.value, VersionedMediaRepositoryResource(hs, version))

0 comments on commit 759162d

Please sign in to comment.