-
Notifications
You must be signed in to change notification settings - Fork 18
Open
Description
Add size support to the storage api so that it can be used to forecast size before comitting to reading the object. This can be useful in cases where one needs to know the size but is not the one reading the file itself. (Like setting up a tar archive). My specific use case is in django-admin commands where I'm creating a tar archive in the consuming django app (the app using django-rest-models). The critical part of the contextual snippet below is the field.size reference
def _dump_files(self, tar, exportmodels):
"""
Dump all uploaded media to the archive.
"""
# Loop through all models and find FileFields
for model in exportmodels:
# Get the name of all file fields in the model
for field in model._meta.fields:
if isinstance(field, models.FileField):
try:
field = getattr(model, field.name)
if field:
field.open()
self.fileshasums[str(model.pk)+'/'+field.name] = sha256sum(field)
field.open() # reset the file to byte 0 using django's FileField
info = TarInfo(str(model.pk)+'/'+field.name)
info.size = int(field.size)
tar.addfile(info, field)
field.close()
except TarError:
self.stdout.write(
self.style.ERROR("Failed to archive {} for stuff {}".format(
field.name, model.pk)
)
)
raise
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels