From 53385e80d3227784b5c5e87c040043b80aa900bb Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Thu, 3 May 2018 13:16:11 +0100 Subject: [PATCH 01/15] Add GET media/v1/limits --- synapse/rest/media/v1/limits_resource.py | 34 +++++++++++++++++++++++ synapse/rest/media/v1/media_repository.py | 3 +- 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 synapse/rest/media/v1/limits_resource.py diff --git a/synapse/rest/media/v1/limits_resource.py b/synapse/rest/media/v1/limits_resource.py new file mode 100644 index 000000000000..d36b2430993c --- /dev/null +++ b/synapse/rest/media/v1/limits_resource.py @@ -0,0 +1,34 @@ +# -*- coding: utf-8 -*- +# Copyright 2018 Will Hunt +# +# 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 synapse.http.servlet + +from twisted.web.server import NOT_DONE_YET +from twisted.web.resource import Resource +from synapse.http.server import respond_with_json + + +class MediaLimitsResource(Resource): + # isLeaf = True + + def __init__(self, hs): + Resource.__init__(self) + self.limits_dict = {} + config = hs.get_config() + self.limits_dict["size"] = config.max_upload_size + + def render_GET(self, request): + respond_with_json(request, 200, self.limits_dict) + return NOT_DONE_YET diff --git a/synapse/rest/media/v1/media_repository.py b/synapse/rest/media/v1/media_repository.py index bb7959937919..33da948020af 100644 --- a/synapse/rest/media/v1/media_repository.py +++ b/synapse/rest/media/v1/media_repository.py @@ -25,6 +25,7 @@ from .thumbnail_resource import ThumbnailResource from .identicon_resource import IdenticonResource from .preview_url_resource import PreviewUrlResource +from .limits_resource import MediaLimitsResource from .filepath import MediaFilePaths from .thumbnailer import Thumbnailer from .storage_provider import StorageProviderWrapper @@ -745,7 +746,6 @@ def __init__(self, hs): Resource.__init__(self) media_repo = hs.get_media_repository() - self.putChild("upload", UploadResource(hs, media_repo)) self.putChild("download", DownloadResource(hs, media_repo)) self.putChild("thumbnail", ThumbnailResource( @@ -756,3 +756,4 @@ def __init__(self, hs): self.putChild("preview_url", PreviewUrlResource( hs, media_repo, media_repo.media_storage, )) + self.putChild("limits", MediaLimitsResource(hs)) From 39763527b0c343c5f373e4362fe68f354cb1f9d5 Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Thu, 3 May 2018 16:59:16 +0100 Subject: [PATCH 02/15] size > upload_size as per spec feedback --- synapse/rest/media/v1/limits_resource.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/synapse/rest/media/v1/limits_resource.py b/synapse/rest/media/v1/limits_resource.py index d36b2430993c..7f936d150d8e 100644 --- a/synapse/rest/media/v1/limits_resource.py +++ b/synapse/rest/media/v1/limits_resource.py @@ -12,12 +12,10 @@ # 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 synapse.http.servlet - +# from twisted.web.server import NOT_DONE_YET from twisted.web.resource import Resource -from synapse.http.server import respond_with_json +from synapse.http.server import respond_with_json, respond_with_json_bytes class MediaLimitsResource(Resource): @@ -27,8 +25,12 @@ def __init__(self, hs): Resource.__init__(self) self.limits_dict = {} config = hs.get_config() - self.limits_dict["size"] = config.max_upload_size + self.limits_dict["upload_size"] = config.max_upload_size def render_GET(self, request): - respond_with_json(request, 200, self.limits_dict) + respond_with_json(request, 200, self.limits_dict, send_cors=True) + return NOT_DONE_YET + + def render_OPTIONS(self, request): + respond_with_json_bytes(request, 200, {}, send_cors=True) return NOT_DONE_YET From f8b06fc0d622f8cfd6331f4e570b05bd65388282 Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Tue, 8 May 2018 15:30:35 +0100 Subject: [PATCH 03/15] Make MediaLimitsResource Leafy --- synapse/rest/media/v1/limits_resource.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse/rest/media/v1/limits_resource.py b/synapse/rest/media/v1/limits_resource.py index 7f936d150d8e..64f41191abaf 100644 --- a/synapse/rest/media/v1/limits_resource.py +++ b/synapse/rest/media/v1/limits_resource.py @@ -19,7 +19,7 @@ class MediaLimitsResource(Resource): - # isLeaf = True + isLeaf = True def __init__(self, hs): Resource.__init__(self) From fee045001a3de4dcda77a70099acc89acd507aec Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Tue, 8 May 2018 15:31:53 +0100 Subject: [PATCH 04/15] Restructure __init__ --- synapse/rest/media/v1/limits_resource.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/synapse/rest/media/v1/limits_resource.py b/synapse/rest/media/v1/limits_resource.py index 64f41191abaf..3cb5f0244b18 100644 --- a/synapse/rest/media/v1/limits_resource.py +++ b/synapse/rest/media/v1/limits_resource.py @@ -23,9 +23,10 @@ class MediaLimitsResource(Resource): def __init__(self, hs): Resource.__init__(self) - self.limits_dict = {} config = hs.get_config() - self.limits_dict["upload_size"] = config.max_upload_size + self.limits_dict = { + "upload_size": config.max_upload_size, + } def render_GET(self, request): respond_with_json(request, 200, self.limits_dict, send_cors=True) From c06b1cb0714a0d82d8e3540d4622700f7829685a Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Wed, 20 Jun 2018 17:25:51 +0100 Subject: [PATCH 05/15] Update and rename limits_resource.py to config_resource.py --- .../rest/media/v1/{limits_resource.py => config_resource.py} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename synapse/rest/media/v1/{limits_resource.py => config_resource.py} (97%) diff --git a/synapse/rest/media/v1/limits_resource.py b/synapse/rest/media/v1/config_resource.py similarity index 97% rename from synapse/rest/media/v1/limits_resource.py rename to synapse/rest/media/v1/config_resource.py index 3cb5f0244b18..5b1e5348231e 100644 --- a/synapse/rest/media/v1/limits_resource.py +++ b/synapse/rest/media/v1/config_resource.py @@ -18,7 +18,7 @@ from synapse.http.server import respond_with_json, respond_with_json_bytes -class MediaLimitsResource(Resource): +class MediaConfigResource(Resource): isLeaf = True def __init__(self, hs): From b1d87771c3d03275e0977d7a92465f5f82267be8 Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Wed, 20 Jun 2018 17:26:38 +0100 Subject: [PATCH 06/15] media limits => config --- synapse/rest/media/v1/media_repository.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse/rest/media/v1/media_repository.py b/synapse/rest/media/v1/media_repository.py index 33da948020af..4f279e235c50 100644 --- a/synapse/rest/media/v1/media_repository.py +++ b/synapse/rest/media/v1/media_repository.py @@ -25,7 +25,7 @@ from .thumbnail_resource import ThumbnailResource from .identicon_resource import IdenticonResource from .preview_url_resource import PreviewUrlResource -from .limits_resource import MediaLimitsResource +from .config_resource import MediaConfigResource from .filepath import MediaFilePaths from .thumbnailer import Thumbnailer from .storage_provider import StorageProviderWrapper From 1d53129fa3525a59c231f2de50b9e12cc5ce60ad Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Thu, 21 Jun 2018 11:05:05 +0100 Subject: [PATCH 07/15] upload_size => m.upload.size --- synapse/rest/media/v1/config_resource.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse/rest/media/v1/config_resource.py b/synapse/rest/media/v1/config_resource.py index 5b1e5348231e..3fd028842540 100644 --- a/synapse/rest/media/v1/config_resource.py +++ b/synapse/rest/media/v1/config_resource.py @@ -25,7 +25,7 @@ def __init__(self, hs): Resource.__init__(self) config = hs.get_config() self.limits_dict = { - "upload_size": config.max_upload_size, + "m.upload.size": config.max_upload_size, } def render_GET(self, request): From 0aa14e409349f74d4a64ffb0e21a0d19d24433d7 Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Thu, 21 Jun 2018 11:09:36 +0100 Subject: [PATCH 08/15] /limits => /config --- synapse/rest/media/v1/media_repository.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse/rest/media/v1/media_repository.py b/synapse/rest/media/v1/media_repository.py index 4f279e235c50..c4e094a39f55 100644 --- a/synapse/rest/media/v1/media_repository.py +++ b/synapse/rest/media/v1/media_repository.py @@ -756,4 +756,4 @@ def __init__(self, hs): self.putChild("preview_url", PreviewUrlResource( hs, media_repo, media_repo.media_storage, )) - self.putChild("limits", MediaLimitsResource(hs)) + self.putChild("config", MediaConfigResource(hs)) From 87fe4f278d4500e3e5b3cd7e7e44814aedcea3df Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Mon, 9 Jul 2018 15:17:12 +0100 Subject: [PATCH 09/15] Add import for MediaConfigResource --- synapse/rest/media/v1/media_repository.py | 1 + 1 file changed, 1 insertion(+) diff --git a/synapse/rest/media/v1/media_repository.py b/synapse/rest/media/v1/media_repository.py index 16485e55198c..268cc4a74d09 100644 --- a/synapse/rest/media/v1/media_repository.py +++ b/synapse/rest/media/v1/media_repository.py @@ -50,6 +50,7 @@ from .thumbnail_resource import ThumbnailResource from .thumbnailer import Thumbnailer from .upload_resource import UploadResource +from .config_resource import MediaConfigResource logger = logging.getLogger(__name__) From 48251dc94c664cc857c981d767c4c04cd57e7c5e Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Mon, 9 Jul 2018 16:09:43 +0100 Subject: [PATCH 10/15] Create 3184.feature --- changelog.d/3184.feature | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/3184.feature diff --git a/changelog.d/3184.feature b/changelog.d/3184.feature new file mode 100644 index 000000000000..9f746a57a0fa --- /dev/null +++ b/changelog.d/3184.feature @@ -0,0 +1 @@ +Add /_media/r0/config From 3f520b20bd8fb22d82fc9249344fece8b5e314ad Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Wed, 18 Jul 2018 14:22:21 +0100 Subject: [PATCH 11/15] wrapped --- synapse/rest/media/v1/config_resource.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/synapse/rest/media/v1/config_resource.py b/synapse/rest/media/v1/config_resource.py index 3fd028842540..2272e35b0cae 100644 --- a/synapse/rest/media/v1/config_resource.py +++ b/synapse/rest/media/v1/config_resource.py @@ -15,7 +15,7 @@ # from twisted.web.server import NOT_DONE_YET from twisted.web.resource import Resource -from synapse.http.server import respond_with_json, respond_with_json_bytes +from synapse.http.server import respond_with_json, respond_with_json_bytes, wrap_json_request_handler class MediaConfigResource(Resource): @@ -28,6 +28,7 @@ def __init__(self, hs): "m.upload.size": config.max_upload_size, } + @wrap_json_request_handler def render_GET(self, request): respond_with_json(request, 200, self.limits_dict, send_cors=True) return NOT_DONE_YET From 8cc46ed4a6ec2fe82678f94fd908b9a711372d76 Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Fri, 27 Jul 2018 12:01:13 +0100 Subject: [PATCH 12/15] wrap_json_request_handlering --- synapse/rest/media/v1/config_resource.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/synapse/rest/media/v1/config_resource.py b/synapse/rest/media/v1/config_resource.py index 2272e35b0cae..c53ba285409b 100644 --- a/synapse/rest/media/v1/config_resource.py +++ b/synapse/rest/media/v1/config_resource.py @@ -13,9 +13,11 @@ # See the License for the specific language governing permissions and # limitations under the License. # -from twisted.web.server import NOT_DONE_YET + +from twisted.internet import defer from twisted.web.resource import Resource -from synapse.http.server import respond_with_json, respond_with_json_bytes, wrap_json_request_handler +from twisted.web.server import NOT_DONE_YET +from synapse.http.server import respond_with_json, wrap_json_request_handler class MediaConfigResource(Resource): @@ -24,15 +26,19 @@ class MediaConfigResource(Resource): def __init__(self, hs): Resource.__init__(self) config = hs.get_config() + self.clock = hs.get_clock() self.limits_dict = { "m.upload.size": config.max_upload_size, } - @wrap_json_request_handler def render_GET(self, request): - respond_with_json(request, 200, self.limits_dict, send_cors=True) + self._async_render_GET(request) return NOT_DONE_YET + @wrap_json_request_handler + def _async_render_GET(self, request): + return respond_with_json(request, 200, self.limits_dict) + def render_OPTIONS(self, request): - respond_with_json_bytes(request, 200, {}, send_cors=True) - return NOT_DONE_YET + respond_with_json(request, 200, {}, send_cors=True) + return NOT_DONE_YET \ No newline at end of file From bf13c184cdb8e44f9d5644c31d8b17b7c83dc321 Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Fri, 27 Jul 2018 12:03:34 +0100 Subject: [PATCH 13/15] linting --- synapse/rest/media/v1/config_resource.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/synapse/rest/media/v1/config_resource.py b/synapse/rest/media/v1/config_resource.py index c53ba285409b..b1018e706793 100644 --- a/synapse/rest/media/v1/config_resource.py +++ b/synapse/rest/media/v1/config_resource.py @@ -14,7 +14,6 @@ # limitations under the License. # -from twisted.internet import defer from twisted.web.resource import Resource from twisted.web.server import NOT_DONE_YET from synapse.http.server import respond_with_json, wrap_json_request_handler @@ -41,4 +40,4 @@ def _async_render_GET(self, request): def render_OPTIONS(self, request): respond_with_json(request, 200, {}, send_cors=True) - return NOT_DONE_YET \ No newline at end of file + return NOT_DONE_YET From bf30fd834229cebb547c7b09510b8990b638afea Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Fri, 27 Jul 2018 12:17:40 +0100 Subject: [PATCH 14/15] Auth media/r0/config --- synapse/rest/media/v1/config_resource.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/synapse/rest/media/v1/config_resource.py b/synapse/rest/media/v1/config_resource.py index b1018e706793..44d3d567aa38 100644 --- a/synapse/rest/media/v1/config_resource.py +++ b/synapse/rest/media/v1/config_resource.py @@ -14,6 +14,7 @@ # limitations under the License. # +from twisted.internet import defer from twisted.web.resource import Resource from twisted.web.server import NOT_DONE_YET from synapse.http.server import respond_with_json, wrap_json_request_handler @@ -26,6 +27,7 @@ def __init__(self, hs): Resource.__init__(self) config = hs.get_config() self.clock = hs.get_clock() + self.auth = hs.get_auth() self.limits_dict = { "m.upload.size": config.max_upload_size, } @@ -35,8 +37,10 @@ def render_GET(self, request): return NOT_DONE_YET @wrap_json_request_handler + @defer.inlineCallbacks def _async_render_GET(self, request): - return respond_with_json(request, 200, self.limits_dict) + yield self.auth.get_user_by_req(request) + respond_with_json(request, 200, self.limits_dict) def render_OPTIONS(self, request): respond_with_json(request, 200, {}, send_cors=True) From 0f6e583e6ff598deab41976d16f7ca9eaa97fbd3 Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Fri, 27 Jul 2018 12:28:08 +0100 Subject: [PATCH 15/15] isorted this mess --- synapse/rest/media/v1/config_resource.py | 1 + synapse/rest/media/v1/media_repository.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/synapse/rest/media/v1/config_resource.py b/synapse/rest/media/v1/config_resource.py index 44d3d567aa38..d6605b6027c4 100644 --- a/synapse/rest/media/v1/config_resource.py +++ b/synapse/rest/media/v1/config_resource.py @@ -17,6 +17,7 @@ from twisted.internet import defer from twisted.web.resource import Resource from twisted.web.server import NOT_DONE_YET + from synapse.http.server import respond_with_json, wrap_json_request_handler diff --git a/synapse/rest/media/v1/media_repository.py b/synapse/rest/media/v1/media_repository.py index 6a16f9043cba..6b08f76e97be 100644 --- a/synapse/rest/media/v1/media_repository.py +++ b/synapse/rest/media/v1/media_repository.py @@ -42,6 +42,7 @@ from synapse.util.stringutils import is_ascii, random_string from ._base import FileInfo, respond_404, respond_with_responder +from .config_resource import MediaConfigResource from .download_resource import DownloadResource from .filepath import MediaFilePaths from .identicon_resource import IdenticonResource @@ -51,7 +52,6 @@ from .thumbnail_resource import ThumbnailResource from .thumbnailer import Thumbnailer from .upload_resource import UploadResource -from .config_resource import MediaConfigResource logger = logging.getLogger(__name__)