🐛 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

+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