Cancellable tasks
All checks were successful
Gitea/kapitanbooru-uploader/pipeline/head This commit looks good

This commit is contained in:
2025-02-28 18:57:55 +01:00
parent 5571e18102
commit 9f187afc22
5 changed files with 283 additions and 114 deletions

View File

@@ -1,16 +1,21 @@
# Klasa pomocnicza do monitorowania postępu uploadu
class ProgressFile:
def __init__(self, f, callback, total_size):
def __init__(self, f, callback, total_size, cancel_event=None):
self.f = f
self.callback = callback
self.cancel_event = cancel_event
self.total_size = total_size
self.read_bytes = 0
def read(self, size=-1):
# Check for cancellation before reading more data
if self.cancel_event is not None and self.cancel_event.is_set():
raise Exception("Upload cancelled by user.")
data = self.f.read(size)
self.read_bytes += len(data)
self.callback(self.read_bytes, self.total_size)
return data
def __getattr__(self, attr):
return getattr(self.f, attr)
return getattr(self.f, attr)