This repository has been archived by the owner on Mar 26, 2024. It is now read-only.
forked from matrix-org/synapse
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
See matrix-org/matrix-spec-proposals#2246 for details Signed-off-by: Sumner Evans <[email protected]>
- Loading branch information
1 parent
e78a821
commit cf0971f
Showing
5 changed files
with
142 additions
and
0 deletions.
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
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,82 @@ | ||
# Copyright 2023 Beeper Inc. | ||
# | ||
# 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.media.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
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
20 changes: 20 additions & 0 deletions
20
synapse/storage/schema/main/delta/78/05_add_unused_expires_at_for_media.sql
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,20 @@ | ||
/* Copyright 2023 Beeper Inc. | ||
* | ||
* 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. | ||
*/ | ||
|
||
-- Add new colums to the `local_media_repository` to keep track of when the | ||
-- media ID must be used by. This is to support async uploads (see MSC2246). | ||
|
||
ALTER TABLE local_media_repository | ||
ADD COLUMN unused_expires_at BIGINT DEFAULT NULL; |