2016-01-25 00:47:27 +01:00
|
|
|
# Copyright 2016 OpenMarket Ltd
|
2021-01-15 16:57:37 +01:00
|
|
|
# Copyright 2020-2021 The Matrix.org Foundation C.I.C.
|
2016-01-25 00:47:27 +01:00
|
|
|
#
|
|
|
|
# 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
|
|
|
|
2023-03-20 19:32:26 +01:00
|
|
|
from typing import TYPE_CHECKING
|
2021-09-07 15:10:34 +02:00
|
|
|
|
2016-04-03 13:56:29 +02:00
|
|
|
from synapse.http.server import (
|
2020-07-03 20:02:19 +02:00
|
|
|
DirectServeJsonResource,
|
2017-11-23 18:52:31 +01:00
|
|
|
respond_with_json,
|
2018-07-09 08:09:20 +02:00
|
|
|
respond_with_json_bytes,
|
2016-04-03 13:56:29 +02:00
|
|
|
)
|
2018-07-13 21:40:14 +02:00
|
|
|
from synapse.http.servlet import parse_integer, parse_string
|
2021-03-12 17:37:57 +01:00
|
|
|
from synapse.http.site import SynapseRequest
|
2023-02-27 14:26:05 +01:00
|
|
|
from synapse.media.media_storage import MediaStorage
|
2023-03-20 19:32:26 +01:00
|
|
|
from synapse.media.url_previewer import UrlPreviewer
|
2018-07-09 08:09:20 +02:00
|
|
|
|
2021-01-15 16:57:37 +01:00
|
|
|
if TYPE_CHECKING:
|
2023-02-27 14:26:05 +01:00
|
|
|
from synapse.media.media_repository import MediaRepository
|
2021-03-23 12:12:48 +01:00
|
|
|
from synapse.server import HomeServer
|
2021-01-15 16:57:37 +01:00
|
|
|
|
2021-09-07 15:10:34 +02:00
|
|
|
|
2020-07-03 20:02:19 +02:00
|
|
|
class PreviewUrlResource(DirectServeJsonResource):
|
2021-09-07 15:10:34 +02:00
|
|
|
"""
|
2022-07-12 21:01:58 +02:00
|
|
|
The `GET /_matrix/media/r0/preview_url` endpoint provides a generic preview API
|
|
|
|
for URLs which outputs Open Graph (https://ogp.me/) responses (with some Matrix
|
|
|
|
specific additions).
|
|
|
|
|
|
|
|
This does have trade-offs compared to other designs:
|
|
|
|
|
|
|
|
* Pros:
|
|
|
|
* Simple and flexible; can be used by any clients at any point
|
|
|
|
* Cons:
|
|
|
|
* If each homeserver provides one of these independently, all the homeservers in a
|
|
|
|
room may needlessly DoS the target URI
|
|
|
|
* The URL metadata must be stored somewhere, rather than just using Matrix
|
|
|
|
itself to store the media.
|
|
|
|
* Matrix cannot be used to distribute the metadata between homeservers.
|
2021-09-07 15:10:34 +02:00
|
|
|
"""
|
|
|
|
|
2016-01-25 00:47:27 +01:00
|
|
|
isLeaf = True
|
|
|
|
|
2021-01-15 16:57:37 +01:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
hs: "HomeServer",
|
|
|
|
media_repo: "MediaRepository",
|
|
|
|
media_storage: MediaStorage,
|
|
|
|
):
|
2019-06-29 09:06:55 +02:00
|
|
|
super().__init__()
|
2016-04-19 15:45:05 +02:00
|
|
|
|
|
|
|
self.auth = hs.get_auth()
|
|
|
|
self.clock = hs.get_clock()
|
2016-04-19 15:51:34 +02:00
|
|
|
self.media_repo = media_repo
|
2018-01-09 15:36:07 +01:00
|
|
|
self.media_storage = media_storage
|
2016-04-19 15:45:05 +02:00
|
|
|
|
2023-03-20 19:32:26 +01:00
|
|
|
self._url_previewer = UrlPreviewer(hs, media_repo, media_storage)
|
2017-09-28 13:18:06 +02:00
|
|
|
|
2021-09-24 12:01:25 +02:00
|
|
|
async def _async_render_OPTIONS(self, request: SynapseRequest) -> None:
|
2019-07-02 20:01:28 +02:00
|
|
|
request.setHeader(b"Allow", b"OPTIONS, GET")
|
2020-07-03 20:02:19 +02:00
|
|
|
respond_with_json(request, 200, {}, send_cors=True)
|
2017-11-23 18:52:31 +01:00
|
|
|
|
2021-03-12 17:37:57 +01:00
|
|
|
async def _async_render_GET(self, request: SynapseRequest) -> None:
|
2016-04-11 11:39:16 +02:00
|
|
|
# XXX: if get_user_by_req fails, what should we do in an async render?
|
2019-06-29 09:06:55 +02:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2021-07-21 15:47:56 +02:00
|
|
|
url = parse_string(request, "url", required=True)
|
|
|
|
ts = parse_integer(request, "ts")
|
|
|
|
if ts is None:
|
2016-04-11 11:39:16 +02:00
|
|
|
ts = self.clock.time_msec()
|
|
|
|
|
2023-03-20 19:32:26 +01:00
|
|
|
og = await self._url_previewer.preview(url, requester.user, ts)
|
2017-11-10 17:34:33 +01:00
|
|
|
respond_with_json_bytes(request, 200, og, send_cors=True)
|