Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support MSC3916 by adding a federation /download endpoint #17172

Merged
merged 19 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions synapse/config/experimental.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,3 +436,7 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None:
self.msc4115_membership_on_events = experimental.get(
"msc4115_membership_on_events", False
)

self.msc3916_authenticated_media_enabled = experimental.get(
"msc3916_authenticated_media_enabled", False
)
11 changes: 6 additions & 5 deletions synapse/federation/transport/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,12 @@ def register_servlets(
):
continue

if (
servletclass == FederationUnstableMediaDownloadServlet
and not hs.config.server.enable_media_repo
):
continue
if servletclass == FederationUnstableMediaDownloadServlet:
if (
not hs.config.server.enable_media_repo
or not hs.config.experimental.msc3916_authenticated_media_enabled
):
continue

servletclass(
hs=hs,
Expand Down
14 changes: 12 additions & 2 deletions synapse/media/media_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,27 +516,37 @@ def beginFileTransfer(
return deferred

def resumeProducing(self) -> None:
anoadragon453 marked this conversation as resolved.
Show resolved Hide resolved
# write the first field, which will always be a json field
if not self.json_field_written:
self.consumer.write(CRLF + b"--" + self.boundary + b"" + CRLF)
self.consumer.write(CRLF + b"--" + self.boundary + CRLF)

content_type = Header(b"Content-Type", b"application/json")
self.consumer.write(bytes(content_type) + CRLF)

json_field = json.dumps(self.json_field)
json_bytes = json_field.encode("utf-8")
self.consumer.write(json_bytes)
self.consumer.write(CRLF + b"--" + self.boundary + b"" + CRLF)
self.consumer.write(CRLF + b"--" + self.boundary + CRLF)

self.json_field_written = True

chunk: Any = ""
if self.file:
# if we haven't written the content type yet, do so
if not self.content_type_written:
type = self.file_content_type.encode("utf-8")
content_type = Header(b"Content-Type", type)
self.consumer.write(bytes(content_type) + CRLF)
self.content_type_written = True

chunk = self.file.read(self.CHUNK_SIZE)

if not chunk:
# we've reached the end of the file
self.consumer.write(CRLF + b"--" + self.boundary + b"--" + CRLF)
self.file = None
self.consumer.unregisterProducer()

if self.deferred:
self.deferred.callback(self.lastSent)
self.deferred = None
Expand Down
4 changes: 4 additions & 0 deletions tests/federation/test_federation_media.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

from tests import unittest
from tests.test_utils import SMALL_PNG
from tests.unittest import override_config


class FederationUnstableMediaDownloads(unittest.FederatingHomeserverTestCase):
Expand All @@ -54,6 +55,9 @@ def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
)
self.media_repo = hs.get_media_repository()

@override_config(
{"experimental_features": {"msc3916_authenticated_media_enabled": True}}
)
H-Shay marked this conversation as resolved.
Show resolved Hide resolved
def test_file_download(self) -> None:
H-Shay marked this conversation as resolved.
Show resolved Hide resolved
content = io.BytesIO(b"file_to_stream")
content_uri = self.get_success(
Expand Down
Loading