Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Don't load the media repo when configured to use an external media repo #5754

Merged
merged 24 commits into from
Aug 13, 2019
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/5754.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add the `external_media_repo` configuration option. This prevents Synapse from checking or loading media repo specific configuration and from loading in the associated servlets which are not required when a third party media repo is used.
15 changes: 15 additions & 0 deletions docs/sample_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,21 @@ log_config: "CONFDIR/SERVERNAME.log.config"



##
## Media Store
##

# Is an external media repo in use?
#
# Set 'False' to use Synapse's media repo. If this is set to 'True',
# Synapse's media repo below must be disabled.
#
#external_media_repo: False

# Enable the media store service inside Synapse.
#
enable_media_repo: True

# Directory where uploaded images and attachments are stored.
#
media_store_path: "DATADIR/media_store"
Expand Down
17 changes: 12 additions & 5 deletions synapse/app/homeserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,18 @@ def _configure_named_resource(self, name, compress=False):
)

if name in ["media", "federation", "client"]:
if self.get_config().enable_media_repo:
if name == "media" and not self.get_config().enable_media_repo:
raise ConfigError(
"'media' resource conflicts with enable_media_repo=False"
)
elif name == "media" and self.get_config().external_media_repo:
raise ConfigError(
"'media' resource conflicts with external_media_repo=True"
)
elif (
self.get_config().enable_media_repo
and not self.get_config().external_media_repo
):
media_repo = self.get_media_repository_resource()
resources.update(
{
Expand All @@ -230,10 +241,6 @@ def _configure_named_resource(self, name, compress=False):
),
}
)
elif name == "media":
raise ConfigError(
"'media' resource conflicts with enable_media_repo=False"
)

if name in ["keys", "federation"]:
resources[SERVER_KEY_V2_PREFIX] = KeyApiV2Resource(self)
Expand Down
7 changes: 7 additions & 0 deletions synapse/app/media_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ def start(config_options):

assert config.worker_app == "synapse.app.media_repository"

if config.external_media_repo:
_base.quit_with_error(
"external_media_repo must be disabled before the \n"
"media_repository worker can be used\n"
"Please add ``external_media_repo: false`` to the main config\n"
)

if config.enable_media_repo:
_base.quit_with_error(
"enable_media_repo must be disabled in the main synapse process\n"
Expand Down
28 changes: 28 additions & 0 deletions synapse/config/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# 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.

import os
from collections import namedtuple

Expand Down Expand Up @@ -87,6 +88,18 @@ def parse_thumbnail_requirements(thumbnail_sizes):

class ContentRepositoryConfig(Config):
def read_config(self, config, **kwargs):
self.external_media_repo = config.get("external_media_repo", False)

if self.external_media_repo and self.enable_media_repo:
raise ConfigError(
"external_media_repo can't be True if the enable_media_repo is True"
)

# If we're using an external media repo, do not load the further
# configuration (which checks if files exists).
if self.external_media_repo:
return

self.max_upload_size = self.parse_size(config.get("max_upload_size", "10M"))
self.max_image_pixels = self.parse_size(config.get("max_image_pixels", "32M"))
self.max_spider_size = self.parse_size(config.get("max_spider_size", "10M"))
Expand Down Expand Up @@ -202,6 +215,21 @@ def generate_config_section(self, data_dir_path, **kwargs):

return (
r"""
hawkowl marked this conversation as resolved.
Show resolved Hide resolved
hawkowl marked this conversation as resolved.
Show resolved Hide resolved
##
hawkowl marked this conversation as resolved.
Show resolved Hide resolved
## Media Store
##

# Is an external media repo in use?
hawkowl marked this conversation as resolved.
Show resolved Hide resolved
#
# Set 'False' to use Synapse's media repo. If this is set to 'True',
hawkowl marked this conversation as resolved.
Show resolved Hide resolved
# Synapse's media repo below must be disabled.
#
#external_media_repo: False
hawkowl marked this conversation as resolved.
Show resolved Hide resolved

# Enable the media store service inside Synapse.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Enable the media store service inside Synapse.
# Enable the media store service in the Synapse master. Uncomment the following if you are using a separate
# media store worker.

#
enable_media_repo: True
hawkowl marked this conversation as resolved.
Show resolved Hide resolved

# Directory where uploaded images and attachments are stored.
#
media_store_path: "%(media_store)s"
Expand Down
10 changes: 7 additions & 3 deletions synapse/rest/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,6 @@ def register_servlets(hs, http_server):
def register_servlets_for_client_rest_resource(hs, http_server):
"""Register only the servlets which need to be exposed on /_matrix/client/xxx"""
WhoisRestServlet(hs).register(http_server)
PurgeMediaCacheRestServlet(hs).register(http_server)
PurgeHistoryStatusRestServlet(hs).register(http_server)
DeactivateAccountRestServlet(hs).register(http_server)
PurgeHistoryRestServlet(hs).register(http_server)
Expand All @@ -834,10 +833,15 @@ def register_servlets_for_client_rest_resource(hs, http_server):
GetUsersPaginatedRestServlet(hs).register(http_server)
SearchUsersRestServlet(hs).register(http_server)
ShutdownRoomRestServlet(hs).register(http_server)
QuarantineMediaInRoom(hs).register(http_server)
ListMediaInRoom(hs).register(http_server)
UserRegisterServlet(hs).register(http_server)
DeleteGroupAdminRestServlet(hs).register(http_server)
AccountValidityRenewServlet(hs).register(http_server)

# Media URLs -- if we're using an external media repo, don't add them.
if not hs.config.external_media_repo:
PurgeMediaCacheRestServlet(hs).register(http_server)
QuarantineMediaInRoom(hs).register(http_server)
ListMediaInRoom(hs).register(http_server)

# don't add more things here: new servlets should only be exposed on
# /_synapse/admin so should not go here. Instead register them in AdminRestResource.
6 changes: 5 additions & 1 deletion synapse/rest/media/v1/media_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,8 +753,12 @@ class MediaRepositoryResource(Resource):
"""

def __init__(self, hs):
Resource.__init__(self)
# If this is not the media repo (which will always load this class),
# raise an exception if it is tried to be used.
if hs.config.external_media_repo:
raise Exception("This Synapse is not configured for media repo use.")

super().__init__()
media_repo = hs.get_media_repository()

self.putChild(b"upload", UploadResource(hs, media_repo))
Expand Down