From f46d12aeda79918fdd95ba27eed8c92d7e8b1376 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Zaoral?= Date: Thu, 9 Nov 2023 12:32:22 +0100 Subject: [PATCH 1/3] upload: HttpResponse expects a byte string argument --- kobo/django/upload/views.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/kobo/django/upload/views.py b/kobo/django/upload/views.py index 9d7ded70..14c59f2a 100644 --- a/kobo/django/upload/views.py +++ b/kobo/django/upload/views.py @@ -39,7 +39,7 @@ def file_upload(request): try: upload = FileUpload.objects.get(id=upload_id, upload_key=upload_key) except: - return HttpResponseForbidden("Not allowed to upload the file.") + return HttpResponseForbidden(b"Not allowed to upload the file.") upload_path = os.path.join(upload.target_dir, upload.name) @@ -47,7 +47,7 @@ def file_upload(request): upload.state = UPLOAD_STATES["FAILED"] upload.save() # remove file - return HttpResponseServerError("File already exists.") + return HttpResponseServerError(b"File already exists.") # TODO: check size # don't re-upload FINISHED or STARTED @@ -72,7 +72,7 @@ def file_upload(request): upload.state = UPLOAD_STATES["FAILED"] upload.save() # remove file - return HttpResponseServerError("Checksum mismatch.") + return HttpResponseServerError(b"Checksum mismatch.") if not os.path.isdir(upload.target_dir): os.makedirs(upload.target_dir) @@ -86,6 +86,6 @@ def file_upload(request): # upload.save can modify state if there is a race if upload.state == UPLOAD_STATES['FAILED']: - return HttpResponseServerError("Checksum mismatch.") + return HttpResponseServerError(b"Checksum mismatch.") - return HttpResponse("Upload finished.") + return HttpResponse(b"Upload finished.") From e87913a711bf263ac03974c3320d365030012211 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Zaoral?= Date: Thu, 9 Nov 2023 12:34:01 +0100 Subject: [PATCH 2/3] upload: fix a misleading error message --- kobo/django/upload/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kobo/django/upload/views.py b/kobo/django/upload/views.py index 14c59f2a..834894ae 100644 --- a/kobo/django/upload/views.py +++ b/kobo/django/upload/views.py @@ -86,6 +86,6 @@ def file_upload(request): # upload.save can modify state if there is a race if upload.state == UPLOAD_STATES['FAILED']: - return HttpResponseServerError(b"Checksum mismatch.") + return HttpResponseServerError(b"File already exists.") return HttpResponse(b"Upload finished.") From 36ed6dc568d80cdc99cf4daac62f28968fd23c70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Zaoral?= Date: Thu, 9 Nov 2023 12:34:45 +0100 Subject: [PATCH 3/3] upload: make the duplicate check more robust Do not classify uploads with different checksums or target directories as duplicates. --- kobo/django/upload/models.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/kobo/django/upload/models.py b/kobo/django/upload/models.py index 68d65c6f..6a7e5a3a 100644 --- a/kobo/django/upload/models.py +++ b/kobo/django/upload/models.py @@ -63,7 +63,8 @@ def save(self, *args, **kwargs): if "update_fields" in kwargs: kwargs["update_fields"] = {"upload_key"}.union(kwargs["update_fields"]) if self.state == UPLOAD_STATES['FINISHED']: - if FileUpload.objects.filter(state = UPLOAD_STATES['FINISHED'], name = self.name).exclude(id = self.id).count() != 0: + if FileUpload.objects.filter(state = UPLOAD_STATES['FINISHED'], name = self.name, + checksum=self.checksum, target_dir=self.target_dir).exclude(id = self.id).count() != 0: # someone created same upload faster self.state = UPLOAD_STATES['FAILED'] if "update_fields" in kwargs: