From 5fba41c44dff4336962fb10ee262da7a58d4c243 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Joz=C3=ADfek?= Date: Wed, 8 Mar 2023 16:40:06 +0100 Subject: [PATCH] Make the file PUT method use the transfer object --- prusa/link/web/files.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/prusa/link/web/files.py b/prusa/link/web/files.py index 71c10fa1..7d5272c4 100644 --- a/prusa/link/web/files.py +++ b/prusa/link/web/files.py @@ -4,12 +4,13 @@ from os.path import basename, exists, join, isdir, split from shutil import rmtree from pathlib import Path -from time import sleep +from time import sleep, monotonic from magic import Magic from poorwsgi import state from poorwsgi.response import JSONResponse, Response -from prusa.connect.printer.const import StorageType, Source, FileType +from prusa.connect.printer.const import StorageType, Source, FileType, \ + TransferType from .. import conditions from ..const import LOCAL_STORAGE_NAME @@ -179,11 +180,20 @@ def file_upload(req, storage, path): filename = basename(abs_path) part_path = partfilepath(filename) + transfer = app.daemon.prusa_link.printer.transfer + transfer.start(TransferType.FROM_CLIENT, filename, + to_print=print_after_upload) + transfer.size = req.content_length + transfer.start_ts = monotonic() + with open(part_path, 'w+b') as temp: block = min(app.cached_size, req.content_length) data = req.read(block) while data: + if transfer.stop_ts: + break uploaded += temp.write(data) + transfer.transferred = uploaded # checksum.update(data) # - we don't use the value yet block = min(app.cached_size, req.content_length - uploaded) if block > 1: @@ -191,6 +201,11 @@ def file_upload(req, storage, path): else: data = b'' + transfer.type = TransferType.NO_TRANSFER + + if req.content_length > uploaded: + raise conditions.FileUploadFailed() + # Mine a real mime_type from the file using magic if req.mime_type == 'application/octet-stream': mime_type = Magic(mime=True).from_file(abs_path)