Skip to content

Add size support to the storage api #55

@RabidCicada

Description

@RabidCicada

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions