Skip to content

Commit

Permalink
Merge pull request #232 from lzaoral/upload-fix-duplicate-detection
Browse files Browse the repository at this point in the history
upload: fix duplicate detection
  • Loading branch information
rohanpm authored Nov 9, 2023
2 parents 5257d83 + 36ed6dc commit 424b279
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
3 changes: 2 additions & 1 deletion kobo/django/upload/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions kobo/django/upload/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ 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)

if os.path.isfile(upload_path):
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
Expand All @@ -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)
Expand All @@ -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"File already exists.")

return HttpResponse("Upload finished.")
return HttpResponse(b"Upload finished.")

0 comments on commit 424b279

Please sign in to comment.