Пример #1
0
def _parse_upload_info(field_storage, error_class):
  """Parse the upload info from file upload field_storage.

  Args:
    field_storage: cgi.FieldStorage that represents uploaded blob.
    error_class: error to raise.

  Returns:
    A dictionary containing the parsed values. None if there was no
    field_storage.

  Raises:
    error_class when provided a field_storage that does not contain enough
    information.
  """
  if field_storage is None:
    return None

  field_name = field_storage.name

  def get_value(dict, name):
    value = dict.get(name, None)
    if value is None:
      raise error_class(
          'Field %s has no %s.' % (field_name, name))
    return value

  filename = get_value(field_storage.disposition_options, 'filename')
  blob_key = field_storage.type_options.get('blob-key', None)

  upload_content = _get_upload_content(field_storage)


  field_storage.file.seek(0)
  content_type = get_value(upload_content, 'content-type')
  size = get_value(upload_content, 'content-length')
  creation_string = get_value(upload_content, UPLOAD_INFO_CREATION_HEADER)
  md5_hash_encoded = get_value(upload_content, 'content-md5')
  md5_hash = base64.urlsafe_b64decode(md5_hash_encoded)
  gs_object_name = upload_content.get(CLOUD_STORAGE_OBJECT_HEADER, None)

  try:
    size = int(size)
  except (TypeError, ValueError):
    raise error_class(
        '%s is not a valid value for %s size.' % (size, field_name))

  try:
    creation = blobstore._parse_creation(creation_string, field_name)
  except blobstore._CreationFormatError as err:
    raise error_class(str(err))

  return {'blob_key': blob_key,
          'content_type': content_type,
          'creation': creation,
          'filename': filename,
          'size': size,
          'md5_hash': md5_hash,
          'gs_object_name': gs_object_name,
         }
Пример #2
0
def parse_blob_info(field_storage):
    """Parse a BlobInfo record from file upload field_storage.

  Args:
    field_storage: cgi.FieldStorage that represents uploaded blob.

  Returns:
    BlobInfo record as parsed from the field-storage instance.
    None if there was no field_storage.

  Raises:
    BlobInfoParseError when provided field_storage does not contain enough
    information to construct a BlobInfo object.
  """
    if field_storage is None:
        return None

    field_name = field_storage.name

    def get_value(dct, name):
        value = dct.get(name, None)
        if value is None:
            raise BlobInfoParseError('Field %s has no %s.' %
                                     (field_name, name))
        return value

    filename = get_value(field_storage.disposition_options, 'filename')
    blob_key_str = get_value(field_storage.type_options, 'blob-key')
    blob_key = BlobKey(blob_key_str)

    if six.PY2:
        upload_content = email.message_from_file(field_storage.file)
    else:
        upload_content = email.message_from_binary_file(field_storage.file)

    content_type = get_value(upload_content, 'content-type')
    size = get_value(upload_content, 'content-length')
    creation_string = get_value(upload_content, UPLOAD_INFO_CREATION_HEADER)
    md5_hash_encoded = get_value(upload_content, 'content-md5')
    md5_hash = base64.urlsafe_b64decode(md5_hash_encoded)

    try:
        size = int(size)
    except (TypeError, ValueError):
        raise BlobInfoParseError('%s is not a valid value for %s size.' %
                                 (size, field_name))

    try:
        creation = blobstore._parse_creation(creation_string, field_name)
    except blobstore._CreationFormatError as err:
        raise BlobInfoParseError(str(err))

    return BlobInfo(
        id=blob_key_str,
        content_type=content_type,
        creation=creation,
        filename=filename,
        size=size,
        md5_hash=md5_hash,
    )
