This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
MSC2246 async uploads #12484
Closed
sumnerevans
wants to merge
11
commits into
matrix-org:develop
from
sumnerevans:sumner/msc2246-async-uploads
Closed
MSC2246 async uploads #12484
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
011cc4d
Remove re-assignment of self.clock in UploadResource
sumnerevans 98b82e0
config: add option to rate limit media creation
sumnerevans 9cd13e9
config: add option to gate MSC2246 async uploads
sumnerevans cb06380
config: add option for how long to wait until media ID expires
sumnerevans 48d3d8d
media: move muxing of versioned endpoints to MediaRepositoryResource
sumnerevans f74e8ed
media/create: add MSC2246 create endpoint
sumnerevans 46619f3
media/upload: add support for async uploads
sumnerevans 91a7ddd
media/{download,thumbnail}: add support for stall parameter
sumnerevans 474ff92
changelog: add entry for async uploads
sumnerevans c45369b
tests: update paths for new media repo structure
sumnerevans 0ab87e5
tests/media_storage: add async media parameter
sumnerevans File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Experimental support for asynchronous uploads as defined by [MSC2246](https://github.com/matrix-org/matrix-spec-proposals/pull/2246). Contributed by @sumnerevans at Beeper. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# Copyright 2014-2016 OpenMarket Ltd | ||
# Copyright 2020-2021 The Matrix.org Foundation C.I.C. | ||
# | ||
# 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. | ||
|
||
import logging | ||
from typing import TYPE_CHECKING | ||
|
||
from synapse.api.errors import LimitExceededError | ||
from synapse.api.ratelimiting import Ratelimiter | ||
from synapse.http.server import DirectServeJsonResource, respond_with_json | ||
from synapse.http.site import SynapseRequest | ||
|
||
if TYPE_CHECKING: | ||
from synapse.rest.media.v1.media_repository import MediaRepository | ||
from synapse.server import HomeServer | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class CreateResource(DirectServeJsonResource): | ||
isLeaf = True | ||
|
||
def __init__(self, hs: "HomeServer", media_repo: "MediaRepository"): | ||
super().__init__() | ||
|
||
self.media_repo = media_repo | ||
self.clock = hs.get_clock() | ||
self.auth = hs.get_auth() | ||
|
||
# A rate limiter for creating new media IDs. | ||
self._create_media_rate_limiter = Ratelimiter( | ||
store=hs.get_datastores().main, | ||
clock=self.clock, | ||
rate_hz=hs.config.ratelimiting.rc_media_create.per_second, | ||
burst_count=hs.config.ratelimiting.rc_media_create.burst_count, | ||
) | ||
|
||
async def _async_render_OPTIONS(self, request: SynapseRequest) -> None: | ||
respond_with_json(request, 200, {}, send_cors=True) | ||
|
||
async def _async_render_POST(self, request: SynapseRequest) -> None: | ||
requester = await self.auth.get_user_by_req(request) | ||
|
||
# If the create media requests for the user are over the limit, drop | ||
# them. | ||
allowed, time_allowed = await self._create_media_rate_limiter.can_do_action( | ||
requester | ||
) | ||
if not allowed: | ||
time_now_s = self.clock.time() | ||
raise LimitExceededError( | ||
retry_after_ms=int(1000 * (time_allowed - time_now_s)) | ||
) | ||
|
||
content_uri, unused_expires_at = await self.media_repo.create_media_id( | ||
requester.user | ||
) | ||
|
||
logger.info( | ||
"Created Media URI %r that if unused will expire at %d", | ||
content_uri, | ||
unused_expires_at, | ||
) | ||
respond_with_json( | ||
request, | ||
200, | ||
{ | ||
"content_uri": content_uri, | ||
"unused_expires_at": unused_expires_at, | ||
}, | ||
send_cors=True, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a new file so should be copy righted to
2022
to whatever legal entity you're contributing as