🐛 Return effective select/print on uploads API

Instead of silently ignoring a select/print
on file upload if the necessary preconditions
(printer is online and free to take new jobs) aren't
met, include the effective print/select flags
on the upload API and in the triggered UPLOAD event.

Cleaner would be to simply abort such an API request
with a 409, but that will cause issues with
existing third party plugins and clients and thus
we leave it at simply making the necessary data
available without changing behaviour.

cc @jneilliii
This commit is contained in:
Gina Häußge committed 2022-01-27 15:19:42 +01:00
1 parent 71fdbea090
commit 8be58b105e
3 files changed
+75 -32

No files matched your search

+26 -9
View File
@@ -399,7 +399,9 @@ Upload file or create folder
}
}
},
"done": false
"done": false,
"effectiveSelect": true,
"effectivePrint": true
}
**Example with UTF-8 encoded filename following RFC 5987**
@@ -440,7 +442,9 @@ Upload file or create folder
}
}
},
"done": true
"done": true,
"effectiveSelect": false,
"effectivePrint": false
}
**Example for creating a folder**
@@ -486,9 +490,12 @@ Upload file or create folder
filename or ``foldername`` - basically the parent folder). If unset will be taken from the provided
``file``'s name or ``foldername`` and default to the root folder of the ``location``.
:form select: Whether to select the file directly after upload (``true``) or not (``false``). Optional, defaults
to ``false``. Ignored when creating a folder.
to ``false``. If the printer is not operational, this will have no
effect and the ``effectiveSelect`` field in the response will be set to ``false``. Ignored when creating a folder.
:form print: Whether to start printing the file directly after upload (``true``) or not (``false``). If set, ``select``
is implicitly ``true`` as well. Optional, defaults to ``false``. Ignored when creating a folder.
is implicitly ``true`` as well. Optional, defaults to ``false``. If the
printer is not operational, this will have no effect and the ``effectivePrint`` field in the response will be set
to ``false``. Ignored when creating a folder.
:form userdata: [Optional] An optional string that if specified will be interpreted as JSON and then saved along
with the file as metadata (metadata key ``userdata``). Ignored when creating a folder.
:form foldername: The name of the folder to create. Ignored when uploading a file.
@@ -869,9 +876,8 @@ Upload response
* - ``files``
- 0..1
- Object
- Abridged information regarding the file that was just uploaded. If only uploaded to ``local`` this will only
- (File only) Abridged information regarding the file that was just uploaded. If only uploaded to ``local`` this will only
contain the ``local`` property. If uploaded to SD card, this will contain both ``local`` and ``sdcard`` properties.
Only contained if a file was uploaded, not present if only a new folder was created.
* - ``files.local``
- 1
- :ref:`sec-api-datamodel-files-fileabridged`
@@ -883,12 +889,23 @@ Upload response
* - ``folder``
- 0..1
- :ref:`sec-api-datamodel-files-fileabridged`
- Abridged information regarding the folder that was just created. Only contained if a folder
was created, not present if a file was uploaded.
- (Folder only) Abridged information regarding the folder that was just created.
* - ``done``
- 1
- Boolean
- Whether any file processing after upload has already finished (``true``) or not, e.g. due to first needing
to perform a slicing step (``false``). Clients may use this information to direct progress displays related to
the upload.
the upload. Always ``true`` for folders.
* - ``effectiveSelect``
- 0..1
- Boolean
- (File only) Whether the file that was just uploaded was selected for printing (``true``) or not (``false``). If this
is ``false`` but was requested to be ``true`` in the upload request, the user lacked permissions, the printer was not
operational or already printing and thus the request could not be fulfilled.
* - ``effectivePrint``
- 0..1
- Boolean
- (File only) Whether the file that was just uploaded was started to print (``true``) or not (``false``). If this
is ``false`` but was requested to be ``true`` in the upload request, the user lacked permissions, the printer was not
operational or already printing and thus the request could not be fulfilled.
+4 -2
View File
@@ -220,8 +220,10 @@ Upload
* ``name``: the file's name
* ``path``: the file's path within its storage location
* ``target``: the target storage location to which the file was uploaded, either ``local`` or ``sdcard``
* ``select``: whether the file will immediately be selected, as requested on the API by the corresponding parameter
* ``print``: whether the file will immediately start printing, as requested on the API by the corresponding parameter
* ``select``: whether an immediate selection of the file was requested on the API by the corresponding parameter
* ``print``: whether an immediate print start of the file was requested on the API by the corresponding parameter
* ``effective_select``: whether the file will actually be selected (``select`` request got granted)
* ``effective_print``: whether the file will actually start printing (``print`` request got granted)
* ``userdata``: optional ``userdata`` if provided on the API, will only be present if supplied in the upload request
.. deprecated:: 1.3.0
+45 -21
View File
@@ -524,23 +524,13 @@ def uploadGcodeFile(target):
except Exception:
abort(400, description="userdata contains invalid JSON")
# check preconditions for SD upload
if target == FileDestinations.SDCARD and not settings().getBoolean(
["feature", "sdSupport"]
):
abort(404)
sd = target == FileDestinations.SDCARD
selectAfterUpload = (
"select" in request.values
and request.values["select"] in valid_boolean_trues
and Permissions.FILES_SELECT.can()
)
printAfterSelect = (
"print" in request.values
and request.values["print"] in valid_boolean_trues
and Permissions.PRINT.can()
)
if sd:
# validate that all preconditions for SD upload are met before attempting it
if not (
@@ -554,6 +544,33 @@ def uploadGcodeFile(target):
if not printer.is_sd_ready():
abort(409, description="Can not upload to SD card, not yet initialized")
# evaluate select and print parameter and if set check permissions & preconditions
# and adjust as necessary
#
# we do NOT abort(409) here since this would be a backwards incompatible behaviour change
# on the API, but instead return the actually effective select and print flags in the response
#
# note that this behaviour might change in a future API version
select_request = (
"select" in request.values
and request.values["select"] in valid_boolean_trues
and Permissions.FILES_SELECT.can()
)
print_request = (
"print" in request.values
and request.values["print"] in valid_boolean_trues
and Permissions.PRINT.can()
)
to_select = select_request
to_print = print_request
if (to_select or to_print) and not (
printer.is_operational()
and not (printer.is_printing() or printer.is_paused())
):
# can't select or print files if not operational or ready
to_select = to_print = False
# determine future filename of file to be uploaded, abort if it can't be uploaded
try:
# FileDestinations.LOCAL = should normally be target, but can't because SDCard handling isn't implemented yet
@@ -606,7 +623,7 @@ def uploadGcodeFile(target):
Callback for when the file processing (upload, optional slicing, addition to analysis queue) has
finished.
Depending on the file's destination triggers either streaming to SD card or directly calls selectAndOrPrint.
Depending on the file's destination triggers either streaming to SD card or directly calls to_select.
"""
if (
@@ -629,16 +646,16 @@ def uploadGcodeFile(target):
the case after they have finished streaming to the printer, which is why this callback is also used
for the corresponding call to addSdFile.
Selects the just uploaded file if either selectAfterUpload or printAfterSelect are True, or if the
Selects the just uploaded file if either to_select or to_print are True, or if the
exact file is already selected, such reloading it.
"""
if octoprint.filemanager.valid_file_type(added_file, "gcode") and (
selectAfterUpload or printAfterSelect or reselect
to_select or to_print or reselect
):
printer.select_file(
absFilename,
destination == FileDestinations.SDCARD,
printAfterSelect,
to_print,
user,
)
@@ -655,9 +672,6 @@ def uploadGcodeFile(target):
abort(415, description="Could not upload file, invalid type")
else:
abort(500, description="Could not upload file")
filename = added_file
done = True
else:
filename = fileProcessingFinished(
added_file,
@@ -680,8 +694,10 @@ def uploadGcodeFile(target):
"name": futureFilename,
"path": filename,
"target": target,
"select": selectAfterUpload,
"print": printAfterSelect,
"select": select_request,
"print": print_request,
"effective_select": to_select,
"effective_print": to_print,
}
if userdata is not None:
payload["userdata"] = userdata
@@ -730,7 +746,15 @@ def uploadGcodeFile(target):
}
)
r = make_response(jsonify(files=files, done=done), 201)
r = make_response(
jsonify(
files=files,
done=done,
effectiveSelect=to_select,
effectivePrint=to_print,
),
201,
)
r.headers["Location"] = location
return r