From cf860829c7a4d817757a3af921c21041ac302c35 Mon Sep 17 00:00:00 2001 From: Ghostkeeper Date: Tue, 12 Oct 2021 17:21:25 +0200 Subject: [PATCH] Provide upload request metadata as body of a PUT request Apparently the API is now a PUT request rather than a GET request. It needs a bit more metadata which can be hard-coded for our client. Contributes to issue CURA-8609. --- cura/PrinterOutput/UploadMaterialsJob.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/cura/PrinterOutput/UploadMaterialsJob.py b/cura/PrinterOutput/UploadMaterialsJob.py index 994971c8d0..8664e5b714 100644 --- a/cura/PrinterOutput/UploadMaterialsJob.py +++ b/cura/PrinterOutput/UploadMaterialsJob.py @@ -1,11 +1,12 @@ # Copyright (c) 2021 Ultimaker B.V. # Cura is released under the terms of the LGPLv3 or higher. -from PyQt5.QtCore import QUrl -import os # To delete the archive when we're done. -import tempfile # To create an archive before we upload it. import enum import functools +import json # To serialise metadata for API calls. +import os # To delete the archive when we're done. +from PyQt5.QtCore import QUrl +import tempfile # To create an archive before we upload it. import cura.CuraApplication # Imported like this to prevent circular imports. from cura.Settings.CuraContainerRegistry import CuraContainerRegistry # To find all printers to upload to. @@ -77,9 +78,20 @@ class UploadMaterialsJob(Job): self._material_sync.exportAll(QUrl.fromLocalFile(self._archive_filename), notify_progress = self.processProgressChanged) file_size = os.path.getsize(self._archive_filename) + request_metadata = { + "data": { + "file_size": file_size, + "file_name": "cura.umm", # File name can be anything as long as it's .umm. It's not used by anyone. + "content_type": "application/zip", # This endpoint won't receive files of different MIME types. + "origin": "cura" # Some identifier against hackers intercepting this upload request, apparently. + } + } + request_payload = json.dumps(request_metadata).encode("UTF-8") + http = HttpRequestManager.getInstance() - http.get( - url = self.UPLOAD_REQUEST_URL + f"?file_size={file_size}&file_name=cura.umm", # File name can be anything as long as it's .umm. It's not used by Cloud or firmware. + http.put( + url = self.UPLOAD_REQUEST_URL, + data = request_payload, callback = self.onUploadRequestCompleted, error_callback = self.onError, scope = self._scope