2023-02-27 08:26:05 -05:00
|
|
|
#
|
2023-11-21 15:29:58 -05:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2023-02-27 08:26:05 -05:00
|
|
|
#
|
|
|
|
#
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
|
|
|
from synapse.config._base import ConfigError
|
2023-10-06 07:22:55 -04:00
|
|
|
from synapse.http.server import HttpServer, JsonResource
|
2023-02-27 08:26:05 -05:00
|
|
|
|
|
|
|
from .config_resource import MediaConfigResource
|
2023-11-15 09:19:24 -05:00
|
|
|
from .create_resource import CreateResource
|
2023-02-27 08:26:05 -05:00
|
|
|
from .download_resource import DownloadResource
|
|
|
|
from .preview_url_resource import PreviewUrlResource
|
|
|
|
from .thumbnail_resource import ThumbnailResource
|
2023-11-15 09:19:24 -05:00
|
|
|
from .upload_resource import AsyncUploadServlet, UploadServlet
|
2023-02-27 08:26:05 -05:00
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from synapse.server import HomeServer
|
|
|
|
|
|
|
|
|
2023-10-06 07:22:55 -04:00
|
|
|
class MediaRepositoryResource(JsonResource):
|
2023-02-27 08:26:05 -05:00
|
|
|
"""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: <media-type>
|
|
|
|
Content-Length: <content-length>
|
|
|
|
|
|
|
|
<media>
|
|
|
|
|
|
|
|
<= HTTP/1.1 200 OK
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{ "content_uri": "mxc://<server-name>/<media-id>" }
|
|
|
|
|
|
|
|
=> GET /_matrix/media/r0/download/<server-name>/<media-id> HTTP/1.1
|
|
|
|
|
|
|
|
<= HTTP/1.1 200 OK
|
|
|
|
Content-Type: <media-type>
|
|
|
|
Content-Disposition: attachment;filename=<upload-filename>
|
|
|
|
|
|
|
|
<media>
|
|
|
|
|
|
|
|
Clients can get thumbnails by supplying a desired width and height and
|
|
|
|
thumbnailing method::
|
|
|
|
|
|
|
|
=> GET /_matrix/media/r0/thumbnail/<server_name>
|
|
|
|
/<media-id>?width=<w>&height=<h>&method=<m> HTTP/1.1
|
|
|
|
|
|
|
|
<= HTTP/1.1 200 OK
|
|
|
|
Content-Type: image/jpeg or image/png
|
|
|
|
|
|
|
|
<thumbnail>
|
|
|
|
|
|
|
|
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.
|
2023-10-06 07:22:55 -04:00
|
|
|
|
|
|
|
This gets mounted at various points under /_matrix/media, including:
|
|
|
|
* /_matrix/media/r0
|
|
|
|
* /_matrix/media/v1
|
|
|
|
* /_matrix/media/v3
|
2023-02-27 08:26:05 -05:00
|
|
|
"""
|
|
|
|
|
|
|
|
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.")
|
|
|
|
|
2023-10-06 07:22:55 -04:00
|
|
|
JsonResource.__init__(self, hs, canonical_json=False)
|
|
|
|
self.register_servlets(self, hs)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def register_servlets(http_server: HttpServer, hs: "HomeServer") -> None:
|
2023-02-27 08:26:05 -05:00
|
|
|
media_repo = hs.get_media_repository()
|
|
|
|
|
2023-10-06 07:22:55 -04:00
|
|
|
# Note that many of these should not exist as v1 endpoints, but empirically
|
|
|
|
# a lot of traffic still goes to them.
|
2023-11-15 09:19:24 -05:00
|
|
|
CreateResource(hs, media_repo).register(http_server)
|
|
|
|
UploadServlet(hs, media_repo).register(http_server)
|
|
|
|
AsyncUploadServlet(hs, media_repo).register(http_server)
|
2023-10-06 07:22:55 -04:00
|
|
|
DownloadResource(hs, media_repo).register(http_server)
|
|
|
|
ThumbnailResource(hs, media_repo, media_repo.media_storage).register(
|
|
|
|
http_server
|
2023-02-27 08:26:05 -05:00
|
|
|
)
|
|
|
|
if hs.config.media.url_preview_enabled:
|
2023-10-06 07:22:55 -04:00
|
|
|
PreviewUrlResource(hs, media_repo, media_repo.media_storage).register(
|
|
|
|
http_server
|
2023-02-27 08:26:05 -05:00
|
|
|
)
|
2023-10-06 07:22:55 -04:00
|
|
|
MediaConfigResource(hs).register(http_server)
|