From 9b2faaaa961a257330cd3ea5235bd300dc3b4582 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 10 Aug 2023 10:40:01 +0200 Subject: [PATCH 1/4] =?UTF-8?q?=F0=9F=90=9B=20Switch=20to=20dedicated=20we?= =?UTF-8?q?bcam=20snapshot=20handler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes deprecation warning logged when creating tornado endpoint /downloads/camera/current. Closes #4867 --- src/octoprint/server/__init__.py | 5 +- src/octoprint/server/util/tornado.py | 100 +++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 3 deletions(-) diff --git a/src/octoprint/server/__init__.py b/src/octoprint/server/__init__.py index 8299d34a3..2fcca6f1b 100644 --- a/src/octoprint/server/__init__.py +++ b/src/octoprint/server/__init__.py @@ -934,11 +934,10 @@ class Server: # camera snapshot ( r"/downloads/camera/current", - util.tornado.UrlProxyHandler, + util.tornado.WebcamSnapshotHandler, joined_dict( { - "url": self._settings.get(["webcam", "snapshot"]), - "as_attachment": True, + "as_attachment": "snapshot", }, camera_permission_validator, ), diff --git a/src/octoprint/server/util/tornado.py b/src/octoprint/server/util/tornado.py index 89e41e9c6..a5bf04adb 100644 --- a/src/octoprint/server/util/tornado.py +++ b/src/octoprint/server/util/tornado.py @@ -1379,6 +1379,106 @@ class StaticDataHandler( self.finish() +class GeneratingDataHandler( + RequestlessExceptionLoggingMixin, CorsSupportMixin, tornado.web.RequestHandler +): + """ + A `RequestHandler` that generates data from a generator function and returns it to the client. + + Arguments: + generator (function): A generator function that returns the data to be written to the client. The function + will be called without any parameters. + content_type (str): The content type with which to respond. Defaults to `text/plain` + as_attachment (bool | str): Whether to serve files with `Content-Disposition: attachment` header (`True`) + or not. Defaults to `False`. If a string is given it will be used as the filename of the attachment. + access_validation (function): Callback to call in the `get` method to validate access to the resource. Will + be called with `self.request` as parameter which contains the full tornado request object. Should raise + a `tornado.web.HTTPError` if access is not allowed in which case the request will not be further processed. + Defaults to `None` and hence no access validation being performed. + """ + + def initialize( + self, + generator=None, + content_type="text/plain", + as_attachment=False, + access_validation=None, + ): + super().initialize() + self._generator = generator + self._content_type = content_type + self._as_attachment = as_attachment + self._access_validation = access_validation + + @tornado.gen.coroutine + def get(self, *args, **kwargs): + if self._access_validation is not None: + self._access_validation(self.request) + + self.set_status(200) + self.set_header("Content-Type", self._content_type) + self.set_content_disposition() + for data in self._generator(): + self.write(data) + yield self.flush() + self.finish() + + def set_content_disposition(self): + if self._as_attachment: + if isinstance(self._as_attachment, str): + self.set_header( + "Content-Disposition", f"attachment; filename={self._as_attachment}" + ) + else: + self.set_header("Content-Disposition", "attachment") + + +class WebcamSnapshotHandler(GeneratingDataHandler): + """ + `GeneratingDataHandler` that returns a snapshot from the configured webcam. + + Arguments: + as_attachment (bool | str): Whether to serve files with `Content-Disposition: attachment` header (`True`) + or not. Defaults to `False`. If a string is given it will be used as the filename of the attachment. + access_validation (function): Callback to call in the `get` method to validate access to the resource. Will + be called with `self.request` as parameter which contains the full tornado request object. Should raise + a `tornado.web.HTTPError` if access is not allowed in which case the request will not be further processed. + Defaults to `None` and hence no access validation being performed. + """ + + def initialize(self, as_attachment=False, access_validation=None): + super().initialize( + content_type="image/jpeg", + as_attachment=as_attachment, + access_validation=access_validation, + ) + + @tornado.gen.coroutine + def get(self, *args, **kwargs): + if self._access_validation is not None: + self._access_validation(self.request) + + import functools + + from octoprint.webcams import get_snapshot_webcam + + webcam = get_snapshot_webcam() + if not webcam: + raise tornado.web.HTTPError(404) + + generator = functools.partial( + webcam.providerPlugin.take_webcam_snapshot, webcam.config.name + ) + + self.set_status(200) + self.set_header("Content-Type", self._content_type) + self.set_content_disposition() + for data in generator(): + self.write(data) + yield self.flush() + self.finish() + + class DeprecatedEndpointHandler(CorsSupportMixin, tornado.web.RequestHandler): """ `tornado.web.RequestHandler `_ that redirects From d4b131700a54df516e18abbb8a0a31d828fca990 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 10 Aug 2023 12:45:28 +0200 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=90=9B=20config=20cli:=20Init=20plugi?= =?UTF-8?q?n=20settings=20if=20needed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Otherwise we won't be able to access the plugin default values and that leads to issues on querying full plugin setting trees. Fixes #4835 --- src/octoprint/cli/config.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/octoprint/cli/config.py b/src/octoprint/cli/config.py index 7ace969c5..debfc78a8 100644 --- a/src/octoprint/cli/config.py +++ b/src/octoprint/cli/config.py @@ -8,7 +8,7 @@ import pprint import click -from octoprint import FatalStartupError, init_settings +from octoprint import FatalStartupError, init_pluginsystem, init_settings from octoprint.cli import get_ctx_obj_option, standard_options from octoprint.util import yaml @@ -41,6 +41,17 @@ def _set_helper(settings, path, value, data_type=None): settings.save() +def _init_pluginsettings(ctx): + try: + ctx.obj.plugin_manager = init_pluginsystem( + ctx.obj.settings, safe_mode=get_ctx_obj_option(ctx, "safe_mode", False) + ) + except FatalStartupError as e: + click.echo(str(e), err=True) + click.echo("There was a fatal error initializing the plugin manager.", err=True) + ctx.exit(-1) + + # ~~ "octoprint config" commands @@ -112,6 +123,8 @@ def remove_command(ctx, path): def append_value_command(ctx, path, value, as_json=False): """Appends value to list behind config path.""" path = _to_settings_path(path) + if len(path) == 0 or path[0] == "plugins": + _init_pluginsettings(ctx) if as_json: try: @@ -141,6 +154,8 @@ def append_value_command(ctx, path, value, as_json=False): def insert_value_command(ctx, path, index, value, as_json=False): """Inserts value at index of list behind config key.""" path = _to_settings_path(path) + if len(path) == 0 or path[0] == "plugins": + _init_pluginsettings(ctx) if as_json: try: @@ -169,6 +184,8 @@ def insert_value_command(ctx, path, index, value, as_json=False): def remove_value_command(ctx, path, value, as_json=False): """Removes value from list at config path.""" path = _to_settings_path(path) + if len(path) == 0 or path[0] == "plugins": + _init_pluginsettings(ctx) if as_json: try: @@ -204,6 +221,8 @@ def remove_value_command(ctx, path, value, as_json=False): def get_command(ctx, path, as_json=False, as_yaml=False, as_raw=False): """Retrieves value from config path.""" path = _to_settings_path(path) + if len(path) == 0 or path[0] == "plugins": + _init_pluginsettings(ctx) value = ctx.obj.settings.get(path, merged=True) if as_json: @@ -228,6 +247,7 @@ def get_command(ctx, path, as_json=False, as_yaml=False, as_raw=False): @click.pass_context def effective_command(ctx, as_json=False, as_yaml=False, as_raw=False): """Retrieves the full effective config.""" + _init_pluginsettings(ctx) value = ctx.obj.settings.effective if as_json: From 415f9120a30efe9f395c9624e976c70348509aaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 10 Aug 2023 12:46:04 +0200 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=90=9B=20Don't=20fail=20settings=20ge?= =?UTF-8?q?t=20on=20lack=20of=20default?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/octoprint/settings/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/octoprint/settings/__init__.py b/src/octoprint/settings/__init__.py index 4ca274dda..84c94db89 100644 --- a/src/octoprint/settings/__init__.py +++ b/src/octoprint/settings/__init__.py @@ -1652,7 +1652,8 @@ class Settings: if default_value is not None: value = dict_merge(default_value, value) except KeyError: - raise NoSuchSettingsPath() + # no default value, so nothing to merge + pass if callable(preprocessor): value = preprocessor(value) From 97f54f2629bb5a5427c5552b6aa9b11a75e5356e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 10 Aug 2023 16:27:20 +0200 Subject: [PATCH 4/4] =?UTF-8?q?=E2=9C=A8=20classicwebcam:=20Support=20conf?= =?UTF-8?q?igured=20aspect=20ratio=20on=20video?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also make sure we don't go beyond a 1:1 aspect ration on rotation. Finally get rid of some CSS issues caused by a loss of flexbox mode in deeper divs. Implements #4837 --- .../static/css/classicwebcam.css | 2 +- .../classicwebcam/static/js/classicwebcam.js | 17 +++-- .../static/less/classicwebcam.less | 68 +++++++++++-------- .../templates/classicwebcam_webcam.jinja2 | 2 +- 4 files changed, 53 insertions(+), 36 deletions(-) diff --git a/src/octoprint/plugins/classicwebcam/static/css/classicwebcam.css b/src/octoprint/plugins/classicwebcam/static/css/classicwebcam.css index 86591a352..420a2beae 100644 --- a/src/octoprint/plugins/classicwebcam/static/css/classicwebcam.css +++ b/src/octoprint/plugins/classicwebcam/static/css/classicwebcam.css @@ -1 +1 @@ -#webcam_video_container{width:100%;position:relative;outline:0}#webcam_video_container .video-controls{position:absolute;bottom:0;right:8px;z-index:10;opacity:.4}#webcam_video_container:hover .video-controls{opacity:1}#webcam_video_container #webcam_hls,#webcam_video_container #webcam_rtsp{width:100%;height:100%}#webcam_video_container .webcam_rotated{position:relative;width:100%;padding-bottom:56.25%;display:flex;justify-content:center;align-items:center;margin:auto}#webcam_video_container .webcam_rotated .rotation_target{position:absolute;transform:rotate(-90deg);width:100%}#webcam_img_container,#webcam_video_container .webcam_unrotated{width:100%;position:relative}#webcam_video_container .webcam_rotated .rotation_target div{width:100%;height:100%}#webcam_img_container{outline:0;background-color:#000}#webcam_img_container .keycontrol_overlay{position:absolute;left:10px;right:10px;bottom:10px;background:rgba(0,0,0,.5);font-size:85%;color:#fff;padding:0}#webcam_img_container .keycontrol_overlay kbd{border:1px solid #eee;border-radius:3px;margin-left:2px;margin-right:2px;font-size:90%;padding:2px;min-width:1em}#webcam_img_container .keycontrol_overlay .keycontrol_overlay_heading{position:relative;padding:10px;font-weight:700}#webcam_img_container .keycontrol_overlay .keycontrol_overlay_column{position:relative;width:45%;padding:10px;float:left}#webcam_img_container .nowebcam{position:absolute;top:0;left:0;right:0;bottom:0}#webcam_img_container .nowebcam .text{color:#fff;text-align:center;position:relative;margin:auto;width:80%;top:50%;transform:translateY(-50%);display:block}#webcam_img_container .nowebcam .text.webcam_loading{animation:pulsate 3s ease-out;animation-iteration-count:infinite}#webcam_img_container .webcam_rotated{position:relative;width:100%;padding-bottom:100%;pointer-events:none}#webcam_img_container .webcam_rotated .webcam_fixed_ratio{position:absolute;transform:rotate(-90deg);top:0;bottom:0;pointer-events:none}#webcam_img_container .webcam_rotated .webcam_fixed_ratio .webcam_fixed_ratio_inner{width:100%;height:100%;pointer-events:none}#webcam_img_container .webcam_unrotated .webcam_fixed_ratio{width:100%;pointer-events:none;padding-bottom:100%;position:relative}#webcam_img_container .webcam_unrotated .webcam_fixed_ratio.ratio43{padding-bottom:75%}#webcam_img_container .webcam_unrotated .webcam_fixed_ratio.ratio169{padding-bottom:56.25%}#webcam_img_container .webcam_unrotated .webcam_fixed_ratio.ratio1610{padding-bottom:62.5%}#webcam_img_container .webcam_unrotated .webcam_fixed_ratio .webcam_fixed_ratio_inner{position:absolute;top:0;bottom:0;left:0;right:0;pointer-events:none}#webcam_img_container img{width:100%;height:100%;object-fit:contain}#webcamTestContainer{min-height:384px} \ No newline at end of file +.classicwebcam_aspect_ratio_mixin{aspect-ratio:1}.classicwebcam_aspect_ratio_mixin.ratio43{aspect-ratio:4/3}.classicwebcam_aspect_ratio_mixin.ratio169{aspect-ratio:16/9}.classicwebcam_aspect_ratio_mixin.ratio1610{aspect-ratio:16/10}.classicwebcam_flexbox_mixin{width:100%;display:flex;justify-content:center;align-items:center}#webcam_video_container{width:100%;position:relative;outline:0}#webcam_video_container .video-controls{position:absolute;bottom:0;right:8px;z-index:10;opacity:.4}#webcam_video_container:hover .video-controls{opacity:1}#webcam_video_container #webcam_hls,#webcam_video_container #webcam_rtsp{width:100%;height:100%}#webcam_video_container .webcam_rotated,#webcam_video_container .webcam_unrotated{position:relative;width:100%;display:flex;justify-content:center;align-items:center;aspect-ratio:1}#webcam_video_container .webcam_rotated.ratio43,#webcam_video_container .webcam_unrotated.ratio43{aspect-ratio:4/3}#webcam_video_container .webcam_rotated.ratio169,#webcam_video_container .webcam_unrotated.ratio169{aspect-ratio:16/9}#webcam_video_container .webcam_rotated.ratio1610,#webcam_video_container .webcam_unrotated.ratio1610{aspect-ratio:16/10}#webcam_video_container .webcam_rotated .rotation_target,#webcam_video_container .webcam_rotated .rotation_target div,#webcam_video_container .webcam_unrotated .rotation_target,#webcam_video_container .webcam_unrotated .rotation_target div{width:100%;display:flex;justify-content:center;align-items:center}#webcam_video_container .webcam_rotated{aspect-ratio:1}#webcam_video_container .webcam_rotated .rotation_target{position:absolute;transform:rotate(-90deg)}#webcam_video_container .webcam_rotated .rotation_target div{height:100%}#webcam_img_container{width:100%;position:relative;outline:0;background-color:#000}#webcam_img_container .keycontrol_overlay{position:absolute;left:10px;right:10px;bottom:10px;background:rgba(0,0,0,.5);font-size:85%;color:#fff;padding:0}#webcam_img_container .keycontrol_overlay kbd{border:1px solid #eee;border-radius:3px;margin-left:2px;margin-right:2px;font-size:90%;padding:2px;min-width:1em}#webcam_img_container .keycontrol_overlay .keycontrol_overlay_heading{position:relative;padding:10px;font-weight:700}#webcam_img_container .keycontrol_overlay .keycontrol_overlay_column{position:relative;width:45%;padding:10px;float:left}#webcam_img_container .nowebcam{position:absolute;top:0;left:0;right:0;bottom:0}#webcam_img_container .nowebcam .text{color:#fff;text-align:center;position:relative;margin:auto;width:80%;top:50%;transform:translateY(-50%);display:block}#webcam_img_container .nowebcam .text.webcam_loading{animation:pulsate 3s ease-out;animation-iteration-count:infinite}#webcam_img_container .webcam_rotated{position:relative;width:100%;padding-bottom:100%;pointer-events:none}#webcam_img_container .webcam_rotated .webcam_fixed_ratio{position:absolute;transform:rotate(-90deg);top:0;bottom:0;pointer-events:none}#webcam_img_container .webcam_rotated .webcam_fixed_ratio .webcam_fixed_ratio_inner{width:100%;height:100%;pointer-events:none}#webcam_img_container .webcam_unrotated .webcam_fixed_ratio{width:100%;pointer-events:none;aspect-ratio:1;position:relative}#webcam_img_container .webcam_unrotated .webcam_fixed_ratio.ratio43{aspect-ratio:4/3}#webcam_img_container .webcam_unrotated .webcam_fixed_ratio.ratio169{aspect-ratio:16/9}#webcam_img_container .webcam_unrotated .webcam_fixed_ratio.ratio1610{aspect-ratio:16/10}#webcam_img_container .webcam_unrotated .webcam_fixed_ratio .webcam_fixed_ratio_inner{position:absolute;top:0;bottom:0;left:0;right:0;pointer-events:none}#webcam_img_container img{width:100%;height:100%;object-fit:contain}#webcamTestContainer{min-height:384px} \ No newline at end of file diff --git a/src/octoprint/plugins/classicwebcam/static/js/classicwebcam.js b/src/octoprint/plugins/classicwebcam/static/js/classicwebcam.js index 1ebc37843..57dc0b6ef 100644 --- a/src/octoprint/plugins/classicwebcam/static/js/classicwebcam.js +++ b/src/octoprint/plugins/classicwebcam/static/js/classicwebcam.js @@ -25,6 +25,14 @@ $(function () { } }); + self.webcamClass = ko.pureComputed(function () { + if (self.settings.rotate90()) { + return "webcam_rotated " + self.webcamRatioClass(); + } else { + return "webcam_unrotated " + self.webcamRatioClass(); + } + }); + self.onBeforeBinding = function () { // Subscribe to rotation event to ensure we update calculations. // We need to wait for the CSS to be updated by KO, thus we use a timeout to @@ -289,11 +297,10 @@ $(function () { // accommodate the rotation). The target is centered in the container and // rotated around its center, so after we manually resized the container // everything will layout nicely. - if (rotationContainer && player.videoWidth && player.videoHeight) { - // Calculate the height the video will have in the UI, based on the - // video width and the aspect ratio. - var aspectRatio = player.videoWidth / player.videoHeight; - var height = aspectRatio * rotationContainer.offsetWidth; + if (rotationContainer) { + // we'll go with an aspect ration of 1:1 for rotated videos, same as + // for mjpg streams + var height = rotationContainer.offsetWidth; // Enforce the height on the rotation container and the rotation target. // Width of the container will be 100%, height will be calculated diff --git a/src/octoprint/plugins/classicwebcam/static/less/classicwebcam.less b/src/octoprint/plugins/classicwebcam/static/less/classicwebcam.less index 47c5cf6d2..bf8993ea7 100644 --- a/src/octoprint/plugins/classicwebcam/static/less/classicwebcam.less +++ b/src/octoprint/plugins/classicwebcam/static/less/classicwebcam.less @@ -1,3 +1,26 @@ +.classicwebcam_aspect_ratio_mixin { + aspect-ratio: 1; + + &.ratio43 { + aspect-ratio: 4 / 3; + } + + &.ratio169 { + aspect-ratio: 16 / 9; + } + + &.ratio1610 { + aspect-ratio: 16 / 10; + } +} + +.classicwebcam_flexbox_mixin { + width: 100%; + display: flex; + justify-content: center; + align-items: center; +} + #webcam_video_container { width: 100%; position: relative; @@ -23,30 +46,29 @@ height: 100%; } - .webcam_rotated { + .webcam_rotated, + .webcam_unrotated { position: relative; - width: 100%; - padding-bottom: 56.25%; - display: flex; - justify-content: center; - align-items: center; - margin: auto; + .classicwebcam_flexbox_mixin(); + .classicwebcam_aspect_ratio_mixin(); .rotation_target { - position: absolute; - transform: rotate(-90deg); - width: 100%; - + .classicwebcam_flexbox_mixin(); div { - width: 100%; - height: 100%; + .classicwebcam_flexbox_mixin(); } } } - .webcam_unrotated { - position: relative; - width: 100%; + .webcam_rotated { + aspect-ratio: 1; + .rotation_target { + position: absolute; + transform: rotate(-90deg); + div { + height: 100%; + } + } } } @@ -141,19 +163,7 @@ width: 100%; pointer-events: none; - padding-bottom: 100%; - - &.ratio43 { - padding-bottom: 75%; - } - - &.ratio169 { - padding-bottom: 56.25%; - } - - &.ratio1610 { - padding-bottom: 62.5%; - } + .classicwebcam_aspect_ratio_mixin(); position: relative; diff --git a/src/octoprint/plugins/classicwebcam/templates/classicwebcam_webcam.jinja2 b/src/octoprint/plugins/classicwebcam/templates/classicwebcam_webcam.jinja2 index 1e6246124..d19ef29ed 100644 --- a/src/octoprint/plugins/classicwebcam/templates/classicwebcam_webcam.jinja2 +++ b/src/octoprint/plugins/classicwebcam/templates/classicwebcam_webcam.jinja2 @@ -13,7 +13,7 @@ -
+