Skip to content

Commit

Permalink
Merge pull request #219 from lzaoral/fix-large-uploads
Browse files Browse the repository at this point in the history
Fix large file uploads
  • Loading branch information
rohanpm authored Aug 24, 2023
2 parents cbe7d25 + f277aae commit ac4e5dc
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
4 changes: 4 additions & 0 deletions kobo/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,10 @@ def upload_file(self, file_name, target_dir):
checksum = sum.hexdigest().lower()

fsize = os.path.getsize(file_name)
# use str only for large uploads to not break compatibility with older hubs
if fsize > xmlrpclib.MAXINT:
fsize = str(fsize)

upload_id, upload_key = self.upload.register_upload(os.path.basename(file_name), checksum, fsize, target_dir)

secure = (scheme == "https")
Expand Down
19 changes: 19 additions & 0 deletions kobo/django/upload/migrations/0002_alter_fileupload_size.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 3.2.20 on 2023-08-22 09:45

import django.core.validators
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('upload', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='fileupload',
name='size',
field=models.BigIntegerField(validators=[django.core.validators.MinValueValidator(0)]),
),
]
7 changes: 6 additions & 1 deletion kobo/django/upload/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os

from django.conf import settings
from django.core.validators import MinValueValidator
from django.db import models
from django.contrib.auth.models import User

Expand All @@ -25,7 +26,8 @@ class FileUpload(models.Model):
owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
name = models.CharField(max_length=255)
checksum = models.CharField(max_length=255)
size = models.PositiveIntegerField()
# models.PositiveBigIntegerField would be even better but it was introduced only in Django 3.1
size = models.BigIntegerField(validators=[MinValueValidator(0)])
target_dir = models.CharField(max_length=255)
upload_key = models.CharField(max_length=255)
state = models.PositiveIntegerField(default=0, choices=UPLOAD_STATES.get_mapping())
Expand Down Expand Up @@ -66,6 +68,9 @@ def save(self, *args, **kwargs):
self.state = UPLOAD_STATES['FAILED']
if "update_fields" in kwargs:
kwargs["update_fields"] = {"state"}.union(kwargs["update_fields"])

# execute validators
self.full_clean()
super(FileUpload, self).save(*args, **kwargs)

def delete(self):
Expand Down
3 changes: 2 additions & 1 deletion kobo/django/upload/xmlrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def register_upload(request, name, checksum, size, target_dir):
upload.owner = request.user
upload.name = name
upload.checksum = checksum.lower()
upload.size = size
# size may be sent as a string to workaround the xmlrpc.client.MAXINT limit
upload.size = int(size)
upload.target_dir = target_dir
upload.save()
return (upload.id, upload.upload_key)
Expand Down

0 comments on commit ac4e5dc

Please sign in to comment.