UploadBackupJob.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import requests
  4. from UM.Job import Job
  5. from UM.Logger import Logger
  6. from UM.Message import Message
  7. from UM.i18n import i18nCatalog
  8. catalog = i18nCatalog("cura")
  9. class UploadBackupJob(Job):
  10. MESSAGE_TITLE = catalog.i18nc("@info:title", "Backups")
  11. # This job is responsible for uploading the backup file to cloud storage.
  12. # As it can take longer than some other tasks, we schedule this using a Cura Job.
  13. def __init__(self, signed_upload_url: str, backup_zip: bytes) -> None:
  14. super().__init__()
  15. self._signed_upload_url = signed_upload_url
  16. self._backup_zip = backup_zip
  17. self._upload_success = False
  18. self.backup_upload_error_message = ""
  19. def run(self) -> None:
  20. upload_message = Message(catalog.i18nc("@info:backup_status", "Uploading your backup..."), title = self.MESSAGE_TITLE, progress = -1)
  21. upload_message.show()
  22. backup_upload = requests.put(self._signed_upload_url, data = self._backup_zip)
  23. upload_message.hide()
  24. if backup_upload.status_code >= 300:
  25. self.backup_upload_error_message = backup_upload.text
  26. Logger.log("w", "Could not upload backup file: %s", backup_upload.text)
  27. Message(catalog.i18nc("@info:backup_status", "There was an error while uploading your backup."), title = self.MESSAGE_TITLE).show()
  28. else:
  29. self._upload_success = True
  30. Message(catalog.i18nc("@info:backup_status", "Your backup has finished uploading."), title = self.MESSAGE_TITLE).show()