Пример #3
0
def _parse_upload_info(field_storage, error_class):
  """Parses the upload information from the file upload `field_storage`.

  Args:
    field_storage: `cgi.FieldStorage` that represents the uploaded blob.
    error_class: The error to raise if the information cannot be parsed.

  Returns:
    A dictionary that contains the parsed values. This method will return `None`
    if `field_storage` was not defined.

  Raises:
    `error_class` when provided with a `field_storage` that does not contain
    enough information.
  """
  if field_storage is None:
    return None

  field_name = field_storage.name

  def get_value(dict, name):
    """Gets the name of the field."""
    value = dict.get(name, None)
    if value is None:
      raise error_class(
          'Field %s has no %s.' % (field_name, name))
    return value

  filename = get_value(field_storage.disposition_options, 'filename')
  blob_key = field_storage.type_options.get('blob-key', None)

  upload_content = _get_upload_content(field_storage)


  field_storage.file.seek(0)
  content_type = get_value(upload_content, 'content-type')
  size = get_value(upload_content, 'content-length')
  creation_string = get_value(upload_content, UPLOAD_INFO_CREATION_HEADER)
  md5_hash_encoded = get_value(upload_content, 'content-md5')
  md5_hash = base64.urlsafe_b64decode(md5_hash_encoded)
  gs_object_name = upload_content.get(CLOUD_STORAGE_OBJECT_HEADER, None)

  try:
    size = int(size)
  except (TypeError, ValueError):
    raise error_class(
        '%s is not a valid value for %s size.' % (size, field_name))

  try:
    creation = blobstore._parse_creation(creation_string, field_name)
  except blobstore._CreationFormatError, err:
    raise error_class(str(err))
Пример #4
0
def _parse_upload_info(field_storage, error_class):
  """Parse the upload info from file upload field_storage.

  Args:
    field_storage: cgi.FieldStorage that represents uploaded blob.
    error_class: error to raise.

  Returns:
    A dictionary containing the parsed values. None if there was no
    field_storage.

  Raises:
    error_class when provided a field_storage that does not contain enough
    information.
  """
  if field_storage is None:
    return None

  field_name = field_storage.name

  def get_value(dict, name):
    value = dict.get(name, None)
    if value is None:
      raise error_class(
          'Field %s has no %s.' % (field_name, name))
    return value

  filename = get_value(field_storage.disposition_options, 'filename')
  blob_key = field_storage.type_options.get('blob-key', None)

  upload_content = email.message_from_file(field_storage.file)


  field_storage.file.seek(0)
  content_type = get_value(upload_content, 'content-type')
  size = get_value(upload_content, 'content-length')
  creation_string = get_value(upload_content, UPLOAD_INFO_CREATION_HEADER)
  #md5_hash_encoded = get_value(upload_content, 'content-md5')
  #md5_hash = base64.urlsafe_b64decode(md5_hash_encoded)
  gs_object_name = upload_content.get(CLOUD_STORAGE_OBJECT_HEADER, None)

  try:
    size = int(size)
  except (TypeError, ValueError):
    raise error_class(
        '%s is not a valid value for %s size.' % (size, field_name))

  try:
    creation = blobstore._parse_creation(creation_string, field_name)
  except blobstore._CreationFormatError, err:
    raise error_class(str(err))
