示例#1
0
def parse_blob_info(file_storage):
    """Parse a BlobInfo record from file upload field_storage.

  Args:
    file_storage: werkzeug.FileStorage 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 file_storage is None:
        return None

    field_name = file_storage.name

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

    filename = file_storage.filename
    content_type, cdict = cgi.parse_header(
        file_storage.headers['Content-Type'])
    blob_key = blobstore.BlobKey(get_value(cdict, 'blob-key'))

    upload_content = email.message_from_file(file_storage.stream)
    content_type = get_value(upload_content, 'content-type')
    size = get_value(upload_content, 'content-length')
    creation_string = get_value(upload_content,
                                blobstore.UPLOAD_INFO_CREATION_HEADER)

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

    try:
        creation = parse_creation(creation_string, field_name)
    except CreationFormatError, e:
        raise blobstore.BlobInfoParseError(
            'Could not parse creation for %s: %s' % (field_name, str(e)))
示例#2
0
    def get_value(dict, name):
        value = dict.get(name, None)
        if value is None:
            raise blobstore.BlobInfoParseError('Field %s has no %s.' %
                                               (field_name, name))

        return value