Пример #5
0
def _parse_upload_info(field_storage, error_class):
    """Parses the upload information from the file upload `field_storage`.

  Args:
    field_storage: `cgi.FieldStorage` that represents the uploaded blob.
    error_class: The error to raise if the information cannot be parsed.

  Returns:
    A dictionary that contains the parsed values. This method will return `None`
    if `field_storage` was not defined.

  Raises:
    `error_class` when provided with a `field_storage` that does not contain
    enough information.
  """
    if field_storage is None:
        return None

    field_name = field_storage.name

    def get_value(dict, name):
        """Gets the name of the field."""
        value = dict.get(name, None)
        if value is None:
            raise error_class('Field %s has no %s.' % (field_name, name))
        return value

    filename = get_value(field_storage.disposition_options, 'filename')
    blob_key = field_storage.type_options.get('blob-key', None)

    upload_content = _get_upload_content(field_storage)

    field_storage.file.seek(0)
    content_type = get_value(upload_content, 'content-type')
    size = get_value(upload_content, 'content-length')
    creation_string = get_value(upload_content, UPLOAD_INFO_CREATION_HEADER)
    md5_hash_encoded = get_value(upload_content, 'content-md5')
    md5_hash = base64.urlsafe_b64decode(md5_hash_encoded)
    gs_object_name = upload_content.get(CLOUD_STORAGE_OBJECT_HEADER, None)

    try:
        size = int(size)
    except (TypeError, ValueError):
        raise error_class('%s is not a valid value for %s size.' %
                          (size, field_name))

    try:
        creation = blobstore._parse_creation(creation_string, field_name)
    except blobstore._CreationFormatError, err:
        raise error_class(str(err))
Пример #6
0
def parse_blob_info(field_storage):
  """Parse a BlobInfo record from file upload field_storage.

  Args:
    field_storage: cgi.FieldStorage that represents uploaded blob.

  Returns:
    BlobInfo record as parsed from the field-storage instance.
    None if there was no field_storage.

  Raises:
    BlobInfoParseError when provided field_storage does not contain enough
    information to construct a BlobInfo object.
  """
  if field_storage is None:
    return None

  field_name = field_storage.name

  def get_value(diction, name):
    """ Gets the value from a dictionary given a name and raises an exception
        if it does not exist. """
    value = diction.get(name, None)
    if value is None:
      raise BlobInfoParseError(
          'Field %s has no %s.' % (field_name, name))
    return value

  filename = get_value(field_storage.disposition_options, 'filename')
  blob_key = BlobKey(get_value(field_storage.type_options, 'blob-key'))

  upload_content = email.message_from_file(field_storage.file)
  content_type = get_value(upload_content, 'content-type')
  size = get_value(upload_content, 'content-length')
  creation_string = get_value(upload_content, UPLOAD_INFO_CREATION_HEADER)
  #md5_hash_encoded = '0' # get_value(upload_content, 'content-md5')
  #md5_hash = base64.urlsafe_b64decode(md5_hash_encoded)

  try:
    size = int(size)
  except (TypeError, ValueError):
    raise BlobInfoParseError(
        '%s is not a valid value for %s size.' % (size, field_name))

  try:
    creation = blobstore._parse_creation(creation_string, field_name)
  except blobstore._CreationFormatError, err:
    raise BlobInfoParseError(str(err))
Пример #7
0
def parse_blob_info(field_storage):
    """Parse a BlobInfo record from file upload field_storage.

  Args:
    field_storage: cgi.FieldStorage that represents uploaded blob.

  Returns:
    BlobInfo record as parsed from the field-storage instance.
    None if there was no field_storage.

  Raises:
    BlobInfoParseError when provided field_storage does not contain enough
    information to construct a BlobInfo object.
  """
    if field_storage is None:
        return None

    field_name = field_storage.name

    def get_value(dict, name):
        value = dict.get(name, None)
        if value is None:
            raise BlobInfoParseError("Field %s has no %s." % (field_name, name))
        return value

    filename = get_value(field_storage.disposition_options, "filename")
    blob_key = BlobKey(get_value(field_storage.type_options, "blob-key"))

    upload_content = email.message_from_file(field_storage.file)
    content_type = get_value(upload_content, "content-type")
    size = get_value(upload_content, "content-length")
    creation_string = get_value(upload_content, UPLOAD_INFO_CREATION_HEADER)

    try:
        size = int(size)
    except (TypeError, ValueError):
        raise BlobInfoParseError("%s is not a valid value for %s size." % (size, field_name))

    try:
        creation = blobstore._parse_creation(creation_string, field_name)
    except blobstore._CreationFormatError, err:
        raise BlobInfoParseError(str(err))